130 lines
2.6 KiB
Go
130 lines
2.6 KiB
Go
|
package rgbled
|
||
|
|
||
|
import (
|
||
|
"sync"
|
||
|
|
||
|
"github.com/go-flucky/flucky/pkg/internal/collect"
|
||
|
"github.com/go-flucky/flucky/pkg/internal/prittyprint"
|
||
|
"github.com/go-flucky/flucky/pkg/types"
|
||
|
)
|
||
|
|
||
|
func Blue(rgbLEDs []RGBLED) error {
|
||
|
color := types.LEDColorBlue
|
||
|
if err := operate(rgbLEDs, true, color); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func CustomColor(rgbLEDs []RGBLED, color types.LEDColor) error {
|
||
|
if err := operate(rgbLEDs, true, color); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func Green(rgbLEDs []RGBLED) error {
|
||
|
color := types.LEDColorGreen
|
||
|
if err := operate(rgbLEDs, true, color); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func Off(rgbLEDs []RGBLED) error {
|
||
|
color := types.LEDColorNone
|
||
|
if err := operate(rgbLEDs, false, color); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func Purple(rgbLEDs []RGBLED) error {
|
||
|
color := types.LEDColorPurple
|
||
|
if err := operate(rgbLEDs, true, color); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func Red(rgbLEDs []RGBLED) error {
|
||
|
color := types.LEDColorRed
|
||
|
if err := operate(rgbLEDs, true, color); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func Turquoise(rgbLEDs []RGBLED) error {
|
||
|
color := types.LEDColorTurquoise
|
||
|
if err := operate(rgbLEDs, true, color); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func White(rgbLEDs []RGBLED) error {
|
||
|
color := types.LEDColorWhite
|
||
|
if err := operate(rgbLEDs, true, color); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func Yellow(rgbLEDs []RGBLED) error {
|
||
|
color := types.LEDColorYellow
|
||
|
if err := operate(rgbLEDs, true, color); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func operate(rgbLEDs []RGBLED, on bool, color types.LEDColor) error {
|
||
|
|
||
|
errorChannel := make(chan error, len(rgbLEDs))
|
||
|
|
||
|
wg := new(sync.WaitGroup)
|
||
|
wg.Add(len(rgbLEDs))
|
||
|
|
||
|
for _, rgbLED := range rgbLEDs {
|
||
|
go func(rgbLED RGBLED, color types.LEDColor, on bool, errorChannel chan<- error, wg *sync.WaitGroup) {
|
||
|
defer wg.Done()
|
||
|
|
||
|
var err error
|
||
|
|
||
|
switch color {
|
||
|
case types.LEDColorBlue:
|
||
|
err = rgbLED.Blue(on)
|
||
|
case types.LEDColorGreen:
|
||
|
err = rgbLED.Green(on)
|
||
|
case types.LEDColorPurple:
|
||
|
err = rgbLED.Purple(on)
|
||
|
case types.LEDColorNone:
|
||
|
err = rgbLED.Off()
|
||
|
case types.LEDColorRed:
|
||
|
err = rgbLED.Red(on)
|
||
|
case types.LEDColorTurquoise:
|
||
|
err = rgbLED.Turquoise(on)
|
||
|
case types.LEDColorWhite:
|
||
|
err = rgbLED.White(on)
|
||
|
case types.LEDColorYellow:
|
||
|
err = rgbLED.Yellow(on)
|
||
|
default:
|
||
|
err = rgbLED.Off()
|
||
|
}
|
||
|
|
||
|
if err != nil {
|
||
|
errorChannel <- err
|
||
|
}
|
||
|
}(rgbLED, color, on, errorChannel, wg)
|
||
|
}
|
||
|
|
||
|
wg.Wait()
|
||
|
|
||
|
errors := collect.Errors(errorChannel)
|
||
|
if len(errors) > 0 {
|
||
|
return prittyprint.FormatErrors(errors)
|
||
|
}
|
||
|
return nil
|
||
|
}
|