changeset 159:4c62b6d416a7

busybox client can send IR codes Ignore-this: 181177e12841989b40fc2263fe7aea8
author drewp@bigasterisk.com
date Tue, 20 Jan 2015 23:50:09 -0800
parents 75b1dc5c0bdc
children b5d6d9a6211f
files service/busyboxArduino/busybox.py service/busyboxArduino/main.ino
diffstat 2 files changed, 100 insertions(+), 111 deletions(-) [+]
line wrap: on
line diff
--- a/service/busyboxArduino/busybox.py	Mon Jan 19 21:38:11 2015 -0800
+++ b/service/busyboxArduino/busybox.py	Tue Jan 20 23:50:09 2015 -0800
@@ -49,35 +49,88 @@
     def setBacklight(self, level):
         self.serial.write(struct.pack('BBB', 0x60, 0x02, level))
 
-sendOneShot((ROOM['greets'],
-             ROOM['change'],
-             ROOM['down']))
+    def sendIr(self, remote, command):
+        code = {
+            'led_22_key': {
+                # LED618 remote command byte (addr is ff)
+                # see https://github.com/alistairallan/RgbIrLed/blob/master/RgbIrLed.cpp#L44
+                'ON': 0xE0,
+                'OFF': 0x60,
+                'BRIGHTNESS_UP': 0xA0,
+                'BRIGHTNESS_DOWN': 0x20,
+                'FLASH': 0xF0,
+                'STROBE': 0xE8,
+                'FADE': 0xD8,
+                'SMOOTH': 0xC8,
+                'RED': 0x90, 'GREEN': 0x10, 'BLUE': 0x50, 'WHITE': 0xD0,
+                'ORANGE': 0xB0, 'YELLOW_DARK': 0xA8, 'YELLOW_MEDIUM': 0x98, 'YELLOW_LIGHT': 0x88,
+                'GREEN_LIGHT': 0x30, 'GREEN_BLUE1': 0x28, 'GREEN_BLUE2': 0x18, 'GREEN_BLUE3': 0x08,
+                'BLUE_RED': 0x70, 'PURPLE_DARK': 0x68, 'PURPLE_LIGHT': 0x58, 'PINK': 0x48,
+            },
+            'led_44_key': {
+                # 44 key remote. command chart: http://blog.allgaiershops.com/2012/05/
+                'up': 0x3A, 'down': 0xBA, 'play': 0x82, 'power': 0x02, 
+                'red0': 0x1A, 'grn0': 0x9A, 'blu0': 0xA2, 'wht0': 0x22, 
+                'red1': 0x2A, 'grn1': 0xAA, 'blu1': 0x92, 'wht1': 0x12, 
+                'red2': 0x0A, 'grn2': 0x8A, 'blu2': 0xB2, 'wht2': 0x32, 
+                'red3': 0x38, 'grn3': 0xB8, 'blu3': 0x78, 'wht3': 0xF8, 
+                'red4': 0x18, 'grn4': 0x98, 'blu4': 0x58, 'wht4': 0xD8, 
+                'RUp': 0x28, 'GUp': 0xA8, 'BUp': 0x68, 
+                'RDn': 0x08, 'GDn': 0x88, 'BDn': 0x48, 
+                'Quick': 0xE8, 'Slow': 0xC8, 
+                'DIY1': 0x30, 'DIY2': 0xB0, 'DIY3': 0x70, 'DIY4': 0x10, 'DIY5': 0x90, 'DIY6': 0x50, 
+                'AUTO': 0xF0, 
+                'Flash': 0xD0, 
+                'JMP3': 0x20, 'JMP7': 0xA0, 
+                'Fade': 0x60, 'Fade7': 0xE0,
+            }
+        }
+
+        address = {
+            'led_22_key': 0x00,
+            'led_44_key': 0x00,
+        }[remote]
+        self.serial.write(struct.pack('BBBB', 0x60, 0x03, address, code[remote][command]))
+        time.sleep(.2)
+        
 
 bb = Busybox()
 words = open('/usr/share/dict/words').readlines()
 
-lastWordTime = 0
-last = None
-s1 = []
+class Poller(object):
+    def __init__(self):
+        self.lastWordTime = 0
+        self.last = None
+        self.s1 = []
 
-def poll():
-    global lastWordTime, s1, last
-    now = time.time()
-    if now - lastWordTime > 1:
-        msg = '%15s' % random.choice(words).strip()[:15]
-        bb.writeMessage(1, 1, msg)
-        lastWordTime = now
-    last = bb.poll()
-    if 'slider1' in last:
-        s1 = s1[-5:] + [last['slider1']]
-        if len(s1) > 4:
-            median = sorted(s1)[2]
-            bb.setBacklight(min(255, median // 4))
-    if 'keyDown' in last:
-        keyNum = last['keyDown']
-        sendOneShot((ROOM['ariBed/button%s' % keyNum],
-                     ROOM['change'],
-                     ROOM['down']))
+    def poll(self):
+        try:
+            self.tryPoll()
+        except Exception as e:
+            print "poll failed: %r" % e
+            
+    def tryPoll(self):
+        now = time.time()
+        if now - self.lastWordTime > 1:
+            msg = '%15s' % random.choice(words).strip()[:15]
+            bb.writeMessage(1, 1, msg)
+            self.lastWordTime = now
+        self.last = bb.poll()
+        print self.last
+        if 'slider1' in self.last:
+            self.s1 = self.s1[-5:] + [self.last['slider1']]
+            if len(self.s1) > 4:
+                median = sorted(self.s1)[2]
+                bb.setBacklight(min(255, median // 4))
+        if 'keyDown' in self.last:
+            keyNum = self.last['keyDown']
+            sendOneShot((ROOM['ariBed/button%s' % keyNum],
+                         ROOM['change'],
+                         ROOM['down']))
+        if self.last['motion']:
+            bb.sendIr('ON')
+        else:
+            bb.sendIr('OFF')
 
 
 @klein.route('/graph', methods=['GET'])
@@ -86,11 +139,15 @@
     g.add((ROOM.busybox, ROOM.localHour, Literal('x')))
     for attr in ['slider1', 'slider2', 'slider3', 'slider4']:
         # needs: smoothing, exp curve correction
-        g.add((ROOM['busybox/%s' % attr], ROOM.value, Literal(round(last[attr] / 1021, 3))))
+        g.add((ROOM['busybox/%s' % attr], ROOM.value, Literal(round(poller.last[attr] / 1021, 3))))
+    g.add((ROOM['busybox/motion'], ROOM.value, Literal(poller.last['motion'])))
     request.setHeader('Content-type', 'application/x-trig')
     return g.asTrig()
 
-task.LoopingCall(poll).start(.05)
-        
+poller = Poller()
+task.LoopingCall(poller.poll).start(.05)
+
+# todo: watch reasoning graph. put lines on display. send updated ir codes.
+
 klein.run('0.0.0.0', port=9056)
 
--- a/service/busyboxArduino/main.ino	Mon Jan 19 21:38:11 2015 -0800
+++ b/service/busyboxArduino/main.ino	Tue Jan 20 23:50:09 2015 -0800
@@ -20,6 +20,9 @@
 #include "DFR_Key.h"
 #include "IRremote.h"
 
+#define motionIn 12
+
+
 // see http://www.dfrobot.com/image/data/DFR0009/LCDKeypad%20Shield%20V1.0%20SCH.pdf
 #define I2C_ADDR      0x27 // I2C address of PCF8574A
 #define BACKLIGHT_PIN 10
@@ -34,8 +37,6 @@
 LiquidCrystal lcd(Rs_pin, Rw_pin, En_pin, D4_pin, D5_pin, D6_pin, D7_pin, BACKLIGHT_PIN, POSITIVE);
 DFR_Key keypad;
 
-#define debugLed 13
-
 byte backlightStandard = 0;
 byte backlightCurrent = 0;
 byte backlightKeypressBoost = 30;
@@ -49,95 +50,21 @@
 IRsend irsend;
 
 void sendnec(byte addr, byte cmd) {
-  uint32_t w = 0x00ff0000;
-  w |= (cmd << 8) & 0xff00;
-  w |= (~cmd) & 0xff;
+  uint32_t w =
+    ((uint32_t(addr) << 24)    & 0xff000000) |
+    (((~uint32_t(addr)) << 16) & 0x00ff0000) |
+    ((uint32_t(cmd) << 8)      & 0x0000ff00) |
+    ((~uint32_t(cmd))          & 0x000000ff);
   irsend.sendNEC(w, 32);
- delay(100);
 }
 
 void setup(void) {
-  pinMode(debugLed, OUTPUT);
+  pinMode(motionIn, INPUT);
+  digitalWrite(motionIn, 1);
+  
   Serial.begin(115200);
   keypad.setRate(10);
   lcd.begin(16,2);
-
-
-  for (int i = 0; i < 1; i++) {
-    digitalWrite(debugLed,1);
-    /*
-44 key remote. command chart: http://blog.allgaiershops.com/2012/05/
-Up  Down Play Pwr
-0x3A 0xBA 0x82 0x02
-
-Red  Grn  Blu  Wht
-1A   9A   A2   22
-
-2A   AA   92   12
-
-0A   8A   B2   32
-
-38   B8   78   F8
-
-18   98   58   D8
-
-RUp  GUp  BUp  Quick
-28   A8   68   E8
-
-RDn  GDn  BDn  Slow
-08   88   48   C8
-
-DIY1 DIY2 DIY3 AUTO
-30   B0   70   F0
-
-DIY4 DIY5 DIY6 Flash
-10   90   50   D0
-
-JMP3 JMP7 Fade Fade7
-20   A0   60   E0
-      
-    irsend.sendNEC(0xff0002fd, 32);
-    irsend.sendNEC(0x00ff02fd, 32);
-    irsend.sendNEC(0x00ff40bf, 32);
-    irsend.sendNEC(0xff0040bf, 32);
-    */
-    delay(100);
-
-    // LED618 remote command byte (addr is ff)
-    // see https://github.com/alistairallan/RgbIrLed/blob/master/RgbIrLed.cpp#L44
-#define ON                0xE0
-#define OFF               0x60
-#define BRIGHTNESS_UP     0xA0
-#define BRIGHTNESS_DOWN   0x20
-#define FLASH             0xF0
-#define STROBE            0xE8
-#define FADE              0xD8
-#define SMOOTH            0xC8
- 
-#define RED               0x90
-#define GREEN             0x10
-#define BLUE              0x50
-#define WHITE             0xD0
- 
-#define ORANGE            0xB0
-#define YELLOW_DARK       0xA8
-#define YELLOW_MEDIUM     0x98
-#define YELLOW_LIGHT      0x88
- 
-#define GREEN_LIGHT       0x30
-#define GREEN_BLUE1       0x28
-#define GREEN_BLUE2       0x18
-#define GREEN_BLUE3       0x08
- 
-#define BLUE_RED          0x70
-#define PURPLE_DARK       0x68
-#define PURPLE_LIGHT      0x58
-#define PINK              0x48
-    sendnec(0xff, ON);
-    
-    digitalWrite(debugLed,0);
-    delay(100);
-  }
 }
 
 
@@ -182,6 +109,7 @@
     Serial.print(",\"slider2\":"); Serial.print(analogRead(2));
     Serial.print(",\"slider3\":"); Serial.print(analogRead(3));
     Serial.print(",\"slider4\":"); Serial.print(analogRead(4));
+    Serial.print(",\"motion\":"); Serial.print(digitalRead(motionIn));
     Serial.print("}\n");
   } else if (i == 1) { // write text
     while (Serial.available() < 3) NULL;
@@ -200,6 +128,10 @@
   } else if (i == 2) { // set backlight
     while (Serial.available() < 1) NULL;
     backlightStandard = Serial.read();
+  } else if (i == 3) { // IR NEC protocol
+    while (Serial.available() < 2) NULL;
+    byte addr = Serial.read();
+    sendnec(addr, Serial.read());
   } else {
     // unknown command
   }