PKGBUILD/pkg/rgbled/rgbled.go

141 lines
3.3 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"
)
// Blue makes all RGB-LEDs which are passed as parameters light up blue.
func Blue(rgbLEDs []RGBLED) error {
color := types.LEDColorBlue
if err := operate(rgbLEDs, true, color); err != nil {
return err
}
return nil
}
// CustomColor makes all RGB-LEDs which are passed as parameters light up in
// custom color.
func CustomColor(rgbLEDs []RGBLED, color types.LEDColor) error {
if err := operate(rgbLEDs, true, color); err != nil {
return err
}
return nil
}
// Green makes all RGB-LEDs which are passed as parameters light up in green.
func Green(rgbLEDs []RGBLED) error {
color := types.LEDColorGreen
if err := operate(rgbLEDs, true, color); err != nil {
return err
}
return nil
}
// Off turns all RGB-LEDs which are passes as parameters off.
func Off(rgbLEDs []RGBLED) error {
color := types.LEDColorNone
if err := operate(rgbLEDs, false, color); err != nil {
return err
}
return nil
}
// Purple makes all RGB-LEDs which are passed as parameters light up in purple.
func Purple(rgbLEDs []RGBLED) error {
color := types.LEDColorPurple
if err := operate(rgbLEDs, true, color); err != nil {
return err
}
return nil
}
// Red makes all RGB-LEDs which are passed as parameters light up in red.
func Red(rgbLEDs []RGBLED) error {
color := types.LEDColorRed
if err := operate(rgbLEDs, true, color); err != nil {
return err
}
return nil
}
// Turquoise makes all RGB-LEDs which are passed as parameters light up in
// turquoise.
func Turquoise(rgbLEDs []RGBLED) error {
color := types.LEDColorTurquoise
if err := operate(rgbLEDs, true, color); err != nil {
return err
}
return nil
}
// White makes all RGB-LEDs which are passed as parameters light up in white.
func White(rgbLEDs []RGBLED) error {
color := types.LEDColorWhite
if err := operate(rgbLEDs, true, color); err != nil {
return err
}
return nil
}
// Yellow makes all RGB-LEDs which are passed as parameters light up in yellow.
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
}