LED 8x8 example

This commit is contained in:
James Bowman 2019-03-03 16:47:47 -08:00
parent b8198d9813
commit 8bc1009e1e
3 changed files with 59 additions and 0 deletions

BIN
python/samples/cp437-8x8 Normal file

Binary file not shown.

22
python/samples/ht16k33.py Normal file
View File

@ -0,0 +1,22 @@
class HT16K33:
def __init__(self, i2, a = 0x70):
self.i2 = i2
self.a = a
self.command(0x21) # Clock on
self.command(0x81) # Display on
self.bright(15)
self.load([0] * 16)
def bright(self, n):
assert 0 <= n < 16
self.command(0xe0 + n)
def command(self, b):
assert(self.i2.start(self.a, 0))
assert(self.i2.write([b]))
self.i2.stop()
def load(self, b128):
self.i2.start(self.a, 0)
self.i2.write([0] + b128)
self.i2.stop()

37
python/samples/led8x8.py Normal file
View File

@ -0,0 +1,37 @@
"""
Example for 8x8 LED Matrix modules, based on HT16K33.
Available from multiple vendors.
"""
import sys
import time
from i2cdriver import I2CDriver
font = open("cp437-8x8", "rb").read()
from ht16k33 import HT16K33
class led8x8(HT16K33):
def image(self, bb):
""" Set the pixels to the bytes bb """
def swiz(b):
bs = [str((b >> n) & 1) for n in range(8)]
return int(bs[7] + bs[0] + bs[1] + bs[2] + bs[3] + bs[4] + bs[5] + bs[6], 2)
bb = [swiz(b) for b in bb]
self.load([b for s in zip(bb,bb) for b in s])
def char(self, c):
""" Set the pixels to character c """
n = ord(c)
ch = font[n * 8:n * 8 + 8]
self.image(ch)
if __name__ == '__main__':
i2 = I2CDriver(sys.argv[1])
d = led8x8(i2)
for c in "I2C":
d.char(c)
time.sleep(1)