annotate service/arduinoNode/devices.py @ 218:f8ffb9d8d982

multi-boards on one service, new devices, devices return their current Ignore-this: e214852bca67519e79f9ddb3644576e1 values in the graph, jsonld support, multiple temp sensors on OW bus
author drewp@bigasterisk.com
date Sun, 03 Jan 2016 02:29:14 -0800
parents 6c6897a139da
children 0aa54404df19
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
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
2 import itertools, logging, struct, os
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#')
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
8 log = logging.getLogger()
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
9
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
10 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
11 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
12 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
13 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
14 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
15
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 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
17 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
18 @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
19 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
20 """
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 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
22 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
23 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
24 (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
25 """
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 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
27 ?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
28 ?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
29 :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
30 ?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
31 } 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
32 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
33 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
34 initNs={'': ROOM}):
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
35 log.info('found %s, a %s', row.dev, cls.deviceType)
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
36 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
37
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
38 # 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
39 def __init__(self, graph, uri, pinNumber):
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
40 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
41 self.pinNumber = pinNumber
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
42 self.hostStateInit()
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
43
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
44 def hostStateInit(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
45 """
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
46 If you don't want to use __init__, you can use this to set up
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
47 whatever storage you might need for hostStatements
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
48 """
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
49
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
50 def description(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
51 return {
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
52 'uri': self.uri,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
53 'className': self.__class__.__name__,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
54 'pinNumber': self.pinNumber,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
55 'outputPatterns': self.outputPatterns(),
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
56 'watchPrefixes': self.watchPrefixes(),
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
57 'outputWidgets': self.outputWidgets(),
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
58 }
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
59
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
60 def readFromPoll(self, read):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
61 """
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
62 read an update message returned as part of a poll bundle. This may
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
63 consume a varying number of bytes depending on the type of
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
64 input (e.g. IR receiver).
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
65 Returns rdf statements.
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
66 """
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
67 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
68
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
69 def hostStatements(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
70 """
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
71 Like readFromPoll but these statements come from the host-side
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
72 python code, not the connected device. Include output state
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
73 (e.g. light brightness) if its master version is in this
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
74 object. This method is called on /graph requests so it should
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
75 be fast.
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
76 """
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
77 return []
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
78
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
79 def watchPrefixes(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
80 """
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
81 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
82 readFromPoll, so the dashboard knows what it should
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
83 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
84 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
85 """
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
86 return []
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
87
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
88 def generateIncludes(self):
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
89 """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
90 return []
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
91
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
92 def generateArduinoLibs(self):
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
93 """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
94 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
95
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
96 def generateGlobalCode(self):
185
2161c71c7b02 support for device code in the idle loop
drewp@bigasterisk.com
parents: 176
diff changeset
97 """C code to emit in the global section.
2161c71c7b02 support for device code in the idle loop
drewp@bigasterisk.com
parents: 176
diff changeset
98
2161c71c7b02 support for device code in the idle loop
drewp@bigasterisk.com
parents: 176
diff changeset
99 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
100 """
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
101 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
102
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
103 def generateSetupCode(self):
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
104 """C code to emit in setup()"""
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
105 return ''
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
106
185
2161c71c7b02 support for device code in the idle loop
drewp@bigasterisk.com
parents: 176
diff changeset
107 def generateIdleCode(self):
2161c71c7b02 support for device code in the idle loop
drewp@bigasterisk.com
parents: 176
diff changeset
108 """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
109 return ''
2161c71c7b02 support for device code in the idle loop
drewp@bigasterisk.com
parents: 176
diff changeset
110
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
111 def generatePollCode(self):
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
112 """
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
113 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
114 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
115 try to poll this device.
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
116 """
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
117 return ''
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
118
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
119 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
120 """
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
121 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
122 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
123 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
124 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
125 """
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
126 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
127
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
128 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
129 """
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
130 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
131 to sendOutput
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 return []
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
134
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
135 def outputWidgets(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
136 """
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
137 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
138 handler you have in sendOutput
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
139 """
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
140 return []
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
141
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
142 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
143 """
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
144 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
145 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
146 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
147 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
148
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
149 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
150 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
151 """
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
152 raise NotImplementedError
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
153
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
154 _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
155 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
156 _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
157 return deviceType
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
158
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
159 @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
160 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
161 @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
162 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
163 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
164
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('k');"
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
167
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
168 def readFromPoll(self, read):
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
169 byte = read(1)
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
170 if byte != 'k':
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
171 raise ValueError('invalid ping response: chr(%s)' % ord(byte))
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
172 return [(self.uri, ROOM['ping'], ROOM['ok'])]
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
173
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
174 def watchPrefixes(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
175 return [(self.uri, ROOM['ping'])]
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
176
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
177 @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
178 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
179 deviceType = ROOM['MotionSensor']
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
180 def generateSetupCode(self):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
181 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
182 'pin': self.pinNumber,
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
183 }
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
184
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
185 def generatePollCode(self):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
186 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
187 'pin': self.pinNumber
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
188 }
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
189
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
190 def readFromPoll(self, read):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
191 b = read(1)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
192 if b not in 'yn':
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
193 raise ValueError('unexpected response %r' % b)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
194 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
195
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 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
197 (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
198 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
199 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
200 ]
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
201
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
202 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
203 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
204 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
205 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
206 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
207 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
208 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
209 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
210 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
211
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
212 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
213 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
214 (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
215 (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
216 ]
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
217
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
218 @register
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
219 class PushbuttonInput(DeviceType):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
220 """add a switch to ground; we'll turn on pullup"""
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
221 deviceType = ROOM['Pushbutton']
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
222 def generateSetupCode(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
223 return 'pinMode(%(pin)d, INPUT); digitalWrite(%(pin)d, HIGH);' % {
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
224 'pin': self.pinNumber,
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
225 }
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
226
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
227 def generatePollCode(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
228 # note: pulldown means unpressed reads as a 1
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
229 return "Serial.write(digitalRead(%(pin)d) ? '0' : '1');" % {
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
230 'pin': self.pinNumber
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
231 }
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
232
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
233 def readFromPoll(self, read):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
234 b = read(1)
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
235 if b not in '01':
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
236 raise ValueError('unexpected response %r' % b)
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
237 motion = b == '1'
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
238
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
239 #and exactly once for the transition
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
240 return [
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
241 (self.uri, ROOM['buttonState'],
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
242 ROOM['pressed'] if motion else ROOM['notPressed']),
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
243 ]
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
244
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
245 def watchPrefixes(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
246 return [
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
247 (self.uri, ROOM['buttonState']),
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
248 ]
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
249
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
250 @register
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
251 class OneWire(DeviceType):
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
252 """
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
253 A OW bus with temperature sensors (and maybe other devices, which
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
254 are also to be handled under this object). We return graph
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
255 statements for all devices we find, even if we don't scan them, so
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
256 you can more easily add them to your config. Onewire search
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
257 happens only at device startup (not even program startup, yet).
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
258
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
259 self.uri is a resource representing the bus.
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
260
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
261 DS18S20 pin 1: ground, pin 2: data and pull-up with 4.7k.
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
262 """
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 deviceType = ROOM['OneWire']
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
264 def hostStateInit(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
265 # eliminate this as part of removing watchPrefixes
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
266 self._knownTempSubjects = set()
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
267 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
268 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
269
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
270 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
271 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
272
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
273 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
274 # 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
275 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
276 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
277 DallasTemperature sensors(&oneWire);
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
278 #define MAX_DEVICES 8
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
279 DeviceAddress tempSensorAddress[MAX_DEVICES];
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
280
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
281 void initSensors() {
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
282 sensors.begin();
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
283 sensors.setResolution(12);
186
57e6d328d45b temp sensor try to work with idle loop (but there are still stutters)
drewp@bigasterisk.com
parents: 185
diff changeset
284 sensors.setWaitForConversion(false);
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
285 for (uint8_t i=0; i < sensors.getDeviceCount(); ++i) {
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
286 sensors.getAddress(tempSensorAddress[i], i);
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
287 }
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
288 }
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
289 ''' % 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
290
57e6d328d45b temp sensor try to work with idle loop (but there are still stutters)
drewp@bigasterisk.com
parents: 185
diff changeset
291 def generateSetupCode(self):
57e6d328d45b temp sensor try to work with idle loop (but there are still stutters)
drewp@bigasterisk.com
parents: 185
diff changeset
292 return 'initSensors();'
57e6d328d45b temp sensor try to work with idle loop (but there are still stutters)
drewp@bigasterisk.com
parents: 185
diff changeset
293
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
294 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
295 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
296 sensors.requestTemperatures();
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
297
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
298 // If we need frequent idle calls or fast polling again, this needs
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
299 // to be changed, but it makes temp sensing work. I had a note that I
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
300 // could just wait until the next cycle to get my reading, but that's
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
301 // not working today, maybe because of a changed poll rate.
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
302 sensors.setWaitForConversion(true); // ~100ms
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
303
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
304 Serial.write((uint8_t)sensors.getDeviceCount());
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
305 for (uint8_t i=0; i < sensors.getDeviceCount(); ++i) {
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
306 float newTemp = sensors.getTempF(tempSensorAddress[i]);
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
307
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
308 Serial.write(tempSensorAddress[i], 8);
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
309 Serial.write((uint8_t*)(&newTemp), 4);
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
310 }
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
311 '''
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
312
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
313 def readFromPoll(self, read):
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
314 t1 = time.time()
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
315 count = ord(read(1))
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
316 stmts = []
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
317 for i in range(count):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
318 addr = struct.unpack('>Q', read(8))[0]
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
319 tempF = struct.unpack('<f', read(4))[0]
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
320 sensorUri = URIRef(os.path.join(self.uri, 'dev-%s' % hex(addr)[2:]))
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
321 stmts.extend([
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
322 (self.uri, ROOM['connectedTo'], sensorUri),
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
323 (sensorUri, ROOM['temperatureF'], Literal(tempF))])
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
324 self._knownTempSubjects.add(sensorUri)
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
325
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
326 log.debug("read temp in %.1fms" % ((time.time() - t1) * 1000))
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
327 return stmts
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
328
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
329 def watchPrefixes(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
330 # 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
331 # about eliminating it.
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
332 return [(uri, ROOM['temperatureF']) for uri in self._knownTempSubjects]
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
333
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
334 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
335 return chr(int(min(255, max(0, f * 255))))
192
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
336
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
337 @register
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
338 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
339 deviceType = ROOM['LedOutput']
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
340 def hostStateInit(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
341 self.value = 0
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
342
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
343 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
344 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
345 '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
346 }
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
347
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
348 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
349 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
350
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
351 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
352 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
353 assert statements[0][:2] == (self.uri, ROOM['brightness'])
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
354 self.value = float(statements[0][2])
172
715c1c42185e obey ActiveLowOutput correctly
drewp@bigasterisk.com
parents: 170
diff changeset
355 if (self.uri, RDF.type, ROOM['ActiveLowOutput']) in self.graph:
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
356 self.value = 1 - self.value
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
357 write(byteFromFloat(self.value))
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
358
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
359 def hostStatements(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
360 return [(self.uri, ROOM['brightness'], Literal(self.value))]
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
361
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
362 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
363 return r'''
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
364 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
365 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
366 ''' % dict(pin=self.pinNumber)
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
367
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
368 def outputWidgets(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
369 return [{
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
370 'element': 'output-slider',
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
371 'min': 0,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
372 'max': 1,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
373 'step': 1 / 255,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
374 'subj': self.uri,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
375 'pred': ROOM['brightness'],
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
376 }]
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
377
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
378 @register
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
379 class DigitalOutput(DeviceType):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
380 deviceType = ROOM['DigitalOutput']
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
381 def hostStateInit(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
382 self.value = 0
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
383
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
384 def generateSetupCode(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
385 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
386 'pin': self.pinNumber,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
387 }
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
388
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
389 def outputPatterns(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
390 return [(self.uri, ROOM['level'], None)]
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
391
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
392 def sendOutput(self, statements, write, read):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
393 assert len(statements) == 1
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
394 assert statements[0][:2] == (self.uri, ROOM['level'])
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
395 self.value = {"high": 1, "low": 0}[str(statements[0][2])]
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
396 write(chr(self.value))
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
397
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
398 def hostStatements(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
399 return [(self.uri, ROOM['level'],
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
400 Literal('high' if self.value else 'low'))]
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
401
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
402 def generateActionCode(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
403 return r'''
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
404 while(Serial.available() < 1) NULL;
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
405 digitalWrite(%(pin)d, Serial.read());
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
406 ''' % dict(pin=self.pinNumber)
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
407
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
408 def outputWidgets(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
409 return [{
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
410 'element': 'output-switch',
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
411 'subj': self.uri,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
412 'pred': ROOM['level'],
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
413 }]
192
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
414
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
415
192
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
416 @register
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
417 class PwmBoard(DeviceType):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
418 deviceType = ROOM['PwmBoard']
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
419 @classmethod
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
420 def findInstances(cls, graph, board):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
421 for row in graph.query("""SELECT DISTINCT ?dev ?sda ?scl WHERE {
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
422 ?board :hasPin ?sdaPin .
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
423 ?board :hasPin ?sclPin .
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
424 ?sdaPin :pinNumber ?sda; :connectedTo ?sdaConn .
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
425 ?sclPin :pinNumber ?scl; :connectedTo ?sclConn .
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
426 ?dev a :PwmBoard;
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
427 :scl ?sclConn;
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
428 :sda ?sdaConn .
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
429 }""", initBindings=dict(board=board), initNs={'': ROOM}):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
430 if (row.sda, row.scl) != (Literal('a4'), Literal('a5')):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
431 raise NotImplementedError(row)
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
432 outs = {}
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
433 for out in graph.query("""SELECT DISTINCT ?area ?chan WHERE {
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
434 ?dev :output [:area ?area; :channel ?chan] .
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
435 }""", initBindings=dict(dev=row.dev), initNs={'': ROOM}):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
436 outs[out.area] = out.chan.toPython()
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
437 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
438
192
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
439 def __init__(self, graph, dev, outs):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
440 self.codeVals = {'pwm': 'pwm%s' % (hash(str(dev)) % 99999)}
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
441 self.outs = outs
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
442 super(PwmBoard, self).__init__(graph, dev, pinNumber=None)
192
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
443
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
444 def hostStateInit(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
445 self.values = {uri: 0 for uri in self.outs.keys()} # uri: brightness
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
446
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
447 def hostStatements(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
448 return [(uri, ROOM['brightness'], Literal(b))
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
449 for uri, b in self.values.items()]
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
450
192
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
451 def generateIncludes(self):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
452 return ['Wire.h', 'Adafruit_PWMServoDriver.h']
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
453
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
454 def generateArduinoLibs(self):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
455 return ['Wire', 'Adafruit-PWM-Servo-Driver-Library']
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
456
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
457 def generateGlobalCode(self):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
458 return r'''
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
459 Adafruit_PWMServoDriver %(pwm)s = Adafruit_PWMServoDriver(0x40);
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
460 ''' % self.codeVals
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
461
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
462 def generateSetupCode(self):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
463 return '''
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
464 %(pwm)s.begin();
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
465 %(pwm)s.setPWMFreq(1200);
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
466 ''' % self.codeVals
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
467
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
468 def generateActionCode(self):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
469 return r'''
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
470 while(Serial.available() < 3) NULL;
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
471 byte chan = Serial.read();
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
472 uint16_t level = uint16_t(Serial.read()) << 8;
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
473 level |= Serial.read();
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
474 %(pwm)s.setPWM(chan, 0, level);
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
475 ''' % self.codeVals
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
476
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
477 def outputPatterns(self):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
478 return [(area, ROOM['brightness'], None) for area in self.outs]
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
479
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
480 def sendOutput(self, statements, write, read):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
481 assert len(statements) == 1
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
482 assert statements[0][1] == ROOM['brightness'];
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
483 chan = self.outs[statements[0][0]]
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
484 value = float(statements[0][2])
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
485 self.values[statements[0][0]] = value
192
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
486 v12 = int(min(4095, max(0, value * 4095)))
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
487 write(chr(chan) + chr(v12 >> 8) + chr(v12 & 0xff))
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
488
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
489 def outputWidgets(self):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
490 return [{
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
491 'element': 'output-slider',
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
492 'min': 0,
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
493 'max': 1,
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
494 'step': 1 / 255,
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
495 'subj': area,
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
496 'pred': ROOM['brightness'],
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
497 } for area in self.outs]
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
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 @register
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
500 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
501 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
502 @classmethod
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
503 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
504 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
505 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
506 ?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
507 ?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
508 :connectedTo ?devPin .
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
509 ?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
510 ?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
511 } 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
512 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
513 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
514 initNs={'': ROOM}),
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
515 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
516 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
517 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
518 in connections)
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
519 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
520
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
521 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
522 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
523 self.connections = connections
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
524 self.text = ''
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
525
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
526 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
527 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
528
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
529 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
530 return ['ST7565']
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
531
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
532 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
533 return '''
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
534 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
535 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
536 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
537 ''' % 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
538 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
539 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
540 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
541 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
542
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
543 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
544 return '''
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
545 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
546 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
547 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
548 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
549
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
550 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
551 '''
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
552
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
553 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
554 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
555
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
556 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
557 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
558 assert statements[0][:2] == (self.uri, ROOM['text'])
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
559 self.text = str(statements[0][2])
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
560 assert len(self.text) < 254, repr(self.text)
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
561 write(chr(len(self.text)) + self.text)
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
562
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
563 def hostStatements(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
564 return [(self.uri, ROOM['text'], Literal(self.text))]
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
565
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
566 def outputWidgets(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
567 return [{
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
568 'element': 'output-fixed-text',
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
569 'cols': 21,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
570 'rows': 8,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
571 'subj': self.uri,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
572 'pred': ROOM['text'],
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
573 }]
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
574
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
575 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
576 return '''
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
577 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
578 byte bufSize = Serial.read();
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
579 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
580 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
581 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
582 }
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
583 for (byte i = bufSize; i < sizeof(newtxt); ++i) {
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
584 newtxt[i] = 0;
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
585 }
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
586 glcd.clear();
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
587 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
588 glcd.display();
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
589 '''
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
590
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
591 @register
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
592 class RgbPixels(DeviceType):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
593 """chain of FastLED-controllable rgb pixels"""
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
594 deviceType = ROOM['RgbPixels']
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
595
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
596 def __init__(self, graph, uri, pinNumber):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
597 super(RgbPixels, self).__init__(graph, uri, pinNumber)
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
598 px = graph.value(self.uri, ROOM['pixels'])
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
599 self.pixelUris = list(graph.items(px))
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
600 self.values = dict((uri, Literal('#000000')) for uri in self.pixelUris)
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
601 self.replace = {'ledArray': 'leds_%s' % self.pinNumber,
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
602 'ledCount': len(self.pixelUris),
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
603 'pin': self.pinNumber,
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
604 'ledType': 'WS2812',
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
605 }
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
606
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
607 def generateIncludes(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
608 """filenames of .h files to #include"""
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
609 return ['FastLED.h']
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
610
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
611 def generateArduinoLibs(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
612 """names of libraries for the ARDUINO_LIBS line in the makefile"""
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
613 return ['FastLED-3.1.0']
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
614
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
615 def myId(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
616 return 'rgb_%s' % self.pinNumber
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
617
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
618 def generateGlobalCode(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
619 return 'CRGB {ledArray}[{ledCount}];'.format(**self.replace)
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
620
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
621 def generateSetupCode(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
622 return 'FastLED.addLeds<{ledType}, {pin}>({ledArray}, {ledCount});'.format(**self.replace)
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
623
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
624 def _rgbFromHex(self, h):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
625 rrggbb = h.lstrip('#')
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
626 return [int(x, 16) for x in [rrggbb[0:2], rrggbb[2:4], rrggbb[4:6]]]
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
627
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
628 def sendOutput(self, statements, write, read):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
629 px, pred, color = statements[0]
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
630 if pred != ROOM['color']:
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
631 raise ValueError(pred)
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
632 rgb = self._rgbFromHex(color)
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
633 if px not in self.values:
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
634 raise ValueError(px)
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
635 self.values[px] = Literal(color)
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
636 write(chr(self.pixelUris.index(px)) +
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
637 chr(rgb[1]) + # my WS2812 need these flipped
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
638 chr(rgb[0]) +
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
639 chr(rgb[2]))
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
640
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
641 def hostStatements(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
642 return [(uri, ROOM['color'], hexCol)
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
643 for uri, hexCol in self.values.items()]
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
644
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
645 def outputPatterns(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
646 return [(px, ROOM['color'], None) for px in self.pixelUris]
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
647
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
648 def generateActionCode(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
649
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
650 return '''
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
651
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
652 while(Serial.available() < 1) NULL;
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
653 byte id = Serial.read();
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
654
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
655 while(Serial.available() < 1) NULL;
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
656 byte r = Serial.read();
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
657
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
658 while(Serial.available() < 1) NULL;
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
659 byte g = Serial.read();
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
660
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
661 while(Serial.available() < 1) NULL;
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
662 byte b = Serial.read();
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
663
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
664 {ledArray}[id] = CRGB(r, g, b); FastLED.show();
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
665
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
666 '''.format(**self.replace)
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
667
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
668 def outputWidgets(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
669 return [{
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
670 'element': 'output-rgb',
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
671 'subj': px,
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
672 'pred': ROOM['color'],
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
673 } for px in self.pixelUris]
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
674
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
675 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
676 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
677 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
678 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
679 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
680