package rgbled import ( "fmt" "github.com/volker-raschek/flucky/pkg/types" "github.com/stianeikeland/go-rpio" ) // DefaultRGBLED is a RGBLED which implement all functions of the interface // RGBLED type DefaultRGBLED struct { *types.RGBLED } // Blue makes the RGBLED shine in blue func (rgbled *DefaultRGBLED) Blue() error { gpios := []*types.GPIO{ rgbled.BaseColorsToGPIO[types.BaseColorBlue], } if err := rgbled.Off(); err != nil { return err } if err := rgbled.switchColors(gpios, true); err != nil { return fmt.Errorf("Can not operate with GPIOs: %v", err) } return nil } // Green makes the RGBLED shine in green func (rgbled *DefaultRGBLED) Green() error { gpios := []*types.GPIO{ rgbled.BaseColorsToGPIO[types.BaseColorGreen], } if err := rgbled.Off(); err != nil { return err } if err := rgbled.switchColors(gpios, true); err != nil { return fmt.Errorf("Can not operate with GPIOs: %v", err) } return nil } // Off turns on the RGBLED off func (rgbled *DefaultRGBLED) Off() error { gpios := []*types.GPIO{ rgbled.BaseColorsToGPIO[types.BaseColorBlue], rgbled.BaseColorsToGPIO[types.BaseColorGreen], rgbled.BaseColorsToGPIO[types.BaseColorRed], } if err := rgbled.switchColors(gpios, false); err != nil { return fmt.Errorf("Can not turn GPIOs off: %v", err) } return nil } // On turns on the RGBLED func (rgbled *DefaultRGBLED) On() error { return rgbled.White() } // Purple makes the RGBLED shine in purple func (rgbled *DefaultRGBLED) Purple() error { gpios := []*types.GPIO{ rgbled.BaseColorsToGPIO[types.BaseColorBlue], rgbled.BaseColorsToGPIO[types.BaseColorRed], } if err := rgbled.Off(); err != nil { return err } if err := rgbled.switchColors(gpios, true); err != nil { return fmt.Errorf("Can not operate with GPIOs: %v", err) } return nil } // Red makes the RGBLED shine in red func (rgbled *DefaultRGBLED) Red() error { gpios := []*types.GPIO{ rgbled.BaseColorsToGPIO[types.BaseColorRed], } if err := rgbled.Off(); err != nil { return err } if err := rgbled.switchColors(gpios, true); err != nil { return fmt.Errorf("Can not operate with GPIOs: %v", err) } return nil } // Turquoise makes the RGBLED shine in turquoise func (rgbled *DefaultRGBLED) Turquoise() error { gpios := []*types.GPIO{ rgbled.BaseColorsToGPIO[types.BaseColorBlue], rgbled.BaseColorsToGPIO[types.BaseColorGreen], } if err := rgbled.Off(); err != nil { return err } if err := rgbled.switchColors(gpios, true); err != nil { return fmt.Errorf("Can not operate with GPIOs: %v", err) } return nil } // White makes the RGBLED shine in white func (rgbled *DefaultRGBLED) White() error { gpios := []*types.GPIO{ rgbled.BaseColorsToGPIO[types.BaseColorBlue], rgbled.BaseColorsToGPIO[types.BaseColorGreen], rgbled.BaseColorsToGPIO[types.BaseColorRed], } if err := rgbled.Off(); err != nil { return err } if err := rgbled.switchColors(gpios, true); err != nil { return fmt.Errorf("Can not operate with GPIOs: %v", err) } return nil } // Yellow makes the RGBLED shine in yellow func (rgbled *DefaultRGBLED) Yellow() error { gpios := []*types.GPIO{ rgbled.BaseColorsToGPIO[types.BaseColorGreen], rgbled.BaseColorsToGPIO[types.BaseColorRed], } if err := rgbled.Off(); err != nil { return err } if err := rgbled.switchColors(gpios, true); err != nil { return fmt.Errorf("Can not operate with GPIOs: %v", err) } return nil } // Error makes the RGBLED shine in the error specified color func (rgbled *DefaultRGBLED) Error() error { return rgbled.switchColorBasedOnAction(rgbled.ActionMapping[types.LEDActionError]) } // Logfile makes the RGBLED shine in the logfile specified color func (rgbled *DefaultRGBLED) Logfile() error { return rgbled.switchColorBasedOnAction(rgbled.ActionMapping[types.LEDActionLogfile]) } // Sync makes the RGBLED shine in the sync specified color func (rgbled *DefaultRGBLED) Sync() error { return rgbled.switchColorBasedOnAction(rgbled.ActionMapping[types.LEDActionSync]) } // Warn makes the RGBLED shine in the warn specified color func (rgbled *DefaultRGBLED) Warn() error { return rgbled.switchColorBasedOnAction(rgbled.ActionMapping[types.LEDActionWarn]) } // Run makes the RGBLED shine in the run specified color func (rgbled *DefaultRGBLED) Run() error { return rgbled.switchColorBasedOnAction(rgbled.ActionMapping[types.LEDActionRun]) } func (rgbled *DefaultRGBLED) switchColors(gpios []*types.GPIO, on bool) error { 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) // if rpio.DetectEdge(rpio.P rpio.AnyEdge) { // log.Println("Test") // } pin.Pull(rpio.PullOff) pin.Output() if on { pin.High() } else { pin.Low() } } return nil } 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() } }