2019-06-16 18:11:10 +00:00
|
|
|
package rgbled
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2020-01-19 12:30:47 +00:00
|
|
|
"github.com/volker-raschek/flucky/pkg/internal/collect"
|
|
|
|
"github.com/volker-raschek/flucky/pkg/internal/prittyprint"
|
|
|
|
"github.com/volker-raschek/flucky/pkg/types"
|
2019-06-16 18:11:10 +00:00
|
|
|
)
|
|
|
|
|
2019-06-16 21:47:41 +00:00
|
|
|
// Blue makes all RGB-LEDs which are passed as parameters light up blue.
|
2019-06-16 18:11:10 +00:00
|
|
|
func Blue(rgbLEDs []RGBLED) error {
|
|
|
|
color := types.LEDColorBlue
|
2019-06-24 20:57:29 +00:00
|
|
|
if err := operate(rgbLEDs, color); err != nil {
|
2019-06-16 18:11:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-16 21:47:41 +00:00
|
|
|
// CustomColor makes all RGB-LEDs which are passed as parameters light up in
|
|
|
|
// custom color.
|
2019-06-16 18:11:10 +00:00
|
|
|
func CustomColor(rgbLEDs []RGBLED, color types.LEDColor) error {
|
2019-06-24 20:57:29 +00:00
|
|
|
if err := operate(rgbLEDs, color); err != nil {
|
2019-06-16 18:11:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-16 21:47:41 +00:00
|
|
|
// Green makes all RGB-LEDs which are passed as parameters light up in green.
|
2019-06-16 18:11:10 +00:00
|
|
|
func Green(rgbLEDs []RGBLED) error {
|
|
|
|
color := types.LEDColorGreen
|
2019-06-24 20:57:29 +00:00
|
|
|
if err := operate(rgbLEDs, color); err != nil {
|
2019-06-16 18:11:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-16 21:47:41 +00:00
|
|
|
// Off turns all RGB-LEDs which are passes as parameters off.
|
2019-06-16 18:11:10 +00:00
|
|
|
func Off(rgbLEDs []RGBLED) error {
|
|
|
|
color := types.LEDColorNone
|
2019-06-24 20:57:29 +00:00
|
|
|
if err := operate(rgbLEDs, color); err != nil {
|
2019-06-16 18:11:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-16 21:47:41 +00:00
|
|
|
// Purple makes all RGB-LEDs which are passed as parameters light up in purple.
|
2019-06-16 18:11:10 +00:00
|
|
|
func Purple(rgbLEDs []RGBLED) error {
|
|
|
|
color := types.LEDColorPurple
|
2019-06-24 20:57:29 +00:00
|
|
|
if err := operate(rgbLEDs, color); err != nil {
|
2019-06-16 18:11:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-16 21:47:41 +00:00
|
|
|
// Red makes all RGB-LEDs which are passed as parameters light up in red.
|
2019-06-16 18:11:10 +00:00
|
|
|
func Red(rgbLEDs []RGBLED) error {
|
|
|
|
color := types.LEDColorRed
|
2019-06-24 20:57:29 +00:00
|
|
|
if err := operate(rgbLEDs, color); err != nil {
|
2019-06-16 18:11:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-16 21:47:41 +00:00
|
|
|
// Turquoise makes all RGB-LEDs which are passed as parameters light up in
|
|
|
|
// turquoise.
|
2019-06-16 18:11:10 +00:00
|
|
|
func Turquoise(rgbLEDs []RGBLED) error {
|
|
|
|
color := types.LEDColorTurquoise
|
2019-06-24 20:57:29 +00:00
|
|
|
if err := operate(rgbLEDs, color); err != nil {
|
2019-06-16 18:11:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-16 21:47:41 +00:00
|
|
|
// White makes all RGB-LEDs which are passed as parameters light up in white.
|
2019-06-16 18:11:10 +00:00
|
|
|
func White(rgbLEDs []RGBLED) error {
|
|
|
|
color := types.LEDColorWhite
|
2019-06-24 20:57:29 +00:00
|
|
|
if err := operate(rgbLEDs, color); err != nil {
|
2019-06-16 18:11:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-16 21:47:41 +00:00
|
|
|
// Yellow makes all RGB-LEDs which are passed as parameters light up in yellow.
|
2019-06-16 18:11:10 +00:00
|
|
|
func Yellow(rgbLEDs []RGBLED) error {
|
|
|
|
color := types.LEDColorYellow
|
2019-06-24 20:57:29 +00:00
|
|
|
if err := operate(rgbLEDs, color); err != nil {
|
2019-06-16 18:11:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-24 20:57:29 +00:00
|
|
|
func operate(rgbLEDs []RGBLED, color types.LEDColor) error {
|
2019-06-16 18:11:10 +00:00
|
|
|
|
|
|
|
errorChannel := make(chan error, len(rgbLEDs))
|
|
|
|
|
|
|
|
wg := new(sync.WaitGroup)
|
|
|
|
wg.Add(len(rgbLEDs))
|
|
|
|
|
|
|
|
for _, rgbLED := range rgbLEDs {
|
2019-06-24 20:57:29 +00:00
|
|
|
go func(rgbLED RGBLED, color types.LEDColor, errorChannel chan<- error, wg *sync.WaitGroup) {
|
2019-06-16 18:11:10 +00:00
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
switch color {
|
|
|
|
case types.LEDColorBlue:
|
2019-06-24 20:57:29 +00:00
|
|
|
err = rgbLED.Blue()
|
2019-06-16 18:11:10 +00:00
|
|
|
case types.LEDColorGreen:
|
2019-06-24 20:57:29 +00:00
|
|
|
err = rgbLED.Green()
|
2019-06-16 18:11:10 +00:00
|
|
|
case types.LEDColorPurple:
|
2019-06-24 20:57:29 +00:00
|
|
|
err = rgbLED.Purple()
|
2019-06-16 18:11:10 +00:00
|
|
|
case types.LEDColorNone:
|
|
|
|
err = rgbLED.Off()
|
|
|
|
case types.LEDColorRed:
|
2019-06-24 20:57:29 +00:00
|
|
|
err = rgbLED.Red()
|
2019-06-16 18:11:10 +00:00
|
|
|
case types.LEDColorTurquoise:
|
2019-06-24 20:57:29 +00:00
|
|
|
err = rgbLED.Turquoise()
|
2019-06-16 18:11:10 +00:00
|
|
|
case types.LEDColorWhite:
|
2019-06-24 20:57:29 +00:00
|
|
|
err = rgbLED.White()
|
2019-06-16 18:11:10 +00:00
|
|
|
case types.LEDColorYellow:
|
2019-06-24 20:57:29 +00:00
|
|
|
err = rgbLED.Yellow()
|
2019-06-16 18:11:10 +00:00
|
|
|
default:
|
|
|
|
err = rgbLED.Off()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errorChannel <- err
|
|
|
|
}
|
2019-06-24 20:57:29 +00:00
|
|
|
}(rgbLED, color, errorChannel, wg)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
errors := collect.Errors(errorChannel)
|
|
|
|
if len(errors) > 0 {
|
|
|
|
return prittyprint.FormatErrors(errors)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-14 17:06:26 +00:00
|
|
|
// Error makes all RGB-LEDs which are passed as parameters light up in their
|
|
|
|
// error specified color.
|
2019-06-24 20:57:29 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-07-14 17:06:26 +00:00
|
|
|
// Logfile makes all RGB-LEDs which are passed as parameters light up in their
|
|
|
|
// logfile specified color.
|
2019-06-24 20:57:29 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-07-14 17:06:26 +00:00
|
|
|
// Run makes all RGB-LEDs which are passed as parameters light up in their run
|
|
|
|
// specified color.
|
2019-06-24 20:57:29 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-07-14 17:06:26 +00:00
|
|
|
// Sync makes all RGB-LEDs which are passed as parameters light up in their sync
|
|
|
|
// specified color.
|
2019-06-24 20:57:29 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-07-14 17:06:26 +00:00
|
|
|
// Warn makes all RGB-LEDs which are passed as parameters light up in their
|
|
|
|
// warn specified color.
|
2019-06-24 20:57:29 +00:00
|
|
|
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)
|
2019-06-16 18:11:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
errors := collect.Errors(errorChannel)
|
|
|
|
if len(errors) > 0 {
|
|
|
|
return prittyprint.FormatErrors(errors)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|