refactor: led color management

This commit is contained in:
Cyrille Nofficial 2021-12-29 18:16:33 +01:00
parent bb032651f5
commit ea15e13def
3 changed files with 64 additions and 83 deletions

View File

@ -18,29 +18,39 @@ func init() {
} }
var (
ColorBlack = Color{0, 0, 0}
ColorRed = Color{255, 0, 0}
ColorGreen = Color{0, 255, 0}
ColorBlue = Color{0, 0, 255}
)
func New() *PiColorLed { func New() *PiColorLed {
led := PiColorLed{ led := PiColorLed{
pinRed: rpi.P1_16, pinRed: rpi.P1_16,
pinGreen: rpi.P1_18, pinGreen: rpi.P1_18,
pinBlue: rpi.P1_22, pinBlue: rpi.P1_22,
redValue: 0, currentColor: ColorBlack,
greenValue: 0,
blueValue: 0,
cancelBlinkChan: make(chan interface{}), cancelBlinkChan: make(chan interface{}),
blinkEnabled: false, blinkEnabled: false,
} }
return &led return &led
} }
type Color struct {
Red int
Green int
Blue int
}
type Led interface { type Led interface {
SetBlink(freq float64) SetBlink(freq float64)
} }
type ColoredLed interface { type ColoredLed interface {
Led Led
SetRed(value int) SetColor(color Color)
SetGreen(value int)
SetBlue(value int)
} }
type PiColorLed struct { type PiColorLed struct {
@ -49,8 +59,8 @@ type PiColorLed struct {
pinGreen gpio.PinIO pinGreen gpio.PinIO
pinBlue gpio.PinIO pinBlue gpio.PinIO
muColorValue sync.RWMutex muColorValue sync.RWMutex
redValue, greenValue, blueValue int currentColor Color
cancelBlinkChan chan interface{} cancelBlinkChan chan interface{}
@ -58,6 +68,22 @@ type PiColorLed struct {
blinkEnabled bool blinkEnabled bool
} }
func (l *PiColorLed) SetColor(color Color) {
l.muColorValue.Lock()
defer l.muColorValue.Unlock()
setLed(color.Red, l.pinRed, &l.muPinRed)
setLed(color.Green, l.pinGreen, &l.muPinGreen)
setLed(color.Blue, l.pinBlue, &l.muPinBlue)
}
func (l *PiColorLed) off() {
l.muColorValue.Lock()
defer l.muColorValue.Unlock()
setLed(0, l.pinRed, &l.muPinRed)
setLed(0, l.pinGreen, &l.muPinGreen)
setLed(0, l.pinBlue, &l.muPinBlue)
}
func (l *PiColorLed) SetBlink(freq float64) { func (l *PiColorLed) SetBlink(freq float64) {
l.muBlink.Lock() l.muBlink.Lock()
defer l.muBlink.Unlock() defer l.muBlink.Unlock()
@ -75,53 +101,26 @@ func (l *PiColorLed) SetBlink(freq float64) {
} }
func (l *PiColorLed) blink(freq float64) { func (l *PiColorLed) blink(freq float64) {
factor := 0
ticker := time.NewTicker(time.Duration(float64(time.Second) / freq)) ticker := time.NewTicker(time.Duration(float64(time.Second) / freq))
red := l.Red()
green := l.Green()
blue := l.Blue()
var tmpR, tmpG, tmpB int
for { for {
select { select {
case <-ticker.C: case <-ticker.C:
case <-l.cancelBlinkChan: case <-l.cancelBlinkChan:
// Restore values // Restore values
l.SetRed(red) l.SetColor(l.Color())
l.SetGreen(green)
l.SetBlue(blue)
return return
} }
l.off()
tmpR = l.Red() select {
tmpG = l.Green() case <-ticker.C:
tmpB = l.Blue() case <-l.cancelBlinkChan:
if factor == 1 { // Restore values
// Led is off l.SetColor(l.Color())
if tmpR > 0 { return
red = tmpR
}
if tmpG > 0 {
green = tmpG
}
if tmpB > 0 {
blue = tmpB
}
} else {
// Led on: get updated value
red = tmpR
green = tmpG
blue = tmpB
}
zap.S().Debugf("factor: %v", factor)
l.SetRed(red * factor)
l.SetGreen(green * factor)
l.SetBlue(blue * factor)
if factor == 0 {
factor = 1
} else {
factor = 0
} }
l.SetColor(l.Color())
} }
} }
@ -141,35 +140,23 @@ var setLed = func(v int, led gpio.PinIO, mutex *sync.Mutex) {
func (l *PiColorLed) Red() int { func (l *PiColorLed) Red() int {
l.muColorValue.RLock() l.muColorValue.RLock()
defer l.muColorValue.RUnlock() defer l.muColorValue.RUnlock()
return l.redValue return l.currentColor.Red
}
func (l *PiColorLed) SetRed(v int) {
setLed(v, l.pinRed, &l.muPinRed)
l.muColorValue.Lock()
defer l.muColorValue.Unlock()
l.redValue = v
} }
func (l *PiColorLed) Green() int { func (l *PiColorLed) Green() int {
l.muColorValue.RLock() l.muColorValue.RLock()
defer l.muColorValue.RUnlock() defer l.muColorValue.RUnlock()
return l.greenValue return l.currentColor.Green
}
func (l *PiColorLed) SetGreen(v int) {
setLed(v, l.pinGreen, &l.muPinGreen)
l.muColorValue.Lock()
defer l.muColorValue.Unlock()
l.greenValue = v
} }
func (l *PiColorLed) Blue() int { func (l *PiColorLed) Blue() int {
l.muColorValue.RLock() l.muColorValue.RLock()
defer l.muColorValue.RUnlock() defer l.muColorValue.RUnlock()
return l.blueValue return l.currentColor.Blue
} }
func (l *PiColorLed) SetBlue(v int) {
setLed(v, l.pinBlue, &l.muPinBlue) func (l *PiColorLed) Color() Color {
l.muColorValue.Lock() l.muColorValue.RLock()
defer l.muColorValue.Unlock() defer l.muColorValue.RUnlock()
l.blueValue = v return l.currentColor
} }

View File

@ -26,7 +26,7 @@ func TestColorLed_Red(t *testing.T) {
t.Errorf("colorValue: %v, wants %v", colorValue, 0) t.Errorf("colorValue: %v, wants %v", colorValue, 0)
} }
l.SetRed(255) l.SetColor(ColorRed)
if l.Red() != 255 { if l.Red() != 255 {
t.Errorf("%T.Red(): %v, wants %v", l, l.Red(), 255) t.Errorf("%T.Red(): %v, wants %v", l, l.Red(), 255)
} }
@ -54,7 +54,7 @@ func TestColorLed_Green(t *testing.T) {
t.Errorf("colorValue: %v, wants %v", colorValue, 0) t.Errorf("colorValue: %v, wants %v", colorValue, 0)
} }
l.SetGreen(255) l.SetColor(ColorGreen)
if l.Green() != 255 { if l.Green() != 255 {
t.Errorf("%T.Green(): %v, wants %v", l, l.Green(), 255) t.Errorf("%T.Green(): %v, wants %v", l, l.Green(), 255)
} }
@ -82,7 +82,7 @@ func TestColorLed_Blue(t *testing.T) {
t.Errorf("colorValue: %v, wants %v", colorValue, 0) t.Errorf("colorValue: %v, wants %v", colorValue, 0)
} }
l.SetBlue(255) l.SetColor(ColorBlue)
if l.Blue() != 255 { if l.Blue() != 255 {
t.Errorf("%T.Blue(): %v, wants %v", l, l.Blue(), 255) t.Errorf("%T.Blue(): %v, wants %v", l, l.Blue(), 255)
} }
@ -111,7 +111,7 @@ func TestColorLed_SetBlink(t *testing.T) {
} }
l := New() l := New()
l.SetBlue(255) l.SetColor(ColorBlue)
v := readValue() v := readValue()
if v != 255 { if v != 255 {
t.Errorf("colorValue: %v, wants %v", v, 255) t.Errorf("colorValue: %v, wants %v", v, 255)
@ -171,14 +171,14 @@ func TestColorLed_SetBlinkAndUpdadeColor(t *testing.T) {
} }
l := New() l := New()
l.SetBlue(255) l.SetColor(ColorBlue)
l.SetBlink(100) l.SetBlink(100)
v := readValue() v := readValue()
if v != 255 { if v != 255 {
t.Errorf("colorValue: %v, wants %v", v, 255) t.Errorf("colorValue: %v, wants %v", v, 255)
} }
time.Sleep(6 * time.Millisecond) time.Sleep(6 * time.Millisecond)
l.SetBlue(128) l.SetColor(ColorBlue)
v = readValue() v = readValue()
if v != 128 { if v != 128 {

View File

@ -27,8 +27,8 @@ func NewPart(client mqtt.Client, driveModeTopic, recordTopic string) *LedPart {
} }
type LedPart struct { type LedPart struct {
led led.ColoredLed led led.ColoredLed
client mqtt.Client client mqtt.Client
onDriveModeTopic string onDriveModeTopic string
onRecordTopic string onRecordTopic string
@ -49,9 +49,7 @@ func (p *LedPart) Start() error {
func (p *LedPart) Stop() { func (p *LedPart) Stop() {
defer p.led.SetBlink(0) defer p.led.SetBlink(0)
defer p.led.SetGreen(0) defer p.led.SetColor(led.ColorBlack)
defer p.led.SetBlue(0)
defer p.led.SetRed(0)
service.StopService("led", p.client, p.onDriveModeTopic, p.onRecordTopic) service.StopService("led", p.client, p.onDriveModeTopic, p.onRecordTopic)
} }
@ -64,13 +62,9 @@ func (p *LedPart) onDriveMode(_ mqtt.Client, message mqtt.Message) {
} }
switch driveModeMessage.GetDriveMode() { switch driveModeMessage.GetDriveMode() {
case events.DriveMode_USER: case events.DriveMode_USER:
p.led.SetRed(0) p.led.SetColor(led.ColorGreen)
p.led.SetGreen(255)
p.led.SetBlue(0)
case events.DriveMode_PILOT: case events.DriveMode_PILOT:
p.led.SetRed(0) p.led.SetColor(led.ColorBlue)
p.led.SetGreen(0)
p.led.SetBlue(255)
} }
} }
@ -88,7 +82,7 @@ func (p *LedPart) onRecord(client mqtt.Client, message mqtt.Message) {
return return
} }
p.recordEnabled = switchRecord.GetEnabled() p.recordEnabled = switchRecord.GetEnabled()
if switchRecord.GetEnabled() { if switchRecord.GetEnabled() {
zap.S().Info("record mode enabled") zap.S().Info("record mode enabled")
p.led.SetBlink(2) p.led.SetBlink(2)