feat(brake): display brake power with 4 colors

This commit is contained in:
Cyrille Nofficial 2023-05-11 20:02:19 +02:00
parent 8b67d8a434
commit f4f83af3c5
3 changed files with 22 additions and 5 deletions

View File

@ -21,6 +21,7 @@ func init() {
var ( var (
ColorBlack = Color{0, 0, 0} ColorBlack = Color{0, 0, 0}
ColorRed = Color{255, 0, 0} ColorRed = Color{255, 0, 0}
ColorPurple = Color{255, 0, 255}
ColorYellow = Color{255, 255, 0} ColorYellow = Color{255, 255, 0}
ColorGreen = Color{0, 255, 0} ColorGreen = Color{0, 255, 0}
ColorBlue = Color{0, 0, 255} ColorBlue = Color{0, 0, 255}
@ -145,6 +146,7 @@ func (l *PiColorLed) blink(freq float64) {
var setLed = func(v int, led gpio.PinIO, mutex *sync.Mutex) { var setLed = func(v int, led gpio.PinIO, mutex *sync.Mutex) {
mutex.Lock() mutex.Lock()
defer mutex.Unlock() defer mutex.Unlock()
lvl := gpio.High lvl := gpio.High
if v == 0 { if v == 0 {
lvl = gpio.Low lvl = gpio.Low

View File

@ -161,7 +161,17 @@ func (p *LedPart) updateColor() {
defer p.muThrottle.Unlock() defer p.muThrottle.Unlock()
if p.throttle <= -0.05 { if p.throttle <= -0.05 {
p.led.SetColor(led.Color{Red: int(p.throttle * -255)}) col := led.ColorWhite
if p.throttle <= -0.25 {
col = led.ColorYellow
if p.throttle <= -0.5 {
col = led.ColorRed
if p.throttle <= -0.75 {
col = led.ColorPurple
}
}
}
p.led.SetColor(col)
return return
} }

View File

@ -138,17 +138,22 @@ func TestLedPart_OnThrottle(t *testing.T) {
{ {
"slow brake", "slow brake",
testtools.NewFakeMessageFromProtobuf("throttle", &events.ThrottleMessage{Throttle: -0.06}), testtools.NewFakeMessageFromProtobuf("throttle", &events.ThrottleMessage{Throttle: -0.06}),
led.Color{Red: 15}, led.ColorWhite,
}, },
{ {
"normal brake", "normal brake",
testtools.NewFakeMessageFromProtobuf("throttle", &events.ThrottleMessage{Throttle: -0.5}), testtools.NewFakeMessageFromProtobuf("throttle", &events.ThrottleMessage{Throttle: -0.4}),
led.Color{Red: 127}, led.ColorYellow,
},
{
"normal high brake",
testtools.NewFakeMessageFromProtobuf("throttle", &events.ThrottleMessage{Throttle: -0.6}),
led.ColorRed,
}, },
{ {
"high brake", "high brake",
testtools.NewFakeMessageFromProtobuf("throttle", &events.ThrottleMessage{Throttle: -1.}), testtools.NewFakeMessageFromProtobuf("throttle", &events.ThrottleMessage{Throttle: -1.}),
led.ColorRed, led.ColorPurple,
}, },
} }