Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates for Adafruit Trellis #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 91 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -405,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)
[1]:(http://dl.lm-sensors.org/i2c-tools/releases/i2c-tools-3.1.0.tar.bz2)
107 changes: 107 additions & 0 deletions Trellis.py
Original file line number Diff line number Diff line change
@@ -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 ([email protected]
#
# Re-ported to the HT16K33 package by Dj Walker-Morgan ([email protected])
#

__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()
3 changes: 2 additions & 1 deletion __init__.py
Original file line number Diff line number Diff line change
@@ -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
68 changes: 68 additions & 0 deletions examples/Trellis/buttonboard.py
Original file line number Diff line number Diff line change
@@ -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()