Mercurial > code > home > repos > homeauto
annotate service/sba/sba.py @ 1229:02e4b84821d5
talk to store graph, second button for holding unlocked, etc
Ignore-this: c2ae7d756e743c26e5e01d99772899bd
darcs-hash:a0750d0bbc4dc7c0f65f63f3e7342b35a175141b
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Thu, 04 Apr 2019 02:16:22 -0700 |
parents | 1757fc19f992 |
children |
rev | line source |
---|---|
808 | 1 from __future__ import division |
844 | 2 import serial, time, json, sys, cgi, argparse, os |
808 | 3 import cyclone.web |
4 from twisted.python import log | |
5 from twisted.internet import reactor | |
817 | 6 from serial.serialutil import SerialException |
808 | 7 |
8 class Sba(object): | |
9 def __init__(self, port="/dev/ttyACM0"): | |
10 self.port = port | |
11 self.reset() | |
12 | |
13 def reset(self): | |
14 log.msg("reopening port") | |
830
6860694eb19a
sba timeouts so it doesn't stall all the time
drewp <drewp@bigasterisk.com>
parents:
817
diff
changeset
|
15 # this timeout will fire every few seconds |
6860694eb19a
sba timeouts so it doesn't stall all the time
drewp <drewp@bigasterisk.com>
parents:
817
diff
changeset
|
16 self.s = serial.Serial(self.port, baudrate=115200, timeout=0.020) |
844 | 17 #log.msg(str(self.s.__dict__)) |
808 | 18 self.sendControl() |
19 | |
809
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
808
diff
changeset
|
20 def ping(self): |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
808
diff
changeset
|
21 pass # waiting for spec |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
808
diff
changeset
|
22 |
808 | 23 def sendControl(self): |
24 controlBits = [0, 1, | |
25 0, 0, 0, | |
26 1, 1, 1, 1, 1, 1, 1, # b correction | |
27 0, 0, 0, | |
28 1, 1, 1, 1, 1, 1, 1, # g correction | |
29 0, | |
30 0, 0, # clock mode 00=internal | |
31 1, 1, 1, 1, 1, 1, 1, # r correction | |
32 ] | |
33 | |
34 control = reduce(lambda a, b: a<<1 | b, | |
35 #controlBits | |
36 reversed(controlBits) | |
37 ) | |
38 self.send("C" + hex(control)[2:].zfill(8)) | |
39 self.send("E0") | |
40 | |
41 def send(self, cmd, getResponse=True): | |
42 """ | |
43 send a command using the protocol from http://engr.biz/prod/SB-A/ | |
44 | |
45 we will attach the carriage return, cmd is just a string like 'V' | |
46 | |
47 Returns the response line, like '+OK' | |
48 """ | |
49 try: | |
50 self.s.write(cmd + "\r") | |
817 | 51 except (OSError, SerialException): |
816
926adf586b08
sba: no retries, just die and get restarted
drewp <drewp@bigasterisk.com>
parents:
812
diff
changeset
|
52 os.abort() |
808 | 53 |
54 if getResponse: | |
55 return self.s.readline().strip() | |
56 | |
57 def rgbs(self, rgbs): | |
58 """ | |
59 send a set of full rgb packets. Values are 0..1023. | |
60 """ | |
61 t1 = time.time() | |
62 for (r,g,b) in rgbs: | |
63 packed = (b & 0x3ff) << 20 | (r & 0x3ff) << 10 | (g & 0x3ff) | |
64 self.send("D%08x" % packed, getResponse=False) | |
65 | |
66 self.send("L1", getResponse=False) | |
67 self.send("L0", getResponse=False) | |
68 sends = time.time() - t1 | |
69 # doing all the reads together triples the transmission rate | |
70 t2 = time.time() | |
71 [self.s.readline() for loop in range(2 + len(rgbs))] | |
72 reads = time.time() - t2 | |
73 | |
74 log.msg("%.1f ms for sends, %.1f ms for reads" % ( | |
75 1000 * sends, 1000 * reads)) | |
76 | |
77 class BriteChain(object): | |
78 def __init__(self, sba): | |
79 self.sba = sba | |
80 self.colors = [] | |
81 | |
812
f97ad4f038c0
fix the no-op ping() call in sba
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
82 def ping(self): |
f97ad4f038c0
fix the no-op ping() call in sba
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
83 self.sba.ping() |
f97ad4f038c0
fix the no-op ping() call in sba
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
84 |
808 | 85 def setColor(self, pos, color): |
86 """color is (r,g,b) 10-bit int. The highest position you ever | |
87 set is how many channels we'll output""" | |
88 if len(self.colors) <= pos: | |
89 self.colors.extend([(0,0,0)]*(pos - len(self.colors) + 1)) | |
90 self.colors[pos] = color | |
91 self.refresh() | |
92 | |
93 def getColor(self, pos): | |
94 try: | |
95 return self.colors[pos] | |
96 except IndexError: | |
97 return (0,0,0) | |
98 | |
99 def refresh(self): | |
100 self.sba.rgbs(self.colors[::-1]) | |
101 | |
102 class IndexHandler(cyclone.web.RequestHandler): | |
103 def get(self): | |
809
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
808
diff
changeset
|
104 self.settings.chain.ping() |
808 | 105 self.set_header("Content-type", "text/html") |
106 self.write(open("sba.html").read()) | |
107 | |
108 class BriteHandler(cyclone.web.RequestHandler): | |
109 """ | |
110 /brite/0 is the first shiftbrite on the chain. Put a text/plain | |
111 color like #ffffff (8-bit) or a application/json value like | |
112 {"rgb10":[1023,1023,1023]} (for 10-bit). GET (with accept, to pick | |
113 your format) to learn the current color. | |
114 | |
115 /brite/1 affects the second shiftbrite on the chain, etc | |
116 """ | |
117 def put(self, pos): | |
118 d = self.request.body | |
119 ctype = self.request.headers.get("Content-Type") | |
120 if ';' in ctype: | |
121 ctype = ctype.split(';')[0].strip() | |
122 if ctype == 'text/plain': | |
123 color = decode8bitHexColor(d) | |
124 elif ctype == 'application/json': | |
844 | 125 color = json.loads(d)['rgb10'] |
808 | 126 elif ctype == 'application/x-www-form-urlencoded': |
127 color = decode8bitHexColor(cgi.parse_qs(d)['color'][0]) | |
128 else: | |
129 self.response.set_status(415, "use text/plain, application/json, " | |
130 "or application/x-www-form-urlencoded") | |
131 return | |
132 | |
133 self.settings.chain.setColor(int(pos), color) | |
134 self.set_header("Content-Type", "text/plain") | |
135 self.write("set %s\n" % pos) | |
136 | |
137 def post(self, pos): | |
138 self.put(pos) | |
139 self.redirect("..") | |
140 | |
141 def get(self, pos): | |
142 # todo: content neg | |
143 color = self.settings.chain.getColor(int(pos)) | |
144 self.set_header("Content-Type", "text/plain") | |
145 self.write(encode8bitHexColor(color)) | |
146 | |
147 def decode8bitHexColor(s): | |
148 return [4 * int(s.lstrip('#')[i:i+2], 16) for i in [0, 2, 4]] | |
149 def encode8bitHexColor(color): | |
150 return "#%02X%02X%02X" % (color[0] // 4, color[1] // 4, color[2] // 4) | |
151 | |
152 class Application(cyclone.web.Application): | |
153 def __init__(self, chain): | |
154 handlers = [ | |
155 (r"/", IndexHandler), | |
156 (r"/brite/(\d+)", BriteHandler), | |
157 ] | |
158 | |
159 settings = { | |
160 "static_path": "./static", | |
161 "template_path": "./template", | |
162 "chain" : chain, | |
163 } | |
164 | |
165 cyclone.web.Application.__init__(self, handlers, **settings) | |
166 | |
167 def main(): | |
168 parser = argparse.ArgumentParser(description='drive sba lights') | |
169 parser.add_argument('-v', '--verbose', action="store_true", help='logging') | |
170 args = parser.parse_args() | |
171 | |
844 | 172 sba = Sba("/dev/ttyUSB3")#/dev/serial/by-id/usb-http:__engr.biz_CDC_RS-232_SB-Av1.0-if00-port0") |
808 | 173 |
174 chain = BriteChain(sba) | |
175 | |
176 if 0: # todo: stick test patterns like this on some other resource | |
177 while 1: | |
178 t1 = time.time() | |
179 steps = 0 | |
180 for x in range(0, 1024, 5): | |
181 steps += 1 | |
182 sba.rgbs([(x, x, x)] * 2) | |
183 print steps / (time.time() - t1) | |
184 | |
185 if args.verbose: | |
186 log.startLogging(sys.stdout) | |
187 reactor.listenTCP(9060, Application(chain)) | |
188 reactor.run() | |
189 | |
190 if __name__ == "__main__": | |
191 main() |