2019-06-16 18:11:10 +00:00
|
|
|
package rgbled
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/go-flucky/flucky/pkg/types"
|
|
|
|
"github.com/stianeikeland/go-rpio"
|
|
|
|
)
|
|
|
|
|
2019-07-14 17:06:26 +00:00
|
|
|
// DefaultRGBLED is a RGBLED which implement all functions of the interface
|
|
|
|
// RGBLED
|
2019-06-24 20:57:29 +00:00
|
|
|
type DefaultRGBLED struct {
|
2019-06-16 18:11:10 +00:00
|
|
|
*types.RGBLED
|
|
|
|
}
|
|
|
|
|
2019-07-14 17:06:26 +00:00
|
|
|
// Blue makes the RGBLED shine in blue
|
2019-06-24 20:57:29 +00:00
|
|
|
func (rgbled *DefaultRGBLED) Blue() error {
|
2019-06-16 18:11:10 +00:00
|
|
|
gpios := []*types.GPIO{
|
2019-06-24 20:57:29 +00:00
|
|
|
rgbled.BaseColorsToGPIO[types.BaseColorBlue],
|
2019-06-16 18:11:10 +00:00
|
|
|
}
|
|
|
|
|
2019-06-24 20:57:29 +00:00
|
|
|
if err := rgbled.Off(); err != nil {
|
2019-06-16 18:11:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-24 20:57:29 +00:00
|
|
|
if err := rgbled.switchColors(gpios, true); err != nil {
|
2019-06-16 18:11:10 +00:00
|
|
|
return fmt.Errorf("Can not operate with GPIOs: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-14 17:06:26 +00:00
|
|
|
// Green makes the RGBLED shine in green
|
2019-06-24 20:57:29 +00:00
|
|
|
func (rgbled *DefaultRGBLED) Green() error {
|
2019-06-16 18:11:10 +00:00
|
|
|
|
|
|
|
gpios := []*types.GPIO{
|
2019-06-24 20:57:29 +00:00
|
|
|
rgbled.BaseColorsToGPIO[types.BaseColorGreen],
|
2019-06-16 18:11:10 +00:00
|
|
|
}
|
|
|
|
|
2019-06-24 20:57:29 +00:00
|
|
|
if err := rgbled.Off(); err != nil {
|
2019-06-16 18:11:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-24 20:57:29 +00:00
|
|
|
if err := rgbled.switchColors(gpios, true); err != nil {
|
2019-06-16 18:11:10 +00:00
|
|
|
return fmt.Errorf("Can not operate with GPIOs: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-07-14 17:06:26 +00:00
|
|
|
// Off turns on the RGBLED off
|
2019-06-24 20:57:29 +00:00
|
|
|
func (rgbled *DefaultRGBLED) Off() error {
|
2019-06-16 18:11:10 +00:00
|
|
|
|
|
|
|
gpios := []*types.GPIO{
|
2019-06-24 20:57:29 +00:00
|
|
|
rgbled.BaseColorsToGPIO[types.BaseColorBlue],
|
|
|
|
rgbled.BaseColorsToGPIO[types.BaseColorGreen],
|
|
|
|
rgbled.BaseColorsToGPIO[types.BaseColorRed],
|
2019-06-16 18:11:10 +00:00
|
|
|
}
|
|
|
|
|
2019-06-24 20:57:29 +00:00
|
|
|
if err := rgbled.switchColors(gpios, false); err != nil {
|
2019-06-16 18:11:10 +00:00
|
|
|
return fmt.Errorf("Can not turn GPIOs off: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-14 17:06:26 +00:00
|
|
|
// On turns on the RGBLED
|
2019-06-24 20:57:29 +00:00
|
|
|
func (rgbled *DefaultRGBLED) On() error {
|
|
|
|
return rgbled.White()
|
|
|
|
}
|
|
|
|
|
2019-07-14 17:06:26 +00:00
|
|
|
// Purple makes the RGBLED shine in purple
|
2019-06-24 20:57:29 +00:00
|
|
|
func (rgbled *DefaultRGBLED) Purple() error {
|
2019-06-16 18:11:10 +00:00
|
|
|
gpios := []*types.GPIO{
|
2019-06-24 20:57:29 +00:00
|
|
|
rgbled.BaseColorsToGPIO[types.BaseColorBlue],
|
|
|
|
rgbled.BaseColorsToGPIO[types.BaseColorRed],
|
2019-06-16 18:11:10 +00:00
|
|
|
}
|
|
|
|
|
2019-06-24 20:57:29 +00:00
|
|
|
if err := rgbled.Off(); err != nil {
|
2019-06-16 18:11:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-24 20:57:29 +00:00
|
|
|
if err := rgbled.switchColors(gpios, true); err != nil {
|
2019-06-16 18:11:10 +00:00
|
|
|
return fmt.Errorf("Can not operate with GPIOs: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-14 17:06:26 +00:00
|
|
|
// Red makes the RGBLED shine in red
|
2019-06-24 20:57:29 +00:00
|
|
|
func (rgbled *DefaultRGBLED) Red() error {
|
2019-06-16 18:11:10 +00:00
|
|
|
gpios := []*types.GPIO{
|
2019-06-24 20:57:29 +00:00
|
|
|
rgbled.BaseColorsToGPIO[types.BaseColorRed],
|
2019-06-16 18:11:10 +00:00
|
|
|
}
|
|
|
|
|
2019-06-24 20:57:29 +00:00
|
|
|
if err := rgbled.Off(); err != nil {
|
2019-06-16 18:11:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-24 20:57:29 +00:00
|
|
|
if err := rgbled.switchColors(gpios, true); err != nil {
|
2019-06-16 18:11:10 +00:00
|
|
|
return fmt.Errorf("Can not operate with GPIOs: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-14 17:06:26 +00:00
|
|
|
// Turquoise makes the RGBLED shine in turquoise
|
2019-06-24 20:57:29 +00:00
|
|
|
func (rgbled *DefaultRGBLED) Turquoise() error {
|
2019-06-16 18:11:10 +00:00
|
|
|
gpios := []*types.GPIO{
|
2019-06-24 20:57:29 +00:00
|
|
|
rgbled.BaseColorsToGPIO[types.BaseColorBlue],
|
|
|
|
rgbled.BaseColorsToGPIO[types.BaseColorGreen],
|
2019-06-16 18:11:10 +00:00
|
|
|
}
|
|
|
|
|
2019-06-24 20:57:29 +00:00
|
|
|
if err := rgbled.Off(); err != nil {
|
2019-06-16 18:11:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-24 20:57:29 +00:00
|
|
|
if err := rgbled.switchColors(gpios, true); err != nil {
|
2019-06-16 18:11:10 +00:00
|
|
|
return fmt.Errorf("Can not operate with GPIOs: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-14 17:06:26 +00:00
|
|
|
// White makes the RGBLED shine in white
|
2019-06-24 20:57:29 +00:00
|
|
|
func (rgbled *DefaultRGBLED) White() error {
|
2019-06-16 18:11:10 +00:00
|
|
|
gpios := []*types.GPIO{
|
2019-06-24 20:57:29 +00:00
|
|
|
rgbled.BaseColorsToGPIO[types.BaseColorBlue],
|
|
|
|
rgbled.BaseColorsToGPIO[types.BaseColorGreen],
|
|
|
|
rgbled.BaseColorsToGPIO[types.BaseColorRed],
|
2019-06-16 18:11:10 +00:00
|
|
|
}
|
|
|
|
|
2019-06-24 20:57:29 +00:00
|
|
|
if err := rgbled.Off(); err != nil {
|
2019-06-16 18:11:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-24 20:57:29 +00:00
|
|
|
if err := rgbled.switchColors(gpios, true); err != nil {
|
2019-06-16 18:11:10 +00:00
|
|
|
return fmt.Errorf("Can not operate with GPIOs: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-14 17:06:26 +00:00
|
|
|
// Yellow makes the RGBLED shine in yellow
|
2019-06-24 20:57:29 +00:00
|
|
|
func (rgbled *DefaultRGBLED) Yellow() error {
|
2019-06-16 18:11:10 +00:00
|
|
|
gpios := []*types.GPIO{
|
2019-06-24 20:57:29 +00:00
|
|
|
rgbled.BaseColorsToGPIO[types.BaseColorGreen],
|
|
|
|
rgbled.BaseColorsToGPIO[types.BaseColorRed],
|
2019-06-16 18:11:10 +00:00
|
|
|
}
|
|
|
|
|
2019-06-24 20:57:29 +00:00
|
|
|
if err := rgbled.Off(); err != nil {
|
2019-06-16 18:11:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-24 20:57:29 +00:00
|
|
|
if err := rgbled.switchColors(gpios, true); err != nil {
|
2019-06-16 18:11:10 +00:00
|
|
|
return fmt.Errorf("Can not operate with GPIOs: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-14 17:06:26 +00:00
|
|
|
// Error makes the RGBLED shine in the error specified color
|
2019-06-24 20:57:29 +00:00
|
|
|
func (rgbled *DefaultRGBLED) Error() error {
|
|
|
|
return rgbled.switchColorBasedOnAction(rgbled.ActionMapping[types.LEDActionError])
|
|
|
|
}
|
|
|
|
|
2019-07-14 17:06:26 +00:00
|
|
|
// Logfile makes the RGBLED shine in the logfile specified color
|
2019-06-24 20:57:29 +00:00
|
|
|
func (rgbled *DefaultRGBLED) Logfile() error {
|
|
|
|
return rgbled.switchColorBasedOnAction(rgbled.ActionMapping[types.LEDActionLogfile])
|
|
|
|
}
|
|
|
|
|
2019-07-14 17:06:26 +00:00
|
|
|
// Sync makes the RGBLED shine in the sync specified color
|
2019-06-24 20:57:29 +00:00
|
|
|
func (rgbled *DefaultRGBLED) Sync() error {
|
|
|
|
return rgbled.switchColorBasedOnAction(rgbled.ActionMapping[types.LEDActionSync])
|
|
|
|
}
|
|
|
|
|
2019-07-14 17:06:26 +00:00
|
|
|
// Warn makes the RGBLED shine in the warn specified color
|
2019-06-24 20:57:29 +00:00
|
|
|
func (rgbled *DefaultRGBLED) Warn() error {
|
|
|
|
return rgbled.switchColorBasedOnAction(rgbled.ActionMapping[types.LEDActionWarn])
|
|
|
|
}
|
|
|
|
|
2019-07-14 17:06:26 +00:00
|
|
|
// Run makes the RGBLED shine in the run specified color
|
2019-06-24 20:57:29 +00:00
|
|
|
func (rgbled *DefaultRGBLED) Run() error {
|
|
|
|
return rgbled.switchColorBasedOnAction(rgbled.ActionMapping[types.LEDActionRun])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rgbled *DefaultRGBLED) switchColors(gpios []*types.GPIO, on bool) error {
|
2019-06-16 18:11:10 +00:00
|
|
|
if err := rpio.Open(); err != nil {
|
|
|
|
return fmt.Errorf("Cam not open rpio connection: %v", err)
|
|
|
|
}
|
|
|
|
defer rpio.Close()
|
|
|
|
|
|
|
|
for _, gpio := range gpios {
|
|
|
|
gpioInt, err := types.GPIOToInt(*gpio)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Can not determine %v into integer: %v", gpio, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pin := rpio.Pin(gpioInt)
|
|
|
|
|
2019-06-28 07:59:11 +00:00
|
|
|
// if rpio.DetectEdge(rpio.P rpio.AnyEdge) {
|
|
|
|
// log.Println("Test")
|
|
|
|
// }
|
|
|
|
|
|
|
|
pin.Pull(rpio.PullOff)
|
|
|
|
pin.Output()
|
|
|
|
|
2019-06-16 18:11:10 +00:00
|
|
|
if on {
|
|
|
|
pin.High()
|
|
|
|
} else {
|
|
|
|
pin.Low()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-06-24 20:57:29 +00:00
|
|
|
|
|
|
|
func (rgbled *DefaultRGBLED) switchColorBasedOnAction(action types.LEDColor) error {
|
|
|
|
switch action {
|
|
|
|
case types.LEDColorBlue:
|
|
|
|
return rgbled.Blue()
|
|
|
|
case types.LEDColorGreen:
|
|
|
|
return rgbled.Green()
|
|
|
|
case types.LEDColorNone:
|
|
|
|
return rgbled.Off()
|
|
|
|
case types.LEDColorPurple:
|
|
|
|
return rgbled.Purple()
|
|
|
|
case types.LEDColorRed:
|
|
|
|
return rgbled.Red()
|
|
|
|
case types.LEDColorTurquoise:
|
|
|
|
return rgbled.Turquoise()
|
|
|
|
case types.LEDColorWhite:
|
|
|
|
return rgbled.White()
|
|
|
|
case types.LEDColorYellow:
|
|
|
|
return rgbled.Yellow()
|
|
|
|
default:
|
|
|
|
return rgbled.Off()
|
|
|
|
}
|
|
|
|
}
|