From 4bc2b662092dd9fb7622622660034e73964d92f1 Mon Sep 17 00:00:00 2001 From: Dj Walker-Morgan Date: Sun, 21 Jan 2018 19:15:10 +0000 Subject: [PATCH 1/2] Updates for Adafruit Trellis --- README.md | 5 +- Trellis.py | 107 ++++++++++++++++++++++++++++++++ __init__.py | 3 +- examples/Trellis/buttonboard.py | 68 ++++++++++++++++++++ 4 files changed, 181 insertions(+), 2 deletions(-) create mode 100644 Trellis.py create mode 100644 examples/Trellis/buttonboard.py diff --git a/README.md b/README.md index 7d7e041..7e864f5 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,14 @@ # HT16K33 Python Library # +Note: This version has a port of the API from [tdicola's AdaFruit Trellis Python](https://github.com/tdicola/Adafruit_Trellis_Python) modified to work in the same way, via smbus, as the other HT16K33 drivers. Presented if anyone finds it useful. (codepope). + A simple python library to control products using the HT16K33 IC. + Adafruit's LED backpacks + [BiColor LED Square Pixel Matrix](http://adafruit.com/products/902) - [HT16K33.BiColor](#bicolor-square) + [8x8 LED Matrix](http://adafruit.com/products/872) - [HT16K33.EightByEight](#eightbyeight) + [4 Digit 7-Segment Display](http://adafruit.com/products/878) - [HT16K33.FourDigit](#fourdigit) + + [Adafruit Trellis Monochrome Driver PCB for 4x4 Keypad & 3mm LEDs](https://www.adafruit.com/product/1616) ## Dependencies @@ -406,4 +409,4 @@ Parent object inherited by all HT16K33 LED backpacks. Not intended for direct us -[1]:(http://dl.lm-sensors.org/i2c-tools/releases/i2c-tools-3.1.0.tar.bz2) \ No newline at end of file +[1]:(http://dl.lm-sensors.org/i2c-tools/releases/i2c-tools-3.1.0.tar.bz2) diff --git a/Trellis.py b/Trellis.py new file mode 100644 index 0000000..f4aeae3 --- /dev/null +++ b/Trellis.py @@ -0,0 +1,107 @@ +from ._HT16K33 import Device + +# This is a library for the Adafruit Trellis w/HT16K33 +# +# Designed specifically to work with the Adafruit Trellis +# ----> https://www.adafruit.com/products/1616 +# ----> https://www.adafruit.com/products/1611 +# +# These displays use I2C to communicate, 2 pins are required to +# interface +# Adafruit invests time and resources providing this open source code, +# please support Adafruit and open-source hardware by purchasing +# products from Adafruit! +# +# Written by Limor Fried/Ladyada for Adafruit Industries. +# MIT license, all text above must be included in any redistribution +# +# Python port created by Tony DiCola (tony@tonydicola.com +# +# Re-ported to the HT16K33 package by Dj Walker-Morgan (codepope@gmail.com) +# + +__all__ = ['Trellis'] + + +class Trellis(Device): + ''' + AdaFruit Trellis + - - - + ''' + + ledLUT = [ 0x3A, 0x37, 0x35, 0x34, + 0x28, 0x29, 0x23, 0x24, + 0x16, 0x1B, 0x11, 0x10, + 0x0E, 0x0D, 0x0C, 0x02 ] + buttonLUT = [ 0x07, 0x04, 0x02, 0x22, + 0x05, 0x06, 0x00, 0x01, + 0x03, 0x10, 0x30, 0x21, + 0x13, 0x12, 0x11, 0x31 ] + + def setUp(self,**kwargs): + super().setUp() + self.displaybuffer=[0]*8 + self._keys=[0]*6 + self._lastkeys=[0]*6 + self.bus.write_byte_data(self.address,0xa1,0) + return self + + def writeDisplay(self): + """Write the LED display buffer values to the hardware.""" + #self._check_i2c() + data = [] + for buf in self.displaybuffer: + data.append(buf & 0xFF) + data.append(buf >> 8) + self.bus.write_i2c_block_data(self.address, 0, data) + + def clear(self): + """Clear all the LEDs in the display buffer.""" + self.displaybuffer = [0] * 8 + + def isKeyPressed(self, k): + """Check if the specified key was pressed during the last readSwitches call.""" + if k > 16 or k < 0: return False + return (self._keys[self.buttonLUT[k] >> 4] & (1 << (self.buttonLUT[k] & 0x0F))) > 0 + + def wasKeyPressed(self, k): + """Check if the specified key was pressed before the last readSwitches call.""" + if k > 16 or k < 0: return False + return (self._lastkeys[self.buttonLUT[k] >> 4] & (1 << (self.buttonLUT[k] & 0x0F))) > 0 + + def isLED(self, x): + """Return True if the specified LED is illuminated in the display buffer.""" + if x > 16 or x < 0: return False + return (self.displaybuffer[self.ledLUT[x] >> 4] & (1 << (self.ledLUT[x] & 0x0F))) > 0 + + def setLED(self, x): + """Turn on the specified LED in the display buffer.""" + if x > 16 or x < 0: return + self.displaybuffer[self.ledLUT[x] >> 4] |= (1 << (self.ledLUT[x] & 0x0F)) + + def clrLED(self, x): + """Turn off the specified LED in the display buffer.""" + if x > 16 or x < 0: return + self.displaybuffer[self.ledLUT[x] >> 4] &= ~(1 << (self.ledLUT[x] & 0x0F)) + + def readSwitches(self): + """Read the state of the buttons from the hardware. + Returns True if a button is pressed, False otherwise. + """ + #self._check_i2c() + self._lastkeys = self._keys + self._keys = self.bus.read_i2c_block_data(self.address,0x40, 6) + return any(map(lambda key, lastkey: key != lastkey, self._keys, self._lastkeys)) + + def justPressed(self, k): + """Return True if the specified key was first pressed in the last readSwitches call.""" + return self.isKeyPressed(k) and not self.wasKeyPressed(k) + + def justReleased(self, k): + """Return True if the specified key was just released in the last readSwitches call.""" + return not self.isKeyPressed(k) and self.wasKeyPressed(k) + + +if __name__ == "__main__": + import doctest + doctest.testmod() diff --git a/__init__.py b/__init__.py index 928ce17..a725d7f 100644 --- a/__init__.py +++ b/__init__.py @@ -1,5 +1,6 @@ -__all__ = ['BiColor', 'EightByEight', 'FourDigit'] +__all__ = ['BiColor', 'EightByEight', 'FourDigit', 'Trellis'] from .BiColor import BiColor from .EightByEight import EightByEight from .FourDigit import FourDigit +from .Trellis import Trellis diff --git a/examples/Trellis/buttonboard.py b/examples/Trellis/buttonboard.py new file mode 100644 index 0000000..335f89a --- /dev/null +++ b/examples/Trellis/buttonboard.py @@ -0,0 +1,68 @@ +#!/bin/env python + +# Trellis - Scanner + +from __future__ import print_function +import time + +from HT16K33 import Trellis + +trellis=Trellis(bus=1).setUp() + + +numKeys=16 + +MOMENTARY = 0 +LATCHING = 1 +# Set the mode here: +MODE = LATCHING +# MODE = MOMENTARY + +for i in range(numKeys): + trellis.setLED(i) + trellis.writeDisplay() + time.sleep(0.05) +# then turn them off +for i in range(numKeys): + trellis.clrLED(i) + trellis.writeDisplay() + time.sleep(0.05) + +# Loop +print('Press Ctrl-C to quit.') +while True: + time.sleep(0.03) + + if MODE == MOMENTARY: + # If a button was just pressed or released... + if trellis.readSwitches(): + # go through every button + for i in range(numKeys): + # if it was pressed, turn it on + if trellis.justPressed(i): + print('v{0}'.format(i)) + trellis.setLED(i) + # if it was released, turn it off + if trellis.justReleased(i): + print('^{0}'.format(i)) + trellis.clrLED(i) + # tell the trellis to set the LEDs we requested + trellis.writeDisplay() + + if MODE == LATCHING: + # If a button was just pressed or released... + if trellis.readSwitches(): + # go through every button + for i in range(numKeys): + # if it was pressed... + if trellis.justPressed(i): + print('v{0}'.format(i)) + # Alternate the LED + if trellis.isLED(i): + trellis.clrLED(i) + else: + trellis.setLED(i) + # tell the trellis to set the LEDs we requested + trellis.writeDisplay() + + From 8ab2f96f44178079c4c618a9df5c6481d015dab1 Mon Sep 17 00:00:00 2001 From: Dj Walker-Morgan Date: Tue, 23 Jan 2018 00:18:34 +0000 Subject: [PATCH 2/2] Updated Readme.md Roughed out some docs --- README.md | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/README.md b/README.md index 7e864f5..ff099a2 100644 --- a/README.md +++ b/README.md @@ -408,5 +408,92 @@ Parent object inherited by all HT16K33 LED backpacks. Not intended for direct us | Write single character to a given postion +### Trellis ### + +#### Example #### + +```python + + #!/bin/env python + # Turn all the lights on then off + + from HT16K33 import Trellis + + trellis=Trellis(bus=1).setUp() + + numKeys=16 + + for i in range(numKeys): + trellis.setLED(i) + trellis.writeDisplay() + time.sleep(0.05) + # then turn them off + for i in range(numKeys): + trellis.clrLED(i) + trellis.writeDisplay() + time.sleep(0.05) + + print('Press Ctrl-C to quit.') + while True: + time.sleep(0.03) + # If a button was just pressed or released... + if trellis.readSwitches(): + # go through every button + for i in range(numKeys): + # if it was pressed, turn it on + if trellis.justPressed(i): + print('v{0}'.format(i)) + trellis.setLED(i) + # if it was released, turn it off + if trellis.justReleased(i): + print('^{0}'.format(i)) + trellis.clrLED(i) + # tell the trellis to set the LEDs we requested + trellis.writeDisplay() + +``` + +#### Methods #### + + class Trellis(_HT16K33.Base) + | Method resolution order: + | Trellis + | _HT16K33.Base + | __builtin__.object + | + | Methods defined here: + | + | writeDisplay(self): + | Write the LED display buffer values to the hardware. + | + |. clear(self): + | Clear all the LEDs in the display buffer. + | + | isKeyPressed(self, k): + |. Check if the specified key was pressed during the last readSwitches call. + | + |. wasKeyPressed(self, k): + | Check if the specified key was pressed before the last readSwitches call. + | + | isLED(self, x): + | Return True if the specified LED is illuminated in the display buffer. + | + | setLED(self, x): + | Turn on the specified LED in the display buffer. + | + |. clrLED(self, x): + | Turn off the specified LED in the display buffer. + | + |. readSwitches(self): + | Read the state of the buttons from the hardware. + | Returns True if a button is pressed, False otherwise. + | + | justPressed(self, k): + | Return True if the specified key was first pressed in the last readSwitches call. + | + | justReleased(self, k): + |. Return True if the specified key was just released in the last readSwitches call. + | + [1]:(http://dl.lm-sensors.org/i2c-tools/releases/i2c-tools-3.1.0.tar.bz2)