annotate service/busyboxArduino/busybox.py @ 157:28c2db876548

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