ws281x

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2022 License: MIT Imports: 6 Imported by: 0

README

go-rpi-ws281x GoDoc

golang binding for rpi_ws281x, userspace Raspberry Pi PWM library for WS281X LEDs. Supports any Raspberry and WS2812, SK6812RGB and SK6812RGBW LEDs strips, this includes Unicorn pHAT and NeoPixels

This is a Fork of github.com/mcuadros/go-rpi-ws281x.

Installation

The recommended way to install go-rpi-ws281x is to first install rpi_ws281x on your system. Please follow the instructions given in their README.md.

go-rpi-ws281x will look for the library in the system folders "/usr/local/lib" and "/usr/local/include/ws2811".

Now download this package and add it to your go.mod file with:

go get github.com/DerLukas15/go-rpi-ws281x
git clone

If you want to clone this repository instead of using go get you can also run make build in the main folder to download and compile rpi_ws281x locally. However you will then need to supply a replace statement in your go.mod file pointing to your local copy.

Optionally run make install-rpi_ws281x as root to install rpi_ws281x in the system folders.

Cross compilation for ARM

In order for valid library files for a ARM compilation, rpi_ws281x has to be build with additional arguments. go-rpi_ws281x includes special CGO lines for arm. For this to work the rpi_ws281x library has to be named libws2811arm.a instead of libws2811.a.

Install the required C compiler arm-linux-gnueabihf-g++ and arm-linux-gnueabihf-gcc via your distribution package manager.

Don't forget to set CGO_ENABLED=1 GOOS=linux GOARCH=arm GOARM=5 CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ during the build of your application.

go get

When using go get build rpi_ws281x from source with cmake following their README.md. During the compilation replace the line:

cmake -D BUILD_TEST=OFF -D BUILD_SHARED=OFF .

with

cmake -D CMAKE_C_COMPILER=arm-linux-gnueabihf-gcc -D CMAKE_CXX_COMPILER=arm-linux-gnueabihf-g++ -D BUILD_TEST=OFF -D BUILD_SHARED=OFF .

and rename the library after make install:

mv /usr/local/lib/libws2811.a /usr/local/lib/libws2811arm.a

Be aware that the install will overwrite an existing libws2811.a file. So either copy the file by yourself instead of make install or build the other architecture afterwards. The header files do not have to be changed between architectures.

Everything else follows the normal way as shown above.

git clone

When cloning this repo you can run make build-arm in the main folder to create the ARM library. For an optional installation in the system folders run make install-rpi_ws281x-arm as root.

Everthing else follows the normal way as shown above.

Examples

// create a new canvas with the given width and height, and the config, in this
// case the configuration is for a Unicorn pHAT (8x4 pixels matrix) with the
// default configuration
c, _ := ws281x.NewCanvas(8, 4, &ws281x.DefaultConfig)


// initialize the canvas and the matrix
c.Initialize()

// since ws281x implements image.Image any function like draw.Draw from the std
// library may be used with it.
//
// now we copy a white image into the ws281x.Canvas, this turn on all the leds
// to white
draw.Draw(c, c.Bounds(), image.NewUniform(color.White), image.ZP, draw.Over)

// render and sleep to see the leds on
c.Render()
time.Sleep(time.Second * 5)

// don't forget close the canvas, if not you leds may remain on
c.Close()

Check the folder examples folder for more examples

License

MIT, see LICENSE

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultConfig = HardwareConfig{
	Pin:        18,
	Frequency:  800000,
	DMA:        10,
	Brightness: 30,
	StripType:  StripGRB,
}

DefaultConfig default WS281x configuration

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(w, h int, config *HardwareConfig) (*Canvas, error)

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

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) Initialize

func (c *Canvas) Initialize() error

Initialize initialize the matrix and the canvas

func (*Canvas) Render

func (c *Canvas) Render() error

Render update the display with the data from the LED buffer

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

type HardwareConfig

type HardwareConfig struct {
	Pin        int       // GPIO Pin with PWM alternate function, 0 if unused
	Frequency  int       // the frequency of the display signal in hertz, can go as low as 400000
	DMA        int       // the DMA channel to use
	Invert     bool      // specifying if the signal line should be inverted
	Channel    int       // PWM channel to use
	Brightness int       // brightness value between 0 and 255
	StripType  StripType // strip color layout
}

HardwareConfig WS281x configuration

type Matrix

type Matrix interface {
	Initialize() error
	At(position int) color.Color
	Set(position int, c color.Color)
	Render() error
	Close() error
}

func NewWS281x

func NewWS281x(size int, config *HardwareConfig) (Matrix, error)

NewWS281x returns a new matrix using the given size and config

type StripType

type StripType int

StripType layout

const (
	// 4 color R, G, B and W ordering
	StripRGBW StripType = 0x18100800
	StripRBGW StripType = 0x18100008
	StripGRBW StripType = 0x18081000
	StripGBRW StripType = 0x18080010
	StripBRGW StripType = 0x18001008
	StripBGRW StripType = 0x18000810

	// 3 color R, G and B ordering
	StripRGB StripType = 0x00100800
	StripRBG StripType = 0x00100008
	StripGRB StripType = 0x00081000
	StripGBR StripType = 0x00080010
	StripBRG StripType = 0x00001008
	StripBGR StripType = 0x00000810
)

type WS281x

type WS281x struct {
	Config *HardwareConfig
	// contains filtered or unexported fields
}

WS281x matrix representation for ws281x

func (*WS281x) At

func (c *WS281x) 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 (*WS281x) Close

func (c *WS281x) Close() error

Close finalizes the ws281x interface

func (*WS281x) Initialize

func (c *WS281x) Initialize() error

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

func (*WS281x) Render

func (c *WS281x) Render() error

Render update the display with the data from the LED buffer

func (*WS281x) Set

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

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

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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