MicroPython Pixel Framebuf
pixel_framebuf adapts MicroPython's framebuf↗ (opens in a new tab) drawing primitives to NeoPixel, WS2812, and DotStar/APA102 LED matrices.
It stores pixels as RGB565, so fill, line, rect, ellipse, text, and the other standard framebuf.FrameBuffer operations work normally. Call display() once the frame is ready to copy it to the LEDs and transmit it.
Install
After publishing this repository to mattytrentini/micropython-pixel-framebuf:
mpremote mip install github:mattytrentini/micropython-pixel-framebuf
The package is self-hosted through package.json, so it is installable directly from GitHub and does not require publication to micropython-lib.
Usage
from machine import Pin
from neopixel import NeoPixel
from pixel_framebuf import PixelFramebuffer, HORIZONTAL
WIDTH = 10
HEIGHT = 6
leds = NeoPixel(Pin(2), WIDTH * HEIGHT)
matrix = PixelFramebuffer(leds, WIDTH, HEIGHT, HORIZONTAL, alternating=True)
matrix.fill(0)
matrix.rect(0, 0, WIDTH, HEIGHT, 0x001f)
matrix.line(0, 0, WIDTH - 1, HEIGHT - 1, 0xf800)
matrix.text("Hi", 1, 0, 0x07e0)
matrix.display()
The Seeed Studio RGB Matrix for XIAO has 60 WS2812 LEDs in a 10-by-6 grid. The example assumes its LEDs are wired row-by-row in a serpentine pattern; use pixel_to_led(x, y) and a one-pixel test pattern to confirm the orientation of a particular board.
Matrix layouts
PixelFramebuffer(pixels, width, height, orientation, alternating, reverse_x, reverse_y) accepts these layout controls:
HORIZONTAL(default): consecutive LEDs travel across a row.VERTICAL: consecutive LEDs travel down a column.alternating=True(default): reverse every second row or column for serpentine matrices.reverse_x/reverse_y: move the logical origin to the other horizontal or vertical edge.
The LED object needs indexed RGB assignment and .write() (as MicroPython neopixel.NeoPixel provides). A compatible DotStar driver can simply define write = show. Set auto_write=False when creating a DotStar object; otherwise every LED assignment during display() transmits a partial frame.
License
MIT. See LICENSE↗ (opens in a new tab).