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 {
|
2020-04-17 08:41:48 +00:00
|
|
|
led = nil
|
2020-04-16 09:05:10 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
led = &LED{
|
|
|
|
GPIO: gpio,
|
|
|
|
Count: count,
|
|
|
|
Brightness: brightness,
|
|
|
|
IsInit: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
return led, nil
|
|
|
|
}
|
|
|
|
|
2020-04-17 08:41:48 +00:00
|
|
|
func IsInit() bool {
|
|
|
|
if led == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return led.IsInit
|
|
|
|
}
|
|
|
|
|
2020-04-16 09:05:10 +00:00
|
|
|
func Close() {
|
|
|
|
ws2811.Clear()
|
|
|
|
ws2811.Fini()
|
2020-04-17 08:41:48 +00:00
|
|
|
led.IsInit = false
|
2020-04-16 09:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ClearAll() {
|
|
|
|
ws2811.Clear()
|
|
|
|
}
|
|
|
|
|
2020-04-17 08:41:48 +00:00
|
|
|
func Len() int {
|
|
|
|
if led == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return led.Count
|
|
|
|
}
|
|
|
|
|
2020-04-16 09:05:10 +00:00
|
|
|
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
|
|
|
|
}
|