add ws2812 controller

This commit is contained in:
Jay 2020-04-16 17:05:10 +08:00
commit d7f1b47860
5 changed files with 111 additions and 0 deletions

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module rpi-ci-led
go 1.14
require github.com/jgarff/rpi_ws281x v0.0.0-20200319211106-6a720cbd42d3

2
go.sum Normal file
View File

@ -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=

28
main.go Normal file
View File

@ -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()
}

16
pkg/config/config.go Normal file
View File

@ -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
}

60
pkg/ws2812b/ws2812b.go Normal file
View File

@ -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
}