annotate service/arduinoNode/devices.py @ 1710:f4009f41f15d

patchablegraph to its own repo
author drewp@bigasterisk.com
date Wed, 24 Nov 2021 10:16:03 -0800
parents 6b80a6c58907
children
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
331
a94f2a522d41 build and import updates for rdfdb, etc
drewp@bigasterisk.com
parents: 305
diff changeset
2
a94f2a522d41 build and import updates for rdfdb, etc
drewp@bigasterisk.com
parents: 305
diff changeset
3 import itertools, logging, struct, os, sys
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
4 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
5 import time
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
6
621
686079900c20 arduinonode: per-device-type timing stats
drewp@bigasterisk.com
parents: 335
diff changeset
7 from greplin import scales
686079900c20 arduinonode: per-device-type timing stats
drewp@bigasterisk.com
parents: 335
diff changeset
8
331
a94f2a522d41 build and import updates for rdfdb, etc
drewp@bigasterisk.com
parents: 305
diff changeset
9 from devices_shared import RgbPixelsAnimation
a94f2a522d41 build and import updates for rdfdb, etc
drewp@bigasterisk.com
parents: 305
diff changeset
10
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
11 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
12 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
13 log = logging.getLogger()
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
14
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
15 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
16 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
17 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
18 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
19 return buf
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
20
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
21 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
22 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
23 @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
24 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
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 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
27 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
28 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
29 (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
30 """
c0180bd2b33a 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 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
32 ?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
33 ?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
34 :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
35 ?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
36 } 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
37 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
38 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
39 initNs={'': ROOM}):
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
40 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
41 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
42
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
43 # 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
44 def __init__(self, graph, uri, pinNumber):
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
45 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
46 self.pinNumber = pinNumber
1685
6b80a6c58907 minor changes to several services
drewp@bigasterisk.com
parents: 622
diff changeset
47 scales.init(self, self.__class__.__name__)
6b80a6c58907 minor changes to several services
drewp@bigasterisk.com
parents: 622
diff changeset
48 self.stats = scales.collection(self.__class__.__name__,
6b80a6c58907 minor changes to several services
drewp@bigasterisk.com
parents: 622
diff changeset
49 scales.PmfStat('poll'),
6b80a6c58907 minor changes to several services
drewp@bigasterisk.com
parents: 622
diff changeset
50 scales.PmfStat('output'),
6b80a6c58907 minor changes to several services
drewp@bigasterisk.com
parents: 622
diff changeset
51 )
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
52 self.hostStateInit()
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
53
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
54 def hostStateInit(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
55 """
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
56 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
57 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
58 """
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
59
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
60 def description(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
61 return {
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
62 'uri': self.uri,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
63 'className': self.__class__.__name__,
233
4ebb5cc30002 server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents: 230
diff changeset
64 'pinNumber': getattr(self, 'pinNumber', None),
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
65 'outputPatterns': self.outputPatterns(),
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
66 'watchPrefixes': self.watchPrefixes(),
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
67 'outputWidgets': self.outputWidgets(),
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
68 }
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
69
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
70 def readFromPoll(self, read):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
71 """
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
72 read an update message returned as part of a poll bundle. This may
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
73 consume a varying number of bytes depending on the type of
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
74 input (e.g. IR receiver).
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
75 Returns rdf statements.
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
76 """
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
77 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
78
334
bb80182195c0 arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents: 331
diff changeset
79 def wantIdleOutput(self):
bb80182195c0 arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents: 331
diff changeset
80 return False
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
81
334
bb80182195c0 arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents: 331
diff changeset
82 def outputIdle(self, write):
bb80182195c0 arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents: 331
diff changeset
83 return
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
84
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
85 def hostStatements(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
86 """
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
87 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
88 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
89 (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
90 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
91 be fast.
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
92 """
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
93 return []
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
94
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
95 def watchPrefixes(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
96 """
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
97 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
98 readFromPoll, so the dashboard knows what it should
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
99 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
100 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
101 """
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
102 return []
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
103
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
104 def generateIncludes(self):
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
105 """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
106 return []
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
107
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
108 def generateArduinoLibs(self):
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
109 """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
110 return []
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
111
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
112 def generateGlobalCode(self):
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
113 """C code to emit in the global section.
185
2161c71c7b02 support for device code in the idle loop
drewp@bigasterisk.com
parents: 176
diff changeset
114
2161c71c7b02 support for device code in the idle loop
drewp@bigasterisk.com
parents: 176
diff changeset
115 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
116 """
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
117 return ''
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
118
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
119 def generateSetupCode(self):
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
120 """C code to emit in setup()"""
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
121 return ''
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
122
185
2161c71c7b02 support for device code in the idle loop
drewp@bigasterisk.com
parents: 176
diff changeset
123 def generateIdleCode(self):
2161c71c7b02 support for device code in the idle loop
drewp@bigasterisk.com
parents: 176
diff changeset
124 """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
125 return ''
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
126
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
127 def generatePollCode(self):
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
128 """
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
129 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
130 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
131 try to poll this device.
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
132 """
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
133 return ''
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
134
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
135 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
136 """
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
137 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
138 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
139 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
140 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
141 """
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
142 return ''
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
143
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
144 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
145 """
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
146 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
147 to sendOutput
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 return []
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
150
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
151 def outputWidgets(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
152 """
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
153 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
154 handler you have in sendOutput
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
155 """
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
156 return []
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
157
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
158 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
159 """
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
160 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
161 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
162 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
163 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
164
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
165 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
166 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
167 """
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
168 raise NotImplementedError
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
169
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
170 _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
171 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
172 _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
173 return deviceType
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
174
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
175 @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
176 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
177 @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
178 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
179 return [cls(graph, board, None)]
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
180
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
181 def generatePollCode(self):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
182 return "Serial.write('k');"
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
183
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
184 def readFromPoll(self, read):
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
185 byte = read(1)
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
186 if byte != 'k':
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
187 raise ValueError('invalid ping response: chr(%s)' % ord(byte))
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
188 return [(self.uri, ROOM['ping'], ROOM['ok'])]
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
189
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
190 def watchPrefixes(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
191 return [(self.uri, ROOM['ping'])]
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
192
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
193 @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
194 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
195 deviceType = ROOM['MotionSensor']
251
254df9f881a6 start sending oneshot events from some devices
drewp@bigasterisk.com
parents: 233
diff changeset
196 def __init__(self, graph, uri, pinNumber):
254df9f881a6 start sending oneshot events from some devices
drewp@bigasterisk.com
parents: 233
diff changeset
197 DeviceType.__init__(self, graph, uri, pinNumber)
254df9f881a6 start sending oneshot events from some devices
drewp@bigasterisk.com
parents: 233
diff changeset
198 self.lastRead = None
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
199
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
200 def generateSetupCode(self):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
201 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
202 'pin': self.pinNumber,
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
203 }
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
204
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
205 def generatePollCode(self):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
206 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
207 'pin': self.pinNumber
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
208 }
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
209
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
210 def readFromPoll(self, read):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
211 b = read(1)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
212 if b not in 'yn':
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
213 raise ValueError('unexpected response %r' % b)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
214 motion = b == 'y'
251
254df9f881a6 start sending oneshot events from some devices
drewp@bigasterisk.com
parents: 233
diff changeset
215
254df9f881a6 start sending oneshot events from some devices
drewp@bigasterisk.com
parents: 233
diff changeset
216 oneshot = []
254df9f881a6 start sending oneshot events from some devices
drewp@bigasterisk.com
parents: 233
diff changeset
217 if self.lastRead is not None and motion != self.lastRead:
254df9f881a6 start sending oneshot events from some devices
drewp@bigasterisk.com
parents: 233
diff changeset
218 oneshot = [(self.uri, ROOM['sees'], ROOM['motionStart'])]
254df9f881a6 start sending oneshot events from some devices
drewp@bigasterisk.com
parents: 233
diff changeset
219 self.lastRead = motion
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
220
251
254df9f881a6 start sending oneshot events from some devices
drewp@bigasterisk.com
parents: 233
diff changeset
221 return {'latest': [
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
222 (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
223 ROOM['motion'] if motion else ROOM['noMotion']),
268
6103688dce6d 'seesRecently30' in arduinoNode
drewp@bigasterisk.com
parents: 257
diff changeset
224 ] + self.recentMotionStatements(motion),
6103688dce6d 'seesRecently30' in arduinoNode
drewp@bigasterisk.com
parents: 257
diff changeset
225 'oneshot': oneshot}
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
226
268
6103688dce6d 'seesRecently30' in arduinoNode
drewp@bigasterisk.com
parents: 257
diff changeset
227 def recentMotionStatements(self, motion):
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
228 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
229 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
230 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
231 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
232 self.lastMotionTime = now
268
6103688dce6d 'seesRecently30' in arduinoNode
drewp@bigasterisk.com
parents: 257
diff changeset
233 dt = now - self.lastMotionTime
6103688dce6d 'seesRecently30' in arduinoNode
drewp@bigasterisk.com
parents: 257
diff changeset
234 return [(self.uri, ROOM['seesRecently'],
6103688dce6d 'seesRecently30' in arduinoNode
drewp@bigasterisk.com
parents: 257
diff changeset
235 ROOM['motion'] if (dt < 60 * 10) else ROOM['noMotion']),
6103688dce6d 'seesRecently30' in arduinoNode
drewp@bigasterisk.com
parents: 257
diff changeset
236 (self.uri, ROOM['seesRecently30'],
6103688dce6d 'seesRecently30' in arduinoNode
drewp@bigasterisk.com
parents: 257
diff changeset
237 ROOM['motion'] if (dt < 30) else ROOM['noMotion'])]
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
238
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
239 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
240 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
241 (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
242 (self.uri, ROOM['seesRecently']),
268
6103688dce6d 'seesRecently30' in arduinoNode
drewp@bigasterisk.com
parents: 257
diff changeset
243 (self.uri, ROOM['seesRecently30']),
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
244 ]
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
245
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
246 @register
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
247 class PushbuttonInput(DeviceType):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
248 """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
249 deviceType = ROOM['Pushbutton']
257
b97495befcab button :press oneshot
drewp@bigasterisk.com
parents: 251
diff changeset
250 def __init__(self, graph, uri, pinNumber):
b97495befcab button :press oneshot
drewp@bigasterisk.com
parents: 251
diff changeset
251 DeviceType.__init__(self, graph, uri, pinNumber)
b97495befcab button :press oneshot
drewp@bigasterisk.com
parents: 251
diff changeset
252 self.lastClosed = None
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
253
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
254 def generateSetupCode(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
255 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
256 'pin': self.pinNumber,
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
257 }
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
258
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
259 def generatePollCode(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
260 # 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
261 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
262 'pin': self.pinNumber
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
263 }
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
264
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
265 def readFromPoll(self, read):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
266 b = read(1)
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
267 if b not in '01':
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
268 raise ValueError('unexpected response %r' % b)
257
b97495befcab button :press oneshot
drewp@bigasterisk.com
parents: 251
diff changeset
269 closed = b == '0'
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
270
257
b97495befcab button :press oneshot
drewp@bigasterisk.com
parents: 251
diff changeset
271 if self.lastClosed is not None and closed != self.lastClosed:
b97495befcab button :press oneshot
drewp@bigasterisk.com
parents: 251
diff changeset
272 oneshot = [
b97495befcab button :press oneshot
drewp@bigasterisk.com
parents: 251
diff changeset
273 (self.uri, ROOM['buttonState'],
b97495befcab button :press oneshot
drewp@bigasterisk.com
parents: 251
diff changeset
274 ROOM['press'] if closed else ROOM['release']),
b97495befcab button :press oneshot
drewp@bigasterisk.com
parents: 251
diff changeset
275 ]
b97495befcab button :press oneshot
drewp@bigasterisk.com
parents: 251
diff changeset
276 else:
b97495befcab button :press oneshot
drewp@bigasterisk.com
parents: 251
diff changeset
277 oneshot = []
b97495befcab button :press oneshot
drewp@bigasterisk.com
parents: 251
diff changeset
278 self.lastClosed = closed
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
279
257
b97495befcab button :press oneshot
drewp@bigasterisk.com
parents: 251
diff changeset
280 return {'latest': [
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
281 (self.uri, ROOM['buttonState'],
257
b97495befcab button :press oneshot
drewp@bigasterisk.com
parents: 251
diff changeset
282 ROOM['pressed'] if closed else ROOM['notPressed']),
b97495befcab button :press oneshot
drewp@bigasterisk.com
parents: 251
diff changeset
283 ],
b97495befcab button :press oneshot
drewp@bigasterisk.com
parents: 251
diff changeset
284 'oneshot': oneshot}
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
285
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
286 def watchPrefixes(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
287 return [
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
288 (self.uri, ROOM['buttonState']),
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
289 ]
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
290
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
291 @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
292 class OneWire(DeviceType):
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
293 """
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
294 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
295 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
296 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
297 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
298 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
299
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
300 self.uri is a resource representing the bus.
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
301
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
302 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
303 """
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
304 deviceType = ROOM['OneWire']
305
6614416dd2c3 influx output for arduino, update web lib path
drewp@bigasterisk.com
parents: 268
diff changeset
305 pollPeriod = 2
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
306 def hostStateInit(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
307 # 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
308 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
309 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
310 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
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 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
313 return ['OneWire', 'DallasTemperature']
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
314
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
315 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
316 # 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
317 return '''
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
318 OneWire oneWire(%(pinNumber)s);
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
319 DallasTemperature sensors(&oneWire);
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
320 #define MAX_DEVICES 8
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
321 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
322
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
323 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
324 sensors.begin();
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
325 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
326 sensors.setWaitForConversion(false);
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
327 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
328 sensors.getAddress(tempSensorAddress[i], i);
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
329 }
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
330 }
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
331 ''' % dict(pinNumber=self.pinNumber)
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
332
186
57e6d328d45b temp sensor try to work with idle loop (but there are still stutters)
drewp@bigasterisk.com
parents: 185
diff changeset
333 def generateSetupCode(self):
57e6d328d45b temp sensor try to work with idle loop (but there are still stutters)
drewp@bigasterisk.com
parents: 185
diff changeset
334 return 'initSensors();'
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
335
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
336 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
337 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
338 sensors.requestTemperatures();
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
339
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
340 // 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
341 // 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
342 // 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
343 // 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
344 sensors.setWaitForConversion(true); // ~100ms
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
345
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
346 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
347 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
348 float newTemp = sensors.getTempF(tempSensorAddress[i]);
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
349
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
350 Serial.write(tempSensorAddress[i], 8);
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
351 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
352 }
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
353 '''
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
354
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
355 def readFromPoll(self, read):
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
356 t1 = time.time()
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
357 count = ord(read(1))
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
358 stmts = []
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
359 for i in range(count):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
360 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
361 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
362 sensorUri = URIRef(os.path.join(self.uri, 'dev-%s' % hex(addr)[2:]))
305
6614416dd2c3 influx output for arduino, update web lib path
drewp@bigasterisk.com
parents: 268
diff changeset
363 if tempF > 180:
6614416dd2c3 influx output for arduino, update web lib path
drewp@bigasterisk.com
parents: 268
diff changeset
364 stmts.extend([
6614416dd2c3 influx output for arduino, update web lib path
drewp@bigasterisk.com
parents: 268
diff changeset
365 (self.uri, ROOM['connectedTo'], sensorUri),
6614416dd2c3 influx output for arduino, update web lib path
drewp@bigasterisk.com
parents: 268
diff changeset
366 (sensorUri, RDF.type, ROOM['FailingTemperatureReading']),
6614416dd2c3 influx output for arduino, update web lib path
drewp@bigasterisk.com
parents: 268
diff changeset
367 ])
6614416dd2c3 influx output for arduino, update web lib path
drewp@bigasterisk.com
parents: 268
diff changeset
368 continue
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
369 stmts.extend([
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
370 (self.uri, ROOM['connectedTo'], sensorUri),
230
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 218
diff changeset
371 # rounding may be working around a bug where the
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 218
diff changeset
372 # Literal gets two representations (at different
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 218
diff changeset
373 # roundings), and this makes an extra value linger on
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 218
diff changeset
374 # the client.
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 218
diff changeset
375 (sensorUri, ROOM['temperatureF'], Literal(round(tempF, 2)))])
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
376 self._knownTempSubjects.add(sensorUri)
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
377
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
378 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
379 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
380
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
381 def watchPrefixes(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
382 # 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
383 # about eliminating it.
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
384 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
385
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
386 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
387 return chr(int(min(255, max(0, f * 255))))
192
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
388
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
389 @register
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
390 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
391 deviceType = ROOM['LedOutput']
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
392 def hostStateInit(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
393 self.value = 0
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
394
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
395 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
396 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
397 '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
398 }
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
399
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
400 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
401 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
402
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
403 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
404 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
405 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
406 self.value = float(statements[0][2])
172
715c1c42185e obey ActiveLowOutput correctly
drewp@bigasterisk.com
parents: 170
diff changeset
407 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
408 self.value = 1 - self.value
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
409 write(byteFromFloat(self.value))
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
410
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
411 def hostStatements(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
412 return [(self.uri, ROOM['brightness'], Literal(self.value))]
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
413
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
414 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
415 return r'''
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
416 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
417 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
418 ''' % dict(pin=self.pinNumber)
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
419
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
420 def outputWidgets(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
421 return [{
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
422 'element': 'output-slider',
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
423 'min': 0,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
424 'max': 1,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
425 'step': 1 / 255,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
426 'subj': self.uri,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
427 'pred': ROOM['brightness'],
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
428 }]
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
429
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
430 @register
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
431 class DigitalOutput(DeviceType):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
432 deviceType = ROOM['DigitalOutput']
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
433 def hostStateInit(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
434 self.value = 0
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
435
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
436 def generateSetupCode(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
437 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
438 'pin': self.pinNumber,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
439 }
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
440
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
441 def outputPatterns(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
442 return [(self.uri, ROOM['level'], None)]
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
443
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
444 def sendOutput(self, statements, write, read):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
445 assert len(statements) == 1
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
446 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
447 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
448 write(chr(self.value))
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
449
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
450 def hostStatements(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
451 return [(self.uri, ROOM['level'],
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
452 Literal('high' if self.value else 'low'))]
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
453
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
454 def generateActionCode(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
455 return r'''
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
456 while(Serial.available() < 1) NULL;
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
457 digitalWrite(%(pin)d, Serial.read());
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
458 ''' % dict(pin=self.pinNumber)
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
459
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
460 def outputWidgets(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
461 return [{
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
462 'element': 'output-switch',
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
463 'subj': self.uri,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
464 'pred': ROOM['level'],
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
465 }]
192
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
466
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
467
192
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
468 @register
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
469 class PwmBoard(DeviceType):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
470 deviceType = ROOM['PwmBoard']
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
471 @classmethod
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
472 def findInstances(cls, graph, board):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
473 for row in graph.query("""SELECT DISTINCT ?dev ?sda ?scl WHERE {
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
474 ?board :hasPin ?sdaPin .
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
475 ?board :hasPin ?sclPin .
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
476 ?sdaPin :pinNumber ?sda; :connectedTo ?sdaConn .
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
477 ?sclPin :pinNumber ?scl; :connectedTo ?sclConn .
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
478 ?dev a :PwmBoard;
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
479 :scl ?sclConn;
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
480 :sda ?sdaConn .
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
481 }""", initBindings=dict(board=board), initNs={'': ROOM}):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
482 if (row.sda, row.scl) != (Literal('a4'), Literal('a5')):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
483 raise NotImplementedError(row)
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
484 outs = {}
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
485 for out in graph.query("""SELECT DISTINCT ?area ?chan WHERE {
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
486 ?dev :output [:area ?area; :channel ?chan] .
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
487 }""", initBindings=dict(dev=row.dev), initNs={'': ROOM}):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
488 outs[out.area] = out.chan.toPython()
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
489 yield cls(graph, row.dev, outs=outs)
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
490
192
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
491 def __init__(self, graph, dev, outs):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
492 self.codeVals = {'pwm': 'pwm%s' % (hash(str(dev)) % 99999)}
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
493 self.outs = outs
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
494 super(PwmBoard, self).__init__(graph, dev, pinNumber=None)
192
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
495
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
496 def hostStateInit(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
497 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
498
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
499 def hostStatements(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
500 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
501 for uri, b in self.values.items()]
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
502
192
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
503 def generateIncludes(self):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
504 return ['Wire.h', 'Adafruit_PWMServoDriver.h']
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
505
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
506 def generateArduinoLibs(self):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
507 return ['Wire', 'Adafruit-PWM-Servo-Driver-Library']
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
508
192
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
509 def generateGlobalCode(self):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
510 return r'''
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
511 Adafruit_PWMServoDriver %(pwm)s = Adafruit_PWMServoDriver(0x40);
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
512 ''' % self.codeVals
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
513
192
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
514 def generateSetupCode(self):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
515 return '''
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
516 %(pwm)s.begin();
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
517 %(pwm)s.setPWMFreq(1200);
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
518 ''' % self.codeVals
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
519
192
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
520 def generateActionCode(self):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
521 return r'''
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
522 while(Serial.available() < 3) NULL;
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
523 byte chan = Serial.read();
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
524 uint16_t level = uint16_t(Serial.read()) << 8;
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
525 level |= Serial.read();
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
526 %(pwm)s.setPWM(chan, 0, level);
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
527 ''' % self.codeVals
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
528
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
529 def outputPatterns(self):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
530 return [(area, ROOM['brightness'], None) for area in self.outs]
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
531
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
532 def sendOutput(self, statements, write, read):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
533 assert len(statements) == 1
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
534 assert statements[0][1] == ROOM['brightness'];
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
535 chan = self.outs[statements[0][0]]
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
536 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
537 self.values[statements[0][0]] = value
192
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
538 v12 = int(min(4095, max(0, value * 4095)))
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
539 write(chr(chan) + chr(v12 >> 8) + chr(v12 & 0xff))
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
540
192
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
541 def outputWidgets(self):
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
542 return [{
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
543 'element': 'output-slider',
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
544 'min': 0,
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
545 'max': 1,
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
546 'step': 1 / 255,
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
547 'subj': area,
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
548 'pred': ROOM['brightness'],
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
549 } for area in self.outs]
6c6897a139da support for pwm board
drewp@bigasterisk.com
parents: 186
diff changeset
550
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
551 @register
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
552 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
553 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
554 @classmethod
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
555 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
556 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
557 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
558 ?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
559 ?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
560 :connectedTo ?devPin .
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
561 ?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
562 ?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
563 } 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
564 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
565 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
566 initNs={'': ROOM}),
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
567 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
568 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
569 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
570 in connections)
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
571 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
572
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
573 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
574 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
575 self.connections = connections
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
576 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
577
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
578 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
579 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
580
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
581 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
582 return ['ST7565']
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
583
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
584 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
585 return '''
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
586 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
587 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
588 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
589 ''' % 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
590 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
591 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
592 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
593 CS=self.connections[ROOM['lcdCS']])
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
594
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
595 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
596 return '''
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
597 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
598 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
599 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
600 glcd.st7565_set_brightness(0x18);
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
601
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
602 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
603 '''
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
604
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
605 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
606 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
607
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
608 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
609 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
610 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
611 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
612 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
613 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
614
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
615 def hostStatements(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
616 return [(self.uri, ROOM['text'], Literal(self.text))]
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
617
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
618 def outputWidgets(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
619 return [{
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
620 'element': 'output-fixed-text',
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
621 'cols': 21,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
622 'rows': 8,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
623 'subj': self.uri,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
624 'pred': ROOM['text'],
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
625 }]
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
626
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
627 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
628 return '''
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
629 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
630 byte bufSize = Serial.read();
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
631 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
632 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
633 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
634 }
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
635 for (byte i = bufSize; i < sizeof(newtxt); ++i) {
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
636 newtxt[i] = 0;
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
637 }
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
638 glcd.clear();
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
639 glcd.drawstring(0,0, newtxt);
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
640 glcd.display();
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
641 '''
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
642
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
643 @register
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
644 class RgbPixels(DeviceType):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
645 """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
646 deviceType = ROOM['RgbPixels']
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 __init__(self, graph, uri, pinNumber):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
649 super(RgbPixels, self).__init__(graph, uri, pinNumber)
335
3010238b94a0 rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents: 334
diff changeset
650 self.anim = RgbPixelsAnimation(graph, uri, self.updateOutput)
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
651
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
652 self.replace = {'ledArray': 'leds_%s' % self.pinNumber,
335
3010238b94a0 rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents: 334
diff changeset
653 'ledCount': self.anim.maxIndex() - 1,
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
654 'pin': self.pinNumber,
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
655 'ledType': 'WS2812',
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
656 }
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
657
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
658 def generateIncludes(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
659 """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
660 return ['FastLED.h']
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
661
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
662 def generateArduinoLibs(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
663 """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
664 return ['FastLED-3.1.0']
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 def myId(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
667 return 'rgb_%s' % self.pinNumber
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
668
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
669 def generateGlobalCode(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
670 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
671
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
672 def generateSetupCode(self):
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
673 return 'FastLED.addLeds<{ledType}, {pin}>({ledArray}, {ledCount});'.format(**self.replace)
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
674
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
675 def sendOutput(self, statements, write, read):
335
3010238b94a0 rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents: 334
diff changeset
676 log.info('sendOutput start')
3010238b94a0 rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents: 334
diff changeset
677 self.write = write
3010238b94a0 rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents: 334
diff changeset
678 self.anim.onStatements(statements)
3010238b94a0 rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents: 334
diff changeset
679 self.anim.updateOutput()
3010238b94a0 rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents: 334
diff changeset
680 log.info('sendOutput end')
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
681
335
3010238b94a0 rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents: 334
diff changeset
682 def updateOutput(self):
3010238b94a0 rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents: 334
diff changeset
683 write = self.write
3010238b94a0 rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents: 334
diff changeset
684 write(chr(self.anim.maxIndex() + 1))
3010238b94a0 rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents: 334
diff changeset
685 for idx, (r, g, b) in self.anim.currentColors():
3010238b94a0 rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents: 334
diff changeset
686 # my WS2812 need these flipped
3010238b94a0 rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents: 334
diff changeset
687 write(chr(idx) + chr(g) + chr(r) + chr(b))
3010238b94a0 rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents: 334
diff changeset
688
3010238b94a0 rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents: 334
diff changeset
689 def wantIdleOutput(self):
3010238b94a0 rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents: 334
diff changeset
690 return True
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
691
335
3010238b94a0 rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents: 334
diff changeset
692 def outputIdle(self, write):
3010238b94a0 rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents: 334
diff changeset
693 self.write = write
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
694 self.updateOutput()
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
695
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
696 def hostStatements(self):
335
3010238b94a0 rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents: 334
diff changeset
697 return self.anim.hostStatements()
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
698
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
699 def outputPatterns(self):
335
3010238b94a0 rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents: 334
diff changeset
700 return self.anim.outputPatterns()
3010238b94a0 rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents: 334
diff changeset
701
3010238b94a0 rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents: 334
diff changeset
702 def outputWidgets(self):
3010238b94a0 rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents: 334
diff changeset
703 return self.anim.outputWidgets()
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
704
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
705 def generateActionCode(self):
335
3010238b94a0 rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents: 334
diff changeset
706 # not yet combining updates- may be slow
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
707 return '''
335
3010238b94a0 rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents: 334
diff changeset
708 while(Serial.available() < 1) NULL;
3010238b94a0 rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents: 334
diff changeset
709 byte count = Serial.read();
3010238b94a0 rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents: 334
diff changeset
710 for (byte elt=0; elt<count; ++elt) {{
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
711 while(Serial.available() < 1) NULL;
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
712 byte id = Serial.read();
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
713
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
714 while(Serial.available() < 1) NULL;
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
715 byte r = Serial.read();
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
716
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
717 while(Serial.available() < 1) NULL;
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
718 byte g = Serial.read();
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
719
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
720 while(Serial.available() < 1) NULL;
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
721 byte b = Serial.read();
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
722
335
3010238b94a0 rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents: 334
diff changeset
723 {ledArray}[id] = CRGB(r, g, b);
3010238b94a0 rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents: 334
diff changeset
724 }}
3010238b94a0 rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents: 334
diff changeset
725 FastLED.show();
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
726
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 192
diff changeset
727 '''.format(**self.replace)
622
4880a9868673 whitespace
drewp@bigasterisk.com
parents: 621
diff changeset
728
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
729 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
730 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
731 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
732 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
733 return out