master
Ryan Rix 8 years ago
parent eeb735640a
commit 9bd04c5563

3
.gitignore vendored

@ -0,0 +1,3 @@
*.pyc
env
secrets.yaml

@ -20,7 +20,6 @@ class Client:
self.join_room(room_id)
self.rooms[room_id].send_text("Started up, set to timebase.")
self.refresh_states()
self.set_from_state()
return
@ -36,8 +35,8 @@ class Client:
def refresh_states(self):
for room in self.rooms:
self.state[room.id] = self.client.api.get_room_state(room.id)
for room_id, room in self.rooms.iteritems():
self.state[room_id] = self.client.api.get_room_state(room_id)
def listen(self):
while(True):

@ -13,15 +13,14 @@ class MockStrip(object):
self.led_dma = dma
self.led_brightness = brightness
self.led_invert = invert
self.strip = list()
for led in range(self.led_count):
self.strip.append(dict())
def setPixel(self, ix, hue, saturation, lightness):
def setPixelHsl(self, ix, hue, saturation, lightness):
rgb = colorsys.hls_to_rgb(hue, lightness, saturation)
self.strip[ix]['r'] = rgb[0]
self.strip[ix]['g'] = rgb[1]
self.strip[ix]['b'] = rgb[2]
self.setPixelRgb(ix, rgb[0], rgb[1], rgb[2])
def setPixelRgb(self, ix, r,g,b):
self.strip[ix]['r'] = r
@ -38,4 +37,5 @@ class MockStrip(object):
return self.led_count
def show(self):
pass
print self.strip

@ -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):

@ -1,4 +1,4 @@
import lightrix.matrix_client as mc
import lightrix.matrix as mc
import lightrix.patterns as lightrix
import lightrix.mock as mock
import click
@ -10,20 +10,25 @@ import yaml
@click.command()
@click.option('--config', '-c', default="~/.mcatrc", type=click.Path(exists=True))
@click.option('--room-id', '-r')
def start(config_path, room_id):
def start(config, room_id):
if room_id is None:
print("Require --room argument")
sys.exit(1)
if not os.path.exists(config_path):
if not os.path.exists(config):
print("Configuration file doesn't exist, please create one")
sys.exit(1)
led_count = 48
lx = lightrix.Lightrix(count=led_count)
with open(config_path) as f:
with open(config) as f:
conf = yaml.safe_load(f)
strip = mock.MockStrip(count=led_count)
lx = lightrix.Lightrix(strip)
client = mc.Client(conf['homeserver_uri'],
conf['username'], conf['password'],
room_id, strip=strip)
lx.matrix = client
client.add_listener(lx.handler)
client.listen()
if __name__ == '__main__':
start()

Loading…
Cancel
Save