moved class stripe to another file

renamed decorator
This commit is contained in:
Giovanni Harting
2015-08-28 02:35:52 +02:00
parent 1aa955ce91
commit 7e421bc676
4 changed files with 77 additions and 64 deletions

View File

@@ -17,8 +17,8 @@
from json import JSONEncoder
import smbus
from colour import Color
from ledd.stripe import Stripe
PCA9685_SUBADR1 = 0x2
PCA9685_SUBADR2 = 0x3
@@ -105,53 +105,6 @@ class Controller:
self.stripes.append(stripe)
class Stripe:
"""
A stripe is the smallest controllable unit.
"""
def __init__(self, controller, name, rgb, channels, sid=-1, from_db=False):
self.controller = controller
self.name = name
self.rgb = bool(rgb)
self.channels = channels
self.id = sid
self._color = Color()
self.gamma_correct = (2.8, 2.8, 2.8) # TODO: add to DB
self.read_color()
if not from_db:
self.save_to_db()
def save_to_db(self):
cur = self.controller.db.cursor()
if self.id == -1:
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])
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]
@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)
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)
def get_color(self):
return self._color
color = property(get_color, set_color)
class ControllerEncoder(JSONEncoder):
def default(self, o):
if isinstance(o, Controller):