Skip to content

Commit

Permalink
Keybow 2040: Simplify RGB data writes.
Browse files Browse the repository at this point in the history
Write all 144 LED values (only 48 are used) in one transaction.

This is faster with 400KHz than trying to be smart.

Signed-off-by: Phil Howard <github@gadgetoid.com>
  • Loading branch information
Gadgetoid committed Jun 9, 2024
1 parent 4026aa6 commit bea247a
Showing 1 changed file with 2 additions and 28 deletions.
30 changes: 2 additions & 28 deletions boards/pimoroni/keybow_2040/keybow_2040_rgb.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,6 @@ def __init__(
i2c.write(bytes([0x00] * 14)) # Clear Mode, Frame, ... etc
i2c.write(bytes([_SHUTDOWN_REGISTER, 0x01])) # 0 == shutdown, 1 == normal

# Keep track of the LEDs we actually use (48 out of 144)
# so we can batch up individual i2c transactions and avoid
# copying unused data.
self.used_leds = [
[17, 18],
[25, 26],
[32, 33],
[40, 41],
[64, 65, 66, 67],
[72, 73, 74, 75],
[80, 81, 82, 83],
[88, 89, 90, 91],
[96, 97, 98, 99],
[104, 105, 106, 107],
[112, 113, 114, 115],
[120, 121, 122, 123],
[128, 129, 130, 131],
[136, 137, 138, 139]]

def _transmit(self, buffer):
# Shuffle the 16 pixel PixelBuf buffer into our 144 LED
# display native format.
Expand All @@ -73,16 +54,9 @@ def _transmit(self, buffer):
# Switch to our new (not currently visible) frame
i2c.write(bytes([_BANK_ADDRESS, self._frame]))

# Lazy non-batched write, probably fast enough?
# i2c.write(bytes([_COLOR_OFFSET]) + self.out_buffer)

# We only actually use 16 * 3 = 48 LEDs out of the 144 total
# They're kind of awkwardly spread around, but if we batch up
# the writes it gets the update time from ~0.016ms to ~0.012ms
for group in self.used_leds:
offset = group[0]
count = len(group)
i2c.write(bytes([_COLOR_OFFSET + offset]) + self.out_buffer[offset:offset + count])
# but at 400KHz I2C it's cheaper just to write the whole lot
i2c.write(bytes([_COLOR_OFFSET]) + self.out_buffer)

# Set the newly written frame as the visible one
i2c.write(bytes([_BANK_ADDRESS, _CONFIG_BANK]))
Expand Down

0 comments on commit bea247a

Please sign in to comment.