2014-03-23 14:09:31 +05:30
|
|
|
// Generic GPIO driver.
|
|
|
|
|
2014-03-03 00:51:23 +05:30
|
|
|
package embd
|
|
|
|
|
|
|
|
import (
|
2014-03-23 04:59:35 +05:30
|
|
|
"errors"
|
2014-03-03 00:51:23 +05:30
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2014-03-22 10:24:29 +05:30
|
|
|
type pin interface {
|
|
|
|
Close() error
|
|
|
|
}
|
2014-03-03 00:51:23 +05:30
|
|
|
|
2014-04-06 05:30:54 +05:30
|
|
|
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 04:52:11 +05:30
|
|
|
|
2014-03-22 10:24:29 +05:30
|
|
|
type gpioDriver struct {
|
2014-03-23 04:59:35 +05:30
|
|
|
pinMap PinMap
|
|
|
|
|
2014-04-06 04:52:11 +05:30
|
|
|
dpf digitalPinFactory
|
|
|
|
apf analogPinFactory
|
|
|
|
ppf pwmPinFactory
|
2014-03-23 04:59:35 +05:30
|
|
|
|
2014-03-23 01:11:24 +05:30
|
|
|
initializedPins map[string]pin
|
2014-03-03 00:51:23 +05:30
|
|
|
}
|
|
|
|
|
2014-04-06 06:50:09 +05:30
|
|
|
// 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-03 00:51:23 +05:30
|
|
|
return &gpioDriver{
|
2014-03-23 04:59:35 +05:30
|
|
|
pinMap: pinMap,
|
|
|
|
dpf: dpf,
|
|
|
|
apf: apf,
|
2014-03-28 08:24:42 +05:30
|
|
|
ppf: ppf,
|
2014-03-23 04:59:35 +05:30
|
|
|
|
2014-03-23 01:11:24 +05:30
|
|
|
initializedPins: map[string]pin{},
|
2014-03-03 00:51:23 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-06 05:30:54 +05:30
|
|
|
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 04:59:35 +05:30
|
|
|
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-03 00:51:23 +05:30
|
|
|
|
2014-03-23 14:11:09 +05:30
|
|
|
pd, found := io.pinMap.Lookup(key, CapDigital)
|
2014-03-03 00:51:23 +05:30
|
|
|
if !found {
|
2014-03-23 04:59:35 +05:30
|
|
|
return nil, fmt.Errorf("gpio: could not find pin matching %v", key)
|
2014-03-03 00:51:23 +05:30
|
|
|
}
|
|
|
|
|
2014-04-06 05:30:54 +05:30
|
|
|
if p, ok := io.initializedPins[pd.ID]; ok {
|
|
|
|
return p.(DigitalPin), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p := io.dpf(pd, io)
|
2014-03-23 04:59:35 +05:30
|
|
|
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-03 00:51:23 +05:30
|
|
|
}
|
|
|
|
|
2014-03-23 04:59:35 +05:30
|
|
|
pd, found := io.pinMap.Lookup(key, CapAnalog)
|
|
|
|
if !found {
|
|
|
|
return nil, fmt.Errorf("gpio: could not find pin matching %v", key)
|
|
|
|
}
|
2014-03-03 00:51:23 +05:30
|
|
|
|
2014-04-06 05:30:54 +05:30
|
|
|
if p, ok := io.initializedPins[pd.ID]; ok {
|
|
|
|
return p.(AnalogPin), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p := io.apf(pd, io)
|
2014-03-23 04:59:35 +05:30
|
|
|
io.initializedPins[pd.ID] = p
|
2014-03-03 00:51:23 +05:30
|
|
|
|
2014-03-23 04:59:35 +05:30
|
|
|
return p, nil
|
2014-03-03 00:51:23 +05:30
|
|
|
}
|
|
|
|
|
2014-03-28 08:24:42 +05:30
|
|
|
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 05:30:54 +05:30
|
|
|
if p, ok := io.initializedPins[pd.ID]; ok {
|
|
|
|
return p.(PWMPin), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p := io.ppf(pd, io)
|
2014-03-28 08:24:42 +05:30
|
|
|
io.initializedPins[pd.ID] = p
|
|
|
|
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2014-03-03 00:51:23 +05:30
|
|
|
func (io *gpioDriver) Close() error {
|
2014-03-22 10:24:29 +05:30
|
|
|
for _, p := range io.initializedPins {
|
|
|
|
if err := p.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-03-03 00:51:23 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|