Mercurial > code > home > repos > homeauto
comparison service/piNode/devices.py @ 1064:d29b9212c4c5
add support for rgb leds on rpi
Ignore-this: cf43ab04f853069450e0f9d42786ea64
darcs-hash:642ac873bfd79da11b39c66f659c5898d41ce9bc
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Mon, 21 Mar 2016 04:24:31 -0700 |
parents | faf1f8677a91 |
children | 9e388c3f032c |
comparison
equal
deleted
inserted
replaced
1063:295d20307b81 | 1064:d29b9212c4c5 |
---|---|
6 try: | 6 try: |
7 import pigpio | 7 import pigpio |
8 except ImportError: | 8 except ImportError: |
9 pigpio = None | 9 pigpio = None |
10 import w1thermsensor | 10 import w1thermsensor |
11 | 11 try: |
12 import NeoPixel | |
13 except ImportError: | |
14 NeoPixel = None | |
15 | |
12 import sys | 16 import sys |
13 | 17 |
14 log = logging.getLogger() | 18 log = logging.getLogger() |
15 ROOM = Namespace('http://projects.bigasterisk.com/room/') | 19 ROOM = Namespace('http://projects.bigasterisk.com/room/') |
16 XSD = Namespace('http://www.w3.org/2001/XMLSchema#') | 20 XSD = Namespace('http://www.w3.org/2001/XMLSchema#') |
373 class OnboardTemperature(DeviceType): | 377 class OnboardTemperature(DeviceType): |
374 deviceType = ROOM['OnboardTemperature'] | 378 deviceType = ROOM['OnboardTemperature'] |
375 pollPeriod = 10 | 379 pollPeriod = 10 |
376 @classmethod | 380 @classmethod |
377 def findInstances(cls, graph, board, pi): | 381 def findInstances(cls, graph, board, pi): |
382 log.debug('graph has any connected devices of type OnboardTemperature on %r?', board) | |
378 for row in graph.query('''SELECT DISTINCT ?uri WHERE { | 383 for row in graph.query('''SELECT DISTINCT ?uri WHERE { |
379 ?board :onboardDevice ?uri . | 384 ?board :onboardDevice ?uri . |
380 ?uri a :OnboardTemperature . | 385 ?uri a :OnboardTemperature . |
381 }''', initBindings=dict(board=board)): | 386 }''', initBindings=dict(board=board)): |
387 log.debug(' found %s', row.uri) | |
382 yield cls(graph, row.uri, pi, pinNumber=None) | 388 yield cls(graph, row.uri, pi, pinNumber=None) |
383 | 389 |
384 def poll(self): | 390 def poll(self): |
385 milliC = open('/sys/class/thermal/thermal_zone0/temp').read().strip() | 391 milliC = open('/sys/class/thermal/thermal_zone0/temp').read().strip() |
386 c = float(milliC) / 1000. | 392 c = float(milliC) / 1000. |
392 def watchPrefixes(self): | 398 def watchPrefixes(self): |
393 # these uris will become dynamic! see note on watchPrefixes | 399 # these uris will become dynamic! see note on watchPrefixes |
394 # about eliminating it. | 400 # about eliminating it. |
395 return [(self.uri, ROOM['temperatureF']), | 401 return [(self.uri, ROOM['temperatureF']), |
396 ] | 402 ] |
403 | |
404 @register | |
405 class RgbPixels(DeviceType): | |
406 """chain of ws2812 rgb pixels on pin GPIO18""" | |
407 deviceType = ROOM['RgbPixels'] | |
408 | |
409 def hostStateInit(self): | |
410 px = self.graph.value(self.uri, ROOM['pixels']) | |
411 self.pixelUris = list(self.graph.items(px)) | |
412 self.values = dict((uri, Literal('#000000')) for uri in self.pixelUris) | |
413 self.replace = {'ledArray': 'leds_%s' % self.pinNumber, | |
414 'ledCount': len(self.pixelUris), | |
415 'pin': self.pinNumber, | |
416 'ledType': 'WS2812', | |
417 } | |
418 self.neo = NeoPixel.NeoPixel(len(self.values)) | |
419 self.neo.begin() | |
420 | |
421 def _rgbFromHex(self, h): | |
422 rrggbb = h.lstrip('#') | |
423 return [int(x, 16) for x in [rrggbb[0:2], rrggbb[2:4], rrggbb[4:6]]] | |
424 | |
425 def sendOutput(self, statements): | |
426 px, pred, color = statements[0] | |
427 if pred != ROOM['color']: | |
428 raise ValueError(pred) | |
429 rgb = self._rgbFromHex(color) | |
430 if px not in self.values: | |
431 raise ValueError(px) | |
432 self.values[px] = Literal(color) | |
433 self.neo.setPixelColor(self.pixelUris.index(px), rgb[0], rgb[1], rgb[2]) | |
434 self.neo.show() | |
435 | |
436 def hostStatements(self): | |
437 return [(uri, ROOM['color'], hexCol) | |
438 for uri, hexCol in self.values.items()] | |
439 | |
440 def outputPatterns(self): | |
441 return [(px, ROOM['color'], None) for px in self.pixelUris] | |
442 | |
443 def outputWidgets(self): | |
444 return [{ | |
445 'element': 'output-rgb', | |
446 'subj': px, | |
447 'pred': ROOM['color'], | |
448 } for px in self.pixelUris] | |
449 | |
397 | 450 |
398 def makeDevices(graph, board, pi): | 451 def makeDevices(graph, board, pi): |
399 out = [] | 452 out = [] |
400 for dt in sorted(_knownTypes, key=lambda cls: cls.__name__): | 453 for dt in sorted(_knownTypes, key=lambda cls: cls.__name__): |
401 out.extend(dt.findInstances(graph, board, pi)) | 454 out.extend(dt.findInstances(graph, board, pi)) |