hybridgroup.gobot/platforms/dragonboard/dragonboard_adaptor.go

115 lines
2.7 KiB
Go
Raw Normal View History

2017-02-23 18:12:17 +08:00
package dragonboard
import (
"fmt"
"sync"
2017-02-23 18:12:17 +08:00
multierror "github.com/hashicorp/go-multierror"
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/platforms/adaptors"
"gobot.io/x/gobot/v2/system"
2017-02-23 18:12:17 +08:00
)
const defaultI2cBusNumber = 0
2017-02-23 23:54:13 +08:00
// Adaptor represents a Gobot Adaptor for a DragonBoard 410c
2017-02-23 18:12:17 +08:00
type Adaptor struct {
name string
sys *system.Accesser
mutex sync.Mutex
pinMap map[string]int
*adaptors.DigitalPinsAdaptor
*adaptors.I2cBusAdaptor
2017-02-23 18:12:17 +08:00
}
// Valid pins are the GPIO_A through GPIO_L pins from the
// extender (pins 23-34 on header J8), as well as the SoC pins
// aka all the other pins, APQ GPIO_0-GPIO_122 and PM_MPP_0-4.
2017-02-23 18:12:17 +08:00
var fixedPins = map[string]int{
"GPIO_A": 36,
"GPIO_B": 12,
"GPIO_C": 13,
"GPIO_D": 69,
"GPIO_E": 115,
"GPIO_F": 507,
"GPIO_G": 24,
"GPIO_H": 25,
"GPIO_I": 35,
"GPIO_J": 34,
"GPIO_K": 28,
"GPIO_L": 33,
"LED_1": 21,
"LED_2": 120,
}
2017-02-23 23:54:13 +08:00
// NewAdaptor creates a DragonBoard 410c Adaptor
//
// Optional parameters:
//
// adaptors.WithGpiodAccess(): use character device gpiod driver instead of sysfs
// adaptors.WithSpiGpioAccess(sclk, ncs, sdo, sdi): use GPIO's instead of /dev/spidev#.#
func NewAdaptor(opts ...func(adaptors.DigitalPinsOptioner)) *Adaptor {
sys := system.NewAccesser()
2017-02-23 18:12:17 +08:00
c := &Adaptor{
2022-11-21 02:22:26 +08:00
name: gobot.DefaultName("DragonBoard"),
sys: sys,
}
c.DigitalPinsAdaptor = adaptors.NewDigitalPinsAdaptor(sys, c.translateDigitalPin, opts...)
c.I2cBusAdaptor = adaptors.NewI2cBusAdaptor(sys, c.validateI2cBusNumber, defaultI2cBusNumber)
c.pinMap = fixedPins
for i := 0; i < 122; i++ {
pin := fmt.Sprintf("GPIO_%d", i)
c.pinMap[pin] = i
2017-02-23 18:12:17 +08:00
}
return c
}
// Name returns the name of the Adaptor
func (c *Adaptor) Name() string { return c.name }
// SetName sets the name of the Adaptor
func (c *Adaptor) SetName(n string) { c.name = n }
// Connect create new connection to board and pins.
func (c *Adaptor) Connect() error {
c.mutex.Lock()
defer c.mutex.Unlock()
if err := c.I2cBusAdaptor.Connect(); err != nil {
return err
}
return c.DigitalPinsAdaptor.Connect()
2017-02-23 18:12:17 +08:00
}
// Finalize closes connection to board and pins
func (c *Adaptor) Finalize() error {
c.mutex.Lock()
defer c.mutex.Unlock()
err := c.DigitalPinsAdaptor.Finalize()
if e := c.I2cBusAdaptor.Finalize(); e != nil {
err = multierror.Append(err, e)
2017-02-23 18:12:17 +08:00
}
return err
2017-02-23 18:12:17 +08:00
}
func (c *Adaptor) validateI2cBusNumber(busNr int) error {
// Valid bus number is [0..1] which corresponds to /dev/i2c-0 through /dev/i2c-1.
if (busNr < 0) || (busNr > 1) {
return fmt.Errorf("Bus number %d out of range", busNr)
2017-02-23 18:12:17 +08:00
}
return nil
2017-02-23 18:12:17 +08:00
}
func (c *Adaptor) translateDigitalPin(id string) (string, int, error) {
if line, ok := c.pinMap[id]; ok {
return "", line, nil
}
return "", -1, fmt.Errorf("'%s' is not a valid id for a digital pin", id)
}