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