|
1 |
| -# SPDX-FileCopyrightText: 2018 Dean Miller for Adafruit Industries |
2 |
| -# |
3 |
| -# SPDX-License-Identifier: MIT |
4 |
| - |
5 |
| -""" |
6 |
| -Interface for connecting together multiple NeoTrellis boards. |
7 |
| -""" |
8 |
| - |
9 |
| -# imports |
10 |
| - |
11 |
| -__version__ = "0.0.0-auto.0" |
12 |
| -__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_neotrellis.git" |
13 |
| - |
14 |
| -from time import sleep |
15 |
| -from micropython import const |
16 |
| -from adafruit_seesaw.keypad import KeyEvent |
17 |
| - |
18 |
| -_NEO_TRELLIS_NUM_KEYS = const(16) |
19 |
| - |
20 |
| - |
21 |
| -def _key(xval): |
22 |
| - return int(int(xval / 4) * 8 + (xval % 4)) |
23 |
| - |
24 |
| - |
25 |
| -def _seesaw_key(xval): |
26 |
| - return int(int(xval / 8) * 4 + (xval % 8)) |
27 |
| - |
28 |
| - |
29 |
| -class MultiTrellis: |
30 |
| - """Driver for multiple connected Adafruit NeoTrellis boards.""" |
31 |
| - |
32 |
| - def __init__(self, neotrellis_array): |
33 |
| - self._trelli = neotrellis_array |
34 |
| - self._rows = len(neotrellis_array) |
35 |
| - self._cols = len(neotrellis_array[0]) |
36 |
| - |
37 |
| - def activate_key(self, x, y, edge, enable=True): |
38 |
| - """Activate or deactivate a key on the trellis. x and y are the index |
39 |
| - of the key measured from the top lefthand corner. Edge specifies what |
40 |
| - edge to register an event on and can be NeoTrellis.EDGE_FALLING or |
41 |
| - NeoTrellis.EDGE_RISING. enable should be set to True if the event is |
42 |
| - to be enabled, or False if the event is to be disabled.""" |
43 |
| - xkey = x % 4 |
44 |
| - ykey = int(int(y % 4) * 4 / 4) |
45 |
| - self._trelli[int(y / 4)][int(x / 4)].activate_key(ykey * 4 + xkey, edge, enable) |
46 |
| - |
47 |
| - def set_callback(self, x, y, function): |
48 |
| - """Set a callback function for when an event for the key at index x, y |
49 |
| - (measured from the top lefthand corner) is detected.""" |
50 |
| - xkey = x % 4 |
51 |
| - ykey = int(int(y % 4) * 4 / 4) |
52 |
| - self._trelli[int(y / 4)][int(x / 4)].callbacks[ykey * 4 + xkey] = function |
53 |
| - |
54 |
| - def color(self, x, y, color): |
55 |
| - """Set the color of the pixel at index x, y measured from the top |
56 |
| - lefthand corner of the matrix""" |
57 |
| - xkey = x % 4 |
58 |
| - ykey = int(int(y % 4) * 4 / 4) |
59 |
| - self._trelli[int(y / 4)][int(x / 4)].pixels[ykey * 4 + xkey] = color |
60 |
| - |
61 |
| - def sync(self): |
62 |
| - """Read all trellis boards in the matrix and call any callbacks""" |
63 |
| - for _n in range(self._rows): |
64 |
| - for _m in range(self._cols): |
65 |
| - |
66 |
| - _t = self._trelli[_n][_m] |
67 |
| - available = _t.count |
68 |
| - sleep(0.0005) |
69 |
| - if available > 0: |
70 |
| - available = available + 2 |
71 |
| - buf = _t.read_keypad(available) |
72 |
| - for raw in buf: |
73 |
| - evt = KeyEvent(_seesaw_key((raw >> 2) & 0x3F), raw & 0x3) |
74 |
| - if ( |
75 |
| - evt.number < _NEO_TRELLIS_NUM_KEYS |
76 |
| - and _t.callbacks[evt.number] is not None |
77 |
| - ): |
78 |
| - y = int(evt.number / 4) + _n * 4 |
79 |
| - x = int(evt.number % 4) + _m * 4 |
80 |
| - _t.callbacks[evt.number](x, y, evt.edge) |
| 1 | +# SPDX-FileCopyrightText: 2018 Dean Miller for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +""" |
| 6 | +Interface for connecting together multiple NeoTrellis boards. |
| 7 | +""" |
| 8 | + |
| 9 | +# imports |
| 10 | + |
| 11 | +__version__ = "1.1.8" |
| 12 | +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_neotrellis.git" |
| 13 | + |
| 14 | +from time import sleep |
| 15 | +from micropython import const |
| 16 | +from adafruit_seesaw.keypad import KeyEvent |
| 17 | + |
| 18 | +_NEO_TRELLIS_NUM_KEYS = const(16) |
| 19 | + |
| 20 | + |
| 21 | +def _key(xval): |
| 22 | + return int(int(xval / 4) * 8 + (xval % 4)) |
| 23 | + |
| 24 | + |
| 25 | +def _seesaw_key(xval): |
| 26 | + return int(int(xval / 8) * 4 + (xval % 8)) |
| 27 | + |
| 28 | + |
| 29 | +class MultiTrellis: |
| 30 | + """Driver for multiple connected Adafruit NeoTrellis boards.""" |
| 31 | + |
| 32 | + def __init__(self, neotrellis_array): |
| 33 | + self._trelli = neotrellis_array |
| 34 | + self._rows = len(neotrellis_array) |
| 35 | + self._cols = len(neotrellis_array[0]) |
| 36 | + |
| 37 | + def activate_key(self, x, y, edge, enable=True): |
| 38 | + """Activate or deactivate a key on the trellis. x and y are the index |
| 39 | + of the key measured from the top lefthand corner. Edge specifies what |
| 40 | + edge to register an event on and can be NeoTrellis.EDGE_FALLING or |
| 41 | + NeoTrellis.EDGE_RISING. enable should be set to True if the event is |
| 42 | + to be enabled, or False if the event is to be disabled.""" |
| 43 | + xkey = x % 4 |
| 44 | + ykey = int(int(y % 4) * 4 / 4) |
| 45 | + self._trelli[int(y / 4)][int(x / 4)].activate_key(ykey * 4 + xkey, edge, enable) |
| 46 | + |
| 47 | + def set_callback(self, x, y, function): |
| 48 | + """Set a callback function for when an event for the key at index x, y |
| 49 | + (measured from the top lefthand corner) is detected.""" |
| 50 | + xkey = x % 4 |
| 51 | + ykey = int(int(y % 4) * 4 / 4) |
| 52 | + self._trelli[int(y / 4)][int(x / 4)].callbacks[ykey * 4 + xkey] = function |
| 53 | + |
| 54 | + def color(self, x, y, color): |
| 55 | + """Set the color of the pixel at index x, y measured from the top |
| 56 | + lefthand corner of the matrix""" |
| 57 | + xkey = x % 4 |
| 58 | + ykey = int(int(y % 4) * 4 / 4) |
| 59 | + self._trelli[int(y / 4)][int(x / 4)].pixels[ykey * 4 + xkey] = color |
| 60 | + |
| 61 | + def sync(self): |
| 62 | + """Read all trellis boards in the matrix and call any callbacks""" |
| 63 | + for _n in range(self._rows): |
| 64 | + for _m in range(self._cols): |
| 65 | + |
| 66 | + _t = self._trelli[_n][_m] |
| 67 | + available = _t.count |
| 68 | + sleep(0.0005) |
| 69 | + if available > 0: |
| 70 | + available = available + 2 |
| 71 | + buf = _t.read_keypad(available) |
| 72 | + for raw in buf: |
| 73 | + evt = KeyEvent(_seesaw_key((raw >> 2) & 0x3F), raw & 0x3) |
| 74 | + if ( |
| 75 | + evt.number < _NEO_TRELLIS_NUM_KEYS |
| 76 | + and _t.callbacks[evt.number] is not None |
| 77 | + ): |
| 78 | + y = int(evt.number / 4) + _n * 4 |
| 79 | + x = int(evt.number % 4) + _m * 4 |
| 80 | + _t.callbacks[evt.number](x, y, evt.edge) |
| 81 | + |
| 82 | + @property |
| 83 | + def brightness(self): |
| 84 | + return self.brightness |
| 85 | + |
| 86 | + @brightness.setter |
| 87 | + def brightness(self, new_brightness): |
| 88 | + self._brightness = new_brightness |
| 89 | + for _r in range(self._rows): |
| 90 | + for _c in range (self._cols): |
| 91 | + self._trelli[_r][_c].brightness = self._brightness |
0 commit comments