PKGBUILD/pkg/rgbled/rgbled.go

276 lines
6.2 KiB
Go
Raw Normal View History

2019-06-16 18:11:10 +00:00
package rgbled
import (
"sync"
"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
)
// 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
if err := operate(rgbLEDs, color); err != nil {
2019-06-16 18:11:10 +00:00
return err
}
return nil
}
// 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 {
if err := operate(rgbLEDs, color); err != nil {
2019-06-16 18:11:10 +00:00
return err
}
return nil
}
// 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
if err := operate(rgbLEDs, color); err != nil {
2019-06-16 18:11:10 +00:00
return err
}
return nil
}
// 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
if err := operate(rgbLEDs, color); err != nil {
2019-06-16 18:11:10 +00:00
return err
}
return nil
}
// 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
if err := operate(rgbLEDs, color); err != nil {
2019-06-16 18:11:10 +00:00
return err
}
return nil
}
// 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
if err := operate(rgbLEDs, color); err != nil {
2019-06-16 18:11:10 +00:00
return err
}
return nil
}
// 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
if err := operate(rgbLEDs, color); err != nil {
2019-06-16 18:11:10 +00:00
return err
}
return nil
}
// 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
if err := operate(rgbLEDs, color); err != nil {
2019-06-16 18:11:10 +00:00
return err
}
return nil
}
// 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
if err := operate(rgbLEDs, color); err != nil {
2019-06-16 18:11:10 +00:00
return err
}
return nil
}
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 {
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:
err = rgbLED.Blue()
2019-06-16 18:11:10 +00:00
case types.LEDColorGreen:
err = rgbLED.Green()
2019-06-16 18:11:10 +00:00
case types.LEDColorPurple:
err = rgbLED.Purple()
2019-06-16 18:11:10 +00:00
case types.LEDColorNone:
err = rgbLED.Off()
case types.LEDColorRed:
err = rgbLED.Red()
2019-06-16 18:11:10 +00:00
case types.LEDColorTurquoise:
err = rgbLED.Turquoise()
2019-06-16 18:11:10 +00:00
case types.LEDColorWhite:
err = rgbLED.White()
2019-06-16 18:11:10 +00:00
case types.LEDColorYellow:
err = rgbLED.Yellow()
2019-06-16 18:11:10 +00:00
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
}
// Error makes all RGB-LEDs which are passed as parameters light up in their
// error specified color.
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
}
// Logfile makes all RGB-LEDs which are passed as parameters light up in their
// logfile specified color.
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
}
// Run makes all RGB-LEDs which are passed as parameters light up in their run
// specified color.
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
}
// Sync makes all RGB-LEDs which are passed as parameters light up in their sync
// specified color.
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
}
// Warn makes all RGB-LEDs which are passed as parameters light up in their
// warn specified color.
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
}