Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // configure the imp (best practice)
- imp.configure("Lego IR Remote", [], []);
- // CREATE a global variabled called led,
- // AND assign pin9 TO it
- led <- hardware.pin9;
- toggle <- [0,0,0,0];
- // CREATE a global variable TO store CURRENT
- // state OF the LED
- state <- 0;
- s2ms <- 0.000001;
- //The used Global variabels
- PWM_FWD7 <- 0x7;
- PWM_FLT <- 0x0;
- //channel
- CH1 <- 0x0;
- //output
- RED <- 0x0;
- // configure led TO be a digital output
- led.configure(DIGITAL_OUT);
- // CREATE a global variable TO store CURRENT
- // state OF the LED
- state <- 0;
- FUNCTION blink() {
- // invert the VALUE OF state:
- // WHEN state = 1, 1-1 = 0
- // WHEN state = 0, 1-0 = 1
- state = 1-state;
- // sWITCHING IR COMMANDS
- server.log("Starting motor");
- SingleOutput(PWM_FWD7,RED,CH1);
- // schedule imp TO wakeup IN .5 seconds AND do it again.
- imp.sleep(5);
- server.log("Stopping motor");
- SingleOutput(PWM_FLT, RED,CH1);
- // schedule imp TO wakeup IN .5 seconds AND do it again.
- imp.wakeup(5, blink);
- }
- FUNCTION SingleOutput(pwm,output,channel) {
- server.log("Sending IR Commands..")
- SINGLE_OUTPUT <- 0x4;
- nib1 <- toggle[channel] | channel;
- nib2 <- SINGLE_OUTPUT | output;
- nib3 <- pwm;
- nib4 <- 0xf ^ nib1 ^ nib2 ^ nib3;
- FOR (LOCAL i=0; i<6; i++)
- {
- message_pause(channel, i);
- pf_send(nib1 << 4 | nib2, nib3 << 4 | nib4);
- }
- IF(toggle[channel] == 0)
- toggle[channel] = 8;
- ELSE
- toggle[channel] = 0;
- server.log("ready")
- }
- FUNCTION start_pause()
- {
- imp.sleep(1014*s2ms);
- }
- FUNCTION high_pause()
- {
- imp.sleep(546*s2ms);
- }
- FUNCTION low_pause()
- {
- imp.sleep(260*s2ms);
- }
- FUNCTION tx_pause()
- {
- imp.sleep(156*s2ms);
- }
- FUNCTION message_pause(channel, COUNT)
- {
- a <- 0;
- IF(COUNT == 0)
- a = 4 - channel + 1;
- ELSE IF(COUNT == 1 || COUNT == 2)
- a = 5;
- ELSE IF(COUNT == 3 || COUNT == 4)
- a = 5 + (channel + 1) * 2;
- imp.sleep(a * 77 * s2ms);
- }
- FUNCTION pf_send(code1, code2)
- {
- x <- 128;
- start_stop_bit();
- while (x)
- {
- oscillationWrite(156 * s2ms);
- IF (code1 & x) //high bit
- high_pause();
- ELSE //low bit
- low_pause();
- x = x >> 1; //NEXT bit
- }
- x = 128;
- while (x)
- {
- oscillationWrite(156*s2ms);
- IF (code2 & x) // high bit
- high_pause();
- ELSE //low bit
- low_pause();
- x = x >> 1; //NEXT bit
- }
- start_stop_bit();
- }
- FUNCTION start_stop_bit()
- {
- oscillationWrite(156*s2ms);
- start_pause();
- }
- FUNCTION oscillationWrite(TIME) {
- FOR( LOCAL i=0; i <= TIME/26; i++) {
- led.WRITE(1)
- imp.sleep(13*s2ms);
- led.WRITE(0)
- imp.sleep(13*s2ms);
- }
- }
- // START the loop
- blink();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement