annotate service/arduinoNode/devices.py @ 176:f81c4d3d774b

arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body Ignore-this: 744c3c7d95655430b8ec547e56f6b4bc
author drewp@bigasterisk.com
date Thu, 14 May 2015 01:26:12 -0700
parents 715c1c42185e
children 2161c71c7b02
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
1 from __future__ import division
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
2 import itertools
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
3 from rdflib import Namespace, RDF, URIRef, Literal
176
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 172
diff changeset
4 import time
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
5
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
6 ROOM = Namespace('http://projects.bigasterisk.com/room/')
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
7 XSD = Namespace('http://www.w3.org/2001/XMLSchema#')
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
8
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
9 def readLine(read):
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
10 buf = ''
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
11 for c in iter(lambda: read(1), '\n'):
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
12 buf += c
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
13 return buf
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
14
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
15 class DeviceType(object):
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
16 deviceType = None
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
17 @classmethod
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
18 def findInstances(cls, graph, board):
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
19 """
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
20 return any number of instances of this class for all the separately
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
21 controlled devices on the board. Two LEDS makes two instances,
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
22 but two sensors on the same onewire bus makes only one device
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
23 (which yields more statements).
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
24 """
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
25 for row in graph.query("""SELECT ?dev ?pinNumber WHERE {
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
26 ?board :hasPin ?pin .
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
27 ?pin :pinNumber ?pinNumber;
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
28 :connectedTo ?dev .
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
29 ?dev a ?thisType .
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
30 } ORDER BY ?dev""",
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
31 initBindings=dict(board=board,
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
32 thisType=cls.deviceType),
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
33 initNs={'': ROOM}):
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
34 yield cls(graph, row.dev, int(row.pinNumber))
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
35
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
36 # subclasses may add args to this
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
37 def __init__(self, graph, uri, pinNumber):
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
38 self.graph, self.uri = graph, uri
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
39 self.pinNumber = pinNumber
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
40
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
41 def description(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
42 return {
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
43 'uri': self.uri,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
44 'className': self.__class__.__name__,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
45 'pinNumber': self.pinNumber,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
46 'outputPatterns': self.outputPatterns(),
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
47 'watchPrefixes': self.watchPrefixes(),
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
48 'outputWidgets': self.outputWidgets(),
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
49 }
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
50
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
51 def readFromPoll(self, read):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
52 """
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
53 read an update message returned as part of a poll bundle. This may
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
54 consume a varying number of bytes depending on the type of
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
55 input (e.g. IR receiver).
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
56 Returns rdf statements.
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
57 """
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
58 raise NotImplementedError('readFromPoll in %s' % self.__class__)
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
59
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
60 def watchPrefixes(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
61 """
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
62 subj,pred pairs of the statements that might be returned from
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
63 readFromPoll, so the dashboard knows what it should
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
64 watch. This should be eliminated, as the dashboard should just
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
65 always watch the whole tree of statements starting self.uri
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
66 """
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
67 return []
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
68
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
69 def generateIncludes(self):
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
70 """filenames of .h files to #include"""
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
71 return []
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
72
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
73 def generateArduinoLibs(self):
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
74 """names of libraries for the ARDUINO_LIBS line in the makefile"""
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
75 return []
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
76
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
77 def generateGlobalCode(self):
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
78 """C code to emit in the global section"""
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
79 return ''
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
80
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
81 def generateSetupCode(self):
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
82 """C code to emit in setup()"""
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
83 return ''
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
84
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
85 def generatePollCode(self):
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
86 """
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
87 C code to run a poll update. This should Serial.write its output
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
88 for readFromPoll to consume. If this returns nothing, we don't
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
89 try to poll this device.
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
90 """
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
91 return ''
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
92
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
93 def generateActionCode(self):
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
94 """
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
95 If the host side runs sendOutput, this C code will be run on the
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
96 board to receive whatever sendOutput writes. Each sendOutput
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
97 write(buf) call should be matched with len(buf) Serial.read()
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
98 calls in here.
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
99 """
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
100 return ''
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
101
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
102 def outputPatterns(self):
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
103 """
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
104 Triple patterns, using None as a wildcard, that should be routed
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
105 to sendOutput
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
106 """
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
107 return []
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
108
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
109 def outputWidgets(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
110 """
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
111 structs to make output widgets on the dashboard. ~1 of these per
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
112 handler you have in sendOutput
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
113 """
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
114 return []
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
115
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
116 def sendOutput(self, statements, write, read):
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
117 """
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
118 If we got statements that match this class's outputPatterns, this
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
119 will be called with the statements that matched, and a serial
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
120 write method. What you write here will be available as
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
121 Serial.read in the generateActionCode C code.
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
122
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
123 Todo: it would be fine to read back confirmations or
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
124 whatever. Just need a way to collect them into graph statements.
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
125 """
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
126 raise NotImplementedError
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
127
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
128 _knownTypes = set()
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
129 def register(deviceType):
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
130 _knownTypes.add(deviceType)
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
131 return deviceType
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
132
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
133 @register
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
134 class PingInput(DeviceType):
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
135 @classmethod
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
136 def findInstances(cls, graph, board):
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
137 return [cls(graph, board, None)]
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
138
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
139 def generatePollCode(self):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
140 return "Serial.write('k');"
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
141
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
142 def readFromPoll(self, read):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
143 if read(1) != 'k':
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
144 raise ValueError('invalid ping response')
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
145 return [(self.uri, ROOM['ping'], ROOM['ok'])]
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
146
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
147 def watchPrefixes(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
148 return [(self.uri, ROOM['ping'])]
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
149
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
150 @register
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
151 class MotionSensorInput(DeviceType):
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
152 deviceType = ROOM['MotionSensor']
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
153 def generateSetupCode(self):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
154 return 'pinMode(%(pin)d, INPUT); digitalWrite(%(pin)d, LOW);' % {
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
155 'pin': self.pinNumber,
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
156 }
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
157
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
158 def generatePollCode(self):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
159 return "Serial.write(digitalRead(%(pin)d) ? 'y' : 'n');" % {
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
160 'pin': self.pinNumber
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
161 }
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
162
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
163 def readFromPoll(self, read):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
164 b = read(1)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
165 if b not in 'yn':
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
166 raise ValueError('unexpected response %r' % b)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
167 motion = b == 'y'
176
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 172
diff changeset
168
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 172
diff changeset
169 return [
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 172
diff changeset
170 (self.uri, ROOM['sees'],
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 172
diff changeset
171 ROOM['motion'] if motion else ROOM['noMotion']),
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 172
diff changeset
172 self.recentMotionStatement(motion),
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 172
diff changeset
173 ]
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
174
176
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 172
diff changeset
175 def recentMotionStatement(self, motion):
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 172
diff changeset
176 if not hasattr(self, 'lastMotionTime'):
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 172
diff changeset
177 self.lastMotionTime = 0
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 172
diff changeset
178 now = time.time()
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 172
diff changeset
179 if motion:
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 172
diff changeset
180 self.lastMotionTime = now
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 172
diff changeset
181 recentMotion = now - self.lastMotionTime < 60 * 10
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 172
diff changeset
182 return (self.uri, ROOM['seesRecently'],
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 172
diff changeset
183 ROOM['motion'] if recentMotion else ROOM['noMotion'])
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 172
diff changeset
184
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
185 def watchPrefixes(self):
176
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 172
diff changeset
186 return [
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 172
diff changeset
187 (self.uri, ROOM['sees']),
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 172
diff changeset
188 (self.uri, ROOM['seesRecently']),
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 172
diff changeset
189 ]
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
190
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
191 @register
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
192 class OneWire(DeviceType):
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
193 """
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
194 A OW bus with temperature sensors (and maybe other devices, which
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
195 are also to be handled under this object)
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
196 """
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
197 deviceType = ROOM['OneWire']
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
198
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
199 def generateIncludes(self):
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
200 return ['OneWire.h', 'DallasTemperature.h']
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
201
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
202 def generateArduinoLibs(self):
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
203 return ['OneWire', 'DallasTemperature']
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
204
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
205 def generateGlobalCode(self):
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
206 # not yet isolated to support multiple OW buses
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
207 return '''
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
208 OneWire oneWire(%(pinNumber)s);
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
209 DallasTemperature sensors(&oneWire);
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
210 DeviceAddress tempSensorAddress;
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
211 #define NUM_TEMPERATURE_RETRIES 2
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
212
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
213 void initSensors() {
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
214 sensors.begin();
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
215 sensors.getAddress(tempSensorAddress, 0);
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
216 sensors.setResolution(tempSensorAddress, 12);
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
217 }
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
218 ''' % dict(pinNumber=self.pinNumber)
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
219
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
220 def generatePollCode(self):
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
221 return r'''
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
222 for (int i=0; i<NUM_TEMPERATURE_RETRIES; i++) {
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
223 sensors.requestTemperatures();
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
224 float newTemp = sensors.getTempF(tempSensorAddress);
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
225 if (i < NUM_TEMPERATURE_RETRIES-1 &&
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
226 (newTemp < -100 || newTemp > 180)) {
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
227 // too many errors that were fixed by restarting arduino.
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
228 // trying repeating this much init
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
229 initSensors();
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
230 continue;
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
231 }
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
232 Serial.print(newTemp);
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
233 Serial.print('\n');
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
234 Serial.print((char)i);
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
235 break;
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
236 }
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
237 '''
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
238
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
239 def readFromPoll(self, read):
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
240 newTemp = readLine(read)
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
241 retries = ord(read(1))
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
242 # uri will change; there could (likely) be multiple connected sensors
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
243 return [
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
244 (self.uri, ROOM['temperatureF'],
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
245 Literal(newTemp, datatype=XSD['decimal'])),
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
246 (self.uri, ROOM['temperatureRetries'], Literal(retries)),
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
247 ]
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
248
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
249 def watchPrefixes(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
250 # these uris will become dynamic! see note on watchPrefixes
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
251 # about eliminating it.
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
252 return [(self.uri, ROOM['temperatureF']),
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
253 (self.uri, ROOM['temperatureRetries']),
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
254 ]
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
255
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
256 def byteFromFloat(f):
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
257 return chr(int(min(255, max(0, f * 255))))
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
258
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
259 @register
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
260 class LedOutput(DeviceType):
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
261 deviceType = ROOM['LedOutput']
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
262 def generateSetupCode(self):
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
263 return 'pinMode(%(pin)d, OUTPUT); digitalWrite(%(pin)d, LOW);' % {
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
264 'pin': self.pinNumber,
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
265 }
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
266
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
267 def outputPatterns(self):
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
268 return [(self.uri, ROOM['brightness'], None)]
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
269
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
270 def sendOutput(self, statements, write, read):
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
271 assert len(statements) == 1
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
272 assert statements[0][:2] == (self.uri, ROOM['brightness'])
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
273 value = float(statements[0][2])
172
715c1c42185e obey ActiveLowOutput correctly
drewp@bigasterisk.com
parents: 170
diff changeset
274 if (self.uri, RDF.type, ROOM['ActiveLowOutput']) in self.graph:
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
275 value = 1 - value
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
276 write(byteFromFloat(value))
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
277
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
278 def generateActionCode(self):
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
279 return r'''
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
280 while(Serial.available() < 1) NULL;
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
281 analogWrite(%(pin)d, Serial.read());
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
282 ''' % dict(pin=self.pinNumber)
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
283
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
284 def outputWidgets(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
285 return [{
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
286 'element': 'output-slider',
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
287 'min': 0,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
288 'max': 1,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
289 'step': 1 / 255,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
290 'subj': self.uri,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
291 'pred': ROOM['brightness'],
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
292 }]
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
293
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
294 @register
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
295 class DigitalOutput(DeviceType):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
296 deviceType = ROOM['DigitalOutput']
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
297 def generateSetupCode(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
298 return 'pinMode(%(pin)d, OUTPUT); digitalWrite(%(pin)d, LOW);' % {
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
299 'pin': self.pinNumber,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
300 }
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
301
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
302 def outputPatterns(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
303 return [(self.uri, ROOM['level'], None)]
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
304
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
305 def sendOutput(self, statements, write, read):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
306 assert len(statements) == 1
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
307 assert statements[0][:2] == (self.uri, ROOM['level'])
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
308 value = {"high": 1, "low": 0}[str(statements[0][2])]
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
309 write(chr(value))
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
310
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
311 def generateActionCode(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
312 return r'''
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
313 while(Serial.available() < 1) NULL;
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
314 digitalWrite(%(pin)d, Serial.read());
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
315 ''' % dict(pin=self.pinNumber)
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
316
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
317 def outputWidgets(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
318 return [{
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
319 'element': 'output-switch',
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
320 'subj': self.uri,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
321 'pred': ROOM['level'],
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
322 }]
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
323
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
324 @register
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
325 class ST7576Lcd(DeviceType):
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
326 deviceType = ROOM['ST7565Lcd']
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
327 @classmethod
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
328 def findInstances(cls, graph, board):
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
329 grouped = itertools.groupby(
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
330 graph.query("""SELECT DISTINCT ?dev ?pred ?pinNumber WHERE {
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
331 ?board :hasPin ?pin .
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
332 ?pin :pinNumber ?pinNumber;
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
333 :connectedTo ?devPin .
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
334 ?dev a :ST7565Lcd .
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
335 ?dev ?pred ?devPin .
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
336 } ORDER BY ?dev""",
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
337 initBindings=dict(board=board,
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
338 thisType=cls.deviceType),
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
339 initNs={'': ROOM}),
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
340 lambda row: row.dev)
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
341 for dev, connections in grouped:
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
342 connections = dict((role, int(num)) for unused_dev, role, num
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
343 in connections)
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
344 yield cls(graph, dev, connections=connections)
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
345
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
346 def __init__(self, graph, dev, connections):
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
347 super(ST7576Lcd, self).__init__(graph, dev, pinNumber=None)
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
348 self.connections = connections
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
349
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
350 def generateIncludes(self):
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
351 return ['ST7565.h']
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
352
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
353 def generateArduinoLibs(self):
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
354 return ['ST7565']
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
355
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
356 def generateGlobalCode(self):
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
357 return '''
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
358 ST7565 glcd(%(SID)d, %(SCLK)d, %(A0)d, %(RST)d, %(CS)d);
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
359 char newtxt[21*8+1];
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
360 unsigned int written;
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
361 ''' % dict(SID=self.connections[ROOM['lcdSID']],
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
362 SCLK=self.connections[ROOM['lcdSCLK']],
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
363 A0=self.connections[ROOM['lcdA0']],
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
364 RST=self.connections[ROOM['lcdRST']],
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
365 CS=self.connections[ROOM['lcdCS']])
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
366
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
367 def generateSetupCode(self):
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
368 return '''
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
369 glcd.st7565_init();
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
370 glcd.st7565_command(CMD_DISPLAY_ON);
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
371 glcd.st7565_command(CMD_SET_ALLPTS_NORMAL);
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
372 glcd.st7565_set_brightness(0x18);
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
373
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
374 glcd.display(); // show splashscreen
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
375 '''
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
376
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
377 def outputPatterns(self):
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
378 return [(self.uri, ROOM['text'], None)]
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
379
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
380 def sendOutput(self, statements, write, read):
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
381 assert len(statements) == 1
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
382 assert statements[0][:2] == (self.uri, ROOM['text'])
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
383 value = str(statements[0][2])
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
384 assert len(value) < 254, repr(value)
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
385 write(chr(len(value)) + value)
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
386
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
387 def outputWidgets(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
388 return [{
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
389 'element': 'output-fixed-text',
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
390 'cols': 21,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
391 'rows': 8,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
392 'subj': self.uri,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
393 'pred': ROOM['text'],
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
394 }]
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
395
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
396 def generateActionCode(self):
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
397 return '''
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
398 while(Serial.available() < 1) NULL;
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
399 byte bufSize = Serial.read();
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
400 for (byte i = 0; i < bufSize; ++i) {
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
401 while(Serial.available() < 1) NULL;
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
402 newtxt[i] = Serial.read();
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
403 }
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
404 for (byte i = bufSize; i < sizeof(newtxt); ++i) {
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
405 newtxt[i] = 0;
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
406 }
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
407 glcd.clear();
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
408 glcd.drawstring(0,0, newtxt);
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
409 glcd.display();
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
410 '''
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
411
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
412 def makeDevices(graph, board):
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
413 out = []
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
414 for dt in sorted(_knownTypes, key=lambda cls: cls.__name__):
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
415 out.extend(dt.findInstances(graph, board))
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
416 return out
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
417