|
|
|
@ -5,12 +5,69 @@ import pytz
|
|
|
|
|
|
|
|
|
|
class Lightrix:
|
|
|
|
|
|
|
|
|
|
def __init__(self, strip):
|
|
|
|
|
def __init__(self, strip, matrix=None):
|
|
|
|
|
self.strip = strip
|
|
|
|
|
self.matrix = matrix
|
|
|
|
|
|
|
|
|
|
self.patterns = {}
|
|
|
|
|
self.patterns[u'red'] = lambda ix: (255, 0, 0)
|
|
|
|
|
self.patterns[u'green'] = lambda ix: (0, 255, 0)
|
|
|
|
|
self.patterns[u'blue'] = lambda ix: (0, 0, 255)
|
|
|
|
|
self.patterns[u'yellow'] = lambda ix: (255, 255, 0)
|
|
|
|
|
self.patterns[u'white'] = lambda ix: (255, 255, 255)
|
|
|
|
|
self.patterns[u'off'] = lambda ix: (0,0,0)
|
|
|
|
|
self.patterns[u'purple'] = lambda ix: (255,0,255)
|
|
|
|
|
self.patterns[u'cyan'] = lambda ix: (0,255,255)
|
|
|
|
|
self.patterns[u'rainbow'] = lambda ix: self.wheel(int(255 * ix / self.strip.count()))
|
|
|
|
|
self.patterns[u'cycle'] = self.rainbowCycle
|
|
|
|
|
self.patterns[u'timebase'] = self.timebased
|
|
|
|
|
self.patterns[u'wheel'] = self.wheelStrip
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def set_brightness(self, brightness):
|
|
|
|
|
if brightness > 255:
|
|
|
|
|
return
|
|
|
|
|
if brightness < 0:
|
|
|
|
|
return
|
|
|
|
|
self.strip.setBrightness(brightness)
|
|
|
|
|
self.strip.show()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def set_pattern(self, pattern, msg=None):
|
|
|
|
|
for led_idx in range(0, self.strip.count()):
|
|
|
|
|
color = pattern(led_idx)
|
|
|
|
|
self.strip.setPixelRgb(led_idx, color[0], color[1], color[2])
|
|
|
|
|
self.strip.show()
|
|
|
|
|
for room_id in self.matrix.room_ids:
|
|
|
|
|
self.matrix.client.api.send_state_event(
|
|
|
|
|
room_id, "m.lightrix.pattern",
|
|
|
|
|
{"pattern": str(pattern), "from_msg": msg}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def handler(self, chunk):
|
|
|
|
|
if not chunk[u'content'].get(u'body'):
|
|
|
|
|
return
|
|
|
|
|
text = chunk[u'content'][u'body'].lower()
|
|
|
|
|
room = self.matrix.rooms[str(chunk[u'room_id'])]
|
|
|
|
|
if text.startswith("!brightness"):
|
|
|
|
|
brightness = int(text.split(' ')[1])
|
|
|
|
|
room.send_text("Setting brightness to %s/255" % brightness)
|
|
|
|
|
self.set_brightness(int(text.split(' ')[1]))
|
|
|
|
|
return
|
|
|
|
|
if text.startswith("!wheel"):
|
|
|
|
|
self.set_pattern(self.patterns[u'wheel'], text)
|
|
|
|
|
return
|
|
|
|
|
if text.startswith("!join"):
|
|
|
|
|
room = text.split(' ')[1:]
|
|
|
|
|
room.send_text("Joining %s" % room)
|
|
|
|
|
self.join_room(room)
|
|
|
|
|
return
|
|
|
|
|
if not text in self.patterns:
|
|
|
|
|
return
|
|
|
|
|
room.send_text("Setting color to %s" % text)
|
|
|
|
|
self.set_pattern(self.patterns[text])
|
|
|
|
|
|
|
|
|
|
def handler(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def wheel(pos):
|
|
|
|
|