annotate service/busyboxArduino/busybox.py @ 962:70fd4c07a326

busybox py and arduino Ignore-this: eea165c21e11600b5ac6787cbbb2239 darcs-hash:20150120020247-312f9-8977dd56e356d2752bc2cdf574aa37cef2036329
author drewp <drewp@bigasterisk.com>
date Mon, 19 Jan 2015 18:02:47 -0800
parents
children 4c62b6d416a7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
962
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
1 from __future__ import division
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
2 import serial, struct, json, sys
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
3
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
4 from rdflib import Namespace, Literal
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
5 sys.path.append("/my/site/magma")
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
6 from stategraph import StateGraph
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
7 import klein
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
8 from twisted.internet import task
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
9 import random, time
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
10
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
11 import restkit
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
12 reasoning = restkit.Resource("http://bang:9071/", timeout=1)
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
13 ROOM = Namespace("http://projects.bigasterisk.com/room/")
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
14
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
15 def sendOneShot(stmt):
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
16 try:
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
17 t1 = time.time()
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
18 print "post to reasoning", stmt
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
19 p = reasoning.post("oneShot",
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
20 headers={"content-type": "text/n3"},
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
21 payload=("%s %s %s ." %
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
22 tuple(n.n3() for n in stmt)))
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
23 except restkit.errors.RequestFailed:
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
24 print "oneShot failed"
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
25 return
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
26 print "posted in %.04f sec. %r" % (time.time() - t1,
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
27 p.body_string())
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
28
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
29
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
30 class Busybox(object):
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
31 def __init__(self, port='/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A4001lVK-if00-port0'):
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
32 self.serial = serial.Serial(port, baudrate=115200, timeout=.2)
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
33
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
34 def poll(self):
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
35 for tries in range(5):
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
36 self.serial.write('\x60\x00')
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
37 line = self.serial.readline()
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
38 if not line.startswith('{'):
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
39 continue
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
40 if tries > 0:
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
41 print "after %s tries" % tries
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
42 return json.loads(line)
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
43 return {'error': 'invalid response'}
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
44
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
45 def writeMessage(self, row, col, text):
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
46 msg = struct.pack('BBBBB', 0x60, 0x01, row, col, len(text)) + text
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
47 self.serial.write(msg)
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
48
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
49 def setBacklight(self, level):
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
50 self.serial.write(struct.pack('BBB', 0x60, 0x02, level))
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
51
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
52 sendOneShot((ROOM['greets'],
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
53 ROOM['change'],
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
54 ROOM['down']))
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
55
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
56 bb = Busybox()
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
57 words = open('/usr/share/dict/words').readlines()
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
58
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
59 lastWordTime = 0
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
60 last = None
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
61 s1 = []
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
62
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
63 def poll():
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
64 global lastWordTime, s1, last
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
65 now = time.time()
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
66 if now - lastWordTime > 1:
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
67 msg = '%15s' % random.choice(words).strip()[:15]
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
68 bb.writeMessage(1, 1, msg)
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
69 lastWordTime = now
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
70 last = bb.poll()
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
71 if 'slider1' in last:
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
72 s1 = s1[-5:] + [last['slider1']]
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
73 if len(s1) > 4:
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
74 median = sorted(s1)[2]
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
75 bb.setBacklight(min(255, median // 4))
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
76 if 'keyDown' in last:
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
77 keyNum = last['keyDown']
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
78 sendOneShot((ROOM['ariBed/button%s' % keyNum],
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
79 ROOM['change'],
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
80 ROOM['down']))
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
81
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
82
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
83 @klein.route('/graph', methods=['GET'])
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
84 def getGraph(request):
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
85 g = StateGraph(ROOM.busybox)
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
86 g.add((ROOM.busybox, ROOM.localHour, Literal('x')))
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
87 for attr in ['slider1', 'slider2', 'slider3', 'slider4']:
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
88 # needs: smoothing, exp curve correction
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
89 g.add((ROOM['busybox/%s' % attr], ROOM.value, Literal(round(last[attr] / 1021, 3))))
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
90 request.setHeader('Content-type', 'application/x-trig')
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
91 return g.asTrig()
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
92
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
93 task.LoopingCall(poll).start(.05)
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
94
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
95 klein.run('0.0.0.0', port=9056)
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
96