rpiws281x

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2024 License: MIT Imports: 9 Imported by: 0

README

rpiws281x GoDoc

Package for controlling WS281X LEDs on a Raspberry Pi using only plain GO.

About

Currently only working with PWM. PCM and SPI are planned to be added later on.

The code is inspired by github.com/jgarff/rpi_ws281x.

Currently only PWM is implemented!

Installation

go get github.com/DerLukas15/rpiws281x

Usage

For details please see GoDoc.

This should be the path to follow:

  • create a Config for desired driver (PWM, PCM, SPI) (only PWM at the moment)
  • create your own object for LED definition (interface LEDs) or use included LEDStrip struct
  • set the strip in the Config
  • Render the Config

License

MIT, see LICENSE

Documentation

Overview

Package rpiws281x is used for controlling WS281x LEDs on the Raspberry Pi using plain GO.

Index

Constants

View Source
const (

	// 4 color R, G, B and W ordering
	SK6812StripRGBW StripType = 0x18100800
	SK6812StripRBGW           = 0x18100008
	SK6812StripGRBW           = 0x18081000
	SK6812StripGBRW           = 0x18080010
	SK6812StripBRGW           = 0x18001008
	SK6812StripBGRW           = 0x18000810

	// 3 color R, G and B ordering
	WS2811StripRGB StripType = 0x00100800
	WS2811StripRBG           = 0x00100008
	WS2811StripGRB           = 0x00081000
	WS2811StripGBR           = 0x00080010
	WS2811StripBRG           = 0x00001008
	WS2811StripBGR           = 0x00000810
)

Valid StripTypes

View Source
const (
	WS2812Strip  = WS2811StripGRB
	SK6812Strip  = WS2811StripGRB
	SK6812WStrip = SK6812StripGRBW
)

Predefined fixed LED types

Variables

View Source
var (
	ErrNoClockMap         = errors.New("clock device map not set. Not initialized?")
	ErrNoHardware         = errors.New("no hardware set. Not initialized?")
	ErrDriverAlreadyUsed  = errors.New("driver already initialized")
	ErrConfigInitialized  = errors.New("config already initialized")
	ErrConfigWrongIndex   = errors.New("wrong strip index")
	ErrDriverNotSupported = errors.New("driver not supported")
	ErrPinNotAllowed      = errors.New("selected pin not allowed")
	ErrNoActiveChannel    = errors.New("No active channel")
	ErrWrongFrequency     = errors.New("Wrong Frequency")
)

Errors

View Source
var Debug bool

Enable Debug output

View Source
var PWMAlwaysUseTwoChannel bool

Enable two channel mode for PWM no matter the configuration

Functions

This section is empty.

Types

type Config

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

Config is the main struct which holds all information.

A Config can only contain one driver type (PWM, PCM, or SPI). However you can define multiple configurations with different driver types. Be aware that only one Config per driver type can be initialized and used at a time.

There are only some methods possible once the Config has been initialized. Use method Stop to deinitialize the Config.

func New

func New(driverType DriverType) (*Config, error)

New returns a new Config for driverType.

Default Frequency: 800 kHz

Default DMAChannel: 10

func (*Config) Initialize

func (c *Config) Initialize() error

Initialize activates a Config. If another Config for the same driverType is already active, an error is returned

func (*Config) Render

func (c *Config) Render(stripIndex int) error

stripIndex -1 renders all stripes for that driver

func (*Config) SetBrightness

func (c *Config) SetBrightness(brightness uint32, stripIndex int) error

SetBrightness sets the output brightness to use for the strip with index stripIndex. Valid values are between 0 and 255. This method can be called once the Config is initialized.

func (*Config) SetDMAChannel

func (c *Config) SetDMAChannel(channel uint32) error

SetDMAChannel sets the DMAChannel to use. Default is 10.

If you want to use multiple Config, you can use the same DMAChannel IF you don't render the Config at the same time.

There are some DMAChannels used by the system. Please check only if you want to use another channel as 10.

func (*Config) SetFrequency

func (c *Config) SetFrequency(frequency uint32) error

SetFrequency sets the output frequency to use. Valid values are 400000 and 800000

func (*Config) SetStrip

func (c *Config) SetStrip(ledStrip LEDs, pin uint32, stripType StripType, stripIndex int, invertSignal bool) error

SetStrip adds LEDs to the Config.

*****

A word about channels:

A channel represents a physical signal output on the Raspberry Pi.

There are two simultaniously available channels when using PWM as a driver. Otherwise there is only one channel available.

*****

For PWM the stripIndex can be 0 or 1. All other drivers require a stripIndex of 0. The pin is checked if it is suitable for the driverType.

func (*Config) Stop

func (c *Config) Stop() error

Stop will dsable the Config so that another Config of same driverType can be initialized. Stopping is also needed if changing of fundamental settings is desired.

type DriverType

type DriverType uint8

DriverType defines the hardware type (PWM, PCM, SPI) which is used for communication

const (
	DriverPWM DriverType = 1 << iota
	DriverPCM
	DriverSPI
)

Valid DriverTypes

type LEDStrip

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

LEDStrip represent a physical continious strip of LEDs. Each LED is represented by a SingleLED.

func NewLEDStrip

func NewLEDStrip(count int) *LEDStrip

NewLEDStrip returns a LEDStrip with count LEDs. Colors can be set with the methods.

func (*LEDStrip) Blue

func (l *LEDStrip) Blue(position int) uint8

Blue returns the blue color amount at position.

func (*LEDStrip) Green

func (l *LEDStrip) Green(position int) uint8

Green returns the green color amount at position.

func (*LEDStrip) Red

func (l *LEDStrip) Red(position int) uint8

Red returns the red color amount at position.

func (*LEDStrip) SetColor

func (l *LEDStrip) SetColor(position int, c color.Color)

SetColor sets the color from color.Color at position.

func (*LEDStrip) SetDirect

func (l *LEDStrip) SetDirect(position int, val uint32)

SetDirect sets the color value for LED at position directly. Format 0xWWRRGGBB

func (*LEDStrip) SetRGBA

func (l *LEDStrip) SetRGBA(position int, r, g, b, a uint32)

SetRGBA sets the color value by r, g, b, and a values at position.

func (*LEDStrip) ShiftLeft

func (l *LEDStrip) ShiftLeft(shift int)

ShiftLeft shifts the LED colors by shift to the left. Everything leaving on the left wraps around. Use ShiftRight instead of negative shitfs.

func (*LEDStrip) ShiftRight

func (l *LEDStrip) ShiftRight(shift int)

ShiftRight shifts the LED colors by shift to the right. Everything leaving on the right wraps around. Use ShiftLeft instead of negative shitfs.

func (*LEDStrip) TotalCount

func (l *LEDStrip) TotalCount() int

TotalCount returns the number of LEDs in the strip.

func (*LEDStrip) UInt32

func (l *LEDStrip) UInt32(position int) uint32

UInt32 returns the color as uint32. Format 0xWWRRGGBB

func (*LEDStrip) White

func (l *LEDStrip) White(position int) uint8

White returns the white color amount at position.

type LEDs

type LEDs interface {
	Red(position int) uint8
	Green(position int) uint8
	Blue(position int) uint8
	White(position int) uint8
	UInt32(position int) uint32 //Format 0xWWRRGGBB
	TotalCount() int
}

LEDs can be a single LED or a slice depending on the implementation. Position defines the physical position on the LED strip starting at 0 to the total number of LEDs on that strip.

type SingleLED

type SingleLED uint32 //0xWWRRGGBB

SingleLED describes one LED with color values as 0xWWRRGGBB which can be used as an LEDs.

func ColorToSingleLED

func ColorToSingleLED(c color.Color) *SingleLED

ColorToSingleLED turns a color.Color to a SingleLED.

func RGBAtoSingleLED

func RGBAtoSingleLED(r, g, b, a uint32) *SingleLED

RGBAtoSingleLED turns rgba valued to a SingleLED.

func (*SingleLED) Blue

func (l *SingleLED) Blue(unused int) uint8

Blue returns the blue color amount.

func (*SingleLED) Green

func (l *SingleLED) Green(unused int) uint8

Green returns the green color amount.

func (*SingleLED) Red

func (l *SingleLED) Red(unused int) uint8

Red returns the red color amount.

func (*SingleLED) SetColor

func (l *SingleLED) SetColor(c color.Color)

SetColor sets the color from color.Color.

func (*SingleLED) SetDirect

func (l *SingleLED) SetDirect(val uint32)

SetDirect sets the color value directly. Format 0xWWRRGGBB

func (*SingleLED) SetRGBA

func (l *SingleLED) SetRGBA(r, g, b, a uint32)

SetRGBA sets the color value by r, g, b, and a values.

func (*SingleLED) ToColor

func (l *SingleLED) ToColor() color.Color

ToColor returns a color.Color object of the SingleLED.

func (*SingleLED) TotalCount

func (l *SingleLED) TotalCount() int

TotalCount returns the number of LEDs.

In this case this will always be 1.

func (*SingleLED) UInt32

func (l *SingleLED) UInt32(unused int) uint32

UInt32 returns the color as uint32. Format 0xWWRRGGBB

func (*SingleLED) White

func (l *SingleLED) White(unused int) uint8

White returns the white color amount.

type StripType

type StripType uint

StripType is the layout of the connected strip an can be different for each output signal.

Jump to

Keyboard shortcuts

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