I should have commited this sooner.

master
Ryan Rix 8 years ago
commit 454435dcfc

@ -0,0 +1,149 @@
#!/bin/env python
from matrix_client.client import MatrixClient
import argparse
import yaml
import sys
import os
from neopixel import *
import time
import datetime
import pytz
LED_COUNT = 48
LED_PIN = 18
LED_FREQ_HZ = 800000
LED_DMA = 5
LED_BRIGHTNESS = 255
LED_INVERT = False
class Lightrix:
def __init__(self, config, room):
self.client = MatrixClient("http://matrix01:8008")
self.token = self.client.login_with_password(username=config['username'],
password=config['password'])
self.join_room(room)
self.strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS)
self.strip.begin()
self.init_patterns()
return
def init_patterns():
def wheel(pos):
"""Generate rainbow colors across 0-255 positions."""
if pos < 85:
return Color(pos * 3, 255 - pos * 3, 0)
elif pos < 170:
pos -= 85
return Color(255 - pos * 3, 0, pos * 3)
else:
pos -= 170
return Color(0, pos * 3, 255 - pos * 3)
def rainbowCycle(idx, wait_ms=20, iterations=5):
"""Draw rainbow that uniformly distributes itself across all pixels."""
for j in range(256*iterations):
for i in range(self.strip.numPixels()):
self.strip.setPixelColor(i, wheel(((i * 256 / self.strip.numPixels()) + j) & 255))
self.strip.show()
time.sleep(wait_ms/1000.0)
return
def timebased(idx):
time = datetime.datetime.now().astimezone(pytz.timezone('US/Pacific'))
m = time.minute
if time.hour < 7 and time.hour > 0: # low red at night
return Color(64, 0, 0)
if time.hour == 7: # fade to brighter yellow over the morning
return Color(64 + m, m, 0)
if time.hour == 8: # fade to brighter yellow over the morning
return Color(128 + m, 64 + m, m)
if time.hour == 9: # fade to brighter yellow over the morning
return Color(202 + m, 128 + m, 64 + m)
if time.hour > 9 and time.hour < 18: # off during the day
return Color(0, 0, 0)
if time.hour > 18 and time.hour < 22: # medium yellow-white in the evening
return Color(128, 128, 64)
if time.hour == 22: # slowly fade to low red over the hour
return Color(128 + 60 - m, 128 + 60 - m, 0)
if time.hour == 23: # slowly fade to low red over the hour
return Color(64 + 60 - m, 64 + 60 - (m * 2), 0)
if time.hour == 24: # low red at night
return Color(64, 0, 0)
np = self.strip.numPixels()
self.patterns = {}
self.patterns['red'] = [Color(255, 0, 0) for i in range(0, np)]
self.patterns['green'] = [Color(0, 255, 0) for i in range(0, np)]
self.patterns['blue'] = [Color(0, 0, 255) for i in range(0, np)]
self.patterns['yellow'] = [Color(255, 255, 0) for i in range(0, np)]
self.patterns['white'] = [Color(255, 255, 255) for i in range(0, np)]
self.patterns['off'] = [Color(0,0,0) for i in range(0, np)]
self.patterns['purple'] = [Color(255,0,255) for i in range(0, np)]
self.patterns['cyan'] = [Color(0,255,255) for i in range(0, np)]
self.patterns['rainbow'] = [wheel(255 * i / np) for i in range(0, np)]
self.patterns['cycle'] = rainbowCycle
self.patterns['timebase'] = timebased
return
def join_room(self, room_id):
room = self.client.join_room(room_id)
self.rooms[room_id] = room
self.room_ids.append = room_id
return
def listen_and_glow(self):
self.idle_since = datetime.datetime.now()
def glow_from_message_maybe(chunk):
def maybe_idle():
tdel = datetime.datetime.now() - self.idle_since
if tdel.seconds > 5 * 60: # idle for five minutes
self.set_pattern(self.patterns['timebase'])
if chunk[u'type'] != u'm.message':
maybe_idle()
return
if not chunk[u'room_id'] in self.room_ids:
maybe_idle()
return
if not chunk[u'content'][u'body'] in self.patterns:
maybe_idle()
return
self.set_pattern(self.patterns[chunk[u'content'][u'body']])
self.client.add_listener(glow_from_message_maybe)
self.client.listen_forever()
return
def set_pattern(self, pattern):
# XXX: Retcon this in to a single thing.
if type(pattern) == list:
for led_idx in range(0, self.strip.numPixels):
self.strip.setPixelColor(led_idx, pattern[led_idx])
elif type(pattern) == function:
for led_idx in range(0, self.strip.numPixels):
self.strip.setPixelColor(led_idx, pattern(led_idx))
self.idle_since = datetime.datetime.now()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Pipe in to and out of a Matrix room")
parser.add_argument('--config_path', "-c", default="~/.mcatrc")
parser.add_argument('--room', "-r")
args = parser.parse_args()
if args.room is None:
print("Require --room argument")
sys.exit(1)
if not os.path.exists(args.config_path):
print("Configuration file doesn't exist, please create one")
sys.exit(1)
mcat = None
with open(args.config_path) as f:
lightrix = Lightrix(yaml.safe_load(f), args.room)
lightrix.listen_and_glow()
Loading…
Cancel
Save