pypixelcolor (iPixel-CLI)

repository·main·Indexed 41 days ago

https://github.com/lucagoc/pypixelcolor

A Python library and CLI tool for controlling iPixel Color LED matrix devices over Bluetooth Low Energy (BLE). Version 0.5.0 supports text animations, image and GIF playback, clock modes, rhythm equalizers, and hardware settings like brightness and orientation.

Tokens
20.9K
Snippets
92
Records
110
Agent score
52%

What's inside pypixelcolor

  1. Overview of pypixelcolor

    main

    pypixelcolor (also known as iPixel-CLI) is a Python library and CLI tool designed to control iPixel Color LED matrix devices using Bluetooth Low Energy (BLE).

    It provides capabilities to:

    • Send text: Display custom messages with various fonts, colors, and animations.
    • Send images: Display static images and GIFs on the LED matrix.
    • Control settings: Adjust device parameters such as brightness, orientation, and power.
    • Clock, Timer & Scoreboard: Use specialized modes for displaying clock faces, controlling timers, and managing scoreboards.
    • Scripting: Use the full Python library via both synchronous and asynchronous APIs.
    • CLI: Use a feature-rich command-line interface that supports interactive status updates and multi-command chaining.
  2. Overview of pypixelcolor features

    main

    pypixelcolor allows you to control iPixel Color LED matrix devices via Bluetooth Low Energy (BLE). Key capabilities include:

    • Send text: Display custom messages with various fonts and animations.
    • Send images: Display images and GIFs on the matrix.
    • Control settings: Adjust brightness, orientation, and power.
    • Clock mode: Display time with various clock faces.
    • Scriptable: Use the full Python library for automation.
    • CLI: Use the command-line interface for quick interactions.
  3. How data slots work in pypixelcolor

    main

    The device provides "slots" to store text and image data. Using the save_slot= argument in respective commands allows for faster access and reduces the time needed to display content on the LED matrix.

    !!! Warning Using slots can cause bootloops if the send command contains corrupted data. Before sending data to a slot, ensure that sending works correctly without using slots first.

  4. Style text with inline color tags

    main

    The send_text command supports inline hex color tags to style individual words or characters.

    Tag Syntax:

    • Opening tag: [#RRGGBB] or [RRGGBB]
    • Closing tag: [/], [/#], or [/color]

    Example: client.send_text("[#ff0000]Red[/] [#00ff00]Green[/]")

    Note: Inline color tags are not supported when var_width=True is enabled in the font configuration. In that mode, the uniform color parameter is used instead.

    # Multi-colored text
    client.send_text("[#ff0000]Red[/] [#00ff00]Green[/] [#0000ff]Blue[/]")
    
    # Nested tags (closes back to the parent color)
    client.send_text("[#ffaa00]Orange [#ffffff]White[/] Orange[/]")
  5. How variable-width rendering works

    main

    By default (var_width=False), text is rendered in a fixed-width format where each character occupies a specific slot. This is ideal for monospace fonts like UNIFONT.

    When var_width=True is enabled, the system uses proportional font rendering. It renders text on a continuous canvas with natural kerning before slicing it into matrix chunks. This allows for more natural-looking text with custom .ttf or .otf files.

    Limitations of var_width=True:

    • Static text truncation: If animation=0 (STATIC), text that exceeds the screen width will be cut off. Use scroll_left for long strings.
    • No inline colors: Inline color tags ([#ff0000]...[/]) are ignored and a warning is issued; the uniform color parameter must be used instead.
  6. Quickstart with pypixelcolor

    main

    To use pypixelcolor as a library, import the necessary clients and models from pypixelcolor or pypixelcolor.models. The library uses a context manager pattern for the Client class to ensure connections to the LED matrix are automatically opened and closed.

    Common tasks include:

    • Inspecting device dimensions via get_device_info().
    • Displaying images or GIFs using send_image() with a ResizeMethod.
    • Sending text with send_text(), supporting animations, emojis, and inline color tags (e.g., [#ff0000]text[/]).
    • Using custom fonts via FontConfig.from_file().
    • Controlling timers with set_timer() and TimerAction.
    • Adjusting brightness and clock modes.
    import time
    from pypixelcolor import (
        Client,
        TextAnimation,
        TimerAction,
        ResizeMethod,
        FontConfig,
    )
    
    MAC_ADDRESS = "30:E1:AF:BD:5F:D0"
    
    # The context manager automatically handles connection and disconnection
    with Client(MAC_ADDRESS) as device:
        # 1. Inspect device dimensions and info
        info = device.get_device_info()
        print(f"Connected to {info.width}x{info.height} LED matrix (Type {info.led_type})")
    
        # 2. Display an image or animated GIF
        device.send_image("./banner.png", resize_method=ResizeMethod.FIT)
        time.sleep(3)
    
        # 3. Send text with animations, emojis, and inline color tags
        device.send_text(
            "[#ff0000]Hello[/] [#00ff00]pypixelcolor[/] 🚀",
            animation=TextAnimation.SCROLL_LEFT,
            speed=80,
        )
        time.sleep(3)
    
        # 4. Smooth pulsing text with a custom font
        custom_font = FontConfig.from_file("./fonts/retro.ttf", font_size=16)
        device.send_text("ALERT", font=custom_font, animation=TextAnimation.FADE)
        time.sleep(2)
    
        # 5. Control stopwatch timer
        device.set_timer(TimerAction.START)
        time.sleep(5)
        device.set_timer(TimerAction.STOP)
    
        # 6. Clock mode and brightness
        device.set_brightness(60)
        device.set_clock_mode(style=1, format_24=True)
  7. Set up the pypixelcolor development environment

    main

    This project uses Hatch for packaging and environment management. To set up a local development environment, follow these steps:

    1. Create and activate the environment using hatch env create and hatch shell.
    2. Install the package in editable mode with development dependencies using hatch run pip install -e '.[dev]'.
    3. Run tests using hatch run pytest.
    4. Build the package using hatch build.
    hatch env create
    hatch shell
    hatch run pip install -e '.[dev]'
    hatch run pytest
    hatch build
  8. Install pypixelcolor optional features

    main

    You can extend pypixelcolor by installing specific optional feature sets via pip extras:

    • Enhanced CLI: Adds advanced command-line interface capabilities.
    • WebSocket server bridge: Enables the WebSocket server functionality.
    • HEIF/HEIC image format support: Adds support for HEIF/HEIC image files.
    • Complete installation: Installs all available optional features at once.
    # Enhanced CLI
    pip install "pypixelcolor[cli]"
    
    # WebSocket server bridge
    pip install "pypixelcolor[server]"
    
    # HEIF/HEIC image format support
    pip install "pypixelcolor[heif]"
    
    # Complete installation with all features
    pip install "pypixelcolor[all]"
  9. Capture and retrieve BLE logs using ADB

    main

    Once the device is prepared, use the Android Debug Bridge (ADB) to extract the logs.

    1. Connect the device: Connect via USB or ensure wireless ADB is active.
    2. Verify connection: Run adb devices to ensure your device is listed. If it shows unauthorized, check your phone to accept the debugging prompt.
    3. Generate bugreport: Run adb bugreport. This creates a zip file in your current directory (e.g., bugreport-<device_name>-<timestamp>.zip).
    4. Locate the log: Unzip the bugreport and navigate to the following path within the archive to find the HCI snoop log: FS/data/misc/bluetooth/logs/btsnoop_hci_<date>_<time>.log

    Troubleshooting: If you find a file named btsnooz_hci.log instead of btsnoop_hci.log, it means BLE data was filtered out. Ensure Bluetooth HCI snoop log is set to Enabled and you have toggled Bluetooth off and on.

    # Check connected devices
    adb devices
    
    # Generate the bugreport containing BLE logs
    adb bugreport
  10. Prepare an Android device for BLE logging

    main

    To capture Bluetooth Low Energy (BLE) logs, you must first enable Developer options and specific logging settings on your Android device.

    1. Enable USB debugging

    • Navigate to Settings > About phone.
    • Tap Build number 7 times until Developer options are enabled.
    • Go to Settings > Developer options and enable USB debugging.

    2. Enable Bluetooth HCI snoop log

    • Go to Settings > Developer options.
    • Locate Bluetooth HCI snoop log and set it to Enabled.
    • Important: Restart the Bluetooth service by toggling Bluetooth OFF and then ON again to ensure the logging starts.

    Note: Steps may vary slightly depending on your Android version or manufacturer.

  11. Install pypixelcolor via pip

    main

    You can install the pypixelcolor library (also known as iPixel-CLI) using pip. This provides both the Python library for automation and the command-line interface for controlling iPixel Color LED matrix devices via Bluetooth Low Energy.

    pip install pypixelcolor