annotate service/arduinoNode/devices.py @ 192:6c6897a139da

support for pwm board Ignore-this: 4bf59699c590c75c79e6ae72d719343a
author drewp@bigasterisk.com
date Sun, 30 Aug 2015 01:19:29 -0700
parents 57e6d328d45b
children f8ffb9d8d982
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):
185
2161c71c7b02 support for device code in the idle loop
drewp@bigasterisk.com
parents: 176
diff changeset
78 """C code to emit in the global section.
2161c71c7b02 support for device code in the idle loop
drewp@bigasterisk.com
parents: 176
diff changeset
79
2161c71c7b02 support for device code in the idle loop
drewp@bigasterisk.com
parents: 176
diff changeset
80 Note that 'frame' (uint8) is available and increments each frame.
2161c71c7b02 support for device code in the idle loop
drewp@bigasterisk.com
parents: 176
diff changeset
81 """
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
82 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
83
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
84 def generateSetupCode(self):
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
85 """C code to emit in setup()"""
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
86 return ''
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
87
185
2161c71c7b02 support for device code in the idle loop
drewp@bigasterisk.com
parents: 176
diff changeset
88 def generateIdleCode(self):
2161c71c7b02 support for device code in the idle loop
drewp@bigasterisk.com
parents: 176
diff changeset
89 """C code to emit in the serial-read loop"""
2161c71c7b02 support for device code in the idle loop
drewp@bigasterisk.com
parents: 176
diff changeset
90 return ''
2161c71c7b02 support for device code in the idle loop
drewp@bigasterisk.com
parents: 176
diff changeset
91
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
92 def generatePollCode(self):
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
93 """
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
94 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
95 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
96 try to poll this device.
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
97 """
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
98 return ''
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
99
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
100 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
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 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
103 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
104 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
105 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
106 """
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
107 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
108
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
109 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
110 """
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
111 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
112 to sendOutput
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
113 """
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
114 return []
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
115
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
116 def outputWidgets(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
117 """
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
118 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
119 handler you have in sendOutput
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
120 """
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
121 return []
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
122
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
123 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
124 """
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
125 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
126 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
127 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
128 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
129
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
130 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
131 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
132 """
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
133 raise NotImplementedError
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
134
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
135 _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
136 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
137 _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
138 return deviceType
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
139
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
140 @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
141 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
142 @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
143 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
144 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
145
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
146 def generatePollCode(self):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
147 return "Serial.write('k');"
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
148
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
149 def readFromPoll(self, read):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
150 if read(1) != 'k':
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
151 raise ValueError('invalid ping response')
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
152 return [(self.uri, ROOM['ping'], ROOM['ok'])]
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
153
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
154 def watchPrefixes(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
155 return [(self.uri, ROOM['ping'])]
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
156
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
157 @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
158 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
159 deviceType = ROOM['MotionSensor']
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
160 def generateSetupCode(self):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
161 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
162 'pin': self.pinNumber,
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
163 }
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
165 def generatePollCode(self):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
166 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
167 'pin': self.pinNumber
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
168 }
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
169
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
170 def readFromPoll(self, read):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
171 b = read(1)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
172 if b not in 'yn':
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
173 raise ValueError('unexpected response %r' % b)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
174 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
175
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 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
177 (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
178 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
179 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
180 ]
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
181
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
182 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
183 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
184 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
185 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
186 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
187 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
188 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
189 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
190 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
191
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
192 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
193 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
194 (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
195 (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
196 ]
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
197
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
198 @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
199 class OneWire(DeviceType):
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
200 """
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
201 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
202 are also to be handled under this object)
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
203 """
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
204 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
205
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 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
207 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
208
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 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
210 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
211
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 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
213 # 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
214 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
215 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
216 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
217 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
218 #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
219
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 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
221 sensors.begin();
186
57e6d328d45b temp sensor try to work with idle loop (but there are still stutters)
drewp@bigasterisk.com
parents: 185
diff changeset
222 sensors.setWaitForConversion(false);
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
223 sensors.getAddress(tempSensorAddress, 0);
186
57e6d328d45b temp sensor try to work with idle loop (but there are still stutters)
drewp@bigasterisk.com
parents: 185
diff changeset
224 sensors.setResolution(tempSensorAddress, 9); // down from 12 to avoid flicker
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
225 }
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
226 ''' % dict(pinNumber=self.pinNumber)
186
57e6d328d45b temp sensor try to work with idle loop (but there are still stutters)
drewp@bigasterisk.com
parents: 185
diff changeset
227
57e6d328d45b temp sensor try to work with idle loop (but there are still stutters)
drewp@bigasterisk.com
parents: 185
diff changeset
228 def generateSetupCode(self):
57e6d328d45b temp sensor try to work with idle loop (but there are still stutters)
drewp@bigasterisk.com
parents: 185
diff changeset
229 return 'initSensors();'
57e6d328d45b temp sensor try to work with idle loop (but there are still stutters)
drewp@bigasterisk.com
parents: 185
diff changeset
230
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
231 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
232 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
233 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
234 sensors.requestTemperatures();
186
57e6d328d45b temp sensor try to work with idle loop (but there are still stutters)
drewp@bigasterisk.com
parents: 185
diff changeset
235 // not waiting for conversion at all is fine- the temps will update soon
57e6d328d45b temp sensor try to work with idle loop (but there are still stutters)
drewp@bigasterisk.com
parents: 185
diff changeset
236 //unsigned long until = millis() + 750; while(millis() < until) {idle();}
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
237 float newTemp = sensors.getTempF(tempSensorAddress);
186
57e6d328d45b temp sensor try to work with idle loop (but there are still stutters)
drewp@bigasterisk.com
parents: 185
diff changeset
238 idle();
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
239 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
240 (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
241 // 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
242 // 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
243 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
244 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
245 }
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 Serial.print(newTemp);
186
57e6d328d45b temp sensor try to work with idle loop (but there are still stutters)
drewp@bigasterisk.com
parents: 185
diff changeset
247 idle();
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
248 Serial.print('\n');
186
57e6d328d45b temp sensor try to work with idle loop (but there are still stutters)
drewp@bigasterisk.com
parents: 185
diff changeset
249 idle();
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
250 Serial.print((char)i);
186
57e6d328d45b temp sensor try to work with idle loop (but there are still stutters)
drewp@bigasterisk.com
parents: 185
diff changeset
251 idle();
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
252 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
253 }
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
254 '''
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
255
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
256 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
257 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
258 retries = ord(read(1))
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
259 # 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
260 return [
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
261 (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
262 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
263 (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
264 ]
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
265
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
266 def watchPrefixes(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
267 # 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
268 # about eliminating it.
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
269 return [(self.uri, ROOM['temperatureF']),
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
270 (self.uri, ROOM['temperatureRetries']),
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
271 ]
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
272
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
273 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
274 return chr(int(min(255, max(0, f * 255))))
192
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
275
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
276 @register
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
277 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
278 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
279 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
280 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
281 '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
282 }
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
283
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
284 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
285 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
286
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
287 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
288 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
289 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
290 value = float(statements[0][2])
172
715c1c42185e obey ActiveLowOutput correctly
drewp@bigasterisk.com
parents: 170
diff changeset
291 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
292 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
293 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
294
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
295 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
296 return r'''
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
297 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
298 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
299 ''' % dict(pin=self.pinNumber)
170
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 def outputWidgets(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
302 return [{
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
303 'element': 'output-slider',
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
304 'min': 0,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
305 'max': 1,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
306 'step': 1 / 255,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
307 'subj': self.uri,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
308 'pred': ROOM['brightness'],
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
309 }]
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 @register
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
312 class DigitalOutput(DeviceType):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
313 deviceType = ROOM['DigitalOutput']
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
314 def generateSetupCode(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
315 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
316 'pin': self.pinNumber,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
317 }
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
318
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
319 def outputPatterns(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
320 return [(self.uri, ROOM['level'], None)]
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
321
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
322 def sendOutput(self, statements, write, read):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
323 assert len(statements) == 1
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
324 assert statements[0][:2] == (self.uri, ROOM['level'])
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
325 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
326 write(chr(value))
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
327
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
328 def generateActionCode(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
329 return r'''
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
330 while(Serial.available() < 1) NULL;
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
331 digitalWrite(%(pin)d, Serial.read());
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
332 ''' % dict(pin=self.pinNumber)
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
333
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
334 def outputWidgets(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
335 return [{
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
336 'element': 'output-switch',
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
337 'subj': self.uri,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
338 'pred': ROOM['level'],
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
339 }]
192
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
340
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
341 @register
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
342 class PwmBoard(DeviceType):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
343 deviceType = ROOM['PwmBoard']
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
344 @classmethod
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
345 def findInstances(cls, graph, board):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
346 for row in graph.query("""SELECT DISTINCT ?dev ?sda ?scl WHERE {
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
347 ?board :hasPin ?sdaPin .
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
348 ?board :hasPin ?sclPin .
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
349 ?sdaPin :pinNumber ?sda; :connectedTo ?sdaConn .
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
350 ?sclPin :pinNumber ?scl; :connectedTo ?sclConn .
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
351 ?dev a :PwmBoard;
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
352 :scl ?sclConn;
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
353 :sda ?sdaConn .
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
354 }""", initBindings=dict(board=board), initNs={'': ROOM}):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
355 if (row.sda, row.scl) != (Literal('a4'), Literal('a5')):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
356 raise NotImplementedError(row)
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
357 outs = {}
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
358 for out in graph.query("""SELECT DISTINCT ?area ?chan WHERE {
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
359 ?dev :output [:area ?area; :channel ?chan] .
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
360 }""", initBindings=dict(dev=row.dev), initNs={'': ROOM}):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
361 outs[out.area] = out.chan.toPython()
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
362 yield cls(graph, row.dev, outs=outs)
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
363
192
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
364 def __init__(self, graph, dev, outs):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
365 super(PwmBoard, self).__init__(graph, dev, pinNumber=None)
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
366 self.codeVals = {'pwm': 'pwm%s' % (hash(str(dev)) % 99999)}
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
367 self.outs = outs
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
368
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
369 def generateIncludes(self):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
370 return ['Wire.h', 'Adafruit_PWMServoDriver.h']
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
371
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
372 def generateArduinoLibs(self):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
373 return ['Wire', 'Adafruit-PWM-Servo-Driver-Library']
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
374
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
375 def generateGlobalCode(self):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
376 return r'''
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
377 Adafruit_PWMServoDriver %(pwm)s = Adafruit_PWMServoDriver(0x40);
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
378 ''' % self.codeVals
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
379
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
380 def generateSetupCode(self):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
381 return '''
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
382 %(pwm)s.begin();
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
383 %(pwm)s.setPWMFreq(1200);
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
384 ''' % self.codeVals
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
385
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
386 def generateActionCode(self):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
387 return r'''
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
388 while(Serial.available() < 3) NULL;
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
389 byte chan = Serial.read();
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
390 uint16_t level = uint16_t(Serial.read()) << 8;
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
391 level |= Serial.read();
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
392 %(pwm)s.setPWM(chan, 0, level);
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
393 ''' % self.codeVals
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
394
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
395 def outputPatterns(self):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
396 return [(area, ROOM['brightness'], None) for area in self.outs]
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
397
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
398 def sendOutput(self, statements, write, read):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
399 assert len(statements) == 1
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
400 assert statements[0][1] == ROOM['brightness'];
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
401 chan = self.outs[statements[0][0]]
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
402 value = float(statements[0][2])
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
403 v12 = int(min(4095, max(0, value * 4095)))
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
404 write(chr(chan) + chr(v12 >> 8) + chr(v12 & 0xff))
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
405
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
406 def outputWidgets(self):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
407 return [{
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
408 'element': 'output-slider',
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
409 'min': 0,
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
410 'max': 1,
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
411 'step': 1 / 255,
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
412 'subj': area,
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
413 'pred': ROOM['brightness'],
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
414 } for area in self.outs]
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
415
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
416 @register
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
417 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
418 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
419 @classmethod
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
420 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
421 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
422 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
423 ?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
424 ?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
425 :connectedTo ?devPin .
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
426 ?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
427 ?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
428 } 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
429 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
430 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
431 initNs={'': ROOM}),
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
432 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
433 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
434 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
435 in connections)
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
436 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
437
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
438 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
439 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
440 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
441
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
442 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
443 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
444
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
445 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
446 return ['ST7565']
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
447
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
448 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
449 return '''
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
450 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
451 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
452 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
453 ''' % 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
454 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
455 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
456 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
457 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
458
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
459 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
460 return '''
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
461 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
462 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
463 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
464 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
465
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
466 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
467 '''
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
468
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
469 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
470 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
471
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
472 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
473 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
474 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
475 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
476 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
477 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
478
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
479 def outputWidgets(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
480 return [{
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
481 'element': 'output-fixed-text',
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
482 'cols': 21,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
483 'rows': 8,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
484 'subj': self.uri,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
485 'pred': ROOM['text'],
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
486 }]
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
487
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
488 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
489 return '''
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
490 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
491 byte bufSize = Serial.read();
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
492 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
493 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
494 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
495 }
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
496 for (byte i = bufSize; i < sizeof(newtxt); ++i) {
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
497 newtxt[i] = 0;
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
498 }
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
499 glcd.clear();
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
500 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
501 glcd.display();
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
502 '''
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
503
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
504 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
505 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
506 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
507 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
508 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
509