rpi-vsts-ci-led/pkg/ws2812b/ws2812b.go
2020-04-17 16:41:48 +08:00

77 lines
1.0 KiB
Go

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 {
led = nil
return nil, err
}
led = &LED{
GPIO: gpio,
Count: count,
Brightness: brightness,
IsInit: true,
}
return led, nil
}
func IsInit() bool {
if led == nil {
return false
}
return led.IsInit
}
func Close() {
ws2811.Clear()
ws2811.Fini()
led.IsInit = false
}
func ClearAll() {
ws2811.Clear()
}
func Len() int {
if led == nil {
return 0
}
return led.Count
}
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
}