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"
|
|
|
|
)
|
|
|
|
|
2014-03-22 05:54:29 +01:00
|
|
|
type pin interface {
|
|
|
|
Close() error
|
|
|
|
}
|
2014-03-02 20:21:23 +01:00
|
|
|
|
2014-04-06 02:00:54 +02:00
|
|
|
type digitalPinFactory func(pd *PinDesc, drv GPIODriver) DigitalPin
|
|
|
|
type analogPinFactory func(pd *PinDesc, drv GPIODriver) AnalogPin
|
|
|
|
type pwmPinFactory func(pd *PinDesc, drv GPIODriver) PWMPin
|
2014-04-06 01:22:11 +02:00
|
|
|
|
2014-03-22 05:54:29 +01:00
|
|
|
type gpioDriver struct {
|
2014-03-23 00:29:35 +01:00
|
|
|
pinMap PinMap
|
|
|
|
|
2014-04-06 01:22:11 +02:00
|
|
|
dpf digitalPinFactory
|
|
|
|
apf analogPinFactory
|
|
|
|
ppf pwmPinFactory
|
2014-03-23 00:29:35 +01:00
|
|
|
|
2014-03-22 20:41:24 +01:00
|
|
|
initializedPins map[string]pin
|
2014-03-02 20:21:23 +01:00
|
|
|
}
|
|
|
|
|
2014-04-06 03:20:09 +02:00
|
|
|
// NewGPIODriver returns a GPIODriver interface which allows control
|
|
|
|
// over the GPIO subsystem.
|
|
|
|
func NewGPIODriver(pinMap PinMap, dpf digitalPinFactory, apf analogPinFactory, ppf pwmPinFactory) 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
|
|
|
|
2014-03-22 20:41:24 +01:00
|
|
|
initializedPins: map[string]pin{},
|
2014-03-02 20:21:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-06 02:00:54 +02:00
|
|
|
func (io *gpioDriver) Unregister(id string) error {
|
|
|
|
if _, ok := io.initializedPins[id]; !ok {
|
|
|
|
return fmt.Errorf("gpio: pin %v is not registered yet, cannot unregister", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(io.initializedPins, id)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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-04-06 02:00:54 +02:00
|
|
|
if p, ok := io.initializedPins[pd.ID]; ok {
|
|
|
|
return p.(DigitalPin), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p := io.dpf(pd, io)
|
2014-03-23 00:29:35 +01:00
|
|
|
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-04-06 02:00:54 +02:00
|
|
|
if p, ok := io.initializedPins[pd.ID]; ok {
|
|
|
|
return p.(AnalogPin), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p := io.apf(pd, io)
|
2014-03-23 00:29:35 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2014-04-06 02:00:54 +02:00
|
|
|
if p, ok := io.initializedPins[pd.ID]; ok {
|
|
|
|
return p.(PWMPin), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p := io.ppf(pd, io)
|
2014-03-28 03:54:42 +01:00
|
|
|
io.initializedPins[pd.ID] = p
|
|
|
|
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2015-01-13 22:55:11 +01:00
|
|
|
func (io *gpioDriver) PinMap() PinMap {
|
|
|
|
return io.pinMap
|
|
|
|
}
|
|
|
|
|
2014-03-02 20:21:23 +01:00
|
|
|
func (io *gpioDriver) Close() error {
|
2014-03-22 05:54:29 +01:00
|
|
|
for _, p := range io.initializedPins {
|
|
|
|
if err := p.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-03-02 20:21:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|