embd/gpiodriver.go

92 lines
1.8 KiB
Go
Raw Normal View History

2014-03-23 09:39:31 +01:00
// Generic GPIO driver.
2014-03-02 20:21:23 +01:00
package embd
import (
2014-03-23 00:29:35 +01:00
"errors"
2014-03-02 20:21:23 +01:00
"fmt"
)
type pin interface {
Close() error
}
2014-03-02 20:21:23 +01:00
type gpioDriver struct {
2014-03-23 00:29:35 +01:00
pinMap PinMap
dpf func(n int) DigitalPin
apf func(n int) AnalogPin
2014-03-28 03:54:42 +01:00
ppf func(n string) PWMPin
2014-03-23 00:29:35 +01:00
initializedPins map[string]pin
2014-03-02 20:21:23 +01:00
}
2014-03-28 03:54:42 +01:00
func newGPIODriver(pinMap PinMap, dpf func(n int) DigitalPin, apf func(n int) AnalogPin, ppf func(n string) PWMPin) GPIODriver {
2014-03-02 20:21:23 +01:00
return &gpioDriver{
2014-03-23 00:29:35 +01:00
pinMap: pinMap,
dpf: dpf,
apf: apf,
2014-03-28 03:54:42 +01:00
ppf: ppf,
2014-03-23 00:29:35 +01:00
initializedPins: map[string]pin{},
2014-03-02 20:21:23 +01:00
}
}
2014-03-23 00:29:35 +01:00
func (io *gpioDriver) DigitalPin(key interface{}) (DigitalPin, error) {
if io.dpf == nil {
return nil, errors.New("gpio: digital io not supported on this host")
}
2014-03-02 20:21:23 +01:00
2014-03-23 09:41:09 +01:00
pd, found := io.pinMap.Lookup(key, CapDigital)
2014-03-02 20:21:23 +01:00
if !found {
2014-03-23 00:29:35 +01:00
return nil, fmt.Errorf("gpio: could not find pin matching %v", key)
2014-03-02 20:21:23 +01:00
}
2014-03-23 00:29:35 +01:00
p := io.dpf(pd.DigitalLogical)
io.initializedPins[pd.ID] = p
return p, nil
}
func (io *gpioDriver) AnalogPin(key interface{}) (AnalogPin, error) {
if io.apf == nil {
return nil, errors.New("gpio: analog io not supported on this host")
2014-03-02 20:21:23 +01:00
}
2014-03-23 00:29:35 +01:00
pd, found := io.pinMap.Lookup(key, CapAnalog)
if !found {
return nil, fmt.Errorf("gpio: could not find pin matching %v", key)
}
2014-03-02 20:21:23 +01:00
2014-03-23 00:29:35 +01:00
p := io.apf(pd.AnalogLogical)
io.initializedPins[pd.ID] = p
2014-03-02 20:21:23 +01:00
2014-03-23 00:29:35 +01:00
return p, nil
2014-03-02 20:21:23 +01:00
}
2014-03-28 03:54:42 +01:00
func (io *gpioDriver) PWMPin(key interface{}) (PWMPin, error) {
if io.ppf == nil {
return nil, errors.New("gpio: pwm not supported on this host")
}
pd, found := io.pinMap.Lookup(key, CapPWM)
if !found {
return nil, fmt.Errorf("gpio: could not find pin matching %v", key)
}
p := io.ppf(pd.ID)
io.initializedPins[pd.ID] = p
return p, nil
}
2014-03-02 20:21:23 +01:00
func (io *gpioDriver) Close() error {
for _, p := range io.initializedPins {
if err := p.Close(); err != nil {
return err
}
2014-03-02 20:21:23 +01:00
}
return nil
}