/ adafruit_neotrellis / neotrellis.py
neotrellis.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  ``adafruit_neotrellis``
 24  ====================================================
 25  
 26  4x4 elastomer buttons and RGB LEDs
 27  
 28  * Author(s): Dean Miller
 29  
 30  Implementation Notes
 31  --------------------
 32  
 33  **Hardware:**
 34  
 35  **Software and Dependencies:**
 36  
 37  * Adafruit CircuitPython firmware for the supported boards:
 38    https://github.com/adafruit/circuitpython/releases
 39  
 40  * Adafruit Seesaw CircuitPython library
 41    https://github.com/adafruit/Adafruit_CircuitPython_seesaw/releases
 42  """
 43  
 44  # imports
 45  
 46  __version__ = "0.0.0-auto.0"
 47  __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_neotrellis.git"
 48  
 49  from time import sleep
 50  from micropython import const
 51  from adafruit_seesaw.keypad import Keypad, KeyEvent
 52  from adafruit_seesaw.neopixel import NeoPixel
 53  
 54  _NEO_TRELLIS_ADDR = const(0x2E)
 55  
 56  _NEO_TRELLIS_NEOPIX_PIN = const(3)
 57  
 58  _NEO_TRELLIS_NUM_ROWS = const(4)
 59  _NEO_TRELLIS_NUM_COLS = const(4)
 60  _NEO_TRELLIS_NUM_KEYS = const(16)
 61  
 62  _NEO_TRELLIS_MAX_CALLBACKS = const(32)
 63  
 64  
 65  def _key(xval):
 66      return int(int(xval / 4) * 8 + (xval % 4))
 67  
 68  
 69  def _seesaw_key(xval):
 70      return int(int(xval / 8) * 4 + (xval % 8))
 71  
 72  
 73  class NeoTrellis(Keypad):
 74      """Driver for the Adafruit NeoTrellis."""
 75  
 76      def __init__(self, i2c_bus, interrupt=False, addr=_NEO_TRELLIS_ADDR, drdy=None):
 77          super().__init__(i2c_bus, addr, drdy)
 78          self.interrupt_enabled = interrupt
 79          self.callbacks = [None] * _NEO_TRELLIS_NUM_KEYS
 80          self.pixels = NeoPixel(self, _NEO_TRELLIS_NEOPIX_PIN, _NEO_TRELLIS_NUM_KEYS)
 81  
 82      def activate_key(self, key, edge, enable=True):
 83          """Activate or deactivate a key on the trellis. Key is the key number from
 84             0 to 16. Edge specifies what edge to register an event on and can be
 85             NeoTrellis.EDGE_FALLING or NeoTrellis.EDGE_RISING. enable should be set
 86             to True if the event is to be enabled, or False if the event is to be
 87             disabled."""
 88          self.set_event(_key(key), edge, enable)
 89  
 90      def sync(self):
 91          """read any events from the Trellis hardware and call associated
 92             callbacks"""
 93          available = self.count
 94          sleep(0.0005)
 95          if available > 0:
 96              available = available + 2
 97              buf = self.read_keypad(available)
 98              for raw in buf:
 99                  evt = KeyEvent(_seesaw_key((raw >> 2) & 0x3F), raw & 0x3)
100                  if (
101                      evt.number < _NEO_TRELLIS_NUM_KEYS
102                      and self.callbacks[evt.number] is not None
103                  ):
104                      self.callbacks[evt.number](evt)