commit d7f1b478607aae01818a6ee31db113793776a042 Author: JayChen Date: Thu Apr 16 17:05:10 2020 +0800 add ws2812 controller diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..0eda161 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module rpi-ci-led + +go 1.14 + +require github.com/jgarff/rpi_ws281x v0.0.0-20200319211106-6a720cbd42d3 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..81571e2 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/jgarff/rpi_ws281x v0.0.0-20200319211106-6a720cbd42d3 h1:Gmtr3J666u+wiChtQ49FYzcdd6UgX0aWoLciRPo3His= +github.com/jgarff/rpi_ws281x v0.0.0-20200319211106-6a720cbd42d3/go.mod h1:xbXlgWZjA66nkwNqkT4ol2EqY7jL8v+1efK5ZnOT/MU= diff --git a/main.go b/main.go new file mode 100644 index 0000000..0a592e7 --- /dev/null +++ b/main.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" + "os" + "os/signal" + "rpi-ci-led/pkg/ws2812b" + "syscall" + + "github.com/joho/godotenv" +) + +func main() { + fmt.Println("GO ci hook") + + godotenv.Load() + + ws2812b.Init(18, 20, 255) + + lock := make(chan os.Signal) + signal.Notify(lock, syscall.SIGINT, syscall.SIGTERM) + + // main + + <-lock + + ws2812b.Close() +} diff --git a/pkg/config/config.go b/pkg/config/config.go new file mode 100644 index 0000000..3fb7032 --- /dev/null +++ b/pkg/config/config.go @@ -0,0 +1,16 @@ +package config + +type Server struct { + Port int `yaml:"port" env:"SERVER_PORT"` +} + +type Config struct { + Server Server `yaml:"server"` +} + +var c *Config + +func Load(p ...string) error { + + return nil +} diff --git a/pkg/ws2812b/ws2812b.go b/pkg/ws2812b/ws2812b.go new file mode 100644 index 0000000..20e7f87 --- /dev/null +++ b/pkg/ws2812b/ws2812b.go @@ -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 +}