rgbmatrix

package
v0.0.80 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2022 License: GPL-3.0 Imports: 16 Imported by: 0

README

rgbmatrix-rpi GoDoc Build Status

Go binding for rpi-rgb-led-matrix an excellent C++ library to control RGB LED displays with Raspberry Pi GPIO.

This library includes the basic bindings to control de LED Matrix directly and also a convenient ToolKit with more high level functions. Also some examples are included to test the library and the configuration.

The Canvas struct implements the image.Image interface from the Go standard library. This makes the interaction with the matrix simple as work with a normal image in Go, allowing the usage of any Go library build around the image.Image interface.

To learn about the configuration and the wiring go to the original library, is highly detailed and well explained.

Installation

The recommended way to install rgbmatrix-rpi is:

go get github.com/fcjr/rgbmatrix-rpi

Then you will get an expected error like this:

# github.com/fcjr/rgbmatrix-rpi
/usr/bin/ld: cannot find -lrgbmatrix
collect2: error: ld returned 1 exit status

This happens because you need to compile the rgbmatrix C bindings:

cd $GOPATH/src/github.com/fcjr/rgbmatrix-rpi/lib/rpi-rgb-led-matrix/
git submodule update --init
make
cd $GOPATH/src/github.com/fcjr/rgbmatrix-rpi/
go install -v ./...

Examples

Setting all the pixels to white:

// create a new Matrix instance with the DefaultConfig & DefaultRuntimeOptions
m, _ := rgbmatrix.NewRGBLedMatrix(&rgbmatrix.DefaultConfig, &rgbmatrix.DefaultRuntimeOptions)

// create the Canvas, implements the image.Image interface
c := rgbmatrix.NewCanvas(m)
defer c.Close() // don't forgot close the Matrix, if not your leds will remain on
 
// using the standard draw.Draw function we copy a white image onto the Canvas
draw.Draw(c, c.Bounds(), &image.Uniform{color.White}, image.Point{}, draw.Src)

// don't forget call Render to display the new led status
c.Render()

Playing a GIF into your matrix during 30 seconds:

// create a new Matrix instance with the DefaultConfig
m, _ := rgbmatrix.NewRGBLedMatrix(&rgbmatrix.DefaultConfig, &rgbmatrix.DefaultRuntimeOptions)

// create a ToolKit instance
tk := rgbmatrix.NewToolKit(m)
defer tk.Close() // don't forgot close the Matrix, if not your leds will remain on

// open the gif file for reading
file, _ := os.Open("mario.gif")

// play of the gif using the io.Reader
close, _ := tk.PlayGIF(f)
fatal(err)

// we wait 30 seconds and then we stop the playing gif sending a True to the returned chan
time.Sleep(time.Second * 30)
close <- true

The image of the header was recorded using this few lines, the running Mario gif, and three 32x64 pannels.

Check the folder examples folder for more examples

Matrix Emulation

As part of the library an small Matrix emulator is provided. The emulator renderize a virtual RGB matrix on a window in your desktop, without needing a real RGB matrix connected to your computer.

To execute the emulator set the MATRIX_EMULATOR environment variable to 1, then when NewRGBLedMatrix is used, a emulator.Emulator is returned instead of a interface the real board.

License

MIT, see LICENSE

Documentation

Index

Constants

View Source
const MatrixEmulatorENV = "MATRIX_EMULATOR"

Variables

View Source
var DefaultConfig = HardwareConfig{
	Rows:              32,
	Cols:              32,
	ChainLength:       1,
	Parallel:          1,
	PWMBits:           11,
	PWMLSBNanoseconds: 130,
	Brightness:        100,
	ScanMode:          Progressive,
}

DefaultConfig default WS281x configuration

View Source
var DefaultRuntimeOptions = RuntimeOptions{
	GPIOSlowdown:   0,
	Daemon:         0,
	DropPrivileges: 1,
	DoGPIOInit:     true,
}

DefaultRuntimeOptions default WS281x runtime options

View Source
var (
	DefaultScrollDelay = 50 * time.Millisecond
)

Functions

This section is empty.

Types

type Canvas

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

Canvas is a image.Image representation of a WS281x matrix, it implements image.Image interface and can be used with draw.Draw for example

func NewCanvas

func NewCanvas(m Matrix) *Canvas

NewCanvas returns a new Canvas using the given width and height and creates a new WS281x matrix using the given config

func (*Canvas) AlwaysRender added in v0.0.29

func (c *Canvas) AlwaysRender() bool

func (*Canvas) At

func (c *Canvas) At(x, y int) color.Color

At returns the color of the pixel at (x, y)

func (*Canvas) Bounds

func (c *Canvas) Bounds() image.Rectangle

Bounds return the topology of the Canvas

func (*Canvas) Clear

func (c *Canvas) Clear() error

Clear set all the leds on the matrix with color.Black

func (*Canvas) Close

func (c *Canvas) Close() error

Close clears the matrix and close the matrix

func (*Canvas) ColorModel

func (c *Canvas) ColorModel() color.Model

ColorModel returns the canvas' color model, always color.RGBAModel

func (*Canvas) Disable added in v0.0.8

func (c *Canvas) Disable()

Disable ...

func (*Canvas) Enable added in v0.0.8

func (c *Canvas) Enable()

Enable ...

func (*Canvas) Enabled added in v0.0.8

func (c *Canvas) Enabled() bool

Enabled ...

func (*Canvas) GetHTTPHandlers added in v0.0.8

func (c *Canvas) GetHTTPHandlers() ([]*board.HTTPHandler, error)

GetHTTPHandlers ...

func (*Canvas) GetWidth added in v0.0.72

func (c *Canvas) GetWidth() int

GetWidth ...

func (*Canvas) Name added in v0.0.8

func (c *Canvas) Name() string

func (*Canvas) PaddedBounds added in v0.0.29

func (c *Canvas) PaddedBounds() image.Rectangle

func (*Canvas) Render

func (c *Canvas) Render(ctx context.Context) error

Render update the display with the data from the LED buffer

func (*Canvas) Scrollable added in v0.0.29

func (c *Canvas) Scrollable() bool

Scrollable ...

func (*Canvas) Set

func (c *Canvas) Set(x, y int, color color.Color)

Set set LED at position x,y to the provided 24-bit color value

func (*Canvas) SetWidth added in v0.0.71

func (c *Canvas) SetWidth(x int)

SetWidth ...

type ConsoleMatrix added in v0.0.6

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

ConsoleMatrix prints a representation of a matrix to a terminal. Useful for testing layouts without a Pi or an LED matrix.

func NewConsoleMatrix added in v0.0.6

func NewConsoleMatrix(width int, height int, out io.Writer, logger *zap.Logger) *ConsoleMatrix

NewConsoleMatrix ...

func (*ConsoleMatrix) Apply added in v0.0.6

func (c *ConsoleMatrix) Apply(leds []color.Color) error

Apply ...

func (*ConsoleMatrix) At added in v0.0.6

func (c *ConsoleMatrix) At(position int) color.Color

At ...

func (*ConsoleMatrix) Close added in v0.0.6

func (c *ConsoleMatrix) Close() error

Close ...

func (*ConsoleMatrix) Geometry added in v0.0.6

func (c *ConsoleMatrix) Geometry() (int, int)

Geometry ...

func (*ConsoleMatrix) Render added in v0.0.6

func (c *ConsoleMatrix) Render() error

Render ...

func (*ConsoleMatrix) Reset added in v0.0.6

func (c *ConsoleMatrix) Reset()

Reset ...

func (*ConsoleMatrix) Set added in v0.0.6

func (c *ConsoleMatrix) Set(position int, clr color.Color)

Set ...

func (*ConsoleMatrix) SetBrightness added in v0.0.6

func (c *ConsoleMatrix) SetBrightness(brightness int)

SetBrightness does nothing

type HardwareConfig

type HardwareConfig struct {
	// Rows the number of rows supported by the display, so 32 or 16.
	Rows int `json:"rows"`
	// Cols the number of columns supported by the display, so 32 or 64 .
	Cols int `json:"cols"`
	// ChainLengthis the number of displays daisy-chained together
	// (output of one connected to input of next).
	ChainLength int `json:"chainLength"`
	// Parallel is the number of parallel chains connected to the Pi; in old Pis
	// with 26 GPIO pins, that is 1, in newer Pis with 40 interfaces pins, that
	// can also be 2 or 3. The effective number of pixels in vertical direction is
	// then thus rows * parallel.
	Parallel int `json:"parallel"`
	// Set PWM bits used for output. Default is 11, but if you only deal with
	// limited comic-colors, 1 might be sufficient. Lower require less CPU and
	// increases refresh-rate.
	PWMBits int `json:"pwmBits"`

	// The lower bits can be time-dithered for higher refresh rate.
	PWMDitherBits int `json:"pwmDitherBits"`

	// Change the base time-unit for the on-time in the lowest significant bit in
	// nanoseconds.  Higher numbers provide better quality (more accurate color,
	// less ghosting), but have a negative impact on the frame rate.
	PWMLSBNanoseconds int `json:"pwmlsbNanoseconds"` // the DMA channel to use
	// Brightness is the initial brightness of the panel in percent. Valid range
	// is 1..100
	Brightness int `json:"brightness"`
	// ScanMode progressive or interlaced
	ScanMode ScanMode `json:"scanMode"` // strip color layout
	// Disable the PWM hardware subsystem to create pulses. Typically, you don't
	// want to disable hardware pulsing, this is mostly for debugging and figuring
	// out if there is interference with the sound system.
	// This won't do anything if output enable is not connected to GPIO 18 in
	// non-standard wirings.
	DisableHardwarePulsing bool `json:"disableHardwarePulsing"`

	ShowRefreshRate bool   `json:"showRefreshRate"`
	InverseColors   bool   `json:"inverseColors"`
	LedRGBSequence  string `json:"ledRgbSequence"`

	// Name of GPIO mapping used
	HardwareMapping string `json:"hardwareMapping"`

	// Limit refresh rate of LED panel. This will help on a loaded system
	// to keep a constant refresh rate. <= 0 for no limit.
	LimitRefreshRateHz int `json:"limitRefreshRateHz"`

	// Type of multiplexing. 0 = direct, 1 = stripe, 2 = checker,...
	Multiplexing int `json:"multiplexing"`

	// A string describing a sequence of pixel mappers that should be applied
	// to this matrix. A semicolon-separated list of pixel-mappers with optional
	// parameter. See https://github.com/hzeller/rpi-rgb-led-matrix#panel-arrangement
	PixelMapperConfig string `json:"pixelMapperConfig"`

	// PanelType. Defaults to "". See https://github.com/hzeller/rpi-rgb-led-matrix#types-of-displays
	PanelType string `json:"panelType"`

	// RowAddr Type Adressing of rows; in particular panels with only AB address lines might indicate that this is needed.
	// See https://github.com/hzeller/rpi-rgb-led-matrix#types-of-displays for more info
	RowAddrType int `json:"rowAddrType"`
}

HardwareConfig rgb-led-matrix configuration

type Matrix

type Matrix interface {
	Geometry() (width, height int)
	At(position int) color.Color
	Set(position int, c color.Color)
	Apply([]color.Color) error
	Render() error
	Close() error
	SetBrightness(brightness int)
}

Matrix is an interface that represent any RGB matrix, very useful for testing

func NewRGBLedMatrix

func NewRGBLedMatrix(config *HardwareConfig, rtOptions *RuntimeOptions) (c Matrix, err error)

NewRGBLedMatrix returns a new matrix using the given size and config

type RGBLedMatrix

type RGBLedMatrix struct {
	Config         *HardwareConfig
	RuntimeOptions *RuntimeOptions

	sync.Mutex
	// contains filtered or unexported fields
}

RGBLedMatrix matrix representation for ws281x

func (*RGBLedMatrix) Apply

func (c *RGBLedMatrix) Apply(leds []color.Color) error

Apply set all the pixels to the values contained in leds

func (*RGBLedMatrix) At

func (c *RGBLedMatrix) At(position int) color.Color

At return an Color which allows access to the LED display data as if it were a sequence of 24-bit RGB values.

func (*RGBLedMatrix) Close

func (c *RGBLedMatrix) Close() error

Close finalizes the ws281x interface

func (*RGBLedMatrix) Geometry

func (c *RGBLedMatrix) Geometry() (width, height int)

Geometry returns the width and the height of the matrix

func (*RGBLedMatrix) Initialize

func (c *RGBLedMatrix) Initialize() error

Initialize initialize library, must be called once before other functions are called.

func (*RGBLedMatrix) Render

func (c *RGBLedMatrix) Render() error

Render update the display with the data from the LED buffer

func (*RGBLedMatrix) Set

func (c *RGBLedMatrix) Set(position int, color color.Color)

Set set LED at position x,y to the provided 24-bit color value.

func (*RGBLedMatrix) SetBrightness

func (c *RGBLedMatrix) SetBrightness(brightness int)

type RuntimeOptions

type RuntimeOptions struct {
	// 0 = no slowdown. (Available 0...4)
	GPIOSlowdown int `json:"gpioSlowdown"`

	// Thre are three possible values here
	//   -1 : don't leave choise of becoming daemon to the command line parsing.
	//        If set to -1, the --led-daemon option is not offered.
	//    0 : do not becoma a daemon, run in forgreound (default value)
	//    1 : become a daemon, run in background.
	//
	// If daemon is disabled (= -1), the user has to call
	// RGBMatrix::StartRefresh() manually once the matrix is created, to leave
	// the decision to become a daemon
	// after the call (which requires that no threads have been started yet).
	// In the other cases (off or on), the choice is already made, so the thread
	// is conveniently already started for you.
	// -1 disabled. 0=off, 1=on.
	Daemon int `json:"daemon"`

	// Drop privileges from 'root' to 'daemon' once the hardware is initialized.
	// This is usually a good idea unless you need to stay on elevated privs.
	DropPrivileges int `json:"dropPrivileges"`

	// By default, the gpio is initialized for you, but if you run on a platform
	// not the Raspberry Pi, this will fail. If you don't need to access GPIO
	// e.g. you want to just create a stream output (see content-streamer.h),
	// set this to false.
	DoGPIOInit bool `json:"doGPIOInit"`
}

type ScanMode

type ScanMode int8
const (
	Progressive ScanMode = 0
	Interlaced  ScanMode = 1
)

type ScrollCanvas added in v0.0.29

type ScrollCanvas struct {
	Matrix Matrix
	// contains filtered or unexported fields
}

func NewScrollCanvas added in v0.0.29

func NewScrollCanvas(m Matrix, logger *zap.Logger, opts ...ScrollCanvasOption) (*ScrollCanvas, error)

func (*ScrollCanvas) AddCanvas added in v0.0.36

func (c *ScrollCanvas) AddCanvas(add draw.Image)

func (*ScrollCanvas) AlwaysRender added in v0.0.29

func (c *ScrollCanvas) AlwaysRender() bool

func (*ScrollCanvas) At added in v0.0.29

func (c *ScrollCanvas) At(x, y int) color.Color

At returns the color of the pixel at (x, y)

func (*ScrollCanvas) Bounds added in v0.0.29

func (c *ScrollCanvas) Bounds() image.Rectangle

Bounds return the topology of the Canvas

func (*ScrollCanvas) Clear added in v0.0.29

func (c *ScrollCanvas) Clear() error

Clear set all the leds on the matrix with color.Black

func (*ScrollCanvas) Close added in v0.0.29

func (c *ScrollCanvas) Close() error

Close clears the matrix and close the matrix

func (*ScrollCanvas) ColorModel added in v0.0.29

func (c *ScrollCanvas) ColorModel() color.Model

ColorModel returns the canvas' color model, always color.RGBAModel

func (*ScrollCanvas) Disable added in v0.0.29

func (c *ScrollCanvas) Disable()

Disable ...

func (*ScrollCanvas) Enable added in v0.0.29

func (c *ScrollCanvas) Enable()

Enable ...

func (*ScrollCanvas) Enabled added in v0.0.29

func (c *ScrollCanvas) Enabled() bool

Enabled ...

func (*ScrollCanvas) GetHTTPHandlers added in v0.0.29

func (c *ScrollCanvas) GetHTTPHandlers() ([]*board.HTTPHandler, error)

GetHTTPHandlers ...

func (*ScrollCanvas) GetPadding added in v0.0.30

func (c *ScrollCanvas) GetPadding() int

GetPadding

func (*ScrollCanvas) GetScrollDirection added in v0.0.30

func (c *ScrollCanvas) GetScrollDirection() ScrollDirection

GetScrollDirection ...

func (*ScrollCanvas) GetScrollSpeed added in v0.0.30

func (c *ScrollCanvas) GetScrollSpeed() time.Duration

GetScrollSpeed ...

func (*ScrollCanvas) GetWidth added in v0.0.72

func (c *ScrollCanvas) GetWidth() int

func (*ScrollCanvas) Merge added in v0.0.36

func (c *ScrollCanvas) Merge(padding int)

func (*ScrollCanvas) Name added in v0.0.29

func (c *ScrollCanvas) Name() string

func (*ScrollCanvas) Render added in v0.0.29

func (c *ScrollCanvas) Render(ctx context.Context) error

Render update the display with the data from the LED buffer

func (*ScrollCanvas) Scrollable added in v0.0.29

func (c *ScrollCanvas) Scrollable() bool

func (*ScrollCanvas) Set added in v0.0.29

func (c *ScrollCanvas) Set(x, y int, color color.Color)

Set set LED at position x,y to the provided 24-bit color value

func (*ScrollCanvas) SetPadding added in v0.0.30

func (c *ScrollCanvas) SetPadding(pad int)

SetPadding ...

func (*ScrollCanvas) SetScrollDirection added in v0.0.30

func (c *ScrollCanvas) SetScrollDirection(d ScrollDirection)

SetScrollDirection ...

func (*ScrollCanvas) SetScrollSpeed added in v0.0.29

func (c *ScrollCanvas) SetScrollSpeed(d time.Duration)

SetScrollSpeed ...

func (*ScrollCanvas) SetWidth added in v0.0.36

func (c *ScrollCanvas) SetWidth(w int)

func (*ScrollCanvas) Width added in v0.0.36

func (c *ScrollCanvas) Width() int

type ScrollCanvasOption added in v0.0.29

type ScrollCanvasOption func(*ScrollCanvas) error

func WithScrollSpeed added in v0.0.29

func WithScrollSpeed(d time.Duration) ScrollCanvasOption

WithScrollSpeed ...

type ScrollDirection added in v0.0.30

type ScrollDirection int

ScrollDirection represents the direction the canvas scrolls

const (
	// RightToLeft ...
	RightToLeft ScrollDirection = iota
	// LeftToRight ...
	LeftToRight
	// BottomToTop ...
	BottomToTop
	// TopToBottom ...
	TopToBottom
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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