2017-02-23 18:12:17 +08:00
|
|
|
package dragonboard
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-05-06 21:11:31 +08:00
|
|
|
"sync"
|
2017-02-23 18:12:17 +08:00
|
|
|
|
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
2023-11-16 03:51:52 +08:00
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2022-12-09 02:29:51 +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 {
|
2022-12-09 02:29:51 +08:00
|
|
|
name string
|
|
|
|
sys *system.Accesser
|
|
|
|
mutex sync.Mutex
|
|
|
|
pinMap map[string]int
|
2022-12-02 00:33:33 +08:00
|
|
|
*adaptors.DigitalPinsAdaptor
|
2022-12-09 02:29:51 +08:00
|
|
|
*adaptors.I2cBusAdaptor
|
2017-02-23 18:12:17 +08:00
|
|
|
}
|
|
|
|
|
2022-12-02 00:33:33 +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
|
2023-01-06 02:04:32 +08:00
|
|
|
//
|
|
|
|
// Optional parameters:
|
2023-05-20 20:25:21 +08:00
|
|
|
//
|
|
|
|
// adaptors.WithGpiodAccess(): use character device gpiod driver instead of sysfs
|
2024-02-14 01:16:18 +08:00
|
|
|
// adaptors.WithSpiGpioAccess(sclk, ncs, sdo, sdi): use GPIO's instead of /dev/spidev#.#
|
2023-12-06 03:26:28 +08:00
|
|
|
func NewAdaptor(opts ...func(adaptors.DigitalPinsOptioner)) *Adaptor {
|
2022-12-02 00:33:33 +08:00
|
|
|
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"),
|
2022-12-02 00:33:33 +08:00
|
|
|
sys: sys,
|
|
|
|
}
|
2023-01-06 02:04:32 +08:00
|
|
|
c.DigitalPinsAdaptor = adaptors.NewDigitalPinsAdaptor(sys, c.translateDigitalPin, opts...)
|
2022-12-09 02:29:51 +08:00
|
|
|
c.I2cBusAdaptor = adaptors.NewI2cBusAdaptor(sys, c.validateI2cBusNumber, defaultI2cBusNumber)
|
2022-12-02 00:33:33 +08:00
|
|
|
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 }
|
|
|
|
|
2022-12-02 00:33:33 +08:00
|
|
|
// Connect create new connection to board and pins.
|
|
|
|
func (c *Adaptor) Connect() error {
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
2022-12-09 02:29:51 +08:00
|
|
|
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
|
2022-12-02 00:33:33 +08:00
|
|
|
func (c *Adaptor) Finalize() error {
|
2017-05-06 21:11:31 +08:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
2022-12-02 00:33:33 +08:00
|
|
|
err := c.DigitalPinsAdaptor.Finalize()
|
|
|
|
|
2022-12-09 02:29:51 +08:00
|
|
|
if e := c.I2cBusAdaptor.Finalize(); e != nil {
|
|
|
|
err = multierror.Append(err, e)
|
2017-02-23 18:12:17 +08:00
|
|
|
}
|
2022-12-09 02:29:51 +08:00
|
|
|
|
2022-12-02 00:33:33 +08:00
|
|
|
return err
|
2017-02-23 18:12:17 +08:00
|
|
|
}
|
|
|
|
|
2022-12-09 02:29:51 +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
|
|
|
}
|
2022-12-09 02:29:51 +08:00
|
|
|
return nil
|
2017-02-23 18:12:17 +08:00
|
|
|
}
|
2017-04-29 18:43:55 +08:00
|
|
|
|
2022-12-02 00:33:33 +08:00
|
|
|
func (c *Adaptor) translateDigitalPin(id string) (string, int, error) {
|
|
|
|
if line, ok := c.pinMap[id]; ok {
|
|
|
|
return "", line, nil
|
2017-04-29 18:43:55 +08:00
|
|
|
}
|
2022-12-02 00:33:33 +08:00
|
|
|
return "", -1, fmt.Errorf("'%s' is not a valid id for a digital pin", id)
|
2022-11-27 23:06:09 +08:00
|
|
|
}
|