Skip to content

Commit

Permalink
Merge pull request #50 from emfcamp/thinkl33t/disable_patterns
Browse files Browse the repository at this point in the history
Add events to enable / disable the pattern generator
  • Loading branch information
MatthewWilkes authored May 28, 2024
2 parents 7a42b88 + 2865183 commit 0572625
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 6 deletions.
44 changes: 44 additions & 0 deletions modules/firmware_apps/patterninhibit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import asyncio
import app

from events.input import Buttons, BUTTON_TYPES
from system.eventbus import eventbus
from system.patterndisplay.events import *

from tildagonos import tildagonos


class PatternInhibit(app.App):
def __init__(self):
self.button_states = Buttons(self)
eventbus.emit(PatternDisable())
self._make_red()
self._inhibiting = True

def _make_red(self):
asyncio.sleep(0.5)
for i in range(1, 13):
tildagonos.leds[i] = (int(i * (255/12)), 0, 0)
tildagonos.leds.write()

def update(self, delta):
if self.button_states.get(BUTTON_TYPES["CANCEL"]):
eventbus.emit(PatternDisable())
self._inhibiting = True
self._make_red()
elif self.button_states.get(BUTTON_TYPES["CONFIRM"]):
eventbus.emit(PatternEnable())
self._inhibiting = False

def draw(self, ctx):
ctx.save()
ctx.font_size = 20
ctx.text_align = ctx.CENTER
ctx.text_baseline = ctx.MIDDLE
if self._inhibiting:
ctx.rgb(0.2, 0, 0).rectangle(-120, -120, 240, 240).fill()
ctx.rgb(1, 0, 0).move_to(0, 0).text("Inhibiting LEDs")
else:
ctx.rgb(0, 0.2, 0).rectangle(-120, -120, 240, 240).fill()
ctx.rgb(0, 1, 0).move_to(0, 0).text("Not Inhibiting LEDs")
ctx.restore()
1 change: 1 addition & 0 deletions modules/system/launcher/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def list_core_apps(self):
("Menu demo", "firmware_apps.menu_demo", "MenuDemo"),
("Kbd demo", "firmware_apps.text_demo", "TextDemo"),
# ("Update Firmware", "otaupdate", "OtaUpdate"),
# ("Inhibit LEDs", "firmware_apps.patterninhibit", "PatternInhibit"),
# ("Wi-Fi Connect", "wifi_client", "WifiClient"),
# ("Sponsors", "sponsors", "Sponsors"),
# ("Battery", "battery", "Battery"),
Expand Down
26 changes: 20 additions & 6 deletions modules/system/patterndisplay/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,44 @@
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
_patternclass = self.pattern[0].upper() + self.pattern[1:] + "Pattern"
_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)
Expand Down
7 changes: 7 additions & 0 deletions modules/system/patterndisplay/events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from events import Event


class PatternEnable(Event): ...


class PatternDisable(Event): ...

0 comments on commit 0572625

Please sign in to comment.