Skip to content

Commit

Permalink
myoy + pad index awareness
Browse files Browse the repository at this point in the history
  • Loading branch information
georgeharker committed Oct 28, 2024
1 parent b6aa649 commit 6a03d5e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
5 changes: 3 additions & 2 deletions adafruit_neotrellis/multitrellis.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_neotrellis.git"

from typing import List, Sequence
from typing import List, Optional, Sequence

from adafruit_neotrellis.neotrellis import CallbackType, NeoTrellis
from adafruit_seesaw.neopixel import PixelType
Expand Down Expand Up @@ -69,6 +69,8 @@ def __init__(self, neotrellis_array: List[List[NeoTrellis]]):

t.x_base = x_base
t.y_base = y_base
t.pad_x = px
t.pad_y = py

self._width = col_size_sum[self._cols - 1]
self._height = row_size_sum[self._rows - 1]
Expand Down Expand Up @@ -123,7 +125,6 @@ def set_callback(self, x: int, y: int, function: CallbackType):
pad = self._key_pads[y][x]
pad.callbacks[pad.key_index(x, y)] = function


def get_callback(self, x: int, y: int) -> Optional[CallbackType]:
"""Get a callback function for when an event for the key at index x, y
(measured from the top lefthand corner) is detected."""
Expand Down
30 changes: 18 additions & 12 deletions adafruit_neotrellis/neotrellis.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_neotrellis.git"

from time import sleep
from typing import Callable, List, Optional
from typing import Callable, List, Optional, TypeAlias

from adafruit_seesaw.keypad import KeyEvent, Keypad, ResponseType
from adafruit_seesaw.neopixel import NeoPixel
Expand All @@ -63,7 +63,7 @@
_NEO_TRELLIS_NUM_KEYS = const(64)


CallbackType = Callable[[KeyEvent], None]
CallbackType: TypeAlias = Callable[[KeyEvent], None]


class NeoTrellis(Keypad):
Expand All @@ -73,15 +73,18 @@ class NeoTrellis(Keypad):
height: int
x_base: int
y_base: int
pad_x: int
pad_y: int
interrupt_enabled: bool
callbacks: List[Optional[CallbackType]]
pixels: NeoPixel

def __init__(self, i2c_bus, interrupt=False,
addr=_NEO_TRELLIS_ADDR, drdy=None,
width=_NEO_TRELLIS_NUM_COLS,
height=_NEO_TRELLIS_NUM_ROWS,
x_base = 0, y_base = 0):
def __init__(self, i2c_bus, interrupt: bool = False,
addr: int = _NEO_TRELLIS_ADDR, drdy = None,
width: int = _NEO_TRELLIS_NUM_COLS,
height: int = _NEO_TRELLIS_NUM_ROWS,
x_base: int = 0, y_base: int = 0,
pad_x: int = 0, pad_y: int = 0):
super().__init__(i2c_bus, addr, drdy)
self.width = width
self.height = height
Expand All @@ -91,18 +94,21 @@ def __init__(self, i2c_bus, interrupt=False,
self.callbacks = [None] * _NEO_TRELLIS_NUM_KEYS
self.pixels = NeoPixel(self, _NEO_TRELLIS_NEOPIX_PIN, _NEO_TRELLIS_NUM_KEYS)

def activate_key(self, key: int, edge: int, enable: bool = True):
def activate_key(self, key: int, edge: int, enable: bool = True) -> None:
"""Activate or deactivate a key on the trellis. Key is the key number from
0 to 16. Edge specifies what edge to register an event on and can be
NeoTrellis.EDGE_FALLING or NeoTrellis.EDGE_RISING. enable should be set
to True if the event is to be enabled, or False if the event is to be
disabled."""
self.set_event(key, edge, enable)

def clear(self):
def clear(self) -> None:
self.pixels.fill((0, 0, 0))

def sync(self):
def show(self) -> None:
self.pixels.show()

def sync(self) -> None:
"""read any events from the Trellis hardware and call associated
callbacks"""
available = self.count
Expand All @@ -120,9 +126,9 @@ def sync(self):
):
callback(evt)

def local_key_index(self, x, y):
def local_key_index(self, x, y) -> int:
return int(y * self.width + x)

def key_index(self, x, y):
def key_index(self, x, y) -> int:
return int((y - self.y_base) * self.width + (x - self.x_base))

0 comments on commit 6a03d5e

Please sign in to comment.