Mercurial > code > home > repos > homeauto
annotate service/piNode/devices.py @ 633:42dab8c6200a
RgbStrip init fix
Ignore-this: 2fe82ee432152e2902cc1d81d0895341
author | drewp@bigasterisk.com |
---|---|
date | Mon, 12 Aug 2019 01:10:12 -0700 |
parents | 487cb5277ae3 |
children | 7c04b4f675ec |
rev | line source |
---|---|
304
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
1 """ |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
2 https://github.com/juniorug/libsensorPy is a similar project |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
3 """ |
182 | 4 from __future__ import division |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
5 |
221
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
6 import time, logging, os |
243
141079644c45
piNode poll switches much faster. mirror the logic in arduinoNode though vari-rate poll is not supported yet
drewp@bigasterisk.com
parents:
233
diff
changeset
|
7 from rdflib import Namespace, URIRef, Literal |
589
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
407
diff
changeset
|
8 from twisted.internet import reactor, threads |
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
407
diff
changeset
|
9 from twisted.internet.defer import inlineCallbacks, returnValue |
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
407
diff
changeset
|
10 from greplin import scales |
182 | 11 |
331
a94f2a522d41
build and import updates for rdfdb, etc
drewp@bigasterisk.com
parents:
324
diff
changeset
|
12 from devices_shared import RgbPixelsAnimation |
a94f2a522d41
build and import updates for rdfdb, etc
drewp@bigasterisk.com
parents:
324
diff
changeset
|
13 |
a94f2a522d41
build and import updates for rdfdb, etc
drewp@bigasterisk.com
parents:
324
diff
changeset
|
14 log = logging.getLogger() |
a94f2a522d41
build and import updates for rdfdb, etc
drewp@bigasterisk.com
parents:
324
diff
changeset
|
15 ROOM = Namespace('http://projects.bigasterisk.com/room/') |
a94f2a522d41
build and import updates for rdfdb, etc
drewp@bigasterisk.com
parents:
324
diff
changeset
|
16 XSD = Namespace('http://www.w3.org/2001/XMLSchema#') |
a94f2a522d41
build and import updates for rdfdb, etc
drewp@bigasterisk.com
parents:
324
diff
changeset
|
17 RDFS = Namespace('http://www.w3.org/2000/01/rdf-schema#') |
a94f2a522d41
build and import updates for rdfdb, etc
drewp@bigasterisk.com
parents:
324
diff
changeset
|
18 |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
19 try: |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
20 import pigpio |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
21 except ImportError: |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
22 pigpio = None |
338
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
23 try: |
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
24 import rpi_ws281x |
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
25 except ImportError: |
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
26 rpi_ws281x = None |
266
1ae78fe6f36e
neopixel lib change. factor out pwm setup and try another pwm rate
drewp@bigasterisk.com
parents:
262
diff
changeset
|
27 |
1ae78fe6f36e
neopixel lib change. factor out pwm setup and try another pwm rate
drewp@bigasterisk.com
parents:
262
diff
changeset
|
28 def setupPwm(pi, pinNumber, hz=8000): |
1ae78fe6f36e
neopixel lib change. factor out pwm setup and try another pwm rate
drewp@bigasterisk.com
parents:
262
diff
changeset
|
29 pi.set_mode(pinNumber, pigpio.OUTPUT) |
1ae78fe6f36e
neopixel lib change. factor out pwm setup and try another pwm rate
drewp@bigasterisk.com
parents:
262
diff
changeset
|
30 # see http://abyz.co.uk/rpi/pigpio/cif.html#gpioCfgClock |
1ae78fe6f36e
neopixel lib change. factor out pwm setup and try another pwm rate
drewp@bigasterisk.com
parents:
262
diff
changeset
|
31 # and http://abyz.co.uk/rpi/pigpio/cif.html#gpioSetPWMfrequency |
1ae78fe6f36e
neopixel lib change. factor out pwm setup and try another pwm rate
drewp@bigasterisk.com
parents:
262
diff
changeset
|
32 actual = pi.set_PWM_frequency(pinNumber, hz) |
1ae78fe6f36e
neopixel lib change. factor out pwm setup and try another pwm rate
drewp@bigasterisk.com
parents:
262
diff
changeset
|
33 if actual != hz: |
1ae78fe6f36e
neopixel lib change. factor out pwm setup and try another pwm rate
drewp@bigasterisk.com
parents:
262
diff
changeset
|
34 raise ValueError('pwm actual=%s' % actual) |
1ae78fe6f36e
neopixel lib change. factor out pwm setup and try another pwm rate
drewp@bigasterisk.com
parents:
262
diff
changeset
|
35 pi.set_PWM_dutycycle(pinNumber, 0) |
259 | 36 |
182 | 37 |
38 class DeviceType(object): | |
221
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
39 deviceType = NotImplementedError |
182 | 40 @classmethod |
41 def findInstances(cls, graph, board, pi): | |
42 """ | |
43 return any number of instances of this class for all the separately | |
44 controlled devices on the board. Two LEDS makes two instances, | |
45 but two sensors on the same onewire bus makes only one device | |
46 (which yields more statements). | |
47 """ | |
221
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
48 log.debug("graph has any connected devices of type %s?", cls.deviceType) |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
49 for row in graph.query("""SELECT ?dev ?gpioNumber WHERE { |
182 | 50 ?board :hasPin ?pin . |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
51 ?pin :gpioNumber ?gpioNumber; |
182 | 52 :connectedTo ?dev . |
53 ?dev a ?thisType . | |
54 } ORDER BY ?dev""", | |
55 initBindings=dict(board=board, | |
221
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
56 thisType=cls.deviceType)): |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
57 yield cls(graph, row.dev, pi, int(row.gpioNumber)) |
182 | 58 |
59 def __init__(self, graph, uri, pi, pinNumber): | |
60 self.graph, self.uri, self.pi = graph, uri, pi | |
61 self.pinNumber = pinNumber | |
625
69a84b3d1dfa
frontdoor configs; move device class timing to greplin stats
drewp@bigasterisk.com
parents:
624
diff
changeset
|
62 scales.init(self, self.__class__.__name__) |
69a84b3d1dfa
frontdoor configs; move device class timing to greplin stats
drewp@bigasterisk.com
parents:
624
diff
changeset
|
63 self.stats = scales.collection(self.__class__.__name__, |
69a84b3d1dfa
frontdoor configs; move device class timing to greplin stats
drewp@bigasterisk.com
parents:
624
diff
changeset
|
64 scales.PmfStat('poll'), |
69a84b3d1dfa
frontdoor configs; move device class timing to greplin stats
drewp@bigasterisk.com
parents:
624
diff
changeset
|
65 scales.PmfStat('output'), |
69a84b3d1dfa
frontdoor configs; move device class timing to greplin stats
drewp@bigasterisk.com
parents:
624
diff
changeset
|
66 ) |
233
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
232
diff
changeset
|
67 self.hostStateInit() |
182 | 68 |
233
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
232
diff
changeset
|
69 def hostStateInit(self): |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
232
diff
changeset
|
70 """ |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
232
diff
changeset
|
71 If you don't want to use __init__, you can use this to set up |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
232
diff
changeset
|
72 whatever storage you might need for hostStatements |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
232
diff
changeset
|
73 """ |
624 | 74 |
182 | 75 def description(self): |
76 return { | |
77 'uri': self.uri, | |
78 'className': self.__class__.__name__, | |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
79 'pinNumber': getattr(self, 'pinNumber', None), |
182 | 80 'outputPatterns': self.outputPatterns(), |
81 'watchPrefixes': self.watchPrefixes(), | |
82 'outputWidgets': self.outputWidgets(), | |
83 } | |
84 | |
233
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
232
diff
changeset
|
85 def hostStatements(self): |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
232
diff
changeset
|
86 """ |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
232
diff
changeset
|
87 Like readFromPoll but these statements come from the host-side |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
232
diff
changeset
|
88 python code, not the connected device. Include output state |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
232
diff
changeset
|
89 (e.g. light brightness) if its master version is in this |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
232
diff
changeset
|
90 object. This method is called on /graph requests so it should |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
232
diff
changeset
|
91 be fast. |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
232
diff
changeset
|
92 """ |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
232
diff
changeset
|
93 return [] |
624 | 94 |
182 | 95 def watchPrefixes(self): |
96 """ | |
97 subj,pred pairs of the statements that might be returned from | |
98 readFromPoll, so the dashboard knows what it should | |
99 watch. This should be eliminated, as the dashboard should just | |
100 always watch the whole tree of statements starting self.uri | |
101 """ | |
102 return [] | |
103 | |
104 def poll(self): | |
105 return [] # statements | |
624 | 106 |
182 | 107 def outputPatterns(self): |
108 """ | |
109 Triple patterns, using None as a wildcard, that should be routed | |
110 to sendOutput | |
111 """ | |
112 return [] | |
113 | |
114 def outputWidgets(self): | |
115 """ | |
116 structs to make output widgets on the dashboard. ~1 of these per | |
117 handler you have in sendOutput | |
118 """ | |
119 return [] | |
624 | 120 |
182 | 121 def sendOutput(self, statements): |
122 """ | |
123 If we got statements that match this class's outputPatterns, this | |
624 | 124 will be called with the statements that matched. |
182 | 125 |
126 Todo: it would be fine to read back confirmations or | |
127 whatever. Just need a way to collect them into graph statements. | |
128 """ | |
129 raise NotImplementedError | |
624 | 130 |
182 | 131 _knownTypes = set() |
132 def register(deviceType): | |
133 _knownTypes.add(deviceType) | |
134 return deviceType | |
135 | |
626
487cb5277ae3
try to workaround pigpio not supporting pullup modes on pi4 yet (didn't work, I think)
drewp@bigasterisk.com
parents:
625
diff
changeset
|
136 def setPud(pi, pinNumber, p): |
487cb5277ae3
try to workaround pigpio not supporting pullup modes on pi4 yet (didn't work, I think)
drewp@bigasterisk.com
parents:
625
diff
changeset
|
137 import socket |
487cb5277ae3
try to workaround pigpio not supporting pullup modes on pi4 yet (didn't work, I think)
drewp@bigasterisk.com
parents:
625
diff
changeset
|
138 if socket.gethostname() == 'frontdoor': |
487cb5277ae3
try to workaround pigpio not supporting pullup modes on pi4 yet (didn't work, I think)
drewp@bigasterisk.com
parents:
625
diff
changeset
|
139 log.warn( |
487cb5277ae3
try to workaround pigpio not supporting pullup modes on pi4 yet (didn't work, I think)
drewp@bigasterisk.com
parents:
625
diff
changeset
|
140 'pud currently broken on pi4, using RPi.GPIO.' |
487cb5277ae3
try to workaround pigpio not supporting pullup modes on pi4 yet (didn't work, I think)
drewp@bigasterisk.com
parents:
625
diff
changeset
|
141 ' https://github.com/joan2937/pigpio/issues/278#issuecomment-506650549', |
487cb5277ae3
try to workaround pigpio not supporting pullup modes on pi4 yet (didn't work, I think)
drewp@bigasterisk.com
parents:
625
diff
changeset
|
142 ) |
487cb5277ae3
try to workaround pigpio not supporting pullup modes on pi4 yet (didn't work, I think)
drewp@bigasterisk.com
parents:
625
diff
changeset
|
143 import RPi.GPIO as GPIO |
487cb5277ae3
try to workaround pigpio not supporting pullup modes on pi4 yet (didn't work, I think)
drewp@bigasterisk.com
parents:
625
diff
changeset
|
144 GPIO.setmode(GPIO.BCM) |
487cb5277ae3
try to workaround pigpio not supporting pullup modes on pi4 yet (didn't work, I think)
drewp@bigasterisk.com
parents:
625
diff
changeset
|
145 GPIO.setup(pinNumber, GPIO.IN, pull_up_down={pigpio.PUD_UP:GPIO.PUD_UP,pigpio.PUD_DOWN:GPIO.PUD_DOWN}[p]) |
487cb5277ae3
try to workaround pigpio not supporting pullup modes on pi4 yet (didn't work, I think)
drewp@bigasterisk.com
parents:
625
diff
changeset
|
146 log.warn(' RPi.GPIO says pin %s is %r', pinNumber, GPIO.input(pinNumber)) |
487cb5277ae3
try to workaround pigpio not supporting pullup modes on pi4 yet (didn't work, I think)
drewp@bigasterisk.com
parents:
625
diff
changeset
|
147 return |
487cb5277ae3
try to workaround pigpio not supporting pullup modes on pi4 yet (didn't work, I think)
drewp@bigasterisk.com
parents:
625
diff
changeset
|
148 pi.set_pull_up_down(pinNumber, p) |
487cb5277ae3
try to workaround pigpio not supporting pullup modes on pi4 yet (didn't work, I think)
drewp@bigasterisk.com
parents:
625
diff
changeset
|
149 |
182 | 150 @register |
151 class MotionSensorInput(DeviceType): | |
290 | 152 """ |
407 | 153 Triggering all the time? Try 5V VCC, per https://electronics.stackexchange.com/a/416295 |
624 | 154 |
290 | 155 0 30s 60s 90s 10min |
624 | 156 | | | | ... | |
290 | 157 Sensor input ******** ** ******* **** |
158 :sees output ........ .. ....... .... | |
159 :seesRecently ............................................................. | |
160 :seesRecently30 .................................... | |
161 :motionStart x x x x | |
162 :motionStart30 x x | |
163 """ | |
164 # compare motion sensor lib at | |
165 # https://gpiozero.readthedocs.org/en/v1.2.0/api_input.html#motion-sensor-d-sun-pir | |
221
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
166 # which is a bit fancier |
182 | 167 deviceType = ROOM['MotionSensor'] |
168 | |
304
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
169 def __init__(self, graph, uri, pi, pinNumber): |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
170 super(MotionSensorInput, self).__init__(graph, uri, pi, pinNumber) |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
171 self.pi.set_mode(pinNumber, pigpio.INPUT) |
626
487cb5277ae3
try to workaround pigpio not supporting pullup modes on pi4 yet (didn't work, I think)
drewp@bigasterisk.com
parents:
625
diff
changeset
|
172 setPud(self.pi, pinNumber, pigpio.PUD_DOWN) |
182 | 173 |
254
ba1149814877
pi device support for motionStart edge trigger
drewp@bigasterisk.com
parents:
251
diff
changeset
|
174 def hostStateInit(self): |
ba1149814877
pi device support for motionStart edge trigger
drewp@bigasterisk.com
parents:
251
diff
changeset
|
175 self.lastRead = None |
290 | 176 self.lastMotionStart30 = 0 |
177 self.lastMotionStart90 = 0 | |
254
ba1149814877
pi device support for motionStart edge trigger
drewp@bigasterisk.com
parents:
251
diff
changeset
|
178 |
182 | 179 def poll(self): |
304
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
180 motion = self.pi.read(self.pinNumber) |
290 | 181 now = time.time() |
624 | 182 |
254
ba1149814877
pi device support for motionStart edge trigger
drewp@bigasterisk.com
parents:
251
diff
changeset
|
183 oneshot = [] |
ba1149814877
pi device support for motionStart edge trigger
drewp@bigasterisk.com
parents:
251
diff
changeset
|
184 if self.lastRead is not None and motion != self.lastRead: |
ba1149814877
pi device support for motionStart edge trigger
drewp@bigasterisk.com
parents:
251
diff
changeset
|
185 oneshot = [(self.uri, ROOM['sees'], ROOM['motionStart'])] |
290 | 186 for v, t in [('lastMotionStart30', 30), ('lastMotionStart90', 90)]: |
187 if now - getattr(self, v) > t: | |
188 oneshot.append((self.uri, ROOM['sees'], ROOM['motionStart%s' % t])) | |
189 setattr(self, v, now) | |
254
ba1149814877
pi device support for motionStart edge trigger
drewp@bigasterisk.com
parents:
251
diff
changeset
|
190 self.lastRead = motion |
624 | 191 |
251
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
244
diff
changeset
|
192 return {'latest': [ |
182 | 193 (self.uri, ROOM['sees'], |
194 ROOM['motion'] if motion else ROOM['noMotion']), | |
290 | 195 ] + self.recentMotionStatements(now, motion), |
267 | 196 'oneshot': oneshot} |
182 | 197 |
290 | 198 def recentMotionStatements(self, now, motion): |
182 | 199 if not hasattr(self, 'lastMotionTime'): |
200 self.lastMotionTime = 0 | |
201 if motion: | |
202 self.lastMotionTime = now | |
267 | 203 dt = now - self.lastMotionTime |
204 return [(self.uri, ROOM['seesRecently'], | |
205 ROOM['motion'] if (dt < 60 * 10) else ROOM['noMotion']), | |
206 (self.uri, ROOM['seesRecently30'], | |
207 ROOM['motion'] if (dt < 30) else ROOM['noMotion'])] | |
624 | 208 |
182 | 209 def watchPrefixes(self): |
210 return [ | |
211 (self.uri, ROOM['sees']), | |
212 (self.uri, ROOM['seesRecently']), | |
267 | 213 (self.uri, ROOM['seesRecently30']), |
182 | 214 ] |
215 | |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
216 |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
217 @register |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
218 class RgbStrip(DeviceType): |
220 | 219 """3 PWMs for r/g/b on a strip""" |
221
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
220 # pigpio daemon is working fine, but |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
221 # https://github.com/RPi-Distro/python-gpiozero/blob/59ba7154c5918745ac894ea03503667d6473c760/gpiozero/output_devices.py#L213 |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
222 # can also apparently do PWM |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
223 deviceType = ROOM['RgbStrip'] |
624 | 224 |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
225 @classmethod |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
226 def findInstances(cls, graph, board, pi): |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
227 for row in graph.query("""SELECT DISTINCT ?dev ?r ?g ?b WHERE { |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
228 ?board |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
229 :hasPin ?rpin; |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
230 :hasPin ?gpin; |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
231 :hasPin ?bpin . |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
232 ?dev a :RgbStrip; |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
233 :redChannel ?rpin; |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
234 :greenChannel ?gpin; |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
235 :blueChannel ?bpin . |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
236 ?rpin :gpioNumber ?r . |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
237 ?gpin :gpioNumber ?g . |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
238 ?bpin :gpioNumber ?b . |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
239 } ORDER BY ?dev""", |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
240 initBindings=dict(board=board), |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
241 initNs={'': ROOM}): |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
242 log.debug('found rgb %r', row) |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
243 yield cls(graph, row.dev, pi, row.r, row.g, row.b) |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
244 |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
245 def __init__(self, graph, uri, pi, r, g, b): |
633 | 246 super(RgbStrip, self).__init__(graph, uri, pi, pinNumber='(multiple)') |
247 self.rgb = list(map(int, [r, g, b])) | |
233
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
232
diff
changeset
|
248 self.value = '#000000' |
624 | 249 |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
250 def setup(self): |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
251 for i in self.rgb: |
266
1ae78fe6f36e
neopixel lib change. factor out pwm setup and try another pwm rate
drewp@bigasterisk.com
parents:
262
diff
changeset
|
252 setupPwm(self.pi, i) |
624 | 253 |
233
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
232
diff
changeset
|
254 def hostStatements(self): |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
232
diff
changeset
|
255 return [(self.uri, ROOM['color'], Literal(self.value))] |
624 | 256 |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
257 def outputPatterns(self): |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
258 return [(self.uri, ROOM['color'], None)] |
220 | 259 |
260 def _rgbFromHex(self, h): | |
261 rrggbb = h.lstrip('#') | |
262 return [int(x, 16) for x in [rrggbb[0:2], rrggbb[2:4], rrggbb[4:6]]] | |
624 | 263 |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
264 def sendOutput(self, statements): |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
265 assert len(statements) == 1 |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
266 assert statements[0][:2] == (self.uri, ROOM['color']) |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
267 |
220 | 268 rgb = self._rgbFromHex(statements[0][2]) |
233
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
232
diff
changeset
|
269 self.value = statements[0][2] |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
270 |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
271 for (i, v) in zip(self.rgb, rgb): |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
272 self.pi.set_PWM_dutycycle(i, v) |
624 | 273 |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
274 def outputWidgets(self): |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
275 return [{ |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
276 'element': 'output-rgb', |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
277 'subj': self.uri, |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
278 'pred': ROOM['color'], |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
279 }] |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
280 |
210 | 281 |
282 @register | |
221
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
283 class TempHumidSensor(DeviceType): |
407 | 284 """ |
285 AM2302/DHT22 pinout is vcc-data-nc-gnd. VCC to 3.3V. Add 10k pullup on data. | |
286 """ | |
221
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
287 deviceType = ROOM['TempHumidSensor'] |
304
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
288 pollPeriod = 5 |
221
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
289 |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
290 def __init__(self, *a, **kw): |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
291 DeviceType.__init__(self, *a, **kw) |
345
d36863e09095
switch to pigpio for reading DHT sensor
drewp@bigasterisk.com
parents:
338
diff
changeset
|
292 import DHT22 |
d36863e09095
switch to pigpio for reading DHT sensor
drewp@bigasterisk.com
parents:
338
diff
changeset
|
293 self.sens = DHT22.sensor(self.pi, self.pinNumber) |
367 | 294 self.recentLowTemp = (0, None) # time, temp |
295 self.recentPeriodSec = 30 | |
624 | 296 |
221
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
297 def poll(self): |
345
d36863e09095
switch to pigpio for reading DHT sensor
drewp@bigasterisk.com
parents:
338
diff
changeset
|
298 stmts = set() |
221
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
299 |
367 | 300 now = time.time() |
301 if self.recentLowTemp[0] < now - self.recentPeriodSec: | |
302 self.recentLowTemp = (0, None) | |
303 | |
345
d36863e09095
switch to pigpio for reading DHT sensor
drewp@bigasterisk.com
parents:
338
diff
changeset
|
304 if self.sens.staleness() < self.pollPeriod * 2: |
d36863e09095
switch to pigpio for reading DHT sensor
drewp@bigasterisk.com
parents:
338
diff
changeset
|
305 humid, tempC = self.sens.humidity(), self.sens.temperature() |
d36863e09095
switch to pigpio for reading DHT sensor
drewp@bigasterisk.com
parents:
338
diff
changeset
|
306 if humid > -999: |
d36863e09095
switch to pigpio for reading DHT sensor
drewp@bigasterisk.com
parents:
338
diff
changeset
|
307 stmts.add((self.uri, ROOM['humidity'], Literal(round(humid, 2)))) |
d36863e09095
switch to pigpio for reading DHT sensor
drewp@bigasterisk.com
parents:
338
diff
changeset
|
308 else: |
d36863e09095
switch to pigpio for reading DHT sensor
drewp@bigasterisk.com
parents:
338
diff
changeset
|
309 stmts.add((self.uri, RDFS['comment'], Literal('No recent humidity measurement'))) |
d36863e09095
switch to pigpio for reading DHT sensor
drewp@bigasterisk.com
parents:
338
diff
changeset
|
310 if tempC > -999: |
367 | 311 # see round() note in arduinoNode/devices.py |
312 tempF = round(tempC * 9 / 5 + 32, 2) | |
313 stmts.add((self.uri, ROOM['temperatureF'], Literal(tempF))) | |
314 if self.recentLowTemp[1] is None or tempF < self.recentLowTemp[1]: | |
315 self.recentLowTemp = (now, tempF) | |
345
d36863e09095
switch to pigpio for reading DHT sensor
drewp@bigasterisk.com
parents:
338
diff
changeset
|
316 else: |
d36863e09095
switch to pigpio for reading DHT sensor
drewp@bigasterisk.com
parents:
338
diff
changeset
|
317 stmts.add((self.uri, RDFS['comment'], Literal('No recent temperature measurement'))) |
221
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
318 else: |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
319 stmts.add((self.uri, RDFS['comment'], |
345
d36863e09095
switch to pigpio for reading DHT sensor
drewp@bigasterisk.com
parents:
338
diff
changeset
|
320 Literal('No recent DHT response (%.02f sec old)' % self.sens.staleness()))) |
367 | 321 |
322 if self.recentLowTemp[1] is not None: | |
323 stmts.add((self.uri, ROOM['recentLowTemperatureF'], Literal(self.recentLowTemp[1]))) | |
624 | 324 |
345
d36863e09095
switch to pigpio for reading DHT sensor
drewp@bigasterisk.com
parents:
338
diff
changeset
|
325 self.sens.trigger() |
624 | 326 |
221
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
327 return stmts |
624 | 328 |
221
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
329 def watchPrefixes(self): |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
330 return [ |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
331 (self.uri, ROOM['temperatureF']), |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
332 (self.uri, ROOM['humidity']), |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
333 ] |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
334 |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
335 @register |
244 | 336 class PushbuttonInput(DeviceType): |
337 """add a switch to ground; we'll turn on pullup""" | |
338 deviceType = ROOM['Pushbutton'] | |
339 | |
340 def __init__(self, *a, **kw): | |
341 DeviceType.__init__(self, *a, **kw) | |
342 log.debug("setup switch on %r", self.pinNumber) | |
343 self.pi.set_mode(self.pinNumber, pigpio.INPUT) | |
626
487cb5277ae3
try to workaround pigpio not supporting pullup modes on pi4 yet (didn't work, I think)
drewp@bigasterisk.com
parents:
625
diff
changeset
|
344 setPud(self.pi, self.pinNumber, pigpio.PUD_UP) |
251
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
244
diff
changeset
|
345 self.lastClosed = None |
262 | 346 self.invert = (self.uri, ROOM['style'], |
347 ROOM['inverted']) in self.graph | |
244 | 348 |
349 def poll(self): | |
350 closed = not self.pi.read(self.pinNumber) | |
262 | 351 if self.invert: |
352 closed = not closed | |
251
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
244
diff
changeset
|
353 |
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
244
diff
changeset
|
354 if self.lastClosed is not None and closed != self.lastClosed: |
262 | 355 log.debug('%s changed to %s', self.uri, closed) |
251
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
244
diff
changeset
|
356 oneshot = [ |
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
244
diff
changeset
|
357 (self.uri, ROOM['buttonState'], |
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
244
diff
changeset
|
358 ROOM['press'] if closed else ROOM['release']), |
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
244
diff
changeset
|
359 ] |
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
244
diff
changeset
|
360 else: |
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
244
diff
changeset
|
361 oneshot = [] |
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
244
diff
changeset
|
362 self.lastClosed = closed |
624 | 363 |
251
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
244
diff
changeset
|
364 return {'latest': [ |
244 | 365 (self.uri, ROOM['buttonState'], |
366 ROOM['pressed'] if closed else ROOM['notPressed']), | |
251
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
244
diff
changeset
|
367 ], |
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
244
diff
changeset
|
368 'oneshot':oneshot} |
624 | 369 |
244 | 370 def watchPrefixes(self): |
371 return [ | |
372 (self.uri, ROOM['buttonState']), | |
373 ] | |
624 | 374 |
244 | 375 @register |
221
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
376 class OneWire(DeviceType): |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
377 """ |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
378 Also see /my/proj/ansible/roles/raspi_io_node/tasks/main.yml for |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
379 some system config that contains the pin number that you want to |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
380 use for onewire. The pin number in this config is currently ignored. |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
381 """ |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
382 deviceType = ROOM['OneWire'] |
304
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
383 pollPeriod = 2 |
221
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
384 # deliberately written like arduinoNode's one for an easier merge. |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
385 def __init__(self, *a, **kw): |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
386 DeviceType.__init__(self, *a, **kw) |
381 | 387 import w1thermsensor |
221
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
388 log.info("scan for w1 devices") |
381 | 389 self.SensorNotReadyError = w1thermsensor.core.SensorNotReadyError |
407 | 390 self.ResetValueError = w1thermsensor.core.ResetValueError |
221
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
391 self._sensors = w1thermsensor.W1ThermSensor.get_available_sensors() |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
392 for s in self._sensors: |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
393 # Something looks different about these ids |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
394 # ('000003a5a94c') vs the ones I get from arduino |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
395 # ('2813bea50300003d'). Not sure if I'm parsing them |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
396 # differently or what. |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
397 s.uri = URIRef(os.path.join(self.uri, 'dev-%s' % s.id)) |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
398 log.info(' found temperature sensor %s' % s.uri) |
589
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
407
diff
changeset
|
399 |
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
407
diff
changeset
|
400 @inlineCallbacks |
221
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
401 def poll(self): |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
402 try: |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
403 stmts = [] |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
404 for sensor in self._sensors: |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
405 stmts.append((self.uri, ROOM['connectedTo'], sensor.uri)) |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
406 try: |
589
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
407
diff
changeset
|
407 tempF = yield threads.deferToThread(sensor.get_temperature, sensor.DEGREES_F) |
221
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
408 stmts.append((sensor.uri, ROOM['temperatureF'], |
232
4e91f3ec460b
more round() workarounds for mismatching literals not patching themselves out
drewp@bigasterisk.com
parents:
221
diff
changeset
|
409 # see round() note in arduinoNode/devices.py |
4e91f3ec460b
more round() workarounds for mismatching literals not patching themselves out
drewp@bigasterisk.com
parents:
221
diff
changeset
|
410 Literal(round(tempF, 2)))) |
407 | 411 except (self.SensorNotReadyError, self.ResetValueError) as e: |
221
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
412 log.warning(e) |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
413 |
589
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
407
diff
changeset
|
414 returnValue(stmts) |
221
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
415 except Exception as e: |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
416 log.error(e) |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
417 os.abort() |
624 | 418 |
221
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
419 def watchPrefixes(self): |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
420 return [(s.uri, ROOM['temperatureF']) for s in self._sensors] |
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
421 |
274 | 422 class FilteredValue(object): |
423 def __init__(self, setter, | |
424 slew=2.0, # step/sec max slew rate | |
425 accel=5, # step/sec^2 acceleration | |
426 ): | |
427 self.setter = setter | |
428 self.slew, self.accel = slew, accel | |
624 | 429 |
274 | 430 self.x = None |
431 self.dx = 0 | |
432 self.goal = self.x | |
433 self.lastStep = 0 | |
434 | |
435 def set(self, goal): | |
436 self.goal = goal | |
437 self.step() | |
438 | |
439 def step(self): | |
440 now = time.time() | |
441 dt = min(.1, now - self.lastStep) | |
442 self.lastStep = now | |
443 | |
444 if self.x is None: | |
445 self.x = self.goal | |
446 | |
447 if self.goal > self.x: | |
448 self.dx = min(self.slew, self.dx + self.accel * dt) | |
449 else: | |
450 self.dx = max(-self.slew, self.dx - self.accel * dt) | |
451 | |
452 nextX = self.x + self.dx * dt | |
453 if self.x == self.goal or (self.x < self.goal < nextX) or (self.x > self.goal > nextX): | |
454 self.x = self.goal | |
455 self.dx = 0 | |
456 else: | |
457 self.x = nextX | |
458 reactor.callLater(.05, self.step) | |
459 | |
460 #print "x= %(x)s dx= %(dx)s goal= %(goal)s" % self.__dict__ | |
461 self.setter(self.x) | |
624 | 462 |
221
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
463 @register |
210 | 464 class LedOutput(DeviceType): |
465 deviceType = ROOM['LedOutput'] | |
233
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
232
diff
changeset
|
466 |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
232
diff
changeset
|
467 def hostStateInit(self): |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
232
diff
changeset
|
468 self.value = 0 |
304
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
469 if (self.uri, ROOM['fade'], None) in self.graph: |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
470 # incomplete- the object could be fade settings |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
471 self.fv = FilteredValue(self._setPwm) |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
472 else: |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
473 _setPwm = self._setPwm |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
474 class Instant(object): |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
475 def set(self, goal): |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
476 _setPwm(goal) |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
477 self.fv = Instant() |
289 | 478 self.gamma = float(self.graph.value(self.uri, ROOM['gamma'], default=1)) |
624 | 479 |
210 | 480 def setup(self): |
266
1ae78fe6f36e
neopixel lib change. factor out pwm setup and try another pwm rate
drewp@bigasterisk.com
parents:
262
diff
changeset
|
481 setupPwm(self.pi, self.pinNumber) |
210 | 482 |
483 def outputPatterns(self): | |
484 return [(self.uri, ROOM['brightness'], None)] | |
624 | 485 |
210 | 486 def sendOutput(self, statements): |
487 assert len(statements) == 1 | |
488 assert statements[0][:2] == (self.uri, ROOM['brightness']) | |
233
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
232
diff
changeset
|
489 self.value = float(statements[0][2]) |
274 | 490 self.fv.set(self.value) |
491 | |
492 def _setPwm(self, x): | |
324 | 493 v = max(0, min(255, int((x ** self.gamma)* 255))) |
210 | 494 self.pi.set_PWM_dutycycle(self.pinNumber, v) |
233
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
232
diff
changeset
|
495 |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
232
diff
changeset
|
496 def hostStatements(self): |
624 | 497 return [(self.uri, ROOM['brightness'], Literal(self.value))] |
498 | |
210 | 499 def outputWidgets(self): |
500 return [{ | |
501 'element': 'output-slider', | |
502 'min': 0, | |
503 'max': 1, | |
504 'step': 1 / 255, | |
505 'subj': self.uri, | |
506 'pred': ROOM['brightness'], | |
507 }] | |
508 | |
624 | 509 |
205 | 510 @register |
511 class OnboardTemperature(DeviceType): | |
512 deviceType = ROOM['OnboardTemperature'] | |
243
141079644c45
piNode poll switches much faster. mirror the logic in arduinoNode though vari-rate poll is not supported yet
drewp@bigasterisk.com
parents:
233
diff
changeset
|
513 pollPeriod = 10 |
205 | 514 @classmethod |
515 def findInstances(cls, graph, board, pi): | |
259 | 516 log.debug('graph has any connected devices of type OnboardTemperature on %r?', board) |
207 | 517 for row in graph.query('''SELECT DISTINCT ?uri WHERE { |
624 | 518 ?board :onboardDevice ?uri . |
205 | 519 ?uri a :OnboardTemperature . |
221
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
520 }''', initBindings=dict(board=board)): |
259 | 521 log.debug(' found %s', row.uri) |
205 | 522 yield cls(graph, row.uri, pi, pinNumber=None) |
624 | 523 |
208 | 524 def poll(self): |
205 | 525 milliC = open('/sys/class/thermal/thermal_zone0/temp').read().strip() |
526 c = float(milliC) / 1000. | |
527 f = c * 1.8 + 32 | |
528 return [ | |
529 (self.uri, ROOM['temperatureF'], Literal(f, datatype=XSD['decimal'])), | |
530 ] | |
531 | |
532 def watchPrefixes(self): | |
533 # these uris will become dynamic! see note on watchPrefixes | |
534 # about eliminating it. | |
535 return [(self.uri, ROOM['temperatureF']), | |
536 ] | |
259 | 537 |
589
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
407
diff
changeset
|
538 pixelStats = scales.collection('/rgbPixels', |
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
407
diff
changeset
|
539 scales.PmfStat('updateOutput'), |
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
407
diff
changeset
|
540 scales.PmfStat('currentColors'), |
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
407
diff
changeset
|
541 scales.PmfStat('poll'), |
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
407
diff
changeset
|
542 ) |
624 | 543 |
259 | 544 @register |
545 class RgbPixels(DeviceType): | |
546 """chain of ws2812 rgb pixels on pin GPIO18""" | |
547 deviceType = ROOM['RgbPixels'] | |
548 | |
549 def hostStateInit(self): | |
338
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
550 self.anim = RgbPixelsAnimation(self.graph, self.uri, self.updateOutput) |
368 | 551 log.debug('%s maxIndex = %s', self.uri, self.anim.maxIndex()) |
552 self.neo = rpi_ws281x.Adafruit_NeoPixel(self.anim.maxIndex() + 1, pin=18) | |
259 | 553 self.neo.begin() |
624 | 554 |
338
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
555 colorOrder, stripType = self.anim.getColorOrder(self.graph, self.uri) |
624 | 556 |
338
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
557 def sendOutput(self, statements): |
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
558 self.anim.onStatements(statements) |
266
1ae78fe6f36e
neopixel lib change. factor out pwm setup and try another pwm rate
drewp@bigasterisk.com
parents:
262
diff
changeset
|
559 |
589
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
407
diff
changeset
|
560 @pixelStats.updateOutput.time() |
338
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
561 def updateOutput(self): |
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
562 if 0: |
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
563 for _, _, sg in self.anim.groups.values(): |
625
69a84b3d1dfa
frontdoor configs; move device class timing to greplin stats
drewp@bigasterisk.com
parents:
624
diff
changeset
|
564 print (sg.uri, sg.current) |
69a84b3d1dfa
frontdoor configs; move device class timing to greplin stats
drewp@bigasterisk.com
parents:
624
diff
changeset
|
565 print (list(self.anim.currentColors())) |
338
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
566 return |
589
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
407
diff
changeset
|
567 |
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
407
diff
changeset
|
568 with pixelStats.currentColors.time(): |
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
407
diff
changeset
|
569 colors = self.anim.currentColors() |
624 | 570 |
589
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
407
diff
changeset
|
571 for idx, (r, g, b) in colors: |
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
407
diff
changeset
|
572 if idx < 4: |
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
407
diff
changeset
|
573 log.debug('out color %s (%s,%s,%s)', idx, r, g, b) |
338
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
574 self.neo.setPixelColorRGB(idx, r, g, b) |
259 | 575 self.neo.show() |
576 | |
589
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
407
diff
changeset
|
577 @pixelStats.poll.time() |
338
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
578 def poll(self): |
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
579 self.anim.step() |
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
580 return [] |
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
581 |
259 | 582 def hostStatements(self): |
338
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
583 return self.anim.hostStatements() |
624 | 584 |
259 | 585 def outputPatterns(self): |
338
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
586 return self.anim.outputPatterns() |
259 | 587 |
588 def outputWidgets(self): | |
338
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
589 return self.anim.outputWidgets() |
259 | 590 |
304
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
591 @register |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
592 class Lcd8544(DeviceType): |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
593 """PCD8544 lcd (nokia 5110)""" |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
594 deviceType = ROOM['RgbStrip'] |
624 | 595 |
304
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
596 @classmethod |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
597 def findInstances(cls, graph, board, pi): |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
598 for row in graph.query(""" |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
599 SELECT DISTINCT ?dev ?din ?clk ?dc ?rst WHERE { |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
600 ?dev a :Lcd8544 . |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
601 ?board :hasPin ?dinPin . ?dev :din ?dinPin . ?dinPin :gpioNumber ?din . |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
602 ?board :hasPin ?clkPin . ?dev :clk ?clkPin . ?clkPin :gpioNumber ?clk . |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
603 ?board :hasPin ?dcPin . ?dev :dc ?dcPin . ?dcPin :gpioNumber ?dc . |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
604 ?board :hasPin ?rstPin . ?dev :rst ?rstPin . ?rstPin :gpioNumber ?rst . |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
605 } ORDER BY ?dev""", |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
606 initBindings=dict(board=board), |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
607 initNs={'': ROOM}): |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
608 log.debug('found lcd %r', row) |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
609 yield cls(graph, row.dev, pi, |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
610 int(row.din), int(row.clk), |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
611 int(row.dc), int(row.rst)) |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
612 |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
613 def __init__(self, graph, uri, pi, din, clk, dc, rst): |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
614 super(Lcd8544, self).__init__(graph, uri, pi, None) |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
615 |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
616 |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
617 import RPi.GPIO |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
618 import Adafruit_Nokia_LCD |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
619 import Adafruit_GPIO.SPI |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
620 self.lcd = Adafruit_Nokia_LCD.PCD8544( |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
621 dc=8, rst=7, |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
622 spi=Adafruit_GPIO.SPI.BitBang( |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
623 Adafruit_Nokia_LCD.GPIO.RPiGPIOAdapter(RPi.GPIO), |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
624 sclk=clk, |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
625 mosi=din)) |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
626 self.lcd.begin(contrast=60) |
624 | 627 |
304
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
628 def hostStatements(self): |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
629 return [] |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
630 return [(self.uri, ROOM['color'], Literal(self.value))] |
624 | 631 |
304
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
632 def outputPatterns(self): |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
633 return [] |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
634 return [(self.uri, ROOM['color'], None)] |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
635 |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
636 def sendOutput(self, statements): |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
637 return |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
638 assert len(statements) == 1 |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
639 assert statements[0][:2] == (self.uri, ROOM['color']) |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
640 |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
641 rgb = self._rgbFromHex(statements[0][2]) |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
642 self.value = statements[0][2] |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
643 |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
644 for (i, v) in zip(self.rgb, rgb): |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
645 self.pi.set_PWM_dutycycle(i, v) |
624 | 646 |
304
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
647 def outputWidgets(self): |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
648 return [] |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
649 return [{ |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
650 'element': 'output-rgb', |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
651 'subj': self.uri, |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
652 'pred': ROOM['color'], |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
653 }] |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
654 |
407 | 655 @register |
656 class PwmBoard(DeviceType): | |
657 """ | |
658 need this in /boot/config.txt | |
659 dtparam=i2c_arm=on | |
660 check for devices with | |
661 apt-get install -y i2c-tools | |
662 sudo i2cdetect -y 1 | |
663 | |
664 gpio8 = bcm2 = sda1 | |
665 gpio9 = bcm3 = scl1 | |
666 They're next to the 3v3 pin. | |
667 """ | |
668 deviceType = ROOM['PwmBoard'] | |
669 @classmethod | |
670 def findInstances(cls, graph, board, pi): | |
671 for row in graph.query("""SELECT DISTINCT ?dev WHERE { | |
672 ?board :hasI2cBus ?bus . | |
673 ?bus :connectedTo ?dev . | |
674 ?dev a :PwmBoard . | |
675 }""", initBindings=dict(board=board), initNs={'': ROOM}): | |
676 outs = {} | |
677 for out in graph.query("""SELECT DISTINCT ?area ?chan WHERE { | |
678 ?dev :output [:area ?area; :channel ?chan] . | |
679 }""", initBindings=dict(dev=row.dev), initNs={'': ROOM}): | |
680 outs[out.area] = out.chan.toPython() | |
681 yield cls(graph, row.dev, pi, outs) | |
624 | 682 |
407 | 683 def __init__(self, graph, dev, pi, outs): |
684 super(PwmBoard, self).__init__(graph, dev, pi, pinNumber=None) | |
685 import PCA9685 | |
686 self.pwm = PCA9685.PWM(pi, bus=1, address=0x40) | |
687 self.pwm.set_frequency(1200) | |
688 self.outs = outs | |
689 self.values = {uri: 0 for uri in self.outs.keys()} # uri: brightness | |
690 | |
691 def hostStatements(self): | |
692 return [(uri, ROOM['brightness'], Literal(b)) | |
693 for uri, b in self.values.items()] | |
694 | |
695 def outputPatterns(self): | |
696 return [(area, ROOM['brightness'], None) for area in self.outs] | |
697 | |
698 def sendOutput(self, statements): | |
699 assert len(statements) == 1 | |
700 assert statements[0][1] == ROOM['brightness']; | |
701 chan = self.outs[statements[0][0]] | |
702 value = float(statements[0][2]) | |
703 self.values[statements[0][0]] = value | |
704 self.pwm.set_duty_cycle(chan, value * 100) | |
624 | 705 |
407 | 706 def outputWidgets(self): |
707 return [{ | |
708 'element': 'output-slider', | |
709 'min': 0, | |
710 'max': 1, | |
711 'step': 1 / 255, | |
712 'subj': area, | |
713 'pred': ROOM['brightness'], | |
714 } for area in self.outs] | |
304
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
290
diff
changeset
|
715 |
624 | 716 |
182 | 717 def makeDevices(graph, board, pi): |
718 out = [] | |
719 for dt in sorted(_knownTypes, key=lambda cls: cls.__name__): | |
720 out.extend(dt.findInstances(graph, board, pi)) | |
721 return out |