switched from colour to spectra

disabled gamma correction for now - needs some more work
implemented set_color and add_stripe
This commit is contained in:
Giovanni Harting
2015-08-28 06:12:20 +02:00
parent 597de55291
commit 0d8105c93b
5 changed files with 77 additions and 29 deletions

View File

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

View File

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

View File

@@ -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 <http://www.gnu.org/licenses/>.
# along with this program. If not, see <http://www.gnu.org/licenses/>.

View File

@@ -13,8 +13,7 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
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

View File

@@ -25,6 +25,6 @@ setup(name='LedD',
license='GPLv3',
packages=['ledd'],
install_requires=[
'nose', 'colour',
'nose', 'spectra',
],
zip_safe=False)