/ adafruit_neotrellis / multitrellis.py
multitrellis.py
1 # The MIT License (MIT) 2 # 3 # Copyright (c) 2018 Dean Miller for Adafruit Industries 4 # 5 # Permission is hereby granted, free of charge, to any person obtaining a copy 6 # of this software and associated documentation files (the "Software"), to deal 7 # in the Software without restriction, including without limitation the rights 8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 # copies of the Software, and to permit persons to whom the Software is 10 # furnished to do so, subject to the following conditions: 11 # 12 # The above copyright notice and this permission notice shall be included in 13 # all copies or substantial portions of the Software. 14 # 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 # THE SOFTWARE. 22 23 """ 24 interface for connecting together multiple NeoTrellis boards. 25 """ 26 27 # imports 28 29 __version__ = "0.0.0-auto.0" 30 __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_neotrellis.git" 31 32 from time import sleep 33 from micropython import const 34 from adafruit_seesaw.keypad import KeyEvent 35 36 _NEO_TRELLIS_NUM_KEYS = const(16) 37 38 39 def _key(xval): 40 return int(int(xval / 4) * 8 + (xval % 4)) 41 42 43 def _seesaw_key(xval): 44 return int(int(xval / 8) * 4 + (xval % 8)) 45 46 47 class MultiTrellis: 48 """Driver for multiple connected Adafruit NeoTrellis boards.""" 49 50 def __init__(self, neotrellis_array): 51 self._trelli = neotrellis_array 52 self._rows = len(neotrellis_array) 53 self._cols = len(neotrellis_array[0]) 54 55 def activate_key(self, x, y, edge, enable=True): 56 """Activate or deactivate a key on the trellis. x and y are the index 57 of the key measured from the top lefthand corner. Edge specifies what 58 edge to register an event on and can be NeoTrellis.EDGE_FALLING or 59 NeoTrellis.EDGE_RISING. enable should be set to True if the event is 60 to be enabled, or False if the event is to be disabled.""" 61 xkey = x % 4 62 ykey = int(int(y % 4) * 4 / 4) 63 self._trelli[int(y / 4)][int(x / 4)].activate_key(ykey * 4 + xkey, edge, enable) 64 65 def set_callback(self, x, y, function): 66 """Set a callback function for when an event for the key at index x, y 67 (measured from the top lefthand corner) is detected.""" 68 xkey = x % 4 69 ykey = int(int(y % 4) * 4 / 4) 70 self._trelli[int(y / 4)][int(x / 4)].callbacks[ykey * 4 + xkey] = function 71 72 def color(self, x, y, color): 73 """Set the color of the pixel at index x, y measured from the top 74 lefthand corner of the matrix""" 75 xkey = x % 4 76 ykey = int(int(y % 4) * 4 / 4) 77 self._trelli[int(y / 4)][int(x / 4)].pixels[ykey * 4 + xkey] = color 78 79 def sync(self): 80 """Read all trellis boards in the matrix and call any callbacks""" 81 for _n in range(self._rows): 82 for _m in range(self._cols): 83 84 _t = self._trelli[_n][_m] 85 available = _t.count 86 sleep(0.0005) 87 if available > 0: 88 available = available + 2 89 buf = _t.read_keypad(available) 90 for raw in buf: 91 evt = KeyEvent(_seesaw_key((raw >> 2) & 0x3F), raw & 0x3) 92 if ( 93 evt.number < _NEO_TRELLIS_NUM_KEYS 94 and _t.callbacks[evt.number] is not None 95 ): 96 y = int(evt.number / 4) + _n * 4 97 x = int(evt.number % 4) + _m * 4 98 _t.callbacks[evt.number](x, y, evt.edge)