Skip to content
Merged
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
47 changes: 30 additions & 17 deletions ht16k33_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,11 @@ def __init__(self, i2c, address=0x70):
self.address = address
self._temp = bytearray(1)

self._buffer = bytearray(16)
self._fb_buffer = bytearray(self.WIDTH * self.HEIGHT
* self.FB_BPP // 8)
self.framebuffer = framebuf.FrameBuffer(
self._fb_buffer, self.WIDTH, self.HEIGHT, self.FORMAT)

self.framebuffer.fill(0)
self.buffer = bytearray(16)
self._write_cmd(_HT16K33_OSCILATOR_ON)
self.blink_rate(0)
self.brightness(15)

self.pixel = self.framebuffer.pixel
self.fill = self.framebuffer.fill

def _write_cmd(self, byte):
self._temp[0] = byte
self.i2c.writeto(self.address, self._temp)
Expand All @@ -47,23 +38,45 @@ def brightness(self, brightness):
self._brightness = brightness
self._write_cmd(_HT16K33_CMD_BRIGHTNESS | brightness)

def show(self):
self.i2c.writeto_mem(self.address, 0x00, self.buffer)

def fill(self, color):
fill = 0xff if color else 0x00
for i in range(16):
self.buffer[i] = fill


class HT16K33Matrix:
def __init__(self, i2c, address=0x70):
super().__init__(i2c, address)

self._fb_buffer = bytearray(self.WIDTH * self.HEIGHT
* self.FB_BPP // 8)
self.framebuffer = framebuf.FrameBuffer(
self._fb_buffer, self.WIDTH, self.HEIGHT, self.FORMAT)

self.framebuffer.fill(0)
self.pixel = self.framebuffer.pixel
self.fill = self.framebuffer.fill

def show(self):
self._copy_buf()
self.i2c.writeto_mem(self.address, 0x00, self._buffer)
super().show()


class Matrix16x8(HT16K33):
class Matrix16x8(HT16K33Matrix):
WIDTH = 16
HEIGHT = 8
FORMAT = framebuf.MONO_HLSB
FB_BPP = 1

def _copy_buf(self):
for y in range(8):
self._buffer[y * 2] = self._fb_buffer[y]
self.buffer[y * 2] = self._fb_buffer[y]


class Matrix8x8(HT16K33):
class Matrix8x8(HT16K33Matrix):
WIDTH = 8
HEIGHT = 8
FORMAT = framebuf.MONO_HLSB
Expand All @@ -72,18 +85,18 @@ class Matrix8x8(HT16K33):
def _copy_buf(self):
for y in range(8):
b = self._fb_buffer[y]
self._buffer[y * 2] = (b >> 1) | (b << 7)
self.buffer[y * 2] = (b >> 1) | (b << 7)


class Matrix8x8x2(HT16K33):
class Matrix8x8x2(HT16K33Matrix):
WIDTH = 8
HEIGHT = 8
FORMAT = framebuf.GS4_HMSB
FB_BPP = 4

def _copy_buf(self):
pixel = self.framebuffer.pixel
_buffer = self._buffer
_buffer = self.buffer
for y in range(8):
b = 0
for x in range(8):
Expand Down