A Python library to drive MAX7219 LED matrices, WS2812 NeoPixels, and APA102 DotStar LEDs on Raspberry Pi
—
LED matrix display driver for MAX7219 chips supporting cascaded displays, rotation, block orientation, and brightness control. The MAX7219 is a popular SPI-driven LED matrix controller that can drive 8x8 LED matrices.
Creates a MAX7219 device instance with configurable display size, cascading, rotation, and layout options.
class max7219:
def __init__(
self,
serial_interface=None,
width=8,
height=8,
cascaded=None,
rotate=0,
block_orientation=0,
blocks_arranged_in_reverse_order=False,
contrast=0x70,
**kwargs
):
"""
Initialize MAX7219 LED matrix device.
Parameters:
- serial_interface: Serial interface object (from luma.core.interface.serial), optional
- width: Display width in pixels (default: 8)
- height: Display height in pixels (default: 8)
- cascaded: Number of cascaded devices (overrides width/height if specified)
- rotate: Rotation angle in degrees (0, 90, 180, 270)
- block_orientation: Block rotation angle (0, 90, -90, 180)
- blocks_arranged_in_reverse_order: Boolean for reverse order (default: False)
- contrast: Initial contrast/brightness value (0-255, default: 0x70)
- **kwargs: Additional parameters passed to parent device class
"""Controls the display output including showing images, brightness adjustment, and power management.
def display(self, image):
"""
Display a 1-bit PIL image on the LED matrix.
Parameters:
- image: PIL Image object in mode '1' (1-bit monochrome)
"""
def contrast(self, value):
"""
Set LED intensity/brightness.
Parameters:
- value: Brightness level (0-255, where 0 is dimmest and 255 is brightest)
"""
def show(self):
"""
Wake the device from low-power sleep mode.
Restores the device to normal operation.
"""
def hide(self):
"""
Put the device in low-power sleep mode.
Turns off all LEDs to save power.
"""Handles image transformations for proper display on the LED matrix hardware.
def preprocess(self, image):
"""
Preprocess image with rotation and reverse order corrections.
Parameters:
- image: PIL Image object
Returns:
- PIL Image object with transformations applied
"""from luma.core.interface.serial import spi, noop
from luma.led_matrix.device import max7219
from luma.core.render import canvas
# Create SPI interface
serial = spi(port=0, device=0, gpio=noop())
device = max7219(serial, width=8, height=8)
# Draw a simple pattern
with canvas(device) as draw:
draw.rectangle((0, 0, 7, 7), outline="white")
draw.point((4, 4), fill="white")from luma.core.interface.serial import spi, noop
from luma.led_matrix.device import max7219
from luma.core.render import canvas
# Create interface for 4 cascaded 8x8 matrices (32x8 total)
serial = spi(port=0, device=0, gpio=noop())
device = max7219(serial, cascaded=4)
# Draw across all matrices
with canvas(device) as draw:
draw.text((0, 0), "Hello World", fill="white")from luma.core.interface.serial import spi, noop
from luma.led_matrix.device import max7219
# Create rotated 90-degree display
serial = spi(port=0, device=0, gpio=noop())
device = max7219(serial, width=8, height=8, rotate=90)
# Brightness control
device.contrast(128) # Medium brightness
# Power management
device.hide() # Sleep mode
device.show() # Wake upfrom luma.core.interface.serial import spi, noop
from luma.led_matrix.device import max7219
# Create display with blocks in reverse order and rotated blocks
serial = spi(port=0, device=0, gpio=noop())
device = max7219(
serial,
cascaded=4,
block_orientation=90,
blocks_arranged_in_reverse_order=True
)The device may raise the following exceptions:
luma.core.error.DeviceDisplayModeError: Invalid display dimensions or unsupported image modeAssertionError: Invalid parameter values (contrast out of range, invalid rotation, etc.)RuntimeError: SPI interface initialization or communication failuresInstall with Tessl CLI
npx tessl i tessl/pypi-luma--led-matrix