diff --git a/LedD/controller.py b/LedD/controller.py index b425376..41492c9 100644 --- a/LedD/controller.py +++ b/LedD/controller.py @@ -21,8 +21,6 @@ ALLLED_OFF_L = 0xFC ALLLED_OFF_H = 0xFD - - class Controller: """ A controller controls a number of stripes. @@ -63,7 +61,7 @@ class Controller: return "".format(len(self.stripes), self.id) 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) def get_channel(self, channel): @@ -81,20 +79,21 @@ class Stripe: self.rgb = bool(rgb) self.channels = channels self._color = Color() - self.gamma_correct = (2.8,2.8,2.8) + self.gamma_correct = (2.8, 2.8, 2.8) self.read_color() 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 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): self._color = c 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): return self._color diff --git a/LedD/daemon.py b/LedD/daemon.py index b856408..9e1ed29 100644 --- a/LedD/daemon.py +++ b/LedD/daemon.py @@ -28,8 +28,10 @@ from . import controller class Daemon: daemonSection = 'daemon' databaseSection = 'db' + instance = None def __init__(self): + Daemon.instance = self config = configparser.ConfigParser() try: self.config = configparser.ConfigParser() @@ -47,8 +49,8 @@ class Daemon: self.sqldb.commit() - self.controller = controller.Controller.from_db(self.sqldb) - print(self.controller) + self.controllers = controller.Controller.from_db(self.sqldb) + print(self.controllers) server = self.SocketServer(self.config.get(self.daemonSection, 'host', fallback='0.0.0.0'), self.config.get(self.daemonSection, 'port', fallback=1425)) @@ -105,12 +107,22 @@ class Daemon: elif json_decoded['action'] == "get_color": # TODO: add stripe color get logic 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: print("no action found, ignoring") except (TypeError, ValueError): print("No valid JSON found!") - class SocketServer(asyncore.dispatcher): def __init__(self, host, port): asyncore.dispatcher.__init__(self)