comparison service/busyboxArduino/busybox.py @ 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 28c2db876548
children 4b0f221d790c
comparison
equal deleted inserted replaced
158:75b1dc5c0bdc 159:4c62b6d416a7
47 self.serial.write(msg) 47 self.serial.write(msg)
48 48
49 def setBacklight(self, level): 49 def setBacklight(self, level):
50 self.serial.write(struct.pack('BBB', 0x60, 0x02, level)) 50 self.serial.write(struct.pack('BBB', 0x60, 0x02, level))
51 51
52 sendOneShot((ROOM['greets'], 52 def sendIr(self, remote, command):
53 ROOM['change'], 53 code = {
54 ROOM['down'])) 54 'led_22_key': {
55 # LED618 remote command byte (addr is ff)
56 # see https://github.com/alistairallan/RgbIrLed/blob/master/RgbIrLed.cpp#L44
57 'ON': 0xE0,
58 'OFF': 0x60,
59 'BRIGHTNESS_UP': 0xA0,
60 'BRIGHTNESS_DOWN': 0x20,
61 'FLASH': 0xF0,
62 'STROBE': 0xE8,
63 'FADE': 0xD8,
64 'SMOOTH': 0xC8,
65 'RED': 0x90, 'GREEN': 0x10, 'BLUE': 0x50, 'WHITE': 0xD0,
66 'ORANGE': 0xB0, 'YELLOW_DARK': 0xA8, 'YELLOW_MEDIUM': 0x98, 'YELLOW_LIGHT': 0x88,
67 'GREEN_LIGHT': 0x30, 'GREEN_BLUE1': 0x28, 'GREEN_BLUE2': 0x18, 'GREEN_BLUE3': 0x08,
68 'BLUE_RED': 0x70, 'PURPLE_DARK': 0x68, 'PURPLE_LIGHT': 0x58, 'PINK': 0x48,
69 },
70 'led_44_key': {
71 # 44 key remote. command chart: http://blog.allgaiershops.com/2012/05/
72 'up': 0x3A, 'down': 0xBA, 'play': 0x82, 'power': 0x02,
73 'red0': 0x1A, 'grn0': 0x9A, 'blu0': 0xA2, 'wht0': 0x22,
74 'red1': 0x2A, 'grn1': 0xAA, 'blu1': 0x92, 'wht1': 0x12,
75 'red2': 0x0A, 'grn2': 0x8A, 'blu2': 0xB2, 'wht2': 0x32,
76 'red3': 0x38, 'grn3': 0xB8, 'blu3': 0x78, 'wht3': 0xF8,
77 'red4': 0x18, 'grn4': 0x98, 'blu4': 0x58, 'wht4': 0xD8,
78 'RUp': 0x28, 'GUp': 0xA8, 'BUp': 0x68,
79 'RDn': 0x08, 'GDn': 0x88, 'BDn': 0x48,
80 'Quick': 0xE8, 'Slow': 0xC8,
81 'DIY1': 0x30, 'DIY2': 0xB0, 'DIY3': 0x70, 'DIY4': 0x10, 'DIY5': 0x90, 'DIY6': 0x50,
82 'AUTO': 0xF0,
83 'Flash': 0xD0,
84 'JMP3': 0x20, 'JMP7': 0xA0,
85 'Fade': 0x60, 'Fade7': 0xE0,
86 }
87 }
88
89 address = {
90 'led_22_key': 0x00,
91 'led_44_key': 0x00,
92 }[remote]
93 self.serial.write(struct.pack('BBBB', 0x60, 0x03, address, code[remote][command]))
94 time.sleep(.2)
95
55 96
56 bb = Busybox() 97 bb = Busybox()
57 words = open('/usr/share/dict/words').readlines() 98 words = open('/usr/share/dict/words').readlines()
58 99
59 lastWordTime = 0 100 class Poller(object):
60 last = None 101 def __init__(self):
61 s1 = [] 102 self.lastWordTime = 0
103 self.last = None
104 self.s1 = []
62 105
63 def poll(): 106 def poll(self):
64 global lastWordTime, s1, last 107 try:
65 now = time.time() 108 self.tryPoll()
66 if now - lastWordTime > 1: 109 except Exception as e:
67 msg = '%15s' % random.choice(words).strip()[:15] 110 print "poll failed: %r" % e
68 bb.writeMessage(1, 1, msg) 111
69 lastWordTime = now 112 def tryPoll(self):
70 last = bb.poll() 113 now = time.time()
71 if 'slider1' in last: 114 if now - self.lastWordTime > 1:
72 s1 = s1[-5:] + [last['slider1']] 115 msg = '%15s' % random.choice(words).strip()[:15]
73 if len(s1) > 4: 116 bb.writeMessage(1, 1, msg)
74 median = sorted(s1)[2] 117 self.lastWordTime = now
75 bb.setBacklight(min(255, median // 4)) 118 self.last = bb.poll()
76 if 'keyDown' in last: 119 print self.last
77 keyNum = last['keyDown'] 120 if 'slider1' in self.last:
78 sendOneShot((ROOM['ariBed/button%s' % keyNum], 121 self.s1 = self.s1[-5:] + [self.last['slider1']]
79 ROOM['change'], 122 if len(self.s1) > 4:
80 ROOM['down'])) 123 median = sorted(self.s1)[2]
124 bb.setBacklight(min(255, median // 4))
125 if 'keyDown' in self.last:
126 keyNum = self.last['keyDown']
127 sendOneShot((ROOM['ariBed/button%s' % keyNum],
128 ROOM['change'],
129 ROOM['down']))
130 if self.last['motion']:
131 bb.sendIr('ON')
132 else:
133 bb.sendIr('OFF')
81 134
82 135
83 @klein.route('/graph', methods=['GET']) 136 @klein.route('/graph', methods=['GET'])
84 def getGraph(request): 137 def getGraph(request):
85 g = StateGraph(ROOM.busybox) 138 g = StateGraph(ROOM.busybox)
86 g.add((ROOM.busybox, ROOM.localHour, Literal('x'))) 139 g.add((ROOM.busybox, ROOM.localHour, Literal('x')))
87 for attr in ['slider1', 'slider2', 'slider3', 'slider4']: 140 for attr in ['slider1', 'slider2', 'slider3', 'slider4']:
88 # needs: smoothing, exp curve correction 141 # needs: smoothing, exp curve correction
89 g.add((ROOM['busybox/%s' % attr], ROOM.value, Literal(round(last[attr] / 1021, 3)))) 142 g.add((ROOM['busybox/%s' % attr], ROOM.value, Literal(round(poller.last[attr] / 1021, 3))))
143 g.add((ROOM['busybox/motion'], ROOM.value, Literal(poller.last['motion'])))
90 request.setHeader('Content-type', 'application/x-trig') 144 request.setHeader('Content-type', 'application/x-trig')
91 return g.asTrig() 145 return g.asTrig()
92 146
93 task.LoopingCall(poll).start(.05) 147 poller = Poller()
94 148 task.LoopingCall(poller.poll).start(.05)
149
150 # todo: watch reasoning graph. put lines on display. send updated ir codes.
151
95 klein.run('0.0.0.0', port=9056) 152 klein.run('0.0.0.0', port=9056)
96 153