added controller adding logic

This commit is contained in:
Giovanni Harting
2015-07-14 16:35:54 +02:00
parent 36d7efc37f
commit d915757185
2 changed files with 21 additions and 10 deletions

View File

@@ -21,8 +21,6 @@ ALLLED_OFF_L = 0xFC
ALLLED_OFF_H = 0xFD ALLLED_OFF_H = 0xFD
class Controller: class Controller:
""" """
A controller controls a number of stripes. A controller controls a number of stripes.
@@ -63,7 +61,7 @@ class Controller:
return "<Controller stripes={} cid={}>".format(len(self.stripes), self.id) return "<Controller stripes={} cid={}>".format(len(self.stripes), self.id)
def set_channel(self, channel, val): def set_channel(self, channel, val):
self.bus.write_word_data(self.address, LED0_OFF_L + 4 * channel, val*4095) self.bus.write_word_data(self.address, LED0_OFF_L + 4 * channel, val * 4095)
self.bus.write_word_data(self.address, LED0_ON_L + 4 * channel, 0) self.bus.write_word_data(self.address, LED0_ON_L + 4 * channel, 0)
def get_channel(self, channel): def get_channel(self, channel):
@@ -81,20 +79,21 @@ class Stripe:
self.rgb = bool(rgb) self.rgb = bool(rgb)
self.channels = channels self.channels = channels
self._color = Color() self._color = Color()
self.gamma_correct = (2.8,2.8,2.8) self.gamma_correct = (2.8, 2.8, 2.8)
self.read_color() self.read_color()
def read_color(self): def read_color(self):
self._color.rgb = [self.controller.get_channel(channel)**(1/2.8) for channel in self.channels] self._color.rgb = [self.controller.get_channel(channel) ** (1 / 2.8) for channel in self.channels]
@classmethod @classmethod
def from_db(cls, controller, row): def from_db(cls, controller, row):
return cls(controller, name=row["name"], rgb=row["rgb"], channels=(row["channel_r"],row["channel_g"],row["channel_b"])) return cls(controller, name=row["name"], rgb=row["rgb"],
channels=(row["channel_r"], row["channel_g"], row["channel_b"]))
def set_color(self, c): def set_color(self, c):
self._color = c self._color = c
for channel, gamma_correct, value in zip(self.channels, self.gamma_correct, c.rgb): for channel, gamma_correct, value in zip(self.channels, self.gamma_correct, c.rgb):
self.controller.set_channel(channel,value**gamma_correct) self.controller.set_channel(channel, value ** gamma_correct)
def get_color(self): def get_color(self):
return self._color return self._color

View File

@@ -28,8 +28,10 @@ from . import controller
class Daemon: class Daemon:
daemonSection = 'daemon' daemonSection = 'daemon'
databaseSection = 'db' databaseSection = 'db'
instance = None
def __init__(self): def __init__(self):
Daemon.instance = self
config = configparser.ConfigParser() config = configparser.ConfigParser()
try: try:
self.config = configparser.ConfigParser() self.config = configparser.ConfigParser()
@@ -47,8 +49,8 @@ class Daemon:
self.sqldb.commit() self.sqldb.commit()
self.controller = controller.Controller.from_db(self.sqldb) self.controllers = controller.Controller.from_db(self.sqldb)
print(self.controller) print(self.controllers)
server = self.SocketServer(self.config.get(self.daemonSection, 'host', fallback='0.0.0.0'), server = self.SocketServer(self.config.get(self.daemonSection, 'host', fallback='0.0.0.0'),
self.config.get(self.daemonSection, 'port', fallback=1425)) self.config.get(self.daemonSection, 'port', fallback=1425))
@@ -105,12 +107,22 @@ class Daemon:
elif json_decoded['action'] == "get_color": elif json_decoded['action'] == "get_color":
# TODO: add stripe color get logic # TODO: add stripe color get logic
print("recieved action: {}".format(json_decoded['action'])) print("recieved action: {}".format(json_decoded['action']))
elif json_decoded['action'] == "add_stripes":
if "stripes" in json_decoded:
for stripe in json_decoded['stripes']:
# TODO: add stripe here
print(len(json_decoded['stripes']))
elif json_decoded['action'] == "add_controller":
ncontroller = controller.Controller(self.daemon.sqldb, 0, json_decoded['channels'],
json_decoded['i2c_dev'], json_decoded['address'])
self.send(ncontroller.id)
Daemon.instance.controllers.append(ncontroller)
else: else:
print("no action found, ignoring") print("no action found, ignoring")
except (TypeError, ValueError): except (TypeError, ValueError):
print("No valid JSON found!") print("No valid JSON found!")
class SocketServer(asyncore.dispatcher): class SocketServer(asyncore.dispatcher):
def __init__(self, host, port): def __init__(self, host, port):
asyncore.dispatcher.__init__(self) asyncore.dispatcher.__init__(self)