2014-03-02 20:21:23 +01:00
|
|
|
package embd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/golang/glog"
|
|
|
|
)
|
|
|
|
|
2014-03-22 05:54:29 +01:00
|
|
|
type pin interface {
|
|
|
|
Close() error
|
|
|
|
}
|
2014-03-02 20:21:23 +01:00
|
|
|
|
2014-03-22 05:54:29 +01:00
|
|
|
type gpioDriver struct {
|
2014-03-02 20:21:23 +01:00
|
|
|
pinMap PinMap
|
2014-03-22 20:41:24 +01:00
|
|
|
initializedPins map[string]pin
|
2014-03-02 20:21:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func newGPIODriver(pinMap PinMap) *gpioDriver {
|
|
|
|
return &gpioDriver{
|
|
|
|
pinMap: pinMap,
|
2014-03-22 20:41:24 +01:00
|
|
|
initializedPins: map[string]pin{},
|
2014-03-02 20:21:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (io *gpioDriver) lookupKey(key interface{}) (*PinDesc, bool) {
|
|
|
|
return io.pinMap.Lookup(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (io *gpioDriver) digitalPin(key interface{}) (*digitalPin, error) {
|
|
|
|
pd, found := io.lookupKey(key)
|
|
|
|
if !found {
|
2014-03-22 05:54:29 +01:00
|
|
|
return nil, fmt.Errorf("gpio: could not find pin matching %q", key)
|
2014-03-02 20:21:23 +01:00
|
|
|
}
|
|
|
|
|
2014-03-22 20:41:24 +01:00
|
|
|
id := pd.ID
|
2014-03-02 20:21:23 +01:00
|
|
|
|
2014-03-22 20:41:24 +01:00
|
|
|
p, ok := io.initializedPins[id]
|
2014-03-02 20:21:23 +01:00
|
|
|
if ok {
|
2014-03-22 05:54:29 +01:00
|
|
|
dp, ok := p.(*digitalPin)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("gpio: sorry, pin %q is already initialized for a different mode", key)
|
|
|
|
}
|
|
|
|
return dp, nil
|
2014-03-02 20:21:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if pd.Caps&CapNormal == 0 {
|
2014-03-22 05:54:29 +01:00
|
|
|
return nil, fmt.Errorf("gpio: sorry, pin %q cannot be used for digital io", key)
|
2014-03-02 20:21:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if pd.Caps != CapNormal {
|
2014-03-22 05:54:29 +01:00
|
|
|
glog.Infof("gpio: pin %q is not a dedicated digital io pin. please refer to the system reference manual for more details", key)
|
2014-03-02 20:21:23 +01:00
|
|
|
}
|
|
|
|
|
2014-03-22 20:41:24 +01:00
|
|
|
dp := newDigitalPin(pd.DigitalLogical)
|
|
|
|
io.initializedPins[id] = dp
|
2014-03-02 20:21:23 +01:00
|
|
|
|
2014-03-22 05:54:29 +01:00
|
|
|
return dp, nil
|
2014-03-02 20:21:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (io *gpioDriver) DigitalPin(key interface{}) (DigitalPin, error) {
|
|
|
|
return io.digitalPin(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|