|
|
|
@ -23,32 +23,35 @@ class Lightrix:
|
|
|
|
|
self.patterns[u'timebase'] = self.timebased
|
|
|
|
|
self.patterns[u'wheel'] = self.wheelStrip
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def handler(self, event):
|
|
|
|
|
etype = event[u'type']
|
|
|
|
|
if etype == "m.room.message":
|
|
|
|
|
self.handle_message(event)
|
|
|
|
|
if etype == "org.lightrix.color":
|
|
|
|
|
self.handle_lightrix(event) # XXX
|
|
|
|
|
|
|
|
|
|
def set_brightness(self, brightness):
|
|
|
|
|
if brightness > 255:
|
|
|
|
|
return
|
|
|
|
|
if brightness < 0:
|
|
|
|
|
return
|
|
|
|
|
self.strip.setBrightness(brightness)
|
|
|
|
|
self.strip.show()
|
|
|
|
|
|
|
|
|
|
def handle_lightrix(self, event):
|
|
|
|
|
color = event[u'content'].get(u'color')
|
|
|
|
|
r = color[u'r']
|
|
|
|
|
g = color[u'g']
|
|
|
|
|
b = color[u'b']
|
|
|
|
|
|
|
|
|
|
def set_pattern(self, pattern, msg=None):
|
|
|
|
|
for led_idx in range(0, self.strip.count()):
|
|
|
|
|
color = pattern(led_idx, msg)
|
|
|
|
|
self.strip.setPixelRgb(led_idx, color[0], color[1], color[2])
|
|
|
|
|
self.strip.setPixelRgb(led_idx, r,g,b)
|
|
|
|
|
for room_id in self.matrix.room_ids:
|
|
|
|
|
self.matrix.client.api.send_state_event(
|
|
|
|
|
room_id, "m.lightrix.pattern",
|
|
|
|
|
{"pattern": self.strip.show(), "from_msg": msg}
|
|
|
|
|
room_id, "org.lightrix.pattern",
|
|
|
|
|
{"state": self.strip.show(), "from_msg": color}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def handler(self, chunk):
|
|
|
|
|
if not chunk[u'content'].get(u'body'):
|
|
|
|
|
|
|
|
|
|
self.strip.show()
|
|
|
|
|
|
|
|
|
|
def handle_message(self, event):
|
|
|
|
|
if not event[u'content'].get(u'body'):
|
|
|
|
|
return
|
|
|
|
|
text = chunk[u'content'][u'body'].lower()
|
|
|
|
|
room = self.matrix.rooms.get(str(chunk[u'room_id']))
|
|
|
|
|
text = event[u'content'][u'body'].lower()
|
|
|
|
|
room = self.matrix.rooms.get(str(event[u'room_id']))
|
|
|
|
|
if not room:
|
|
|
|
|
return
|
|
|
|
|
if text.startswith("!brightness"):
|
|
|
|
@ -57,19 +60,38 @@ class Lightrix:
|
|
|
|
|
self.set_brightness(int(text.split(' ')[1]))
|
|
|
|
|
return
|
|
|
|
|
if text.startswith("!wheel"):
|
|
|
|
|
self.set_pattern(self.patterns[u'wheel'], text)
|
|
|
|
|
self.set_pattern(u'wheel', text)
|
|
|
|
|
return
|
|
|
|
|
if text.startswith("!join"):
|
|
|
|
|
room = text.split(' ')[1:]
|
|
|
|
|
room.send_text("Joining %s" % room)
|
|
|
|
|
self.join_room(room)
|
|
|
|
|
self.matrix.join_room(str(room))
|
|
|
|
|
return
|
|
|
|
|
if not text in self.patterns:
|
|
|
|
|
return
|
|
|
|
|
room.send_text("Setting color to %s" % text)
|
|
|
|
|
self.set_pattern(self.patterns[text])
|
|
|
|
|
self.set_pattern(text, text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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):
|
|
|
|
|
patfunc = self.patterns[pattern]
|
|
|
|
|
for led_idx in range(0, self.strip.count()):
|
|
|
|
|
color = patfunc(led_idx, msg)
|
|
|
|
|
self.strip.setPixelRgb(led_idx, color[0], color[1], color[2])
|
|
|
|
|
for room_id in self.matrix.room_ids:
|
|
|
|
|
self.matrix.client.api.send_state_event(
|
|
|
|
|
room_id, "org.lightrix.pattern",
|
|
|
|
|
{"pattern": pattern, "state": self.strip.show(), "from_msg": msg}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def wheel(self, pos):
|
|
|
|
|
"""Generate rainbow colors across 0-255 positions."""
|
|
|
|
|
if pos < 85:
|
|
|
|
|