refactor: move led and part modules to pkg
This commit is contained in:
175
pkg/led/led.go
Normal file
175
pkg/led/led.go
Normal file
@ -0,0 +1,175 @@
|
||||
package led
|
||||
|
||||
import (
|
||||
"go.uber.org/zap"
|
||||
"periph.io/x/conn/v3/gpio"
|
||||
"periph.io/x/host/v3"
|
||||
"periph.io/x/host/v3/rpi"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
zap.S().Info("init pin")
|
||||
// Load all the drivers:
|
||||
if _, err := host.Init(); err != nil {
|
||||
zap.S().Fatalf("unable to init host driver: %v", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func New() *PiColorLed {
|
||||
led := PiColorLed{
|
||||
pinRed: rpi.P1_16,
|
||||
pinGreen: rpi.P1_18,
|
||||
pinBlue: rpi.P1_22,
|
||||
redValue: 0,
|
||||
greenValue: 0,
|
||||
blueValue: 0,
|
||||
cancelBlinkChan: make(chan interface{}),
|
||||
blinkEnabled: false,
|
||||
}
|
||||
return &led
|
||||
}
|
||||
|
||||
type Led interface {
|
||||
SetBlink(freq float64)
|
||||
}
|
||||
|
||||
type ColoredLed interface {
|
||||
Led
|
||||
SetRed(value int)
|
||||
SetGreen(value int)
|
||||
SetBlue(value int)
|
||||
}
|
||||
|
||||
type PiColorLed struct {
|
||||
muPinRed, muPinGreen, muPinBlue sync.Mutex
|
||||
pinRed gpio.PinIO
|
||||
pinGreen gpio.PinIO
|
||||
pinBlue gpio.PinIO
|
||||
|
||||
muColorValue sync.RWMutex
|
||||
redValue, greenValue, blueValue int
|
||||
|
||||
cancelBlinkChan chan interface{}
|
||||
|
||||
muBlink sync.Mutex
|
||||
blinkEnabled bool
|
||||
}
|
||||
|
||||
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) {
|
||||
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)
|
||||
return
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
zap.S().Errorf("unable to sed pin to %v: %v", lvl, err)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (l *PiColorLed) Blue() int {
|
||||
l.muColorValue.RLock()
|
||||
defer l.muColorValue.RUnlock()
|
||||
return l.blueValue
|
||||
}
|
||||
func (l *PiColorLed) SetBlue(v int) {
|
||||
setLed(v, l.pinBlue, &l.muPinBlue)
|
||||
l.muColorValue.Lock()
|
||||
defer l.muColorValue.Unlock()
|
||||
l.blueValue = v
|
||||
}
|
214
pkg/led/led_test.go
Normal file
214
pkg/led/led_test.go
Normal file
@ -0,0 +1,214 @@
|
||||
package led
|
||||
|
||||
import (
|
||||
"periph.io/x/conn/v3/gpio"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestColorLed_Red(t *testing.T) {
|
||||
setLedBackup := setLed
|
||||
defer func() { setLed = setLedBackup }()
|
||||
|
||||
var colorValue int
|
||||
setLed = func(v int, led gpio.PinIO, mutex *sync.Mutex) {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
colorValue = v
|
||||
}
|
||||
|
||||
l := New()
|
||||
if l.Red() != 0 {
|
||||
t.Errorf("%T.Red(): %v, wants %v", l, l.Red(), 0)
|
||||
}
|
||||
if colorValue != 0 {
|
||||
t.Errorf("colorValue: %v, wants %v", colorValue, 0)
|
||||
}
|
||||
|
||||
l.SetRed(255)
|
||||
if l.Red() != 255 {
|
||||
t.Errorf("%T.Red(): %v, wants %v", l, l.Red(), 255)
|
||||
}
|
||||
if colorValue != 255 {
|
||||
t.Errorf("colorValue: %v, wants %v", colorValue, 255)
|
||||
}
|
||||
}
|
||||
|
||||
func TestColorLed_Green(t *testing.T) {
|
||||
setLedBackup := setLed
|
||||
defer func() { setLed = setLedBackup }()
|
||||
|
||||
var colorValue int
|
||||
setLed = func(v int, led gpio.PinIO, mutex *sync.Mutex) {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
colorValue = v
|
||||
}
|
||||
|
||||
l := New()
|
||||
if l.Green() != 0 {
|
||||
t.Errorf("%T.Green(): %v, wants %v", l, l.Green(), 0)
|
||||
}
|
||||
if colorValue != 0 {
|
||||
t.Errorf("colorValue: %v, wants %v", colorValue, 0)
|
||||
}
|
||||
|
||||
l.SetGreen(255)
|
||||
if l.Green() != 255 {
|
||||
t.Errorf("%T.Green(): %v, wants %v", l, l.Green(), 255)
|
||||
}
|
||||
if colorValue != 255 {
|
||||
t.Errorf("colorValue: %v, wants %v", colorValue, 255)
|
||||
}
|
||||
}
|
||||
|
||||
func TestColorLed_Blue(t *testing.T) {
|
||||
setLedBackup := setLed
|
||||
defer func() { setLed = setLedBackup }()
|
||||
|
||||
var colorValue int
|
||||
setLed = func(v int, led gpio.PinIO, mutex *sync.Mutex) {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
colorValue = v
|
||||
}
|
||||
|
||||
l := New()
|
||||
if l.Blue() != 0 {
|
||||
t.Errorf("%T.Blue(): %v, wants %v", l, l.Blue(), 0)
|
||||
}
|
||||
if colorValue != 0 {
|
||||
t.Errorf("colorValue: %v, wants %v", colorValue, 0)
|
||||
}
|
||||
|
||||
l.SetBlue(255)
|
||||
if l.Blue() != 255 {
|
||||
t.Errorf("%T.Blue(): %v, wants %v", l, l.Blue(), 255)
|
||||
}
|
||||
if colorValue != 255 {
|
||||
t.Errorf("colorValue: %v, wants %v", colorValue, 255)
|
||||
}
|
||||
}
|
||||
|
||||
func TestColorLed_SetBlink(t *testing.T) {
|
||||
setLedBackup := setLed
|
||||
defer func() { setLed = setLedBackup }()
|
||||
|
||||
var muFakeValue sync.Mutex
|
||||
var colorValue int
|
||||
setLed = func(v int, led gpio.PinIO, mutex *sync.Mutex) {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
muFakeValue.Lock()
|
||||
defer muFakeValue.Unlock()
|
||||
colorValue = v
|
||||
}
|
||||
readValue := func() int {
|
||||
muFakeValue.Lock()
|
||||
defer muFakeValue.Unlock()
|
||||
return colorValue
|
||||
}
|
||||
|
||||
l := New()
|
||||
l.SetBlue(255)
|
||||
v := readValue()
|
||||
if v != 255 {
|
||||
t.Errorf("colorValue: %v, wants %v", v, 255)
|
||||
}
|
||||
l.SetBlink(100)
|
||||
v = readValue()
|
||||
if v != 255 {
|
||||
t.Errorf("colorValue: %v, wants %v", v, 255)
|
||||
}
|
||||
time.Sleep(12 * time.Millisecond)
|
||||
v = readValue()
|
||||
if v != 0 {
|
||||
t.Errorf("colorValue: %v, wants %v", v, 0)
|
||||
}
|
||||
time.Sleep(12 * time.Millisecond)
|
||||
v = readValue()
|
||||
if v != 255 {
|
||||
t.Errorf("colorValue: %v, wants %v", v, 255)
|
||||
}
|
||||
time.Sleep(12 * time.Millisecond)
|
||||
v = readValue()
|
||||
if v != 0 {
|
||||
t.Errorf("colorValue: %v, wants %v", v, 0)
|
||||
}
|
||||
|
||||
// Stop blink
|
||||
l.SetBlink(0)
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
v = readValue()
|
||||
if v != 255 {
|
||||
t.Errorf("colorValue: %v, wants %v", v, 255)
|
||||
}
|
||||
time.Sleep(12 * time.Millisecond)
|
||||
v = readValue()
|
||||
if v != 255 {
|
||||
t.Errorf("colorValue: %v, wants %v", v, 255)
|
||||
}
|
||||
}
|
||||
|
||||
func TestColorLed_SetBlinkAndUpdadeColor(t *testing.T) {
|
||||
setLedBackup := setLed
|
||||
defer func() { setLed = setLedBackup }()
|
||||
|
||||
var muFakeValue sync.Mutex
|
||||
var colorValue int
|
||||
setLed = func(v int, led gpio.PinIO, mutex *sync.Mutex) {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
muFakeValue.Lock()
|
||||
defer muFakeValue.Unlock()
|
||||
colorValue = v
|
||||
}
|
||||
readValue := func() int {
|
||||
muFakeValue.Lock()
|
||||
defer muFakeValue.Unlock()
|
||||
return colorValue
|
||||
}
|
||||
|
||||
l := New()
|
||||
l.SetBlue(255)
|
||||
l.SetBlink(100)
|
||||
v := readValue()
|
||||
if v != 255 {
|
||||
t.Errorf("colorValue: %v, wants %v", v, 255)
|
||||
}
|
||||
time.Sleep(6 * time.Millisecond)
|
||||
l.SetBlue(128)
|
||||
|
||||
v = readValue()
|
||||
if v != 128 {
|
||||
t.Errorf("colorValue: %v, wants %v", v, 128)
|
||||
}
|
||||
time.Sleep(6 * time.Millisecond)
|
||||
|
||||
time.Sleep(12 * time.Millisecond)
|
||||
|
||||
v = readValue()
|
||||
if v != 128 {
|
||||
t.Errorf("colorValue: %v, wants %v", v, 128)
|
||||
}
|
||||
time.Sleep(12 * time.Millisecond)
|
||||
|
||||
v = readValue()
|
||||
if v != 0 {
|
||||
t.Errorf("colorValue: %v, wants %v", v, 0)
|
||||
}
|
||||
|
||||
// Stop blink
|
||||
l.SetBlink(0)
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
v = readValue()
|
||||
if v != 128 {
|
||||
t.Errorf("colorValue: %v, wants %v", v, 128)
|
||||
}
|
||||
time.Sleep(12 * time.Millisecond)
|
||||
v = readValue()
|
||||
if v != 128 {
|
||||
t.Errorf("colorValue: %v, wants %v", v, 128)
|
||||
}
|
||||
}
|
112
pkg/part/part.go
Normal file
112
pkg/part/part.go
Normal file
@ -0,0 +1,112 @@
|
||||
package part
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/cyrilix/robocar-base/service"
|
||||
"github.com/cyrilix/robocar-led/pkg/led"
|
||||
"github.com/cyrilix/robocar-protobuf/go/events"
|
||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||
"github.com/golang/protobuf/proto"
|
||||
"go.uber.org/zap"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
func NewPart(client mqtt.Client, driveModeTopic, recordTopic string) *LedPart {
|
||||
return &LedPart{
|
||||
led: led.New(),
|
||||
client: client,
|
||||
onDriveModeTopic: driveModeTopic,
|
||||
onRecordTopic: recordTopic,
|
||||
muDriveMode: sync.Mutex{},
|
||||
m: events.DriveMode_INVALID,
|
||||
muRecord: sync.Mutex{},
|
||||
recordEnabled: false,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
type LedPart struct {
|
||||
led led.ColoredLed
|
||||
client mqtt.Client
|
||||
onDriveModeTopic string
|
||||
onRecordTopic string
|
||||
|
||||
muDriveMode sync.Mutex
|
||||
m events.DriveMode
|
||||
muRecord sync.Mutex
|
||||
recordEnabled bool
|
||||
}
|
||||
|
||||
func (p *LedPart) Start() error {
|
||||
if err := p.registerCallbacks(); err != nil {
|
||||
return fmt.Errorf("unable to start service: %v", err)
|
||||
}
|
||||
for {
|
||||
time.Sleep(1 * time.Hour)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *LedPart) Stop() {
|
||||
defer p.led.SetBlink(0)
|
||||
defer p.led.SetGreen(0)
|
||||
defer p.led.SetBlue(0)
|
||||
defer p.led.SetRed(0)
|
||||
service.StopService("led", p.client, p.onDriveModeTopic, p.onRecordTopic)
|
||||
}
|
||||
|
||||
func (p *LedPart) onDriveMode(_ mqtt.Client, message mqtt.Message) {
|
||||
var driveModeMessage events.DriveModeMessage
|
||||
err := proto.Unmarshal(message.Payload(), &driveModeMessage)
|
||||
if err != nil {
|
||||
zap.S().Errorf("unable to unmarshal %T message: %v", driveModeMessage, err)
|
||||
return
|
||||
}
|
||||
switch driveModeMessage.GetDriveMode() {
|
||||
case events.DriveMode_USER:
|
||||
p.led.SetRed(0)
|
||||
p.led.SetGreen(255)
|
||||
p.led.SetBlue(0)
|
||||
case events.DriveMode_PILOT:
|
||||
p.led.SetRed(0)
|
||||
p.led.SetGreen(0)
|
||||
p.led.SetBlue(255)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *LedPart) onRecord(client mqtt.Client, message mqtt.Message) {
|
||||
var switchRecord events.SwitchRecordMessage
|
||||
err := proto.Unmarshal(message.Payload(), &switchRecord)
|
||||
if err != nil {
|
||||
zap.S().Errorf("unable to unmarchal %T message: %v", switchRecord, err)
|
||||
return
|
||||
}
|
||||
|
||||
p.muRecord.Lock()
|
||||
defer p.muRecord.Unlock()
|
||||
if p.recordEnabled == switchRecord.GetEnabled() {
|
||||
return
|
||||
}
|
||||
p.recordEnabled = switchRecord.GetEnabled()
|
||||
|
||||
if switchRecord.GetEnabled() {
|
||||
zap.S().Info("record mode enabled")
|
||||
p.led.SetBlink(2)
|
||||
} else {
|
||||
zap.S().Info("record mode disabled")
|
||||
p.led.SetBlink(0)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *LedPart) registerCallbacks() error {
|
||||
err := service.RegisterCallback(p.client, p.onDriveModeTopic, p.onDriveMode)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = service.RegisterCallback(p.client, p.onRecordTopic, p.onRecord)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
104
pkg/part/part_test.go
Normal file
104
pkg/part/part_test.go
Normal file
@ -0,0 +1,104 @@
|
||||
package part
|
||||
|
||||
import (
|
||||
"github.com/cyrilix/robocar-base/testtools"
|
||||
"github.com/cyrilix/robocar-protobuf/go/events"
|
||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||
"github.com/golang/protobuf/proto"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type fakeLed struct {
|
||||
red, green, blue int
|
||||
blink bool
|
||||
}
|
||||
|
||||
func (f *fakeLed) SetBlink(freq float64) {
|
||||
if freq > 0 {
|
||||
f.blink = true
|
||||
} else {
|
||||
f.blink = false
|
||||
}
|
||||
}
|
||||
|
||||
func (f *fakeLed) SetRed(value int) {
|
||||
f.red = value
|
||||
}
|
||||
|
||||
func (f *fakeLed) SetGreen(value int) {
|
||||
f.green = value
|
||||
}
|
||||
|
||||
func (f *fakeLed) SetBlue(value int) {
|
||||
f.blue = value
|
||||
}
|
||||
|
||||
func TestLedPart_OnDriveMode(t *testing.T) {
|
||||
led := fakeLed{}
|
||||
p := LedPart{led: &led}
|
||||
|
||||
cases := []struct {
|
||||
msg mqtt.Message
|
||||
red, green, blue int
|
||||
}{
|
||||
{testtools.NewFakeMessageFromProtobuf("drive", &events.DriveModeMessage{DriveMode: events.DriveMode_USER}), 0, 255, 0},
|
||||
{testtools.NewFakeMessageFromProtobuf("drive", &events.DriveModeMessage{DriveMode: events.DriveMode_PILOT}), 0, 0, 255},
|
||||
{testtools.NewFakeMessageFromProtobuf("drive", &events.DriveModeMessage{DriveMode: events.DriveMode_INVALID}), 0, 0, 255},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
p.onDriveMode(nil, c.msg)
|
||||
time.Sleep(1 * time.Millisecond)
|
||||
var msg events.DriveModeMessage
|
||||
err := proto.Unmarshal(c.msg.Payload(), &msg)
|
||||
if err != nil {
|
||||
t.Errorf("unable to unmarshal drive mode message: %v", err)
|
||||
}
|
||||
value := msg.DriveMode
|
||||
if led.red != c.red {
|
||||
t.Errorf("driveMode(%v)=invalid value for red channel: %v, wants %v", value, led.red, c.red)
|
||||
}
|
||||
if led.green != c.green {
|
||||
if err != nil {
|
||||
t.Errorf("payload isn't a led value: %v", err)
|
||||
}
|
||||
t.Errorf("driveMode(%v)=invalid value for green channel: %v, wants %v", value, led.green, c.green)
|
||||
}
|
||||
if led.blue != c.blue {
|
||||
if err != nil {
|
||||
t.Errorf("payload isn't a led value: %v", err)
|
||||
}
|
||||
t.Errorf("driveMode(%v)=invalid value for blue channel: %v, wants %v", value, led.blue, c.blue)
|
||||
}
|
||||
}
|
||||
}
|
||||
func TestLedPart_OnRecord(t *testing.T) {
|
||||
led := fakeLed{}
|
||||
p := LedPart{led: &led}
|
||||
|
||||
cases := []struct {
|
||||
msg mqtt.Message
|
||||
record bool
|
||||
blink bool
|
||||
}{
|
||||
{testtools.NewFakeMessageFromProtobuf("record", &events.SwitchRecordMessage{Enabled: false}), true, false},
|
||||
{testtools.NewFakeMessageFromProtobuf("record", &events.SwitchRecordMessage{Enabled: true}), false, true},
|
||||
{testtools.NewFakeMessageFromProtobuf("record", &events.SwitchRecordMessage{Enabled: false}), true, false},
|
||||
{testtools.NewFakeMessageFromProtobuf("record", &events.SwitchRecordMessage{Enabled: true}), false, true},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
p.onRecord(nil, c.msg)
|
||||
if led.blink != c.blink {
|
||||
var msg events.SwitchRecordMessage
|
||||
err := proto.Unmarshal(c.msg.Payload(), &msg)
|
||||
if err != nil {
|
||||
t.Errorf("unable to unmarshal %T message: %v", msg, err)
|
||||
}
|
||||
|
||||
value := msg.Enabled
|
||||
t.Errorf("onRecord(%v): %v, wants %v", value, c.record, led.blink)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user