|
1 |
| -# SPDX-FileCopyrightText: 2021 Dean Miller for Adafruit Industries |
2 |
| -# |
3 |
| -# SPDX-License-Identifier: MIT |
4 |
| - |
5 |
| -""" |
6 |
| -``adafruit_multitrellis`` |
7 |
| -==================================================== |
8 |
| -
|
9 |
| -A CircuitPython driver class for interfacing clusters of 4x4 NeoTrellis with |
10 |
| -elastomer buttons and NeoPixel RGB LEDs. |
11 |
| -
|
12 |
| -* Author(s): Dean Miller, JG for CedarGroveMakerStudios |
13 |
| -
|
14 |
| -Implementation Notes |
15 |
| --------------------- |
16 |
| -
|
17 |
| -**Hardware:** |
18 |
| -
|
19 |
| -* 'NeoTrellis RGB Driver PCB for 4x4 Keypad, PID: 3954 |
20 |
| - <https://www.adafruit.com/product/3954>' |
21 |
| -
|
22 |
| -**Software and Dependencies:** |
23 |
| -
|
24 |
| -* Adafruit CircuitPython firmware for the supported boards: |
25 |
| - https://github.com/adafruit/circuitpython/releases |
26 |
| -
|
27 |
| -* Adafruit Seesaw CircuitPython library |
28 |
| - https://github.com/adafruit/Adafruit_CircuitPython_seesaw/releases |
29 |
| -""" |
30 |
| - |
31 |
| -__version__ = "0.0.0-auto.0" |
32 |
| -__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_neotrellis.git" |
33 |
| - |
34 |
| - |
35 |
| -from time import sleep |
36 |
| -from micropython import const |
37 |
| -from adafruit_seesaw.keypad import KeyEvent |
38 |
| - |
39 |
| -_NEO_TRELLIS_NUM_KEYS = const(16) |
40 |
| - |
41 |
| - |
42 |
| -def _key(xval): |
43 |
| - return int(int(xval / 4) * 8 + (xval % 4)) |
44 |
| - |
45 |
| - |
46 |
| -def _seesaw_key(xval): |
47 |
| - return int(int(xval / 8) * 4 + (xval % 8)) |
48 |
| - |
49 |
| - |
50 |
| -class MultiTrellis: |
51 |
| - """Driver for multiple connected Adafruit NeoTrellis boards.""" |
52 |
| - |
53 |
| - def __init__(self, neotrellis_array): |
54 |
| - self._trelli = neotrellis_array |
55 |
| - self._rows = len(neotrellis_array) |
56 |
| - self._cols = len(neotrellis_array[0]) |
57 |
| - |
58 |
| - def activate_key(self, x, y, edge, enable=True): |
59 |
| - """Activate or deactivate a key on the trellis. x and y are the index |
60 |
| - of the key measured from the top lefthand corner. Edge specifies what |
61 |
| - edge to register an event on and can be NeoTrellis.EDGE_FALLING or |
62 |
| - NeoTrellis.EDGE_RISING. enable should be set to True if the event is |
63 |
| - to be enabled, or False if the event is to be disabled.""" |
64 |
| - xkey = x % 4 |
65 |
| - ykey = int(int(y % 4) * 4 / 4) |
66 |
| - self._trelli[int(y / 4)][int(x / 4)].activate_key(ykey * 4 + xkey, edge, enable) |
67 |
| - |
68 |
| - def set_callback(self, x, y, function): |
69 |
| - """Set a callback function for when an event for the key at index x, y |
70 |
| - (measured from the top lefthand corner) is detected.""" |
71 |
| - xkey = x % 4 |
72 |
| - ykey = int(int(y % 4) * 4 / 4) |
73 |
| - self._trelli[int(y / 4)][int(x / 4)].callbacks[ykey * 4 + xkey] = function |
74 |
| - |
75 |
| - def color(self, x, y, color): |
76 |
| - """Set the color of the pixel at index x, y measured from the top |
77 |
| - lefthand corner of the matrix""" |
78 |
| - xkey = x % 4 |
79 |
| - ykey = int(int(y % 4) * 4 / 4) |
80 |
| - self._trelli[int(y / 4)][int(x / 4)].pixels[ykey * 4 + xkey] = color |
81 |
| - |
82 |
| - def sync(self): |
83 |
| - """Read all trellis boards in the matrix and call any callbacks""" |
84 |
| - for _n in range(self._rows): |
85 |
| - for _m in range(self._cols): |
86 |
| - |
87 |
| - _t = self._trelli[_n][_m] |
88 |
| - available = _t.count |
89 |
| - sleep(0.0005) |
90 |
| - if available > 0: |
91 |
| - available = available + 2 |
92 |
| - buf = _t.read_keypad(available) |
93 |
| - for raw in buf: |
94 |
| - evt = KeyEvent(_seesaw_key((raw >> 2) & 0x3F), raw & 0x3) |
95 |
| - if ( |
96 |
| - evt.number < _NEO_TRELLIS_NUM_KEYS |
97 |
| - and _t.callbacks[evt.number] is not None |
98 |
| - ): |
99 |
| - y = int(evt.number / 4) + _n * 4 |
100 |
| - x = int(evt.number % 4) + _m * 4 |
101 |
| - _t.callbacks[evt.number](x, y, evt.edge) |
102 |
| - |
103 |
| - @property |
104 |
| - def brightness(self): |
105 |
| - """The NeoPixel brightness level of all clustered NeoTrellis boards.""" |
106 |
| - return self._brightness |
107 |
| - |
108 |
| - @brightness.setter |
109 |
| - def brightness(self, new_brightness): |
110 |
| - """Select a NeoPixel brightness level for all all clustered boards. A |
111 |
| - valid brightness value is in the range of 0.0 to 1.0.""" |
112 |
| - self._brightness = new_brightness |
113 |
| - for _r in range(self._rows): |
114 |
| - for _c in range(self._cols): |
115 |
| - self._trelli[_r][_c].brightness = self._brightness |
| 1 | +# SPDX-FileCopyrightText: 2021 Dean Miller for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +""" |
| 6 | +``adafruit_multitrellis`` |
| 7 | +==================================================== |
| 8 | +
|
| 9 | +A CircuitPython driver class for interfacing clusters of 4x4 NeoTrellis with |
| 10 | +elastomer buttons and NeoPixel RGB LEDs. |
| 11 | +
|
| 12 | +* Author(s): Dean Miller, JG for CedarGroveMakerStudios |
| 13 | +
|
| 14 | +Implementation Notes |
| 15 | +-------------------- |
| 16 | +
|
| 17 | +**Hardware:** |
| 18 | +
|
| 19 | +* 'NeoTrellis RGB Driver PCB for 4x4 Keypad, PID: 3954 |
| 20 | + <https://www.adafruit.com/product/3954>' |
| 21 | +
|
| 22 | +**Software and Dependencies:** |
| 23 | +
|
| 24 | +* Adafruit CircuitPython firmware for the supported boards: |
| 25 | + https://github.com/adafruit/circuitpython/releases |
| 26 | +
|
| 27 | +* Adafruit Seesaw CircuitPython library |
| 28 | + https://github.com/adafruit/Adafruit_CircuitPython_seesaw/releases |
| 29 | +""" |
| 30 | + |
| 31 | +__version__ = "0.0.0-auto.0" |
| 32 | +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_neotrellis.git" |
| 33 | + |
| 34 | + |
| 35 | +from time import sleep |
| 36 | +from micropython import const |
| 37 | +from adafruit_seesaw.keypad import KeyEvent |
| 38 | + |
| 39 | +_NEO_TRELLIS_NUM_KEYS = const(16) |
| 40 | + |
| 41 | + |
| 42 | +def _key(xval): |
| 43 | + return int(int(xval / 4) * 8 + (xval % 4)) |
| 44 | + |
| 45 | + |
| 46 | +def _seesaw_key(xval): |
| 47 | + return int(int(xval / 8) * 4 + (xval % 8)) |
| 48 | + |
| 49 | + |
| 50 | +class MultiTrellis: |
| 51 | + """Driver for multiple connected Adafruit NeoTrellis boards.""" |
| 52 | + |
| 53 | + def __init__(self, neotrellis_array): |
| 54 | + self._trelli = neotrellis_array |
| 55 | + self._rows = len(neotrellis_array) |
| 56 | + self._cols = len(neotrellis_array[0]) |
| 57 | + |
| 58 | + def activate_key(self, x, y, edge, enable=True): |
| 59 | + """Activate or deactivate a key on the trellis. x and y are the index |
| 60 | + of the key measured from the top lefthand corner. Edge specifies what |
| 61 | + edge to register an event on and can be NeoTrellis.EDGE_FALLING or |
| 62 | + NeoTrellis.EDGE_RISING. enable should be set to True if the event is |
| 63 | + to be enabled, or False if the event is to be disabled.""" |
| 64 | + xkey = x % 4 |
| 65 | + ykey = int(int(y % 4) * 4 / 4) |
| 66 | + self._trelli[int(y / 4)][int(x / 4)].activate_key(ykey * 4 + xkey, edge, enable) |
| 67 | + |
| 68 | + def set_callback(self, x, y, function): |
| 69 | + """Set a callback function for when an event for the key at index x, y |
| 70 | + (measured from the top lefthand corner) is detected.""" |
| 71 | + xkey = x % 4 |
| 72 | + ykey = int(int(y % 4) * 4 / 4) |
| 73 | + self._trelli[int(y / 4)][int(x / 4)].callbacks[ykey * 4 + xkey] = function |
| 74 | + |
| 75 | + def color(self, x, y, color): |
| 76 | + """Set the color of the pixel at index x, y measured from the top |
| 77 | + lefthand corner of the matrix""" |
| 78 | + xkey = x % 4 |
| 79 | + ykey = int(int(y % 4) * 4 / 4) |
| 80 | + self._trelli[int(y / 4)][int(x / 4)].pixels[ykey * 4 + xkey] = color |
| 81 | + |
| 82 | + def sync(self): |
| 83 | + """Read all trellis boards in the matrix and call any callbacks""" |
| 84 | + for _n in range(self._rows): |
| 85 | + for _m in range(self._cols): |
| 86 | + |
| 87 | + _t = self._trelli[_n][_m] |
| 88 | + available = _t.count |
| 89 | + sleep(0.0005) |
| 90 | + if available > 0: |
| 91 | + available = available + 2 |
| 92 | + buf = _t.read_keypad(available) |
| 93 | + for raw in buf: |
| 94 | + evt = KeyEvent(_seesaw_key((raw >> 2) & 0x3F), raw & 0x3) |
| 95 | + if ( |
| 96 | + evt.number < _NEO_TRELLIS_NUM_KEYS |
| 97 | + and _t.callbacks[evt.number] is not None |
| 98 | + ): |
| 99 | + y = int(evt.number / 4) + _n * 4 |
| 100 | + x = int(evt.number % 4) + _m * 4 |
| 101 | + _t.callbacks[evt.number](x, y, evt.edge) |
| 102 | + |
| 103 | + @property |
| 104 | + def brightness(self): |
| 105 | + """The NeoPixel brightness level of all clustered NeoTrellis boards.""" |
| 106 | + return self._brightness |
| 107 | + |
| 108 | + @brightness.setter |
| 109 | + def brightness(self, new_brightness): |
| 110 | + """Select a NeoPixel brightness level for all all clustered boards. A |
| 111 | + valid brightness value is in the range of 0.0 to 1.0.""" |
| 112 | + self._brightness = new_brightness |
| 113 | + for _r in range(self._rows): |
| 114 | + for _c in range(self._cols): |
| 115 | + self._trelli[_r][_c].brightness = self._brightness |
0 commit comments