LED Matrix Shield DoneΒΆ

Published on 2017-10-18 in Trinket Shields.

Today I sat down and wrote the CircuitPython driver for the IS31FL3728 chip that I used for this shield. The effect looks as expected:

../../../_images/5291981508348759119.gif

The code is rather simple too:

class Matrix7x5:
    _ROWS = (3, 2, 4, 1, 0)
    _COLS = (6, 5, 0, 4, 1, 2, 3)
    _word = bytearray(2)
 
    def __init__(self, i2c, address=0x60):
        self.i2c = i2c
        self.address = address
        self.buffer = bytearray(8)
        self._config_register = 0
        self._effect_register = 0
 
    def _register(self, register, value):
        self._word[0] = register
        self._word[1] = value
        self.i2c.writeto(self.address, self._word)
 
    def fill(self, color=1):
        color = 0xff if color else 0x00
        for y in range(8):
            self.buffer[y] = color
 
    def pixel(self, x, y, color=None):
        if not (0 <= x <= 6 and 0 <= y <= 4):
            return
        x, y = self._COLS[x], self._ROWS[y]
        if color is None:
            return bool(self.buffer[x] & (0x01 << y))
        elif color:
            self.buffer[x] |= 0x01 << y
        else:
            self.buffer[x] &= ~(0x01 << y)
 
    def show(self):
        for y, row in enumerate(self.buffer):
            self._register(0x01 + y, row)
        self._register(0x0c, 0xff)
 
    def brightness(self, value):
        if value is None:
            value = self._effect_register & 0x0f
            if value & 0b1000:
                value &= 0b0111
            else:
                value += 7
            return value
        elif (0 <= value <= 6):
            self._effect_register = (
                self._effect_register & ~0x0f | (0b1000 | value) & 0x0f)
        elif (7 <= value <= 14):
            self._effect_register = (
                self._effect_register & ~0x0f | (value - 7) & 0x0f)
        else:
            raise ValueError("out of range")
        self._register(0x0d, self._effect_register)
 
    def active(self, value=None):
        if value is None:
            return bool(self._config_register & 0x80)
        if value:
            self._config_register |= 0x80
        else:
            self._config_register &= ~0x80
        self._register(0x00, self._config_register)