From d41a60cf460960fdaa201cb133bd0221d443b0a9 Mon Sep 17 00:00:00 2001 From: Bob Clough Date: Mon, 27 May 2024 17:25:49 +0100 Subject: [PATCH] add events to enable / disable the pattern generator --- modules/system/patterndisplay/app.py | 26 +++++++++++++++++++------ modules/system/patterndisplay/events.py | 7 +++++++ 2 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 modules/system/patterndisplay/events.py diff --git a/modules/system/patterndisplay/app.py b/modules/system/patterndisplay/app.py index e92e066..4a1ebc0 100644 --- a/modules/system/patterndisplay/app.py +++ b/modules/system/patterndisplay/app.py @@ -2,10 +2,14 @@ from tildagonos import tildagonos import settings import asyncio +from system.patterndisplay.events import * +from system.eventbus import eventbus class PatternDisplay(App): def __init__(self): + eventbus.on_async(PatternEnable, self._enable, self) + eventbus.on_async(PatternDisable, self._disable, self) self.pattern = settings.get("pattern", "rainbow") try: _patternpath = "patterns." + self.pattern @@ -13,19 +17,29 @@ def __init__(self): _pmodule = __import__(_patternpath, globals(), locals(), [_patternclass]) _pclass = getattr(_pmodule, _patternclass) self._p = _pclass() + self.enabled = True except: raise ImportError(f"Pattern {self.pattern} not found!") + async def _enable(self, event: PatternEnable): + self.enabled = True + + async def _disable(self, event: PatternDisable): + self.enabled = False + async def background_task(self): while True: brightness = settings.get("pattern_brightness", 1.0) next_frame = self._p.next() - for l in range(12): - if brightness < 1.0: - tildagonos.leds[l + 1] = tuple(int(i*brightness) for i in next_frame[l]) - else: - tildagonos.leds[l + 1] = next_frame[l] - tildagonos.leds.write() + if self.enabled: + for l in range(12): + if brightness < 1.0: + tildagonos.leds[l + 1] = tuple( + int(i * brightness) for i in next_frame[l] + ) + else: + tildagonos.leds[l + 1] = next_frame[l] + tildagonos.leds.write() if not self._p.fps: break await asyncio.sleep(1 / self._p.fps) diff --git a/modules/system/patterndisplay/events.py b/modules/system/patterndisplay/events.py new file mode 100644 index 0000000..3f9fd7d --- /dev/null +++ b/modules/system/patterndisplay/events.py @@ -0,0 +1,7 @@ +from events import Event + + +class PatternEnable(Event): ... + + +class PatternDisable(Event): ...