266 lines
5.6 KiB
Go
266 lines
5.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"
|
|
)
|
|
|
|
// 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, 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, 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, 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, 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, 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, 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, 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, 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, color); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func operate(rgbLEDs []RGBLED, 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, errorChannel chan<- error, wg *sync.WaitGroup) {
|
|
defer wg.Done()
|
|
|
|
var err error
|
|
|
|
switch color {
|
|
case types.LEDColorBlue:
|
|
err = rgbLED.Blue()
|
|
case types.LEDColorGreen:
|
|
err = rgbLED.Green()
|
|
case types.LEDColorPurple:
|
|
err = rgbLED.Purple()
|
|
case types.LEDColorNone:
|
|
err = rgbLED.Off()
|
|
case types.LEDColorRed:
|
|
err = rgbLED.Red()
|
|
case types.LEDColorTurquoise:
|
|
err = rgbLED.Turquoise()
|
|
case types.LEDColorWhite:
|
|
err = rgbLED.White()
|
|
case types.LEDColorYellow:
|
|
err = rgbLED.Yellow()
|
|
default:
|
|
err = rgbLED.Off()
|
|
}
|
|
|
|
if err != nil {
|
|
errorChannel <- err
|
|
}
|
|
}(rgbLED, color, errorChannel, wg)
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
errors := collect.Errors(errorChannel)
|
|
if len(errors) > 0 {
|
|
return prittyprint.FormatErrors(errors)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func Error(rgbLEDs []RGBLED) error {
|
|
errorChannel := make(chan error, len(rgbLEDs))
|
|
|
|
wg := new(sync.WaitGroup)
|
|
wg.Add(len(rgbLEDs))
|
|
|
|
for _, rgbLED := range rgbLEDs {
|
|
go func(rgbLED RGBLED, errorChannel chan<- error, wg *sync.WaitGroup) {
|
|
defer wg.Done()
|
|
err := rgbLED.Error()
|
|
if err != nil {
|
|
errorChannel <- err
|
|
}
|
|
}(rgbLED, errorChannel, wg)
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
errors := collect.Errors(errorChannel)
|
|
if len(errors) > 0 {
|
|
return prittyprint.FormatErrors(errors)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func Logfile(rgbLEDs []RGBLED) error {
|
|
errorChannel := make(chan error, len(rgbLEDs))
|
|
|
|
wg := new(sync.WaitGroup)
|
|
wg.Add(len(rgbLEDs))
|
|
|
|
for _, rgbLED := range rgbLEDs {
|
|
go func(rgbLED RGBLED, errorChannel chan<- error, wg *sync.WaitGroup) {
|
|
defer wg.Done()
|
|
err := rgbLED.Logfile()
|
|
if err != nil {
|
|
errorChannel <- err
|
|
}
|
|
}(rgbLED, errorChannel, wg)
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
errors := collect.Errors(errorChannel)
|
|
if len(errors) > 0 {
|
|
return prittyprint.FormatErrors(errors)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func Run(rgbLEDs []RGBLED) error {
|
|
errorChannel := make(chan error, len(rgbLEDs))
|
|
|
|
wg := new(sync.WaitGroup)
|
|
wg.Add(len(rgbLEDs))
|
|
|
|
for _, rgbLED := range rgbLEDs {
|
|
go func(rgbLED RGBLED, errorChannel chan<- error, wg *sync.WaitGroup) {
|
|
defer wg.Done()
|
|
err := rgbLED.Run()
|
|
if err != nil {
|
|
errorChannel <- err
|
|
}
|
|
}(rgbLED, errorChannel, wg)
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
errors := collect.Errors(errorChannel)
|
|
if len(errors) > 0 {
|
|
return prittyprint.FormatErrors(errors)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func Sync(rgbLEDs []RGBLED) error {
|
|
errorChannel := make(chan error, len(rgbLEDs))
|
|
|
|
wg := new(sync.WaitGroup)
|
|
wg.Add(len(rgbLEDs))
|
|
|
|
for _, rgbLED := range rgbLEDs {
|
|
go func(rgbLED RGBLED, errorChannel chan<- error, wg *sync.WaitGroup) {
|
|
defer wg.Done()
|
|
err := rgbLED.Sync()
|
|
if err != nil {
|
|
errorChannel <- err
|
|
}
|
|
}(rgbLED, errorChannel, wg)
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
errors := collect.Errors(errorChannel)
|
|
if len(errors) > 0 {
|
|
return prittyprint.FormatErrors(errors)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func Warn(rgbLEDs []RGBLED) error {
|
|
errorChannel := make(chan error, len(rgbLEDs))
|
|
|
|
wg := new(sync.WaitGroup)
|
|
wg.Add(len(rgbLEDs))
|
|
|
|
for _, rgbLED := range rgbLEDs {
|
|
go func(rgbLED RGBLED, errorChannel chan<- error, wg *sync.WaitGroup) {
|
|
defer wg.Done()
|
|
err := rgbLED.Warn()
|
|
if err != nil {
|
|
errorChannel <- err
|
|
}
|
|
}(rgbLED, errorChannel, wg)
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
errors := collect.Errors(errorChannel)
|
|
if len(errors) > 0 {
|
|
return prittyprint.FormatErrors(errors)
|
|
}
|
|
return nil
|
|
}
|