robocar-led/pkg/led/led.go

179 lines
3.3 KiB
Go
Raw Normal View History

2019-12-14 10:56:22 +00:00
package led
import (
2021-10-12 17:00:46 +00:00
"go.uber.org/zap"
2021-09-01 19:41:28 +00:00
"periph.io/x/conn/v3/gpio"
"periph.io/x/host/v3"
2021-09-11 18:52:44 +00:00
"periph.io/x/host/v3/rpi"
2019-12-14 10:56:22 +00:00
"sync"
"time"
)
func init() {
2021-10-12 17:00:46 +00:00
zap.S().Info("init pin")
2019-12-14 10:56:22 +00:00
// Load all the drivers:
if _, err := host.Init(); err != nil {
2021-10-12 17:00:46 +00:00
zap.S().Fatalf("unable to init host driver: %v", err)
2019-12-14 10:56:22 +00:00
}
}
2021-12-29 17:16:33 +00:00
var (
ColorBlack = Color{0, 0, 0}
ColorRed = Color{255, 0, 0}
ColorGreen = Color{0, 255, 0}
ColorBlue = Color{0, 0, 255}
)
2019-12-14 10:56:22 +00:00
func New() *PiColorLed {
2019-12-17 23:28:49 +00:00
led := PiColorLed{
2021-09-11 18:52:44 +00:00
pinRed: rpi.P1_16,
pinGreen: rpi.P1_18,
pinBlue: rpi.P1_22,
2021-12-29 17:16:33 +00:00
currentColor: ColorBlack,
2019-12-14 10:56:22 +00:00
cancelBlinkChan: make(chan interface{}),
blinkEnabled: false,
}
2021-12-29 17:16:33 +00:00
2019-12-17 23:28:49 +00:00
return &led
2019-12-14 10:56:22 +00:00
}
2021-12-29 17:16:33 +00:00
type Color struct {
Red int
Green int
Blue int
}
2019-12-14 10:56:22 +00:00
type Led interface {
SetBlink(freq float64)
}
type ColoredLed interface {
Led
2021-12-29 17:16:33 +00:00
SetColor(color Color)
2019-12-14 10:56:22 +00:00
}
type PiColorLed struct {
muPinRed, muPinGreen, muPinBlue sync.Mutex
pinRed gpio.PinIO
pinGreen gpio.PinIO
pinBlue gpio.PinIO
2021-12-29 17:16:33 +00:00
muColorValue sync.RWMutex
currentColor Color
2019-12-14 10:56:22 +00:00
cancelBlinkChan chan interface{}
muBlink sync.Mutex
blinkEnabled bool
}
2021-12-29 17:16:33 +00:00
func (l *PiColorLed) SetColor(color Color) {
l.muColorValue.Lock()
defer l.muColorValue.Unlock()
if color == l.currentColor {
return
}
l.currentColor = color
2021-12-29 17:16:33 +00:00
setLed(color.Red, l.pinRed, &l.muPinRed)
setLed(color.Green, l.pinGreen, &l.muPinGreen)
setLed(color.Blue, l.pinBlue, &l.muPinBlue)
}
func (l *PiColorLed) on() {
l.muColorValue.RLock()
defer l.muColorValue.RUnlock()
setLed(l.currentColor.Red, l.pinRed, &l.muPinRed)
setLed(l.currentColor.Green, l.pinGreen, &l.muPinGreen)
setLed(l.currentColor.Blue, l.pinBlue, &l.muPinBlue)
}
2021-12-29 17:16:33 +00:00
func (l *PiColorLed) off() {
l.muColorValue.RLock()
defer l.muColorValue.RUnlock()
2021-12-29 17:16:33 +00:00
setLed(0, l.pinRed, &l.muPinRed)
setLed(0, l.pinGreen, &l.muPinGreen)
setLed(0, l.pinBlue, &l.muPinBlue)
}
2019-12-14 10:56:22 +00:00
func (l *PiColorLed) SetBlink(freq float64) {
l.muBlink.Lock()
defer l.muBlink.Unlock()
if freq > 0 {
if !l.blinkEnabled {
l.blinkEnabled = true
go l.blink(freq)
}
} else {
if l.blinkEnabled {
l.blinkEnabled = false
l.cancelBlinkChan <- struct{}{}
}
}
}
func (l *PiColorLed) blink(freq float64) {
log := zap.S().With("func", "blink")
2019-12-14 10:56:22 +00:00
ticker := time.NewTicker(time.Duration(float64(time.Second) / freq))
2021-12-29 17:16:33 +00:00
// Restore values
defer l.on()
2019-12-14 10:56:22 +00:00
for {
select {
case <-ticker.C:
case <-l.cancelBlinkChan:
return
}
log.Debugf("off with color %v", ColorBlack)
2021-12-29 17:16:33 +00:00
l.off()
2019-12-14 10:56:22 +00:00
2021-12-29 17:16:33 +00:00
select {
case <-ticker.C:
case <-l.cancelBlinkChan:
return
2019-12-14 10:56:22 +00:00
}
log.Debugf("on with color %v", l.Color())
l.on()
2019-12-14 10:56:22 +00:00
}
2019-12-14 10:56:22 +00:00
}
var setLed = func(v int, led gpio.PinIO, mutex *sync.Mutex) {
mutex.Lock()
defer mutex.Unlock()
lvl := gpio.High
if v == 0 {
lvl = gpio.Low
}
err := led.Out(lvl)
if err != nil {
2021-10-12 17:00:46 +00:00
zap.S().Errorf("unable to sed pin to %v: %v", lvl, err)
2019-12-14 10:56:22 +00:00
}
}
func (l *PiColorLed) Red() int {
l.muColorValue.RLock()
defer l.muColorValue.RUnlock()
2021-12-29 17:16:33 +00:00
return l.currentColor.Red
2019-12-14 10:56:22 +00:00
}
func (l *PiColorLed) Green() int {
l.muColorValue.RLock()
defer l.muColorValue.RUnlock()
2021-12-29 17:16:33 +00:00
return l.currentColor.Green
2019-12-14 10:56:22 +00:00
}
func (l *PiColorLed) Blue() int {
l.muColorValue.RLock()
defer l.muColorValue.RUnlock()
2021-12-29 17:16:33 +00:00
return l.currentColor.Blue
2019-12-14 10:56:22 +00:00
}
2021-12-29 17:16:33 +00:00
func (l *PiColorLed) Color() Color {
l.muColorValue.RLock()
defer l.muColorValue.RUnlock()
return l.currentColor
2019-12-14 10:56:22 +00:00
}