More refactoring
parent
da4a4e802a
commit
eeb735640a
@ -0,0 +1,61 @@
|
||||
|
||||
import time
|
||||
import datetime
|
||||
import pytz
|
||||
|
||||
class Lightrix:
|
||||
|
||||
def __init__(self, strip):
|
||||
self.strip = strip
|
||||
|
||||
|
||||
def handler(self):
|
||||
pass
|
||||
|
||||
|
||||
def wheel(pos):
|
||||
"""Generate rainbow colors across 0-255 positions."""
|
||||
if pos < 85:
|
||||
return (pos * 3, 255 - pos * 3, 0)
|
||||
elif pos < 170:
|
||||
pos -= 85
|
||||
return (255 - pos * 3, 0, pos * 3)
|
||||
else:
|
||||
pos -= 170
|
||||
return (0, pos * 3, 255 - pos * 3)
|
||||
|
||||
def rainbowCycle(self, idx, msg, 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.count()):
|
||||
self.strip.setPixelRgb(i, self.wheel(((i * 256 / self.strip.count()) + j) & 255))
|
||||
self.strip.show()
|
||||
time.sleep(wait_ms/1000.0)
|
||||
return
|
||||
|
||||
def timebased(self, idx, msg):
|
||||
"""Set different colors depending on the time of the day"""
|
||||
time = datetime.datetime.now(tz=pytz.timezone('US/Pacific'))
|
||||
m = time.minute
|
||||
if time.hour < 7 and time.hour >= 0: # low red at night
|
||||
return (64, 0, 0)
|
||||
if time.hour == 7: # fade to brighter yellow over the morning
|
||||
return (64 + m, m, 0)
|
||||
if time.hour == 8: # fade to brighter yellow over the morning
|
||||
return (128 + m, 64 + m, m)
|
||||
if time.hour == 9: # fade to brighter yellow over the morning
|
||||
return (202 + m, 128 + m, 64 + m)
|
||||
if time.hour > 9 and time.hour < 18: # off during the day
|
||||
return (0, 0, 0)
|
||||
if time.hour >= 18 and time.hour < 22: # medium yellow-white in the evening
|
||||
return (255, 255, 128)
|
||||
if time.hour == 22: # slowly fade to low red over the hour
|
||||
return (255 - m*2, 255 - m*2, 128 - m)
|
||||
if time.hour == 23: # slowly fade to low red over the hour
|
||||
return (64 + 60 - m, 64 + 60 - (m * 2), 0)
|
||||
return (64, 0, 0) # failback
|
||||
|
||||
def wheelStrip(self, idx, msg):
|
||||
"""A strip of colors based on a wheel index"""
|
||||
val = int(msg.split(' ')[1])
|
||||
return self.wheel(val)
|
@ -1,22 +1,29 @@
|
||||
import lightrix.matrix_client as mc
|
||||
|
||||
import argparse
|
||||
import lightrix.patterns as lightrix
|
||||
import lightrix.mock as mock
|
||||
import click
|
||||
import os
|
||||
import sys
|
||||
import yaml
|
||||
|
||||
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:
|
||||
|
||||
@click.command()
|
||||
@click.option('--config', '-c', default="~/.mcatrc", type=click.Path(exists=True))
|
||||
@click.option('--room-id', '-r')
|
||||
def start(config_path, room_id):
|
||||
if room_id is None:
|
||||
print("Require --room argument")
|
||||
sys.exit(1)
|
||||
if not os.path.exists(args.config_path):
|
||||
if not os.path.exists(config_path):
|
||||
print("Configuration file doesn't exist, please create one")
|
||||
sys.exit(1)
|
||||
mcat = None
|
||||
with open(args.config_path) as f:
|
||||
lightrix = mc.Client(yaml.safe_load(f), args.room)
|
||||
lightrix.listen()
|
||||
led_count = 48
|
||||
lx = lightrix.Lightrix(count=led_count)
|
||||
with open(config_path) as f:
|
||||
conf = yaml.safe_load(f)
|
||||
strip = mock.MockStrip(count=led_count)
|
||||
client = mc.Client(conf['homeserver_uri'],
|
||||
conf['username'], conf['password'],
|
||||
room_id, strip=strip)
|
||||
client.add_listener(lx.handler)
|
||||
client.listen()
|
||||
|
Loading…
Reference in New Issue