comparison service/starArduino/starArduino.py @ 828:a422d875d94d

barcode support on star. triggers mpd song Ignore-this: 1038c3d0501bba595fbf701e30acac6 darcs-hash:20120304114228-312f9-8fc9a9198be2044359f35aebcd1c428a58cb4ea2.gz
author drewp <drewp@bigasterisk.com>
date Sun, 04 Mar 2012 03:42:28 -0800
parents bebb8f7c5a3e
children 0ab069867c64
comparison
equal deleted inserted replaced
827:f97547561b1d 828:a422d875d94d
4 """ 4 """
5 from __future__ import division 5 from __future__ import division
6 6
7 import sys, jsonlib 7 import sys, jsonlib
8 from twisted.internet import reactor, task 8 from twisted.internet import reactor, task
9 import cyclone.web 9 from twisted.internet.task import LoopingCall
10 import cyclone.web, restkit
10 11
11 sys.path.append("/my/proj/pixel/shiftweb") 12 sys.path.append("/my/proj/pixel/shiftweb")
12 from drvarduino import ShiftbriteArduino 13 from drvarduino import ShiftbriteArduino
13 from shiftweb import hexFromRgb, rgbFromHex 14 from shiftweb import hexFromRgb, rgbFromHex
14 15
43 colors[channel] = rgbFromHex(self.request.body) 44 colors[channel] = rgbFromHex(self.request.body)
44 self.settings.arduino.update(colors) 45 self.settings.arduino.update(colors)
45 self.set_header("Content-Type", "text/plain") 46 self.set_header("Content-Type", "text/plain")
46 self.write("updated %r" % colors) 47 self.write("updated %r" % colors)
47 48
49 class Barcode(PrettyErrorHandler, cyclone.web.RequestHandler):
50 def get(self):
51 self.set_header("Content-Type", "text/plain")
52 ser = self.settings.arduino.ser
53 ser.write("\x60\x02")
54 self.write(str(ser.readJson()))
55
56 class BarcodeBeep(PrettyErrorHandler, cyclone.web.RequestHandler):
57 def put(self):
58 self.set_header("Content-Type", "text/plain")
59 ser = self.settings.arduino.ser
60 ser.write("\x60\x03")
61 self.write(str(ser.readJson()))
62
63 def barcodeWatch(arduino, postBarcode):
64 try:
65 arduino.ser.write("\xff\xfb")
66 ret = arduino.readJson()
67 if ret['barcode']:
68 if ret['barcode'] == "27 80 48 13 ":
69 pass # scanner's beep response
70 else:
71 arduino.ser.write("\xff\xfc")
72 arduino.readJson() # my beep response
73 s = ''.join(chr(int(x)) for x in ret['barcode'].split())
74 for code in s.split('\x02'):
75 if not code:
76 continue
77 if not code.endswith('\x03'):
78 print "couldn't read %r" % code
79 return
80 codeType = {'A':'UPC-A',
81 'B':'JAN-8',
82 'E':'UPC-E',
83 'N':'NW-7',
84 'C':'CODE39',
85 'I':'ITF',
86 'K':'CODE128',
87 }[code[0]]
88 code = code[1:-1]
89 postBarcode.post(payload=jsonlib.dumps(
90 {'barcodeType':codeType, 'code':code}),
91 headers={"content-type" :
92 "application/json"})
93
94 except ValueError, e:
95 print "err", e
96
97
48 class Graph(PrettyErrorHandler, cyclone.web.RequestHandler): 98 class Graph(PrettyErrorHandler, cyclone.web.RequestHandler):
49 def get(self): 99 def get(self):
50 raise NotImplementedError 100 raise NotImplementedError
51 101
52 if __name__ == '__main__': 102 if __name__ == '__main__':
53 sb = ShiftbriteArduino(numChannels=3) 103 class A(ShiftbriteArduino):
104 # from loggingserial.py
105 def readJson(self):
106 line = ''
107 while True:
108 c = self.ser.read(1)
109 #print "gotchar", repr(c)
110 if c:
111 line += c
112 if c == "\n":
113 break
114 else:
115 raise ValueError("timed out waiting for chars")
116 return jsonlib.loads(line)
117
118 sb = A(numChannels=3)
54 119
55 colors = [(0,0,0)] * sb.numChannels 120 colors = [(0,0,0)] * sb.numChannels
56 121
57 aw = ArduinoWatcher(sb) 122 aw = ArduinoWatcher(sb)
58 task.LoopingCall(aw.poll).start(1.0/20) 123 task.LoopingCall(aw.poll).start(1.0/20)
124
125 postBarcode = restkit.Resource('http://star:9011/barcodeScan')
126 task.LoopingCall(barcodeWatch, sb, postBarcode).start(interval=.5)
59 127
60 reactor.listenTCP(9014, cyclone.web.Application([ 128 reactor.listenTCP(9014, cyclone.web.Application([
61 (r'/', Index), 129 (r'/', Index),
62 (r'/temperature', Temperature), 130 (r'/temperature', Temperature),
63 (r'/brite/(\d+)', Brite), 131 (r'/brite/(\d+)', Brite),
132 (r'/barcode', Barcode),
133 (r'/barcode/beep', BarcodeBeep),
64 (r'/graph', Graph), 134 (r'/graph', Graph),
65 ], arduino=sb, colors=colors)) 135 ], arduino=sb, colors=colors))
66 reactor.run() 136 reactor.run()