PKGBUILD/pkg/led/led.go

72 lines
1.2 KiB
Go

package led
import (
"fmt"
"github.com/go-flucky/flucky/pkg/types"
"github.com/stianeikeland/go-rpio"
)
type LED interface {
On() error
Off() error
Toggel() error
}
type RGBLED struct {
*types.LED
}
func (l *RGBLED) On() error {
if err := rpio.Open(); err != nil {
return fmt.Errorf("Can not open rpio: %v", err)
}
defer rpio.Close()
gpio, err := types.GPIOToInt(*l.GPIONumber)
if err != nil {
return fmt.Errorf("Can not determine %v into integer: %v", l.GPIONumber, err)
}
pin := rpio.Pin(gpio)
pin.High()
return nil
}
func (l *RGBLED) Off() error {
if err := rpio.Open(); err != nil {
return fmt.Errorf("Can not open rpio: %v", err)
}
defer rpio.Close()
gpio, err := types.GPIOToInt(*l.GPIONumber)
if err != nil {
return fmt.Errorf("Can not determine %v into integer: %v", l.GPIONumber, err)
}
pin := rpio.Pin(gpio)
pin.Low()
return nil
}
func (l *RGBLED) Toggel() error {
if err := rpio.Open(); err != nil {
return fmt.Errorf("Can not open rpio: %v", err)
}
defer rpio.Close()
gpio, err := types.GPIOToInt(*l.GPIONumber)
if err != nil {
return fmt.Errorf("Can not determine %v into integer: %v", l.GPIONumber, err)
}
pin := rpio.Pin(gpio)
pin.Toggle()
return nil
}