fwmatrix

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2024 License: MIT Imports: 4 Imported by: 0

README

fwmatrix

GoDoc

fwmatrix is a library for drawing pixels on the Framework 16 LED Matrix module.

It provides simple libraries to easily allow programmers to control LEDs on the module and even perform animations, while provided low-level access to raw module commands for 100% custom interactions.

Table of Contents

Installation

To install, simply use the following go get command:

go get github.com/littlehawk93/fwmatrix
Features

fwmatrix provides tools for easily drawing 1-bit black and white images, 8bit greyscale images, displaying pre-programmed patterns, or sending raw commands to the LED matrix module. Additionally, there is a light framework for rendering simple animations smoothly.

Basic Commands

To send raw commands to the LED matrix module, you must first connect to the module via a serial port:

port, err := serial.OpenPort(&serial.Config{Name: "COM3", Baud: 115200})

Commands can then be sent using the WriteCommand function:

if err := fwmatrix.WriteCommand(port, fwmatrix.CmdDrawBW, paramBytes); err != nil {
    log.Fatal(err)
}

Some commands do not require additional bytes to be sent as parameters, in that case, simply send nil:

if err := fwmatrix.WriteCommand(port, fwmatrix.CmdDrawBW, nil); err != nil {
    log.Fatal(err)
}

Visit this page to view documentation on the various commands the LED module supports. Currently supported commands are also defined using the Command type.

Showing Patterns

The function ShowPattern provides an easy shortcut to writing raw pattern commands to the module. Simply send the pattern you want to display. Supported patterns are included as constants of the type Pattern:

if err := fwmatrix.ShowPattern(port, fwmatrix.PatZigZag, 0); err != nil {
    log.Fatal(err)
}

The third parameter is only used for the PatPercentage pattern, which displays a progress bar based on the value from 0 - 100.

The functions: GetSleepState, SetSleepState, GetBrightness, and SetBrightness are other shortcuts for sending simple commands for setting / retrieving the sleep or brightness status of the module without sending raw commands.

Matrix Renderers

fwmatrix uses matrix renderers for drawing graphics on an LED matrix module. The renderers handle all of the logic for setting LED values and sending various commands to the module in order to draw the graphics, making drawing shapes and various things easy.

There are some basic graphics tools for drawing lines and shapes that work with any MatrixRenderer:

fwmatrix.DrawLine(renderer, 0, 0, 34, 9) // draw a line

fwmatrix.DrawRect(renderer, 5, 0, 3, 4) // draw a rectangle

fwmatrix.DrawFillRect(renderer, 5, 0, 3, 4) // draw a filled rectangle
Black & White Rendering

A matrix renderer for simple 1 bit (Black & White) graphics. Use the global SetBrightness function to set uniform brightness for all LEDs, and then draw graphics with this renderer for higher performance (more FPS) graphics, with no brightness control.

Example:

port, err := serial.OpenPort(&serial.Config{Name: "COM3", Baud: 115200})

if err != nil {
    log.Fatal(err)
}

mat := fwmatrix.NewBWWithPort(port)

fwmatrix.SetBrightness(port, 100)

fwmatrix.DrawRect(mat, 0, 0, 5, 5)

if err = mat.Flush(); err != nil {
    log.Fatal(err)
}
Greyscale Rendering

A matrix renderer that supports brightness control for individual pixels. Use the matrix's SetBrightness function to set the current pixel brightness and each subsequent draw function will draw pixels at that brightness.

Example:

mat, err := fwmatrix.NewGS("COM3", 115200)

if err != nil {
    log.Fatal(err)
}

mat.SetBrightness(100)

fwmatrix.DrawRect(mat, 0, 0, 5, 5)

mat.SetBrightness(50)

fwmatrix.DrawLine(mat, 6, 5, 10, 5)

if err = mat.Flush(); err != nil {
    log.Fatal(err)
}
Animations

fwmatrix also provides a simple animation engine for easily updating graphics at a fixed frame rate. Simply call the StartAnimation function with a rendering callback function of type RenderFrameFunc. This function will be passed in the current matrix renderer, along with a delta of the time passed (in milliseconds) since the last frame was rendered. If the callback function returns an error value other than nil, the animation will stop. Return the error ErrorStopAnimation to stop animation without throwing an error. You do not need to call Clear or Flush inside the RenderFrameFunc.

Example:

boxWidth := 3
boxHeight := 3

port, err := serial.OpenPort(&serial.Config{Name: "COM3", Baud: 115200})

if err != nil {
    log.Fatal(err)
}

mat := fwmatrix.NewBWWithPort(port)

fwmatrix.SetBrightness(port, 100)

rng := rand.New(rand.NewSource(time.Now().UnixMicro()))
mat.Clear()

boxX := 3.0
boxY := 0.0

scale := 0.4

boxSpeedX := rng.Float64() * scale / float64(fwmatrix.FrameTime30FPS)
boxSpeedY := rng.Float64() * scale / float64(fwmatrix.FrameTime30FPS) * float64(fwmatrix.MatrixHeight) / float64(fwmatrix.MatrixWidth)

bounces := 0

err = fwmatrix.StartAnimation(func(m fwmatrix.MatrixRenderer, delta int64) error {

    boxX += boxSpeedX * float64(delta)
    boxY += boxSpeedY * float64(delta)

    if boxX < 0 {
        boxX = 0
        boxSpeedX = -boxSpeedX
        bounces++
    } else if boxX+float64(boxWidth) > float64(fwmatrix.MatrixWidth-1) {
        boxX = float64(fwmatrix.MatrixWidth - boxWidth - 1)
        boxSpeedX = -boxSpeedX
        bounces++
    }

    if boxY < 0 {
        boxY = 0
        boxSpeedY = -boxSpeedY
        bounces++
    } else if boxY+float64(boxHeight) > float64(fwmatrix.MatrixHeight-1) {
        boxY = float64(fwmatrix.MatrixHeight) - float64(boxHeight) - 1
        boxSpeedY = -boxSpeedY
        bounces++
    }

    x := int(math.Round(boxX))
    y := int(math.Round(boxY))

    fwmatrix.DrawFillRect(m, x, y, x+boxWidth, y+boxHeight)
    if bounces >= 20 {
        return fwmatrix.ErrorStopAnimation
    }
    return nil
}, mat, fwmatrix.FrameTime30FPS)

if err != nil {
    log.Fatal(err)
}
Custom Matrix Renderers

You can implement the MatrixRenderer interface to have your own custom rendering engine that will work with the various graphical and animation tools in fwmatrix. To do so, implement the following methods:

Close - close the underlying serial port used by the matrix renderer Clear - set all LEDs to off in the matrix module Flush - commit and send LED changes to the matrix module using any number of commands via serial SetPixel - set an individual LED in the matrix module on

Feedback

Thanks for checking out fwmatrix! For any feedback or suggestions on how to improve the library, please create an issue in the GitHub project.

Documentation

Index

Constants

View Source
const (
	// FrameTime60FPS frame time for 60 fps animation
	FrameTime60FPS int64 = 17

	// FrameTime30FPS frame time for 30 fps animation
	FrameTime30FPS int64 = 33

	// FrameTime24FPS frame time for 24 fps animation
	FrameTime24FPS int64 = 42
)
View Source
const (

	// MatrixWidth the width (in pixels) of the LED Matrix module
	MatrixWidth int = 9

	// MatrixHeight the height (in pixels) of the LED Matrix module
	MatrixHeight int = 34
)

Variables

View Source
var ErrorStopAnimation error = errors.New("fwmatrix - stop animation")

ErrorStopAnimation error used to signal to the

Functions

func DrawFillRect

func DrawFillRect(r MatrixRenderer, x0, y0, x1, y1 int)

DrawFillRect draws a filled rectangle with the provided matrix with opposite corners at x0,y0 and x1,y1

func DrawLine

func DrawLine(r MatrixRenderer, x0, y0, x1, y1 int)

DrawLine draws a line with the provided matrix renderer from a point to another

func DrawRect

func DrawRect(r MatrixRenderer, x0, y0, x1, y1 int)

DrawRect draws an empty rectangle with the provided matrix with opposite corners at x0,y0 and x1,y1

func GetAnimateState

func GetAnimateState(p *serial.Port) (bool, error)

GetAnimateState returns whether or not the LED matrix module is in the middle of an animation Returns true if in an animation, false otherwise. Returns any errors encountered during serial communications

func GetSleepState

func GetSleepState(p *serial.Port) (bool, error)

GetSleepState returns whether or not the LED matrix module is in sleep mode. Returns true if in sleep mode, false otherwise. Returns any errors encountered during serial communications

func Panic

func Panic(p *serial.Port) error

Panic cause a FW panic. Returns any errors encountered during serial communications

func SetAnimationEnabled

func SetAnimationEnabled(p *serial.Port, enable bool) error

SetAnimation if enabled, scrolls through various pre-programmed animations until disabled. Returns any errors encountered during serial communications

func SetBrightness

func SetBrightness(p *serial.Port, brightness uint8) error

SetBrightness sets the global brightness for all pixels on the LED Matrix module. Brightness values range from 0 - 255. Returns any errors encountered during serial communications

func SetSleepState

func SetSleepState(p *serial.Port, sleep bool) error

SetSleepState if sleep is true, sets the LED Matrix module to sleep until turned off (set to false) Returns any errors encountered during serial communications

func ShowPattern

func ShowPattern(p *serial.Port, pat Pattern, per uint8) error

ShowPattern displays a pre-programmed pattern on a LED matrix module via the provided serial port. The PatPercentage pattern requires the per parameter to set the percentage to display. Returns any errors encountered during serial communications

func StartAnimation

func StartAnimation(render RenderFrameFunc, m MatrixRenderer, frameTime int64) error

StartAnimation begins an animation on the provided MatrixRenderer using the provided rendering function. Frame time defines how many milliseconds should pass between rendering each frame. Stops whenever the returned error of the render function is not null. If the returned error is ErrorStopAnimation, this function will return nil. Otherwise, the error is returned

func WriteCommand

func WriteCommand(p *serial.Port, c Command, params []byte) error

WriteCommand send a command to a LED matrix module via the provided serial port. Set params to nil if no parameters are needed. Returns any errors encountered during serial communications

Types

type BWMatrix

type BWMatrix struct {
	// contains filtered or unexported fields
}

BWMatrix a tool for drawing basic 1bit black & white pixel data on a LED Matrix module

func NewBW

func NewBW(names string, baud int) (*BWMatrix, error)

NewBW opens a serial port with the provided name and baud rate and initializes a new BWMatrix with it. Returns any errors that occurred when opening the serial port

func NewBWWithPort

func NewBWWithPort(port *serial.Port) *BWMatrix

NewBWWithPort create and initialize a new BWMatrix using the provided serial port

func (*BWMatrix) Clear

func (me *BWMatrix) Clear()

Clear sets all pixels to off

func (*BWMatrix) Close

func (me *BWMatrix) Close() error

Close closes the underlying serial port used by this BWMatrix

func (*BWMatrix) Flush

func (me *BWMatrix) Flush() error

Flush writes the current stored pixel buffer to the LED Matrix module to display. Returns any errors encountered during serial communications

func (*BWMatrix) SetPixel

func (me *BWMatrix) SetPixel(x, y int)

SetPixel turns a pixel at the provided coordinate on

type Command

type Command uint8
const (
	// CmdBrightness command to set LED brightness
	CmdBrightness Command = 0x00

	// CmdPattern command to display a pre-programmed pattern
	CmdPattern Command = 0x01

	// CmdBootloader command to jump to the bootloader
	CmdBootloader Command = 0x02

	// CmdBootloader command to get or set the sleep state
	CmdSleep Command = 0x03

	// CmdAnimate command to get or set an animation
	CmdAnimate Command = 0x04

	// CmdPanic command to cause a FW panic
	CmdPanic Command = 0x05

	// CmdDrawBW command to draw a 1bit B&W image
	CmdDrawBW Command = 0x06

	// CmdStageCol command to set a greyscale column of pixels
	CmdStageCol Command = 0x07

	// CmdFlushCols command to display greyscale columns
	CmdFlushCols Command = 0x08
)

type GSMatrix

type GSMatrix struct {
	// contains filtered or unexported fields
}

GSMatrix a tool for drawing basic 8bit greyscale pixel data on a LED Matrix module

func NewGS

func NewGS(names string, baud int) (*GSMatrix, error)

NewGS opens a serial port with the provided name and baud rate and initializes a new GSMatrix with it. Returns any errors that occurred when opening the serial port

func NewGSWithPort

func NewGSWithPort(port *serial.Port) *GSMatrix

NewGSWithPort create and initialize a new GSMatrix using the provided serial port

func (*GSMatrix) Clear

func (me *GSMatrix) Clear()

Clear sets all pixels to zero

func (*GSMatrix) Close

func (me *GSMatrix) Close() error

Close closes the underlying serial port used by this BWMatrix

func (*GSMatrix) Flush

func (me *GSMatrix) Flush() error

Flush writes the current stored pixel buffer to the LED Matrix module to display. Returns any errors encountered during serial communications

func (*GSMatrix) GetBrightness

func (me *GSMatrix) GetBrightness() uint8

GetBrightness get the current pixel rendering brightness

func (*GSMatrix) SetBrightness

func (me *GSMatrix) SetBrightness(brightness uint8)

SetBrightness set the current pixel rendering brightness

func (*GSMatrix) SetPixel

func (me *GSMatrix) SetPixel(x, y int)

SetPixel sets the pixel at the provided coordinate to the current brightness

type MatrixRenderer

type MatrixRenderer interface {
	Close() error
	Clear()
	Flush() error
	SetPixel(x, y int)
}

MatrixRenderer renders pixels on the Framework 16 LED Matrix Module

type Pattern

type Pattern uint8
const (
	// PatPercentage displays a progress indicator using a provided percetange
	PatPercentage Pattern = 0x00

	// PatGradient displays a brightness gradient from top to bottom
	PatGradient Pattern = 0x01

	// PatDoubleGradient displays a brightness gradient from the middle to both top and bottom
	PatDoubleGradient Pattern = 0x02

	// PatLotusHor displays the text "LOTUS" horizontally across the matrix
	PatLotusHor Pattern = 0x03

	// PatZigZag displays a zigzag pattern
	PatZigZag Pattern = 0x04

	// PatFullBrightness displays all LEDs at 100% brightness
	PatFullBrightness Pattern = 0x05

	// PatPanic displays the text "PANIC" across the matrix
	PatPanic Pattern = 0x06

	// PatLotusVert displays the text "LOTUS" vertically across the matrix
	PatLotusVert Pattern = 0x07
)

type RenderFrameFunc

type RenderFrameFunc func(m MatrixRenderer, delta int64) error

RenderFrameFunc function for rendering a single frame of an animation. Delta is the time (in milliseconds) since the last frame render call was made Should return any errors encountered during rendering. Do not call r.Flush() inside the this function. Return ErrorStopAnimation to halt animation without throwing an error

Jump to

Keyboard shortcuts

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