From 1a568b194ce20a2357f2bbd68316da71bd0676dd Mon Sep 17 00:00:00 2001 From: Giovanni Harting Date: Sun, 4 Oct 2015 01:19:19 +0200 Subject: [PATCH] renamed stuff added first gamma correction, closes #1 added db upgrade logic --- ledd/controller.py | 11 ++++++++-- ledd/daemon.py | 46 +++++++++++++++++++++++++--------------- ledd/plugins/__init__.py | 15 +++++++++++++ ledd/sql/ledd.sql | 11 ++++++---- ledd/sql/upgrade_1_2.sql | 5 +++++ ledd/stripe.py | 21 ++++++++++++------ 6 files changed, 80 insertions(+), 29 deletions(-) create mode 100644 ledd/plugins/__init__.py create mode 100644 ledd/sql/upgrade_1_2.sql diff --git a/ledd/controller.py b/ledd/controller.py index ab5cacf..2c5f978 100644 --- a/ledd/controller.py +++ b/ledd/controller.py @@ -100,10 +100,17 @@ class Controller: def __repr__(self): 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, int(val * 4095)) + def set_channel(self, channel, val, gamma): + self.bus.write_word_data(self._address, LED0_OFF_L + 4 * channel, self.gamma_correct(gamma, int(val * 4095), + 4095)) self.bus.write_word_data(self._address, LED0_ON_L + 4 * channel, 0) + @staticmethod + def gamma_correct(gamma, val, maxval): + corrected = int(pow(float(val) / float(maxval), float(gamma)) * float(maxval) + 0.5) + logging.getLogger(__name__).debug("GammaCorrect: in=%s out=%s, gamma=%s", val, corrected, gamma) + return corrected + def get_channel(self, channel): return self.bus.read_word_data(self._address, LED0_OFF_L + 4 * channel) / 4095 diff --git a/ledd/daemon.py b/ledd/daemon.py index 38e1460..a1713a3 100644 --- a/ledd/daemon.py +++ b/ledd/daemon.py @@ -72,11 +72,14 @@ class Daemon: logging.getLogger("asyncio").setLevel(log.getEffectiveLevel()) # sigterm handler - def sigterm_handler(_signo, _stack_frame): + def sigterm_handler(): sys.exit(0) signal.signal(signal.SIGTERM, sigterm_handler) + # init plugins + # TODO: check all plugins for existing hooks + # main loop self.loop = asyncio.get_event_loop() coro = self.loop.create_server(LedDProtocol, @@ -110,10 +113,19 @@ class Daemon: if db_version is not None: log.info("DB connection established; db-version=%s", db_version[0]) + + if int(db_version[0]) < 2: + with open("ledd/sql/upgrade_1_2.sql", "r") as ufile: + u = self.sqldb.cursor() + u.executescript(ufile.read()) + u.close() + log.info("Database upgraded to version %s", 2) + return True else: return False - except sqlite3.OperationalError: + except sqlite3.OperationalError as e: + log.debug("SQLite error: %s", e) c.close() return False @@ -214,20 +226,6 @@ class Daemon: else: found_s.set_color(spectra.hsv(req_json['hsv']['h'], req_json['hsv']['s'], req_json['hsv']['v'])) - def find_stripe(self, sid): - """ - Finds a given stripeid in the currently known controllers - :param sid stripe id - :return: stripe if found or none - :rtype: ledd.Stripe | None - """ - for c in self.controllers: - for s in c.stripes: - if s.id == sid: - return s - - return None - @ledd_protocol(protocol) def add_controller(self, req_json): """ @@ -353,7 +351,7 @@ class Daemon: """ :type : ledd.controller.Controller """ if result is not None: - result.set_channel(req_json['channel'], req_json['value']) + result.set_channel(req_json['channel'], req_json['value'], 2.8) rjson = { 'success': True, @@ -387,6 +385,20 @@ class Daemon: } return json.dumps(rjson) + def find_stripe(self, sid): + """ + Finds a given stripeid in the currently known controllers + :param sid stripe id + :return: stripe if found or none + :rtype: ledd.Stripe | None + """ + for c in self.controllers: + for s in c.stripes: + if s.id == sid: + return s + + return None + class LedDProtocol(asyncio.Protocol): transport = None diff --git a/ledd/plugins/__init__.py b/ledd/plugins/__init__.py new file mode 100644 index 0000000..52e96e3 --- /dev/null +++ b/ledd/plugins/__init__.py @@ -0,0 +1,15 @@ +# LEDD Project +# Copyright (C) 2015 LEDD Team +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . diff --git a/ledd/sql/ledd.sql b/ledd/sql/ledd.sql index 0206a16..ddc56e1 100644 --- a/ledd/sql/ledd.sql +++ b/ledd/sql/ledd.sql @@ -5,13 +5,16 @@ CREATE TABLE `stripes` ( `controller_id` INTEGER, `channel_r` INTEGER, `channel_g` INTEGER, - `channel_b` INTEGER + `channel_b` INTEGER, + `channel_r_gamma` REAL DEFAULT 2.8, + `channel_g_gamma` REAL DEFAULT 2.8, + `channel_b_gamma` REAL DEFAULT 2.8 ); CREATE TABLE "meta" ( - `option` TEXT, - `value` TEXT + `option` TEXT, + `value` TEXT ); -INSERT INTO `meta` VALUES ('db_version','1'); +INSERT INTO `meta` VALUES ('db_version','2'); CREATE TABLE "controller" ( `id` INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, `address` TEXT, diff --git a/ledd/sql/upgrade_1_2.sql b/ledd/sql/upgrade_1_2.sql new file mode 100644 index 0000000..a8845db --- /dev/null +++ b/ledd/sql/upgrade_1_2.sql @@ -0,0 +1,5 @@ +ALTER TABLE stripes ADD COLUMN channel_r_gamma REAL DEFAULT 2.8; +ALTER TABLE stripes ADD COLUMN channel_g_gamma REAL DEFAULT 2.8; +ALTER TABLE stripes ADD COLUMN channel_b_gamma REAL DEFAULT 2.8; + +REPLACE INTO meta (`option`, `value`) VALUES (`db_version`, `2`); \ No newline at end of file diff --git a/ledd/stripe.py b/ledd/stripe.py index c07de75..b5758c0 100644 --- a/ledd/stripe.py +++ b/ledd/stripe.py @@ -21,14 +21,14 @@ class Stripe: A stripe is the smallest controllable unit. """ - def __init__(self, controller, name, rgb, channels, sid=-1, from_db=False): + def __init__(self, controller, name, rgb, channels, sid=-1, gamma_correct=(2.8, 2.8, 2.8), from_db=False): self.controller = controller self.name = name self.rgb = bool(rgb) self.channels = channels self.id = sid self._color = None - self.gamma_correct = (2.8, 2.8, 2.8) # TODO: add to DB + self.gamma_correct = gamma_correct self.read_color() if not from_db: self.save_to_db() @@ -39,8 +39,16 @@ class Stripe: cur.execute("INSERT INTO stripes DEFAULT VALUES") self.id = cur.lastrowid cur.execute( - "UPDATE stripes SET channel_r = ?, channel_g = ?, channel_b = ?,controller_id = ?, name = ? WHERE id = ?", - self.channels + (self.controller.id, self.name, self.id)) + "UPDATE stripes SET " + "channel_r_gamma = ?," + "channel_g_gamma = ?," + "channel_b_gamma = ?," + "channel_r = ?," + "channel_g = ?," + "channel_b = ?," + "controller_id = ?," + "name = ? WHERE id = ?", + self.gamma_correct + self.channels + (self.controller.id, self.name, self.id)) cur.close() self.controller.db.commit() @@ -55,12 +63,13 @@ class Stripe: @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"]), sid=row["id"], from_db=True) + channels=(row["channel_r"], row["channel_g"], row["channel_b"]), sid=row["id"], + gamma_correct=(row["channel_r_gamma"], row["channel_g_gamma"], row["channel_b_gamma"]), from_db=True) def set_color(self, c): self._color = c for channel, gamma_correct, value in zip(self.channels, self.gamma_correct, c.clamped_rgb): - self.controller.set_channel(channel, value) + self.controller.set_channel(channel, value, gamma_correct) def get_color(self): return self._color