renamed stuff

added first gamma correction, closes #1
added db upgrade logic
This commit is contained in:
Giovanni Harting
2015-10-04 01:19:19 +02:00
parent ad009bd0d5
commit 1a568b194c
6 changed files with 80 additions and 29 deletions

View File

@@ -100,10 +100,17 @@ class Controller:
def __repr__(self):
return "<Controller stripes={} cid={}>".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

View File

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

15
ledd/plugins/__init__.py Normal file
View File

@@ -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 <http://www.gnu.org/licenses/>.

View File

@@ -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
);
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,

5
ledd/sql/upgrade_1_2.sql Normal file
View File

@@ -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`);

View File

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