1
0
mirror of https://github.com/kidoman/embd synced 2024-12-22 21:00:05 +01:00
embd/gpiodriver.go

119 lines
2.4 KiB
Go
Raw Normal View History

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"
)
type pin interface {
Close() error
}
2014-03-03 00:51:23 +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
type gpioDriver struct {
2014-03-23 04:59:35 +05:30
pinMap PinMap
dpf digitalPinFactory
apf analogPinFactory
ppf pwmPinFactory
2014-03-23 04:59:35 +05:30
initializedPins map[string]pin
2014-03-03 00:51:23 +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
initializedPins: map[string]pin{},
2014-03-03 00:51:23 +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
}
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
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)
}
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 {
for _, p := range io.initializedPins {
if err := p.Close(); err != nil {
return err
}
2014-03-03 00:51:23 +05:30
}
return nil
}