Skip to content

Commit

Permalink
Presto: consolidate features into a helper library.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed Nov 29, 2024
1 parent e3322b7 commit 27b2fe6
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
2 changes: 1 addition & 1 deletion modules/c/presto/presto.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ MP_DEFINE_CONST_OBJ_TYPE(

/***** Globals Table *****/
static const mp_map_elem_t presto_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_presto) },
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR__presto) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_Presto), (mp_obj_t)&Presto_type },
};

Expand Down
6 changes: 6 additions & 0 deletions modules/py_frozen/backlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ def zone(index, x, y):
self.bl = plasma.WS2812(7, 0, 0, 33)
self.bl.start()

def clear(self):
self.bl.clear()

def set_rgb(self, i, r, g, b):
self.bl.set_rgb(i, r, g, b)

@micropython.native
def update(self):
mv = self.mv
Expand Down
6 changes: 5 additions & 1 deletion modules/py_frozen/ezwifi.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,17 @@ def _callback(self, handler_name, *args, **kwargs):
def _log(self, text, level=LogLevel.INFO):
self._callback(LogLevel.text[level], text) or (self._verbose and print(text))

def on(self, handler_name):
def on(self, handler_name, handler=None):
if handler_name not in self._events.keys():
raise ValueError(f"Invalid event: \"{handler_name}\"")

def _on(handler):
self._events[handler_name] = handler

if handler is not None:
_on(handler)
return True

return _on

def error(self):
Expand Down
64 changes: 64 additions & 0 deletions modules/py_frozen/presto.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import asyncio
import _presto
from touch import FT6236
from backlight import Reactive
from ezwifi import EzWiFi

from collections import namedtuple
from picographics import PicoGraphics, DISPLAY_PRESTO, DISPLAY_PRESTO_FULL_RES


Touch = namedtuple("touch", ("x", "y", "touched"))


class Presto():
def __init__(self, full_res=False, reactive_backlight=False, direct_to_fb=False):
# WiFi - *must* happen before Presto bringup
# Note: Forces WiFi details to be in secrets.py
self.wifi = EzWiFi()

# Touch Input
self.touch = FT6236(full_res=full_res)

# Display Driver & PicoGraphics
self.presto = _presto.Presto(full_res=full_res)
self.buffer = None if (full_res and not direct_to_fb) else memoryview(self.presto)
self.display = PicoGraphics(DISPLAY_PRESTO_FULL_RES if full_res else DISPLAY_PRESTO, buffer=self.buffer)
self.width, self.height = self.display.get_bounds()

# Reactive Backlight
self.backlight = Reactive(memoryview(self.presto), self.width, self.height) if reactive_backlight else None

@property
def touch_a(self):
return Touch(self.touch.x, self.touch.y, self.touch.state)

@property
def touch_b(self):
return Touch(self.touch.x2, self.touch.y2, self.touch.state2)

@property
def touch_delta(self):
return self.touch.distance, self.touch.angle

async def async_connect(self):
await self.wifi.connect()

def connect(self):
return asyncio.get_event_loop().run_until_complete(self.wifi.connect())

def update(self):
self.presto.update(self.display)
try:
self.backlight.update()
except AttributeError:
pass
self.touch.poll()

def clear(self):
self.display.clear()
self.presto.update(self.display)
try:
self.backlight.bl.clear()
except AttributeError:
pass

0 comments on commit 27b2fe6

Please sign in to comment.