package rgbled import ( "fmt" "github.com/go-flucky/flucky/pkg/types" "github.com/stianeikeland/go-rpio" ) type DefaultRGBLED struct { *types.RGBLED } 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 } 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 } 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 } func (rgbled *DefaultRGBLED) On() error { return rgbled.White() } 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 } 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 } 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 } 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 } 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 } func (rgbled *DefaultRGBLED) Error() error { return rgbled.switchColorBasedOnAction(rgbled.ActionMapping[types.LEDActionError]) } func (rgbled *DefaultRGBLED) Logfile() error { return rgbled.switchColorBasedOnAction(rgbled.ActionMapping[types.LEDActionLogfile]) } func (rgbled *DefaultRGBLED) Sync() error { return rgbled.switchColorBasedOnAction(rgbled.ActionMapping[types.LEDActionSync]) } func (rgbled *DefaultRGBLED) Warn() error { return rgbled.switchColorBasedOnAction(rgbled.ActionMapping[types.LEDActionWarn]) } 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() } }