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