CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-luma--led-matrix

A Python library to drive MAX7219 LED matrices, WS2812 NeoPixels, and APA102 DotStar LEDs on Raspberry Pi

Pending
Overview
Eval results
Files

max7219.mddocs/

MAX7219 LED Matrix

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.

Capabilities

Device Initialization

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
        """

Display Control

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.
    """

Image Preprocessing

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
    """

Usage Examples

Basic 8x8 Matrix

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")

Cascaded Multiple Matrices

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")

Rotated Display

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 up

Custom Block Arrangement

from 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
)

Hardware Requirements

  • Platform: Raspberry Pi or compatible Linux SBC
  • Interface: SPI (Serial Peripheral Interface)
  • Connections:
    • VCC: 5V power supply
    • GND: Ground
    • DIN: SPI MOSI (GPIO 10)
    • CS: SPI CE0 (GPIO 8)
    • CLK: SPI SCLK (GPIO 11)

Error Handling

The device may raise the following exceptions:

  • luma.core.error.DeviceDisplayModeError: Invalid display dimensions or unsupported image mode
  • AssertionError: Invalid parameter values (contrast out of range, invalid rotation, etc.)
  • RuntimeError: SPI interface initialization or communication failures

Install with Tessl CLI

npx tessl i tessl/pypi-luma--led-matrix

docs

apa102.md

constants.md

index.md

max7219.md

neopixel.md

sevensegment.md

unicornhathd.md

tile.json