refactor: move led and part modules to pkg

This commit is contained in:
2021-10-12 19:05:34 +02:00
parent 7f94e749b7
commit 15c3fcae95
5 changed files with 4 additions and 4 deletions

175
pkg/led/led.go Normal file
View 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
View 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)
}
}