2019-12-14 10:56:22 +00:00
|
|
|
package led
|
|
|
|
|
|
|
|
import (
|
2021-09-01 19:41:28 +00:00
|
|
|
"periph.io/x/conn/v3/gpio"
|
2019-12-14 10:56:22 +00:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestColorLed_Red(t *testing.T) {
|
|
|
|
setLedBackup := setLed
|
|
|
|
defer func() { setLed = setLedBackup }()
|
|
|
|
|
2023-03-09 18:42:47 +00:00
|
|
|
l := New()
|
|
|
|
fakeLed := struct {
|
|
|
|
redValue int
|
|
|
|
greenValue int
|
|
|
|
blueValue int
|
|
|
|
}{}
|
2019-12-14 10:56:22 +00:00
|
|
|
setLed = func(v int, led gpio.PinIO, mutex *sync.Mutex) {
|
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
2023-03-09 18:42:47 +00:00
|
|
|
switch led {
|
|
|
|
case l.pinRed:
|
|
|
|
fakeLed.redValue = v
|
|
|
|
case l.pinGreen:
|
|
|
|
fakeLed.greenValue = v
|
|
|
|
case l.pinBlue:
|
|
|
|
fakeLed.blueValue = v
|
|
|
|
}
|
2019-12-14 10:56:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if l.Red() != 0 {
|
|
|
|
t.Errorf("%T.Red(): %v, wants %v", l, l.Red(), 0)
|
|
|
|
}
|
2023-03-09 18:42:47 +00:00
|
|
|
if fakeLed.redValue != 0 {
|
|
|
|
t.Errorf("colorValue: %v, wants %v", fakeLed.redValue, 0)
|
2019-12-14 10:56:22 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 17:16:33 +00:00
|
|
|
l.SetColor(ColorRed)
|
2019-12-14 10:56:22 +00:00
|
|
|
if l.Red() != 255 {
|
|
|
|
t.Errorf("%T.Red(): %v, wants %v", l, l.Red(), 255)
|
|
|
|
}
|
2023-03-09 18:42:47 +00:00
|
|
|
if fakeLed.redValue != 255 {
|
|
|
|
t.Errorf("colorValue: %v, wants %v", fakeLed.redValue, 255)
|
2019-12-14 10:56:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestColorLed_Green(t *testing.T) {
|
|
|
|
setLedBackup := setLed
|
|
|
|
defer func() { setLed = setLedBackup }()
|
|
|
|
|
2023-03-09 18:42:47 +00:00
|
|
|
l := New()
|
|
|
|
fakeLed := struct {
|
|
|
|
redValue int
|
|
|
|
greenValue int
|
|
|
|
blueValue int
|
|
|
|
}{}
|
2019-12-14 10:56:22 +00:00
|
|
|
setLed = func(v int, led gpio.PinIO, mutex *sync.Mutex) {
|
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
2023-03-09 18:42:47 +00:00
|
|
|
switch led {
|
|
|
|
case l.pinRed:
|
|
|
|
fakeLed.redValue = v
|
|
|
|
case l.pinGreen:
|
|
|
|
fakeLed.greenValue = v
|
|
|
|
case l.pinBlue:
|
|
|
|
fakeLed.blueValue = v
|
|
|
|
}
|
2019-12-14 10:56:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if l.Green() != 0 {
|
|
|
|
t.Errorf("%T.Green(): %v, wants %v", l, l.Green(), 0)
|
|
|
|
}
|
2023-03-09 18:42:47 +00:00
|
|
|
if fakeLed.greenValue != 0 {
|
|
|
|
t.Errorf("colorValue: %v, wants %v", fakeLed.greenValue, 0)
|
2019-12-14 10:56:22 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 17:16:33 +00:00
|
|
|
l.SetColor(ColorGreen)
|
2019-12-14 10:56:22 +00:00
|
|
|
if l.Green() != 255 {
|
|
|
|
t.Errorf("%T.Green(): %v, wants %v", l, l.Green(), 255)
|
|
|
|
}
|
2023-03-09 18:42:47 +00:00
|
|
|
if fakeLed.greenValue != 255 {
|
|
|
|
t.Errorf("colorValue: %v, wants %v", fakeLed.greenValue, 255)
|
2019-12-14 10:56:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestColorLed_Blue(t *testing.T) {
|
|
|
|
setLedBackup := setLed
|
|
|
|
defer func() { setLed = setLedBackup }()
|
|
|
|
|
2023-03-09 18:42:47 +00:00
|
|
|
l := New()
|
|
|
|
fakeLed := struct {
|
|
|
|
redValue int
|
|
|
|
greenValue int
|
|
|
|
blueValue int
|
|
|
|
}{}
|
2019-12-14 10:56:22 +00:00
|
|
|
setLed = func(v int, led gpio.PinIO, mutex *sync.Mutex) {
|
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
2023-03-09 18:42:47 +00:00
|
|
|
switch led {
|
|
|
|
case l.pinRed:
|
|
|
|
fakeLed.redValue = v
|
|
|
|
case l.pinGreen:
|
|
|
|
fakeLed.greenValue = v
|
|
|
|
case l.pinBlue:
|
|
|
|
fakeLed.blueValue = v
|
|
|
|
}
|
2019-12-14 10:56:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if l.Blue() != 0 {
|
|
|
|
t.Errorf("%T.Blue(): %v, wants %v", l, l.Blue(), 0)
|
|
|
|
}
|
2023-03-09 18:42:47 +00:00
|
|
|
if fakeLed.blueValue != 0 {
|
|
|
|
t.Errorf("colorValue: %v, wants %v", fakeLed.blueValue, 0)
|
2019-12-14 10:56:22 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 17:16:33 +00:00
|
|
|
l.SetColor(ColorBlue)
|
2019-12-14 10:56:22 +00:00
|
|
|
if l.Blue() != 255 {
|
|
|
|
t.Errorf("%T.Blue(): %v, wants %v", l, l.Blue(), 255)
|
|
|
|
}
|
2023-03-09 18:42:47 +00:00
|
|
|
if fakeLed.blueValue != 255 {
|
|
|
|
t.Errorf("colorValue: %v, wants %v", fakeLed.blueValue, 255)
|
2019-12-14 10:56:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestColorLed_SetBlink(t *testing.T) {
|
|
|
|
setLedBackup := setLed
|
|
|
|
defer func() { setLed = setLedBackup }()
|
|
|
|
|
|
|
|
var muFakeValue sync.Mutex
|
2022-06-15 08:59:18 +00:00
|
|
|
ledColors := make(map[gpio.PinIO]int)
|
2019-12-14 10:56:22 +00:00
|
|
|
setLed = func(v int, led gpio.PinIO, mutex *sync.Mutex) {
|
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
|
|
|
muFakeValue.Lock()
|
|
|
|
defer muFakeValue.Unlock()
|
2022-06-15 08:59:18 +00:00
|
|
|
ledColors[led] = v
|
2019-12-14 10:56:22 +00:00
|
|
|
}
|
2022-06-15 08:59:18 +00:00
|
|
|
readValue := func(p gpio.PinIO) int {
|
2019-12-14 10:56:22 +00:00
|
|
|
muFakeValue.Lock()
|
|
|
|
defer muFakeValue.Unlock()
|
2022-06-15 08:59:18 +00:00
|
|
|
return ledColors[p]
|
2019-12-14 10:56:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
l := New()
|
2021-12-29 17:16:33 +00:00
|
|
|
l.SetColor(ColorBlue)
|
2022-06-15 08:59:18 +00:00
|
|
|
v := ledColors[l.pinBlue]
|
2019-12-14 10:56:22 +00:00
|
|
|
if v != 255 {
|
|
|
|
t.Errorf("colorValue: %v, wants %v", v, 255)
|
|
|
|
}
|
|
|
|
l.SetBlink(100)
|
2022-06-15 08:59:18 +00:00
|
|
|
v = readValue(l.pinBlue)
|
2019-12-14 10:56:22 +00:00
|
|
|
if v != 255 {
|
|
|
|
t.Errorf("colorValue: %v, wants %v", v, 255)
|
|
|
|
}
|
|
|
|
time.Sleep(12 * time.Millisecond)
|
2022-06-15 08:59:18 +00:00
|
|
|
v = readValue(l.pinBlue)
|
2019-12-14 10:56:22 +00:00
|
|
|
if v != 0 {
|
|
|
|
t.Errorf("colorValue: %v, wants %v", v, 0)
|
|
|
|
}
|
|
|
|
time.Sleep(12 * time.Millisecond)
|
2022-06-15 08:59:18 +00:00
|
|
|
v = readValue(l.pinBlue)
|
2019-12-14 10:56:22 +00:00
|
|
|
if v != 255 {
|
|
|
|
t.Errorf("colorValue: %v, wants %v", v, 255)
|
|
|
|
}
|
|
|
|
time.Sleep(12 * time.Millisecond)
|
2022-06-15 08:59:18 +00:00
|
|
|
v = readValue(l.pinBlue)
|
2019-12-14 10:56:22 +00:00
|
|
|
if v != 0 {
|
|
|
|
t.Errorf("colorValue: %v, wants %v", v, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop blink
|
|
|
|
l.SetBlink(0)
|
|
|
|
time.Sleep(5 * time.Millisecond)
|
2022-06-15 08:59:18 +00:00
|
|
|
v = readValue(l.pinBlue)
|
2019-12-14 10:56:22 +00:00
|
|
|
if v != 255 {
|
|
|
|
t.Errorf("colorValue: %v, wants %v", v, 255)
|
|
|
|
}
|
|
|
|
time.Sleep(12 * time.Millisecond)
|
2022-06-15 08:59:18 +00:00
|
|
|
v = readValue(l.pinBlue)
|
2019-12-14 10:56:22 +00:00
|
|
|
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
|
2022-06-15 08:59:18 +00:00
|
|
|
ledColors := make(map[gpio.PinIO]int)
|
2019-12-14 10:56:22 +00:00
|
|
|
setLed = func(v int, led gpio.PinIO, mutex *sync.Mutex) {
|
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
|
|
|
muFakeValue.Lock()
|
|
|
|
defer muFakeValue.Unlock()
|
2022-06-15 08:59:18 +00:00
|
|
|
ledColors[led] = v
|
2019-12-14 10:56:22 +00:00
|
|
|
}
|
2022-06-15 08:59:18 +00:00
|
|
|
readValue := func(p gpio.PinIO) int {
|
2019-12-14 10:56:22 +00:00
|
|
|
muFakeValue.Lock()
|
|
|
|
defer muFakeValue.Unlock()
|
2022-06-15 08:59:18 +00:00
|
|
|
return ledColors[p]
|
2019-12-14 10:56:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
l := New()
|
2021-12-29 17:16:33 +00:00
|
|
|
l.SetColor(ColorBlue)
|
2019-12-14 10:56:22 +00:00
|
|
|
l.SetBlink(100)
|
2022-06-15 08:59:18 +00:00
|
|
|
v := readValue(l.pinBlue)
|
2019-12-14 10:56:22 +00:00
|
|
|
if v != 255 {
|
|
|
|
t.Errorf("colorValue: %v, wants %v", v, 255)
|
|
|
|
}
|
|
|
|
time.Sleep(6 * time.Millisecond)
|
2021-12-29 17:16:33 +00:00
|
|
|
l.SetColor(ColorBlue)
|
2019-12-14 10:56:22 +00:00
|
|
|
|
2022-06-15 08:59:18 +00:00
|
|
|
v = readValue(l.pinBlue)
|
|
|
|
if v != 255 {
|
2019-12-14 10:56:22 +00:00
|
|
|
t.Errorf("colorValue: %v, wants %v", v, 128)
|
|
|
|
}
|
|
|
|
time.Sleep(6 * time.Millisecond)
|
|
|
|
|
|
|
|
time.Sleep(12 * time.Millisecond)
|
|
|
|
|
2022-06-15 08:59:18 +00:00
|
|
|
v = readValue(l.pinBlue)
|
|
|
|
if v != 255 {
|
2019-12-14 10:56:22 +00:00
|
|
|
t.Errorf("colorValue: %v, wants %v", v, 128)
|
|
|
|
}
|
|
|
|
time.Sleep(12 * time.Millisecond)
|
|
|
|
|
2022-06-15 08:59:18 +00:00
|
|
|
v = readValue(l.pinBlue)
|
2019-12-14 10:56:22 +00:00
|
|
|
if v != 0 {
|
|
|
|
t.Errorf("colorValue: %v, wants %v", v, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop blink
|
|
|
|
l.SetBlink(0)
|
|
|
|
time.Sleep(5 * time.Millisecond)
|
2022-06-15 08:59:18 +00:00
|
|
|
v = readValue(l.pinBlue)
|
|
|
|
if v != 255 {
|
2019-12-14 10:56:22 +00:00
|
|
|
t.Errorf("colorValue: %v, wants %v", v, 128)
|
|
|
|
}
|
|
|
|
time.Sleep(12 * time.Millisecond)
|
2022-06-15 08:59:18 +00:00
|
|
|
v = readValue(l.pinBlue)
|
|
|
|
if v != 255 {
|
2019-12-14 10:56:22 +00:00
|
|
|
t.Errorf("colorValue: %v, wants %v", v, 128)
|
|
|
|
}
|
|
|
|
}
|