diff --git a/ledd/daemon.py b/ledd/daemon.py index 6bafac2..35f7a53 100644 --- a/ledd/daemon.py +++ b/ledd/daemon.py @@ -26,10 +26,11 @@ import asyncio import spectra -from zeroconf import Zeroconf, ServiceInfo +from zeroconf import Zeroconf from ledd import controller, VERSION from ledd.decorators import ledd_protocol +from ledd.effectstack import EffectStack from ledd.stripe import Stripe log = logging.getLogger(__name__) @@ -43,6 +44,7 @@ class Daemon: loop = None """ :type : asyncio.BaseEventLoop """ protocol = {} + effects = [] def __init__(self): Daemon.instance = self @@ -57,8 +59,8 @@ class Daemon: log.info("No config file found!") # create zeroconf service info - self.zinfo = ServiceInfo("_ledd._tcp", "LedD Daemon", - port=self.config.get(self.daemonSection, 'port', fallback=1425)) + # self.zinfo = ServiceInfo("_ledd._tcp", "LedD Daemon", + # port=self.config.get(self.daemonSection, 'port', fallback=1425)) # SQL init self.sqldb = sqlite3.connect(self.config.get(self.databaseSection, 'name', fallback='ledd.sqlite')) @@ -75,7 +77,7 @@ class Daemon: logging.getLogger("asyncio").setLevel(logging.DEBUG) # announce server to network - self.register_zeroconf() + #self.register_zeroconf() # main loop self.loop = asyncio.get_event_loop() @@ -134,7 +136,33 @@ class Daemon: def deregister_zeroconf(self): zeroconf = Zeroconf() zeroconf.unregister_service(self.zinfo) - log.info("Unregistered ledd daemon with zeroconf") + + @ledd_protocol(protocol) + def start_effect(self, req_json): + """ + + :param req_json: dict of request json + """ + effect = EffectStack() + self.effects.append(effect) + effect.stripes.append(self.controllers[1].stripes[0]) + effect.start() + + # asyncio.ensure_future(asyncio.get_event_loop().run_in_executor(self.executor, effect.execute)) + + log.debug("recieved action: %s", req_json['action']) + + @ledd_protocol(protocol) + def start_effect(self, req_json): + """ + + :param req_json: dict of request json + """ + effect = BaseEffect(Stripe(),self.loop) + self.effects.append(effect) + asyncio.ensure_future(asyncio.get_event_loop().run_in_executor(self.executor, effect.execute)) + + log.debug("recieved action: %s", req_json['action']) @ledd_protocol(protocol) def set_color(self, req_json): diff --git a/ledd/effects/baseeffect.py b/ledd/effects/baseeffect.py index 52e96e3..f179893 100644 --- a/ledd/effects/baseeffect.py +++ b/ledd/effects/baseeffect.py @@ -13,3 +13,11 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see . + +class BaseEffect(object): + """ + This class only defines default meta-data for effects. + """ + name = "BaseEffect" + version = "0.1" + author = "LeDD-Freaks" diff --git a/ledd/effects/fadeeffect.py b/ledd/effects/fadeeffect.py new file mode 100644 index 0000000..1479b8d --- /dev/null +++ b/ledd/effects/fadeeffect.py @@ -0,0 +1,18 @@ +from ledd.effects.generatoreffect import GeneratorEffect +import spectra + + +class FadeEffect(GeneratorEffect): + author = "LeDD-Freaks" + version = "0.1" + + name = "Fade Effect" + description = "Fades through the HSV color wheel" + + def execute(self): + scale = spectra.scale([spectra.hsv(0.0, 1.0, 1.0), spectra.hsv(360, 1.0, 1.0)]).domain([0, 20000]) + + i = 0 + while True: + yield scale(i) + i = (i + 1) % 20000 diff --git a/ledd/effects/generatoreffect.py b/ledd/effects/generatoreffect.py new file mode 100644 index 0000000..cb83e3a --- /dev/null +++ b/ledd/effects/generatoreffect.py @@ -0,0 +1,30 @@ + +from ledd.effects.baseeffect import BaseEffect +from spectra import Color + + +class GeneratorEffect(BaseEffect): + """ + This is a base class for simple effects. + It should yield a new color on each execution. + """ + + def __init__(self): + """ + Do not override, use setup instead. + """ + self.generator = self.execute() + + def setup(self): + pass + + def execute_internal(self): + c = next(self.generator) + assert isinstance(c, Color) + return c + + def execute(self): + pass + + def tear_down(self): + pass diff --git a/ledd/effectstack.py b/ledd/effectstack.py new file mode 100644 index 0000000..6100135 --- /dev/null +++ b/ledd/effectstack.py @@ -0,0 +1,23 @@ +import asyncio + +from ledd.effects.fadeeffect import FadeEffect + + +class EffectStack(object): + def __init__(self): + self.stripes = [] + self.effect = FadeEffect() + # TODO + self.modifiers = [] + + def start(self): + asyncio.get_event_loop().call_soon(self.execute) + + def execute(self): + color = self.effect.execute_internal() + + for stripe in self.stripes: + stripe.set_color(color) + + # schedule next execution + asyncio.get_event_loop().call_later(0.1, self.execute)