add ws2812 controller
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user