170 lines
3.0 KiB
Go
170 lines
3.0 KiB
Go
package rgbled
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/go-flucky/flucky/pkg/types"
|
|
"github.com/stianeikeland/go-rpio"
|
|
)
|
|
|
|
type LED struct {
|
|
*types.RGBLED
|
|
}
|
|
|
|
func (l *LED) Blue(on bool) error {
|
|
gpios := []*types.GPIO{
|
|
l.BaseColorsToGPIO[types.BaseColorBlue],
|
|
}
|
|
|
|
if err := l.Off(); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := l.operate(gpios, on); err != nil {
|
|
return fmt.Errorf("Can not operate with GPIOs: %v", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (l *LED) Green(on bool) error {
|
|
|
|
gpios := []*types.GPIO{
|
|
l.BaseColorsToGPIO[types.BaseColorGreen],
|
|
}
|
|
|
|
if err := l.Off(); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := l.operate(gpios, on); err != nil {
|
|
return fmt.Errorf("Can not operate with GPIOs: %v", err)
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (l *LED) Off() error {
|
|
|
|
gpios := []*types.GPIO{
|
|
l.BaseColorsToGPIO[types.BaseColorBlue],
|
|
l.BaseColorsToGPIO[types.BaseColorGreen],
|
|
l.BaseColorsToGPIO[types.BaseColorRed],
|
|
}
|
|
|
|
if err := l.operate(gpios, false); err != nil {
|
|
return fmt.Errorf("Can not turn GPIOs off: %v", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (l *LED) Purple(on bool) error {
|
|
gpios := []*types.GPIO{
|
|
l.BaseColorsToGPIO[types.BaseColorBlue],
|
|
l.BaseColorsToGPIO[types.BaseColorRed],
|
|
}
|
|
|
|
if err := l.Off(); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := l.operate(gpios, on); err != nil {
|
|
return fmt.Errorf("Can not operate with GPIOs: %v", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (l *LED) Red(on bool) error {
|
|
gpios := []*types.GPIO{
|
|
l.BaseColorsToGPIO[types.BaseColorRed],
|
|
}
|
|
|
|
if err := l.Off(); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := l.operate(gpios, on); err != nil {
|
|
return fmt.Errorf("Can not operate with GPIOs: %v", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (l *LED) Turquoise(on bool) error {
|
|
gpios := []*types.GPIO{
|
|
l.BaseColorsToGPIO[types.BaseColorBlue],
|
|
l.BaseColorsToGPIO[types.BaseColorGreen],
|
|
}
|
|
|
|
if err := l.Off(); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := l.operate(gpios, on); err != nil {
|
|
return fmt.Errorf("Can not operate with GPIOs: %v", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (l *LED) White(on bool) error {
|
|
gpios := []*types.GPIO{
|
|
l.BaseColorsToGPIO[types.BaseColorBlue],
|
|
l.BaseColorsToGPIO[types.BaseColorGreen],
|
|
l.BaseColorsToGPIO[types.BaseColorRed],
|
|
}
|
|
|
|
if err := l.Off(); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := l.operate(gpios, on); err != nil {
|
|
return fmt.Errorf("Can not operate with GPIOs: %v", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (l *LED) Yellow(on bool) error {
|
|
gpios := []*types.GPIO{
|
|
l.BaseColorsToGPIO[types.BaseColorGreen],
|
|
l.BaseColorsToGPIO[types.BaseColorRed],
|
|
}
|
|
|
|
if err := l.Off(); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := l.operate(gpios, on); err != nil {
|
|
return fmt.Errorf("Can not operate with GPIOs: %v", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (l *LED) operate(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 on {
|
|
pin.High()
|
|
} else {
|
|
pin.Low()
|
|
}
|
|
}
|
|
return nil
|
|
}
|