refactor: led color management
This commit is contained in:
parent
bb032651f5
commit
ea15e13def
115
pkg/led/led.go
115
pkg/led/led.go
@ -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 {
|
||||
led := PiColorLed{
|
||||
pinRed: rpi.P1_16,
|
||||
pinGreen: rpi.P1_18,
|
||||
pinBlue: rpi.P1_22,
|
||||
redValue: 0,
|
||||
greenValue: 0,
|
||||
blueValue: 0,
|
||||
currentColor: ColorBlack,
|
||||
cancelBlinkChan: make(chan interface{}),
|
||||
blinkEnabled: false,
|
||||
}
|
||||
|
||||
return &led
|
||||
}
|
||||
|
||||
type Color struct {
|
||||
Red int
|
||||
Green int
|
||||
Blue int
|
||||
}
|
||||
|
||||
type Led interface {
|
||||
SetBlink(freq float64)
|
||||
}
|
||||
|
||||
type ColoredLed interface {
|
||||
Led
|
||||
SetRed(value int)
|
||||
SetGreen(value int)
|
||||
SetBlue(value int)
|
||||
SetColor(color Color)
|
||||
}
|
||||
|
||||
type PiColorLed struct {
|
||||
@ -50,7 +60,7 @@ type PiColorLed struct {
|
||||
pinBlue gpio.PinIO
|
||||
|
||||
muColorValue sync.RWMutex
|
||||
redValue, greenValue, blueValue int
|
||||
currentColor Color
|
||||
|
||||
cancelBlinkChan chan interface{}
|
||||
|
||||
@ -58,6 +68,22 @@ type PiColorLed struct {
|
||||
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) {
|
||||
l.muBlink.Lock()
|
||||
defer l.muBlink.Unlock()
|
||||
@ -75,53 +101,26 @@ func (l *PiColorLed) SetBlink(freq float64) {
|
||||
}
|
||||
|
||||
func (l *PiColorLed) blink(freq float64) {
|
||||
factor := 0
|
||||
ticker := time.NewTicker(time.Duration(float64(time.Second) / freq))
|
||||
red := l.Red()
|
||||
green := l.Green()
|
||||
blue := l.Blue()
|
||||
var tmpR, tmpG, tmpB int
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
case <-l.cancelBlinkChan:
|
||||
// Restore values
|
||||
l.SetRed(red)
|
||||
l.SetGreen(green)
|
||||
l.SetBlue(blue)
|
||||
l.SetColor(l.Color())
|
||||
return
|
||||
}
|
||||
l.off()
|
||||
|
||||
tmpR = l.Red()
|
||||
tmpG = l.Green()
|
||||
tmpB = l.Blue()
|
||||
if factor == 1 {
|
||||
// Led is off
|
||||
if tmpR > 0 {
|
||||
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
|
||||
select {
|
||||
case <-ticker.C:
|
||||
case <-l.cancelBlinkChan:
|
||||
// Restore values
|
||||
l.SetColor(l.Color())
|
||||
return
|
||||
}
|
||||
l.SetColor(l.Color())
|
||||
}
|
||||
}
|
||||
|
||||
@ -141,35 +140,23 @@ var setLed = func(v int, led gpio.PinIO, mutex *sync.Mutex) {
|
||||
func (l *PiColorLed) Red() int {
|
||||
l.muColorValue.RLock()
|
||||
defer l.muColorValue.RUnlock()
|
||||
return l.redValue
|
||||
}
|
||||
func (l *PiColorLed) SetRed(v int) {
|
||||
setLed(v, l.pinRed, &l.muPinRed)
|
||||
l.muColorValue.Lock()
|
||||
defer l.muColorValue.Unlock()
|
||||
l.redValue = v
|
||||
return l.currentColor.Red
|
||||
}
|
||||
|
||||
func (l *PiColorLed) Green() int {
|
||||
l.muColorValue.RLock()
|
||||
defer l.muColorValue.RUnlock()
|
||||
return l.greenValue
|
||||
}
|
||||
func (l *PiColorLed) SetGreen(v int) {
|
||||
setLed(v, l.pinGreen, &l.muPinGreen)
|
||||
l.muColorValue.Lock()
|
||||
defer l.muColorValue.Unlock()
|
||||
l.greenValue = v
|
||||
return l.currentColor.Green
|
||||
}
|
||||
|
||||
func (l *PiColorLed) Blue() int {
|
||||
l.muColorValue.RLock()
|
||||
defer l.muColorValue.RUnlock()
|
||||
return l.blueValue
|
||||
return l.currentColor.Blue
|
||||
}
|
||||
func (l *PiColorLed) SetBlue(v int) {
|
||||
setLed(v, l.pinBlue, &l.muPinBlue)
|
||||
l.muColorValue.Lock()
|
||||
defer l.muColorValue.Unlock()
|
||||
l.blueValue = v
|
||||
|
||||
func (l *PiColorLed) Color() Color {
|
||||
l.muColorValue.RLock()
|
||||
defer l.muColorValue.RUnlock()
|
||||
return l.currentColor
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ func TestColorLed_Red(t *testing.T) {
|
||||
t.Errorf("colorValue: %v, wants %v", colorValue, 0)
|
||||
}
|
||||
|
||||
l.SetRed(255)
|
||||
l.SetColor(ColorRed)
|
||||
if 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)
|
||||
}
|
||||
|
||||
l.SetGreen(255)
|
||||
l.SetColor(ColorGreen)
|
||||
if 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)
|
||||
}
|
||||
|
||||
l.SetBlue(255)
|
||||
l.SetColor(ColorBlue)
|
||||
if 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.SetBlue(255)
|
||||
l.SetColor(ColorBlue)
|
||||
v := readValue()
|
||||
if v != 255 {
|
||||
t.Errorf("colorValue: %v, wants %v", v, 255)
|
||||
@ -171,14 +171,14 @@ func TestColorLed_SetBlinkAndUpdadeColor(t *testing.T) {
|
||||
}
|
||||
|
||||
l := New()
|
||||
l.SetBlue(255)
|
||||
l.SetColor(ColorBlue)
|
||||
l.SetBlink(100)
|
||||
v := readValue()
|
||||
if v != 255 {
|
||||
t.Errorf("colorValue: %v, wants %v", v, 255)
|
||||
}
|
||||
time.Sleep(6 * time.Millisecond)
|
||||
l.SetBlue(128)
|
||||
l.SetColor(ColorBlue)
|
||||
|
||||
v = readValue()
|
||||
if v != 128 {
|
||||
|
@ -49,9 +49,7 @@ func (p *LedPart) Start() error {
|
||||
|
||||
func (p *LedPart) Stop() {
|
||||
defer p.led.SetBlink(0)
|
||||
defer p.led.SetGreen(0)
|
||||
defer p.led.SetBlue(0)
|
||||
defer p.led.SetRed(0)
|
||||
defer p.led.SetColor(led.ColorBlack)
|
||||
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() {
|
||||
case events.DriveMode_USER:
|
||||
p.led.SetRed(0)
|
||||
p.led.SetGreen(255)
|
||||
p.led.SetBlue(0)
|
||||
p.led.SetColor(led.ColorGreen)
|
||||
case events.DriveMode_PILOT:
|
||||
p.led.SetRed(0)
|
||||
p.led.SetGreen(0)
|
||||
p.led.SetBlue(255)
|
||||
p.led.SetColor(led.ColorBlue)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user