PKGBUILD/pkg/rgbled/default.go

240 lines
5.4 KiB
Go
Raw Normal View History

2019-06-16 18:11:10 +00:00
package rgbled
import (
"fmt"
"github.com/go-flucky/flucky/pkg/types"
"github.com/stianeikeland/go-rpio"
)
// DefaultRGBLED is a RGBLED which implement all functions of the interface
// RGBLED
type DefaultRGBLED struct {
2019-06-16 18:11:10 +00:00
*types.RGBLED
}
// Blue makes the RGBLED shine in blue
func (rgbled *DefaultRGBLED) Blue() error {
2019-06-16 18:11:10 +00:00
gpios := []*types.GPIO{
rgbled.BaseColorsToGPIO[types.BaseColorBlue],
2019-06-16 18:11:10 +00:00
}
if err := rgbled.Off(); err != nil {
2019-06-16 18:11:10 +00:00
return err
}
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
}
// Green makes the RGBLED shine in green
func (rgbled *DefaultRGBLED) Green() error {
2019-06-16 18:11:10 +00:00
gpios := []*types.GPIO{
rgbled.BaseColorsToGPIO[types.BaseColorGreen],
2019-06-16 18:11:10 +00:00
}
if err := rgbled.Off(); err != nil {
2019-06-16 18:11:10 +00:00
return err
}
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
}
// Off turns on the RGBLED off
func (rgbled *DefaultRGBLED) Off() error {
2019-06-16 18:11:10 +00:00
gpios := []*types.GPIO{
rgbled.BaseColorsToGPIO[types.BaseColorBlue],
rgbled.BaseColorsToGPIO[types.BaseColorGreen],
rgbled.BaseColorsToGPIO[types.BaseColorRed],
2019-06-16 18:11:10 +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
}
// 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 {
2019-06-16 18:11:10 +00:00
gpios := []*types.GPIO{
rgbled.BaseColorsToGPIO[types.BaseColorBlue],
rgbled.BaseColorsToGPIO[types.BaseColorRed],
2019-06-16 18:11:10 +00:00
}
if err := rgbled.Off(); err != nil {
2019-06-16 18:11:10 +00:00
return err
}
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
}
// Red makes the RGBLED shine in red
func (rgbled *DefaultRGBLED) Red() error {
2019-06-16 18:11:10 +00:00
gpios := []*types.GPIO{
rgbled.BaseColorsToGPIO[types.BaseColorRed],
2019-06-16 18:11:10 +00:00
}
if err := rgbled.Off(); err != nil {
2019-06-16 18:11:10 +00:00
return err
}
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
}
// Turquoise makes the RGBLED shine in turquoise
func (rgbled *DefaultRGBLED) Turquoise() error {
2019-06-16 18:11:10 +00:00
gpios := []*types.GPIO{
rgbled.BaseColorsToGPIO[types.BaseColorBlue],
rgbled.BaseColorsToGPIO[types.BaseColorGreen],
2019-06-16 18:11:10 +00:00
}
if err := rgbled.Off(); err != nil {
2019-06-16 18:11:10 +00:00
return err
}
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
}
// White makes the RGBLED shine in white
func (rgbled *DefaultRGBLED) White() error {
2019-06-16 18:11:10 +00:00
gpios := []*types.GPIO{
rgbled.BaseColorsToGPIO[types.BaseColorBlue],
rgbled.BaseColorsToGPIO[types.BaseColorGreen],
rgbled.BaseColorsToGPIO[types.BaseColorRed],
2019-06-16 18:11:10 +00:00
}
if err := rgbled.Off(); err != nil {
2019-06-16 18:11:10 +00:00
return err
}
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
}
// Yellow makes the RGBLED shine in yellow
func (rgbled *DefaultRGBLED) Yellow() error {
2019-06-16 18:11:10 +00:00
gpios := []*types.GPIO{
rgbled.BaseColorsToGPIO[types.BaseColorGreen],
rgbled.BaseColorsToGPIO[types.BaseColorRed],
2019-06-16 18:11:10 +00:00
}
if err := rgbled.Off(); err != nil {
2019-06-16 18:11:10 +00:00
return err
}
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
}
// 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 {
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
}
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()
}
}