rpi-vsts-ci-led/pkg/ws2812b/ws2812b.go

61 lines
856 B
Go
Raw Normal View History

2020-04-16 09:05:10 +00:00
package ws2812b
import (
"errors"
"github.com/jgarff/rpi_ws281x/golang/ws2811"
)
type LED struct {
GPIO int
Count int
Brightness int
IsInit bool
}
var led *LED
func Init(gpio, count, brightness int) (*LED, error) {
if led != nil && led.IsInit == true {
ws2811.Clear()
ws2811.Fini()
}
err := ws2811.Init(gpio, count, brightness)
if err != nil {
return nil, err
}
led = &LED{
GPIO: gpio,
Count: count,
Brightness: brightness,
IsInit: true,
}
return led, nil
}
func Close() {
ws2811.Clear()
ws2811.Fini()
}
func ClearAll() {
ws2811.Clear()
}
func WriteColor(pos int, color uint32) error {
if pos < 0 || pos > led.Count {
return errors.New("position out of range")
}
ws2811.SetLed(pos, color)
if err := ws2811.Render(); err != nil {
ws2811.Clear()
return err
}
return nil
}