Mercurial > code > home > repos > homeauto
comparison service/arduinoNode/devices.py @ 335:3010238b94a0
rgb strip animation support in arduinoNode
Ignore-this: 5f50c7b54ce1588243735c43d2cbea0f
author | drewp@bigasterisk.com |
---|---|
date | Sat, 03 Mar 2018 17:55:37 -0800 |
parents | bb80182195c0 |
children | 686079900c20 |
comparison
equal
deleted
inserted
replaced
334:bb80182195c0 | 335:3010238b94a0 |
---|---|
639 """chain of FastLED-controllable rgb pixels""" | 639 """chain of FastLED-controllable rgb pixels""" |
640 deviceType = ROOM['RgbPixels'] | 640 deviceType = ROOM['RgbPixels'] |
641 | 641 |
642 def __init__(self, graph, uri, pinNumber): | 642 def __init__(self, graph, uri, pinNumber): |
643 super(RgbPixels, self).__init__(graph, uri, pinNumber) | 643 super(RgbPixels, self).__init__(graph, uri, pinNumber) |
644 px = graph.value(self.uri, ROOM['pixels']) | 644 self.anim = RgbPixelsAnimation(graph, uri, self.updateOutput) |
645 self.pixelUris = list(graph.items(px)) | 645 |
646 self.values = dict((uri, Literal('#000000')) for uri in self.pixelUris) | |
647 self.replace = {'ledArray': 'leds_%s' % self.pinNumber, | 646 self.replace = {'ledArray': 'leds_%s' % self.pinNumber, |
648 'ledCount': len(self.pixelUris), | 647 'ledCount': self.anim.maxIndex() - 1, |
649 'pin': self.pinNumber, | 648 'pin': self.pinNumber, |
650 'ledType': 'WS2812', | 649 'ledType': 'WS2812', |
651 } | 650 } |
652 | 651 |
653 def generateIncludes(self): | 652 def generateIncludes(self): |
665 return 'CRGB {ledArray}[{ledCount}];'.format(**self.replace) | 664 return 'CRGB {ledArray}[{ledCount}];'.format(**self.replace) |
666 | 665 |
667 def generateSetupCode(self): | 666 def generateSetupCode(self): |
668 return 'FastLED.addLeds<{ledType}, {pin}>({ledArray}, {ledCount});'.format(**self.replace) | 667 return 'FastLED.addLeds<{ledType}, {pin}>({ledArray}, {ledCount});'.format(**self.replace) |
669 | 668 |
670 def _rgbFromHex(self, h): | |
671 rrggbb = h.lstrip('#') | |
672 return [int(x, 16) for x in [rrggbb[0:2], rrggbb[2:4], rrggbb[4:6]]] | |
673 | |
674 def sendOutput(self, statements, write, read): | 669 def sendOutput(self, statements, write, read): |
675 px, pred, color = statements[0] | 670 log.info('sendOutput start') |
676 if pred != ROOM['color']: | 671 self.write = write |
677 raise ValueError(pred) | 672 self.anim.onStatements(statements) |
678 rgb = self._rgbFromHex(color) | 673 self.anim.updateOutput() |
679 if px not in self.values: | 674 log.info('sendOutput end') |
680 raise ValueError(px) | 675 |
681 self.values[px] = Literal(color) | 676 def updateOutput(self): |
682 write(chr(self.pixelUris.index(px)) + | 677 write = self.write |
683 chr(rgb[1]) + # my WS2812 need these flipped | 678 write(chr(self.anim.maxIndex() + 1)) |
684 chr(rgb[0]) + | 679 for idx, (r, g, b) in self.anim.currentColors(): |
685 chr(rgb[2])) | 680 # my WS2812 need these flipped |
686 | 681 write(chr(idx) + chr(g) + chr(r) + chr(b)) |
682 | |
683 def wantIdleOutput(self): | |
684 return True | |
685 | |
686 def outputIdle(self, write): | |
687 self.write = write | |
688 self.updateOutput() | |
689 | |
687 def hostStatements(self): | 690 def hostStatements(self): |
688 return [(uri, ROOM['color'], hexCol) | 691 return self.anim.hostStatements() |
689 for uri, hexCol in self.values.items()] | |
690 | 692 |
691 def outputPatterns(self): | 693 def outputPatterns(self): |
692 return [(px, ROOM['color'], None) for px in self.pixelUris] | 694 return self.anim.outputPatterns() |
695 | |
696 def outputWidgets(self): | |
697 return self.anim.outputWidgets() | |
693 | 698 |
694 def generateActionCode(self): | 699 def generateActionCode(self): |
695 | 700 # not yet combining updates- may be slow |
696 return ''' | 701 return ''' |
697 | 702 while(Serial.available() < 1) NULL; |
703 byte count = Serial.read(); | |
704 for (byte elt=0; elt<count; ++elt) {{ | |
698 while(Serial.available() < 1) NULL; | 705 while(Serial.available() < 1) NULL; |
699 byte id = Serial.read(); | 706 byte id = Serial.read(); |
700 | 707 |
701 while(Serial.available() < 1) NULL; | 708 while(Serial.available() < 1) NULL; |
702 byte r = Serial.read(); | 709 byte r = Serial.read(); |
705 byte g = Serial.read(); | 712 byte g = Serial.read(); |
706 | 713 |
707 while(Serial.available() < 1) NULL; | 714 while(Serial.available() < 1) NULL; |
708 byte b = Serial.read(); | 715 byte b = Serial.read(); |
709 | 716 |
710 {ledArray}[id] = CRGB(r, g, b); FastLED.show(); | 717 {ledArray}[id] = CRGB(r, g, b); |
718 }} | |
719 FastLED.show(); | |
711 | 720 |
712 '''.format(**self.replace) | 721 '''.format(**self.replace) |
713 | |
714 def outputWidgets(self): | |
715 return [{ | |
716 'element': 'output-rgb', | |
717 'subj': px, | |
718 'pred': ROOM['color'], | |
719 } for px in self.pixelUris] | |
720 | 722 |
721 def makeDevices(graph, board): | 723 def makeDevices(graph, board): |
722 out = [] | 724 out = [] |
723 for dt in sorted(_knownTypes, key=lambda cls: cls.__name__): | 725 for dt in sorted(_knownTypes, key=lambda cls: cls.__name__): |
724 out.extend(dt.findInstances(graph, board)) | 726 out.extend(dt.findInstances(graph, board)) |