SmartMatrix Library

repository·master·Indexed 20 days ago

https://github.com/pixelmatix/smartmatrix

A high-performance library for driving HUB75 LED matrix panels and APA102 addressable LEDs using Teensy (3.x and 4.x) and ESP32 microcontrollers. It leverages hardware peripherals such as DMA, I2S, and FlexIO to handle high-speed graphics refreshing in the background via interrupts. The library supports various panel types, tiling options, and integrates with Adafruit_GFX and FastLED for graphics and color management.

Tokens
7K
Snippets
9
Records
27
Agent score
62%

What's inside SmartMatrix

  1. Overview of SmartMatrix Library

    master
    SmartMatrix Library is designed to refresh HUB75 LED matrix panels and APA102-compatible addressable LEDs with high-quality graphics using simple Arduino sketches. The library leverages platform-specific peripherals like DMA, I2S, and FlexIO to run the refresh process in the background using interrupts, ensuring high performance.
  2. Supported Hardware Platforms

    master

    SmartMatrix Library supports the following hardware:

    Teensy

    • Teensy 4.x: Teensy 4.1 and Teensy 4.0 (Recommended for best performance).
    • Teensy 3.x: Teensy 3.6, 3.5, 3.2/3.1, 3.0.

    ESP32

    • ESP32: Experimental support is available in version 4.0, though some features may not be as mature as the Teensy ports.
  3. Understanding HUB75 Panels

    master

    HUB75 RGB panels are cost-effective LED billboards that require an external controller (like a Teensy running SmartMatrix) to refresh them line by line.

    • Compatibility: Panels from Adafruit and Sparkfun are known to be compatible, but most panels from sources like AliExpress are also compatible.
    • Search Terms: When looking for panels, use terms like "pixel pitch" and "RGB" (e.g., "P6 RGB" for a 6mm pitch panel).
  4. Configure panel stacking and tiling options

    master

    When chaining multiple panels to create a larger display, use kMatrixOptions to define how they are tiled. You can combine multiple options using the bitwise-OR operator (|).

    Available Options

    • SM_HUB75_OPTIONS_C_SHAPE_STACKING: Inverts the panel on each row to create a 'C' shape. This minimizes cable length between rows but is incompatible with panels requiring Multi Row Refresh Mapping (e.g., types containing column size like SM_PANELTYPE_HUB75_16ROW_32COL_MOD2SCAN).
    • SM_HUB75_OPTIONS_BOTTOM_TO_TOP_STACKING: Stacks panels from the bottom up instead of the default top-down.

    Example: C-shape stacking with Bottom-to-top direction

    const uint8_t kMatrixOptions = (SM_HUB75_OPTIONS_C_SHAPE_STACKING | SM_HUB75_OPTIONS_BOTTOM_TO_TOP_STACKING);

    Note: As of version 4.0.3, the stacking direction is reversed for the ESP32 platform.

  5. Migrating from SmartMatrix Library 3.x to 4.x

    master

    Sketches written for version 3.x are generally compatible with 4.0 but require minor adjustments.

    Key Migration Steps

    • Review MIGRATION.md for specific code changes required.
    • File Renaming: Many files were renamed using case-sensitive changes.
    • Git Tip: If you encounter errors like The following untracked working tree files would be overwritten by checkout when switching versions, use the command line with the -f flag to force the checkout (note: this will discard local modifications).

    New Features in 4.0

    • Support for Teensy 4 and ESP32.
    • Support for driving APA102 LEDs on Teensy platforms.
    • Rewritten "GFX" layers for better efficiency, utilizing Adafruit_GFX for drawing and fonts.
    • Support for non-standard mapping panels (e.g., 16x32/4 MOD4 panels).
  6. Setup the SmartMatrix Basic Spectrum Analyzer example

    master

    The SmartMatrix Basic Spectrum Analyzer is a demo that visualizes audio on a 32x32 RGB LED matrix. It utilizes the SmartMatrix library for matrix control, FastLED for color management, and the Teensy Audio Library for signal processing.

    Hardware Requirements

    • LED Matrix: 32x32 RGB LED matrix.
    • Controller: Teensy 3 or Teensy 4.
    • Shield: A SmartMatrix shield is required to interface the controller with the matrix.
    • Audio Input: Uses analog line-in on pin A2.

    Analog Input Circuit

    For optimal performance and to prevent damage, use a recommended analog input circuit. You can find the recommended schematic via the Teensy Audio GUI documentation: Teensy Audio Input Analog.

  7. Configure SmartMatrix hardware in examples

    master

    To run library examples like FeatureDemo, you must configure the sketch to match your specific hardware setup:

    1. Select Hardware Configuration: Open the example sketch and find the section at the top with the comment // uncomment one line to select your MatrixHardware configuration. Uncomment the line corresponding to your specific board (e.g., Teensy 4 or ESP32).
    2. Set Resolution: Adjust kMatrixWidth and kMatrixHeight to match your total display dimensions (in pixels).
    3. Set Panel Type: Adjust kPanelType based on your HUB75 panel specifications. Common settings include:
      • 32-pixel high panels (e.g., 32x32, 64x32): SM_PANELTYPE_HUB75_32ROW_MOD16SCAN
      • 16-pixel high panels (e.g., 32x16): SMARTMATRIX_HUB75_16ROW_MOD8SCAN
      • 64-pixel high panels (e.g., 64x64, 128x64): SM_PANELTYPE_HUB75_64ROW_MOD32SCAN

    Teensy CPU Speed Settings:

    • Teensy 3: Under Tools > CPU Speed, select either 48 MHz or 96MHz (overclock). Avoid 72MHz as it may be incompatible with some libraries.
    • Teensy 4: Use the default CPU speed.
  8. Install required libraries for Spectrum Analyzer

    master

    To run the Spectrum Analyzer example, you must install the following libraries in your Arduino/Teensy development environment:

    1. Teensy Audio Library: https://github.com/PaulStoffregen/Audio
    2. SerialFlash Library (dependency for Teensy Audio): https://github.com/PaulStoffregen/SerialFlash
    3. SmartMatrix Library: https://github.com/pixelmatix/smartmatrix/releases
    4. FastLED (v3.1 or higher): https://github.com/FastLED/FastLED/releases
  9. Install the SmartMatrix Library

    master

    You can install the SmartMatrix library using one of two methods:

    1. Arduino Library Manager: Search for SmartMatrix. Note that during the transition to version 4.x, you might find it listed under SmartMatrix3.
    2. Manual Download: Download the latest release from the GitHub Releases page and import it into your Arduino IDE.

    Prerequisites:

    • Arduino IDE: Version 1.6.5 or later recommended.
    • Teensyduino: Install the latest version via the official download page.
  10. Handle CRGB and rgb24 type conversions in FastLED ports

    master

    SmartMatrix uses rgb24 for pixel data, which is not automatically compatible with FastLED's CRGB or CHSV types. Use the following casting patterns to resolve compiler errors:

    • CHSV to rgb24: Wrap CHSV in CRGB(): buffer[XY(i,j)] = CRGB(CHSV(...));
    • CRGB pointer casting: If a function expects CRGB*, cast the rgb24* buffer: (CRGB*)buffer.
    • HTMLColorCode: Cast to CRGB: (CRGB)CRGB::Black.
    • Function arguments (nblend, etc.): Cast the buffer element to CRGB&: nblend((CRGB&)buffer[pixelnumber], newcolor, 64);
  11. Update Setup() for SmartMatrix 3.0 Layers

    master

    In SmartMatrix 3.0, you must explicitly add your allocated layers to the matrix instance before initializing the hardware. If you skip this step, the screen may remain blank even if the code compiles.

    // Add layers before matrix.begin()
    matrix.addLayer(&backgroundLayer); 
    matrix.addLayer(&scrollingLayer); 
    matrix.addLayer(&indexedLayer); 
    
    matrix.begin();

    If you do not need a specific layer, omit the addLayer call and remove the corresponding SMARTMATRIX_ALLOCATE_* macro from your sketch.

    matrix.addLayer(&backgroundLayer); 
    matrix.addLayer(&scrollingLayer); 
    matrix.addLayer(&indexedLayer); 
    matrix.begin();
  12. Migrate from SmartMatrix 3.x to 4.0

    master

    To upgrade a sketch from version 3.x to 4.0, perform the following changes:

    1. Update Includes: Change #include <SmartMatrix3.h> to #include <SmartMatrix.h> or #include <SmartMatrix4.h>.

    2. Update Hardware Selection: Hardware selection is now handled via specific header files included before SmartMatrix.h:

      • Bare Teensy 3 (no latch) or SmartMatrix Shield V1-V3: Add #include <MatrixHardware_Teensy3_ShieldV1toV3.h>.
      • SmartLED Shield for Teensy 3 (V4) or external latch: Add #include <MatrixHardware_Teensy3_ShieldV4.h>. (Note: If your sketch previously used #include <SmartLEDShieldV4.h>, replace it with the MatrixHardware version above).
    3. Custom Hardware: If you previously modified the library for custom pinouts, you can now include a custom header file from your sketch folder before #include <SmartMatrix.h>. Copy the relevant header from the library's /src/ folder to your sketch folder and rename it.

    Note on Coexistence: You can run 3.x and 4.x side-by-side in the Arduino Libraries folder by placing them in unique directories (e.g., SmartMatrix3 and SmartMatrix4). Use the specific versioned headers to target the desired version.

    // Example for Teensy 3 with Shield V1-V3
    #include <MatrixHardware_Teensy3_ShieldV1toV3.h>
    #include <SmartMatrix.h>