diff --git a/ledd/controller.py b/ledd/controller.py
index 8181ad4..32cdf18 100644
--- a/ledd/controller.py
+++ b/ledd/controller.py
@@ -99,7 +99,7 @@ class Controller:
self.bus.write_word_data(int(self.address, 16), LED0_ON_L + 4 * channel, 0)
def get_channel(self, channel):
- return self.bus.read_word_data(self.address, LED0_OFF_L + 4 * channel)
+ return self.bus.read_word_data(int(self.address, 16), LED0_OFF_L + 4 * channel) / 4095
def add_stripe(self, stripe):
self.stripes.append(stripe)
diff --git a/ledd/daemon.py b/ledd/daemon.py
index 5c144af..3839a51 100644
--- a/ledd/daemon.py
+++ b/ledd/daemon.py
@@ -24,8 +24,11 @@ import traceback
import time
import asyncio
+import spectra
+
from ledd import controller, VERSION
from ledd.decorators import ledd_protocol
+from ledd.stripe import Stripe
log = logging.getLogger(__name__)
@@ -66,12 +69,13 @@ class Daemon:
coro = self.loop.create_server(LedDProtocol,
self.config.get(self.daemonSection, 'host', fallback='0.0.0.0'),
self.config.get(self.daemonSection, 'port', fallback=1425))
- server = self.loop.run_until_complete(coro)
+ self.server = self.loop.run_until_complete(coro)
self.loop.run_forever()
except (KeyboardInterrupt, SystemExit):
log.info("Exiting")
self.sqldb.close()
- self.loop.run_until_complete(server.wait_closed())
+ self.server.close()
+ self.loop.run_until_complete(self.server.wait_closed())
self.loop.close()
sys.exit(0)
@@ -112,12 +116,29 @@ class Daemon:
def set_color(self, req_json):
"""
Part of the Color API. Used to set color of a stripe.
- Required JSON parameters: stripe ID: sid; HSV values: h,s,v
+ Required JSON parameters: stripe ID: sid; HSV values hsv: h,s,v, controller id: cid
:param req_json: dict of request json
"""
- # TODO: add adapter setting stripe with color here
log.debug("recieved action: %s", req_json['action'])
+ if "stripes" in req_json:
+ for stripe in req_json['stripes']:
+ def find_stripe():
+ for c in self.controllers:
+ for s in c.stripes:
+ if s.id == stripe['sid']:
+ return s
+
+ return None
+
+ found_s = find_stripe()
+
+ if found_s is None:
+ log.warning("Stripe not found: id=%s", stripe['sid'])
+ continue
+
+ found_s.set_color(spectra.hsv(stripe['hsv']['h'], stripe['hsv']['s'], stripe['hsv']['v']))
+
@ledd_protocol(protocol)
def add_controller(self, req_json):
"""
@@ -158,20 +179,47 @@ class Daemon:
:param req_json: dict of request json
"""
log.debug("recieved action: %s", req_json['action'])
- # TODO: Add get color logic
+ # TODO: add get color
@ledd_protocol(protocol)
def add_stripes(self, req_json):
"""
Part of the Color API. Used to add stripes.
- Required JSON parameters:
+ Required JSON parameters: name; rgb: bool; map: r: r-channel, g: g-channel, b: b-channel
:param req_json: dict of request json
"""
log.debug("recieved action: %s", req_json['action'])
+
+ res_stripes = []
+
if "stripes" in req_json:
for stripe in req_json['stripes']:
+ c = next((x for x in self.controllers if x.id == stripe['cid']), None)
- log.debug(len(req_json['stripes']))
+ if c is None:
+ res_stripes.append({
+ 'success': False,
+ 'message': "Controller not found",
+ 'ref': stripe['ref']
+ })
+ continue
+
+ s = Stripe(c, stripe['name'], stripe['rgb'],
+ (stripe['map']['r'], stripe['map']['g'], stripe['map']['b']))
+
+ res_stripes.append({
+ 'success': True,
+ 'sid': s.id,
+ 'ref': stripe['ref']
+ })
+
+ rjson = {
+ 'success': True,
+ 'stripes': res_stripes,
+ 'ref': req_json['ref']
+ }
+
+ return json.dumps(rjson)
@ledd_protocol(protocol)
def get_controllers(self, req_json):
@@ -234,6 +282,14 @@ class Daemon:
return json.dumps(rjson)
+ def no_action_found(self, req_json):
+ rjson = {
+ 'success': False,
+ 'message': "No action found",
+ 'ref': req_json['ref']
+ }
+ return json.dumps(rjson)
+
class LedDProtocol(asyncio.Protocol):
transport = None
@@ -243,7 +299,7 @@ class LedDProtocol(asyncio.Protocol):
self.transport = transport
def data_received(self, data):
- log.info("Received: %s\nfrom: ", data.decode(), self.transport.get_extra_info("peername"))
+ log.info("Received: %s from: %s", data.decode(), self.transport.get_extra_info("peername"))
self.select_task(data)
def select_task(self, data):
@@ -252,8 +308,8 @@ class LedDProtocol(asyncio.Protocol):
json_decoded = json.loads(data.decode())
if "action" in json_decoded and "ref" in json_decoded:
- return_data = Daemon.instance.protocol.get(json_decoded['action'], self.no_action_found)(
- json_decoded)
+ return_data = Daemon.instance.protocol.get(json_decoded['action'], Daemon.no_action_found)(
+ Daemon.instance, json_decoded)
if return_data is not None:
self.transport.write("{}\n".format(return_data).encode())
@@ -264,15 +320,6 @@ class LedDProtocol(asyncio.Protocol):
except ValueError:
log.debug("No valid JSON detected: %s", traceback.format_exc())
- @staticmethod
- def no_action_found(req_json):
- rjson = {
- 'success': False,
- 'message': "No action found",
- 'ref': req_json['ref']
- }
- return json.dumps(rjson)
-
def connection_lost(self, exc):
# The socket has been closed, stop the event loop
# Daemon.loop.stop()
diff --git a/ledd/effects/baseeffect.py b/ledd/effects/baseeffect.py
index 22cc96d..52e96e3 100644
--- a/ledd/effects/baseeffect.py
+++ b/ledd/effects/baseeffect.py
@@ -12,4 +12,4 @@
# 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 .
\ No newline at end of file
+# along with this program. If not, see .
diff --git a/ledd/stripe.py b/ledd/stripe.py
index 2d09104..2ac857c 100644
--- a/ledd/stripe.py
+++ b/ledd/stripe.py
@@ -13,8 +13,7 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
-
-from colour import Color
+from spectra import Color
class Stripe:
@@ -28,7 +27,7 @@ class Stripe:
self.rgb = bool(rgb)
self.channels = channels
self.id = sid
- self._color = Color()
+ self._color = None
self.gamma_correct = (2.8, 2.8, 2.8) # TODO: add to DB
self.read_color()
if not from_db:
@@ -41,12 +40,14 @@ class Stripe:
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])
+ self.channels + (self.controller.id, self.name, self.id))
cur.close()
self.controller.db.commit()
def read_color(self):
- self._color.rgb = [self.controller.get_channel(channel) ** (1 / 2.8) for channel in self.channels]
+ rc = tuple([float(self.controller.get_channel(channel)) for channel in self.channels])
+ c = Color("rgb", rc[0], rc[1], rc[2])
+ self._color = c.to("hsv")
@classmethod
def from_db(cls, controller, row):
@@ -55,8 +56,8 @@ class Stripe:
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)
+ for channel, gamma_correct, value in zip(self.channels, self.gamma_correct, c.clamped_rgb):
+ self.controller.set_channel(channel, value * 255)
def get_color(self):
return self._color
diff --git a/setup.py b/setup.py
index 0f5c239..e89ed7d 100644
--- a/setup.py
+++ b/setup.py
@@ -25,6 +25,6 @@ setup(name='LedD',
license='GPLv3',
packages=['ledd'],
install_requires=[
- 'nose', 'colour',
+ 'nose', 'spectra',
],
zip_safe=False)