ipxl

package module
v0.0.0-...-54b38f4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 27, 2025 License: BSD-3-Clause Imports: 12 Imported by: 0

README

go-ipxl

A Go library for controlling iPixel Color LED displays via Bluetooth Low Energy (BLE).

Overview

go-ipxl provides a clean, idiomatic Go interface for communicating with iPixel Color devices. The library handles BLE connectivity, device discovery, image processing, and data transmission protocols required to control LED matrix displays.

Features

  • BLE Connectivity: Seamless Bluetooth Low Energy connection management
  • Device Information: Query device specifications, firmware versions, and screen dimensions
  • Image Display: Send images directly to LED matrices with automatic scaling and color conversion
  • Format Support: Built-in support for PNG and JPEG image formats
  • DIY Modes: Switch between different display modes including DIY functionality
  • Low-Level Control: Direct LED control and raw data transmission capabilities
  • Robust Protocol: Implements chunked data transmission with CRC verification

Installation

go get github.com/yyewolf/go-ipxl

Quick Start

package main

import (
    "context"
    "log"
    "time"
    
    "github.com/yyewolf/go-ipxl"
)

func main() {
    // Create a new display instance
    display := ipxl.NewDisplay("AA:BB:CC:DD:EE:FF")
    
    // Connect to the device
    ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
    defer cancel()
    
    if err := display.Connect(ctx); err != nil {
        log.Fatal("Failed to connect:", err)
    }
    defer display.Disconnect()
    
    // Get device information
    info, err := display.GetDeviceInfo(ctx)
    if err != nil {
        log.Fatal("Failed to get device info:", err)
    }
    
    log.Printf("Device: %dx%d matrix, MCU: %s, WiFi: %s", 
        info.Width, info.Height, info.MCUVersion, info.WiFiVersion)
    
    // Load and send an image
    processor := ipxl.NewBitmapProcessor(info.Width, info.Height)
    img, err := processor.LoadImageFromFile("image.png")
    if err != nil {
        log.Fatal("Failed to load image:", err)
    }
    
    if err := display.SendImage(ctx, img); err != nil {
        log.Fatal("Failed to send image:", err)
    }
}

API Reference

Display Interface

The main interface for interacting with iPixel devices:

type Display interface {
    Connect(ctx context.Context) error
    Disconnect() error
    GetScreenSize(ctx context.Context) (width, height int, err error)
    GetDeviceInfo(ctx context.Context) (*DeviceInfo, error)
    SetLED(on bool) error
    SendImage(ctx context.Context, image image.Image) error
    SwitchToDiyFunMode(mode int) error
}
Device Information

Query device specifications and capabilities:

type DeviceInfo struct {
    DeviceType   byte   // Device type identifier
    MCUVersion   string // Microcontroller firmware version
    WiFiVersion  string // WiFi/BLE module version
    Width        int    // LED matrix width
    Height       int    // LED matrix height
    HasWifi      bool   // WiFi capability flag
    PasswordFlag byte   // Password requirement status
}
Image Processing

Convert and process images for LED display:

// Create a bitmap processor for your display dimensions
processor := ipxl.NewBitmapProcessor(64, 64)

// Load from file
img, err := processor.LoadImageFromFile("path/to/image.png")

// Convert to RGB data
rgbData := processor.ImageToRGB(img)

Supported Image Formats

  • PNG
  • JPEG
  • Any format supported by Go's image package

Images are automatically resized and color-converted to match the target LED matrix dimensions.

Connection Management

The library handles BLE connection lifecycle automatically:

  • Device scanning and discovery
  • Characteristic enumeration
  • Connection timeout handling
  • Automatic cleanup on disconnect

Data Transmission

Images are transmitted using a chunked protocol with:

  • 12KB chunk size for optimal BLE throughput
  • CRC32 verification for data integrity
  • Sequential transmission with proper packet ordering
  • Support for various data types (images, videos, text, etc.)

Error Handling

All operations return descriptive errors for proper error handling:

if err := display.Connect(ctx); err != nil {
    switch {
    case errors.Is(err, context.DeadlineExceeded):
        log.Println("Connection timeout")
    case strings.Contains(err.Error(), "characteristic not found"):
        log.Println("Device not compatible")
    default:
        log.Printf("Connection failed: %v", err)
    }
}

Requirements

Platform Support

The library uses TinyGo's bluetooth package, which supports:

  • Linux (BlueZ)
  • macOS (Core Bluetooth)
  • Windows (WinRT)

Contributing

Contributions are welcome! Please feel free to submit pull requests, report bugs, or suggest features.

Acknowledgments

Based on the iPixel protocol implementation and inspired by the original Android SDK.

Documentation

Index

Constants

View Source
const (
	WRITE_CHAR_UUID  = "0000fa02-0000-1000-8000-00805f9b34fb"
	NOTIFY_CHAR_UUID = "0000fa03-0000-1000-8000-00805f9b34fb"
)
View Source
const (
	// Data types
	TYPE_CAMERA           = 0
	TYPE_VIDEO            = 1
	TYPE_IMAGE            = 2
	TYPE_GIF              = 3
	TYPE_TEXT             = 4
	TYPE_DIY_IMAGE        = 5
	TYPE_DIY_IMAGE_UNREDO = 6
	TYPE_TEM              = 7

	// Default values
	DEFAULT_LED_FRAME_SIZE = 1024
	DEFAULT_BRIGHTNESS     = 50
	UBYTE_MAX_VALUE        = 255

	DIY_IMAGE_FUN_QUIT_NOSAVE_KEEP_PREV   = 0
	DIY_IMAGE_FUN_ENTER_CLEAR_CUR_SHOW    = 1
	DIY_IMAGE_FUN_QUIT_STILL_CUR_SHOW     = 2
	DIY_IMAGE_FUN_ENTER_NO_CLEAR_CUR_SHOW = 3
)
View Source
const CHUNK_SIZE = 12288

Variables

View Source
var LEDConfigs = map[string]struct {
	Width       int
	Height      int
	AspectRatio float32
	Description string
}{
	"16x16":  {16, 16, 1.0, "Standard 16x16 matrix"},
	"32x8":   {32, 8, 4.0, "Wide 32x8 display"},
	"64x16":  {64, 16, 4.0, "Large 64x16 display"},
	"96x16":  {96, 16, 6.0, "Extra wide 96x16"},
	"64x32":  {64, 32, 2.0, "Large square-ish 64x32"},
	"128x16": {128, 16, 8.0, "Ultra wide banner"},
	"192x16": {192, 16, 12.0, "Super wide banner"},
	"80x16":  {80, 16, 5.0, "Medium wide display"},
	"48x16":  {48, 16, 3.0, "Small wide display"},
	"64x20":  {64, 20, 3.2, "Special ratio display"},
}
View Source
var ZeroCharacteristic = bluetooth.DeviceCharacteristic{}
View Source
var ZeroDevice = bluetooth.Device{}

Functions

func IsCharacteristicZero

func IsCharacteristicZero(char bluetooth.DeviceCharacteristic) bool

func IsDeviceZero

func IsDeviceZero(dev bluetooth.Device) bool

Types

type BitmapProcessor

type BitmapProcessor struct {
	Width  int
	Height int
}

BitmapProcessor handles bitmap-to-RGB conversion for LED displays

func NewBitmapProcessor

func NewBitmapProcessor(width, height int) *BitmapProcessor

NewBitmapProcessor creates a new bitmap processor with specified dimensions

func (*BitmapProcessor) CreateFromColorArray

func (bp *BitmapProcessor) CreateFromColorArray(colors []color.RGBA) ([]byte, error)

CreateFromColorArray creates RGB data from a color array (useful for patterns)

func (*BitmapProcessor) CreateSolidColorRGB

func (bp *BitmapProcessor) CreateSolidColorRGB(r, g, b byte) []byte

CreateSolidColorRGB creates RGB data for a solid color image

func (*BitmapProcessor) ImageToRGB

func (bp *BitmapProcessor) ImageToRGB(img image.Image) []byte

ImageToRGB converts an image to RGB byte array (similar to BGRUtils.bitmap2RGB) Returns byte array in format: [R, G, B, R, G, B, ...]

func (*BitmapProcessor) LoadImageFromFile

func (bp *BitmapProcessor) LoadImageFromFile(filepath string) (image.Image, error)

LoadImageFromFile loads an image from file path (supports PNG, JPEG)

func (*BitmapProcessor) ProcessImageFile

func (bp *BitmapProcessor) ProcessImageFile(filepath string) ([]byte, error)

ProcessImageFile loads, resizes, and converts an image file to RGB byte array

func (*BitmapProcessor) ResizeImage

func (bp *BitmapProcessor) ResizeImage(src image.Image, width, height int) image.Image

ResizeImage resizes an image to the specified width and height using nearest neighbor

type DeviceInfo

type DeviceInfo struct {
	DeviceType   byte   // Byte 4 from device response
	MCUVersion   string // Major.Minor version
	WiFiVersion  string // Major.Minor version (or BLE version)
	Width        int    // LED matrix width
	Height       int    // LED matrix height
	HasWifi      bool   // Whether device supports WiFi
	PasswordFlag byte   // Password requirement flag
}

DeviceInfo contains information retrieved from the LED device

type Display

type Display interface {
	Connect(ctx context.Context) error
	Disconnect() error

	GetScreenSize(ctx context.Context) (width, height int, err error)
	GetDeviceInfo(ctx context.Context) (*DeviceInfo, error)
	SetLED(on bool) error
	SendImage(ctx context.Context, image image.Image) error
	SwitchToDiyFunMode(mode int) error
}

func NewDisplay

func NewDisplay(address string) Display

type PacketBuilder

type PacketBuilder struct{}

PacketBuilder represents the core sending functionality

func NewPacketBuilder

func NewPacketBuilder() *PacketBuilder

NewPacketBuilder creates a new SendCore instance

func (*PacketBuilder) BuildCameraData

func (sc *PacketBuilder) BuildCameraData(rgbData []byte) [][]byte

BuildCameraData prepares camera/image data for BLE transmission

func (*PacketBuilder) Payload

func (sc *PacketBuilder) Payload(dataType int, data []byte, totalData []byte, option int, totalLength int, bright int) []byte

Payload creates a payload packet for the given parameters This is the direct translation of the payload method in Java

func (*PacketBuilder) PayloadDefault

func (sc *PacketBuilder) PayloadDefault(dataType int, data []byte, totalData []byte, option int, totalLength int, bright int, mask int) []byte

PayloadDefault provides default parameter handling for Payload function This is equivalent to the payload$default method in Java

type Position

type Position struct {
	X, Y int
}

Position represents a pixel coordinate

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL