diff 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
line wrap: on
line diff
--- a/service/starArduino/starArduino.py	Sun Mar 04 03:41:33 2012 -0800
+++ b/service/starArduino/starArduino.py	Sun Mar 04 03:42:28 2012 -0800
@@ -6,7 +6,8 @@
 
 import sys, jsonlib
 from twisted.internet import reactor, task
-import cyclone.web
+from twisted.internet.task import LoopingCall
+import cyclone.web, restkit
 
 sys.path.append("/my/proj/pixel/shiftweb")
 from drvarduino import ShiftbriteArduino
@@ -45,22 +46,91 @@
         self.set_header("Content-Type", "text/plain")
         self.write("updated %r" % colors)
     
+class Barcode(PrettyErrorHandler, cyclone.web.RequestHandler):
+    def get(self):
+        self.set_header("Content-Type", "text/plain")
+        ser = self.settings.arduino.ser
+        ser.write("\x60\x02")
+        self.write(str(ser.readJson()))
+
+class BarcodeBeep(PrettyErrorHandler, cyclone.web.RequestHandler):
+    def put(self):
+        self.set_header("Content-Type", "text/plain")
+        ser = self.settings.arduino.ser
+        ser.write("\x60\x03")
+        self.write(str(ser.readJson()))
+
+def barcodeWatch(arduino, postBarcode):
+    try:
+        arduino.ser.write("\xff\xfb")
+        ret = arduino.readJson()
+        if ret['barcode']:
+            if ret['barcode'] == "27 80 48 13 ":
+                pass # scanner's beep response
+            else:
+                arduino.ser.write("\xff\xfc")
+                arduino.readJson() # my beep response
+                s = ''.join(chr(int(x)) for x in ret['barcode'].split())
+                for code in s.split('\x02'):
+                    if not code:
+                        continue
+                    if not code.endswith('\x03'):
+                        print "couldn't read %r" % code
+                        return
+                    codeType = {'A':'UPC-A',
+                                'B':'JAN-8',
+                                'E':'UPC-E',
+                                'N':'NW-7',
+                                'C':'CODE39',
+                                'I':'ITF',
+                                'K':'CODE128',
+                                }[code[0]]
+                    code = code[1:-1]
+                    postBarcode.post(payload=jsonlib.dumps(
+                        {'barcodeType':codeType, 'code':code}),
+                                     headers={"content-type" :
+                                              "application/json"})
+
+    except ValueError, e:
+        print "err", e
+
+    
 class Graph(PrettyErrorHandler, cyclone.web.RequestHandler):    
     def get(self):
         raise NotImplementedError
     
 if __name__ == '__main__':
-    sb = ShiftbriteArduino(numChannels=3)
+    class A(ShiftbriteArduino):
+        # from loggingserial.py
+        def readJson(self):
+            line = ''
+            while True:
+                c = self.ser.read(1)
+                #print "gotchar", repr(c)
+                if c:
+                    line += c
+                    if c == "\n":
+                        break
+                else:
+                    raise ValueError("timed out waiting for chars")
+            return jsonlib.loads(line)
+
+    sb = A(numChannels=3)
 
     colors = [(0,0,0)] * sb.numChannels
 
     aw = ArduinoWatcher(sb)
     task.LoopingCall(aw.poll).start(1.0/20)
+
+    postBarcode = restkit.Resource('http://star:9011/barcodeScan')
+    task.LoopingCall(barcodeWatch, sb, postBarcode).start(interval=.5)
     
     reactor.listenTCP(9014, cyclone.web.Application([
         (r'/', Index),
         (r'/temperature', Temperature),
         (r'/brite/(\d+)', Brite),
+        (r'/barcode', Barcode),
+        (r'/barcode/beep', BarcodeBeep),
         (r'/graph', Graph),
         ], arduino=sb, colors=colors))
     reactor.run()