2016-01-30 18:00:27 +08:00
|
|
|
package chip
|
|
|
|
|
|
|
|
import (
|
2017-01-11 08:39:23 +08:00
|
|
|
"fmt"
|
2023-06-11 01:25:01 +08:00
|
|
|
"os"
|
2017-01-11 08:39:23 +08:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2016-11-28 04:56:09 +08:00
|
|
|
"strings"
|
2017-05-06 21:11:13 +08:00
|
|
|
"sync"
|
2016-01-30 18:00:27 +08:00
|
|
|
|
2016-11-08 00:43:55 +08:00
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
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"
|
2016-01-30 18:00:27 +08:00
|
|
|
)
|
|
|
|
|
2022-12-09 02:29:51 +08:00
|
|
|
const defaultI2cBusNumber = 1
|
|
|
|
|
2022-12-02 00:33:33 +08:00
|
|
|
// Valids pins are the XIO-P0 through XIO-P7 pins from the
|
|
|
|
// extender (pins 13-20 on header 14), as well as the SoC pins
|
|
|
|
// aka all the other pins.
|
2017-04-22 16:41:47 +08:00
|
|
|
type sysfsPin struct {
|
|
|
|
pin int
|
|
|
|
pwmPin int
|
|
|
|
}
|
|
|
|
|
2016-11-28 04:56:09 +08:00
|
|
|
// Adaptor represents a Gobot Adaptor for a C.H.I.P.
|
2016-09-26 02:19:19 +08:00
|
|
|
type Adaptor struct {
|
2022-12-09 02:29:51 +08:00
|
|
|
name string
|
|
|
|
sys *system.Accesser
|
|
|
|
mutex sync.Mutex
|
|
|
|
pinmap map[string]sysfsPin
|
2022-12-02 00:33:33 +08:00
|
|
|
*adaptors.DigitalPinsAdaptor
|
2022-12-06 01:28:57 +08:00
|
|
|
*adaptors.PWMPinsAdaptor
|
2022-12-09 02:29:51 +08:00
|
|
|
*adaptors.I2cBusAdaptor
|
2016-11-28 04:56:09 +08:00
|
|
|
}
|
|
|
|
|
2016-09-26 02:19:19 +08:00
|
|
|
// NewAdaptor creates a C.H.I.P. 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
|
|
|
|
// adaptors.WithSpiGpioAccess(sclk, nss, mosi, miso): use GPIO's instead of /dev/spidev#.#
|
2023-01-06 02:04:32 +08:00
|
|
|
func NewAdaptor(opts ...func(adaptors.Optioner)) *Adaptor {
|
2022-12-02 00:33:33 +08:00
|
|
|
sys := system.NewAccesser()
|
2016-09-26 02:19:19 +08:00
|
|
|
c := &Adaptor{
|
2022-12-02 00:33:33 +08:00
|
|
|
name: gobot.DefaultName("CHIP"),
|
|
|
|
sys: sys,
|
2017-04-27 16:04:54 +08:00
|
|
|
}
|
|
|
|
|
2022-12-02 00:33:33 +08:00
|
|
|
c.pinmap = chipPins
|
|
|
|
baseAddr, _ := getXIOBase()
|
|
|
|
for i := 0; i < 8; i++ {
|
|
|
|
pin := fmt.Sprintf("XIO-P%d", i)
|
|
|
|
c.pinmap[pin] = sysfsPin{pin: baseAddr + i, pwmPin: -1}
|
|
|
|
}
|
|
|
|
|
2023-01-06 02:04:32 +08:00
|
|
|
c.DigitalPinsAdaptor = adaptors.NewDigitalPinsAdaptor(sys, c.translateDigitalPin, opts...)
|
2022-12-06 01:28:57 +08:00
|
|
|
c.PWMPinsAdaptor = adaptors.NewPWMPinsAdaptor(sys, c.translatePWMPin)
|
2022-12-09 02:29:51 +08:00
|
|
|
c.I2cBusAdaptor = adaptors.NewI2cBusAdaptor(sys, c.validateI2cBusNumber, defaultI2cBusNumber)
|
2017-04-27 16:04:54 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2022-11-05 14:42:28 +08:00
|
|
|
// NewProAdaptor creates a C.H.I.P. Pro Adaptor
|
2017-04-27 16:04:54 +08:00
|
|
|
func NewProAdaptor() *Adaptor {
|
2022-12-02 00:33:33 +08:00
|
|
|
c := NewAdaptor()
|
|
|
|
c.name = gobot.DefaultName("CHIP Pro")
|
|
|
|
c.pinmap = chipProPins
|
2016-01-30 18:00:27 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2016-09-26 02:19:19 +08:00
|
|
|
// 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 }
|
2016-01-30 18:00:27 +08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-12-06 01:28:57 +08:00
|
|
|
if err := c.PWMPinsAdaptor.Connect(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.DigitalPinsAdaptor.Connect()
|
2016-01-30 18:00:27 +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:13 +08:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
2022-12-02 00:33:33 +08:00
|
|
|
err := c.DigitalPinsAdaptor.Finalize()
|
|
|
|
|
2022-12-06 01:28:57 +08:00
|
|
|
if e := c.PWMPinsAdaptor.Finalize(); e != nil {
|
|
|
|
err = multierror.Append(err, e)
|
2017-04-22 16:41:47 +08:00
|
|
|
}
|
2022-12-06 01:28:57 +08:00
|
|
|
|
2022-12-09 02:29:51 +08:00
|
|
|
if e := c.I2cBusAdaptor.Finalize(); e != nil {
|
|
|
|
err = multierror.Append(err, e)
|
2017-01-20 07:17:41 +08:00
|
|
|
}
|
|
|
|
|
2022-12-09 02:29:51 +08:00
|
|
|
return err
|
2017-02-06 21:50:14 +08:00
|
|
|
}
|
|
|
|
|
2017-01-11 08:39:23 +08:00
|
|
|
func getXIOBase() (baseAddr int, err error) {
|
|
|
|
// Default to original base from 4.3 kernel
|
|
|
|
baseAddr = 408
|
|
|
|
const expanderID = "pcf8574a"
|
|
|
|
|
|
|
|
labels, err := filepath.Glob("/sys/class/gpio/*/label")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, labelPath := range labels {
|
2023-06-11 01:25:01 +08:00
|
|
|
label, err := os.ReadFile(labelPath)
|
2017-01-11 08:39:23 +08:00
|
|
|
if err != nil {
|
|
|
|
return baseAddr, err
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(string(label), expanderID) {
|
|
|
|
expanderPath, _ := filepath.Split(labelPath)
|
|
|
|
basePath := filepath.Join(expanderPath, "base")
|
2023-06-11 01:25:01 +08:00
|
|
|
base, err := os.ReadFile(basePath)
|
2017-01-11 08:39:23 +08:00
|
|
|
if err != nil {
|
|
|
|
return baseAddr, err
|
|
|
|
}
|
|
|
|
baseAddr, _ = strconv.Atoi(strings.TrimSpace(string(base)))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-11-28 04:56:09 +08:00
|
|
|
|
2017-01-11 08:39:23 +08:00
|
|
|
return baseAddr, nil
|
2016-11-28 04:56:09 +08:00
|
|
|
}
|
2017-04-22 17:50:39 +08:00
|
|
|
|
2022-12-09 02:29:51 +08:00
|
|
|
func (c *Adaptor) validateI2cBusNumber(busNr int) error {
|
|
|
|
// Valid bus number is [0..2] which corresponds to /dev/i2c-0 through /dev/i2c-2.
|
|
|
|
if (busNr < 0) || (busNr > 2) {
|
|
|
|
return fmt.Errorf("Bus number %d out of range", busNr)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-02 00:33:33 +08:00
|
|
|
func (c *Adaptor) translateDigitalPin(id string) (string, int, error) {
|
2022-11-27 23:06:09 +08:00
|
|
|
if val, ok := c.pinmap[id]; ok {
|
2022-12-02 00:33:33 +08:00
|
|
|
return "", val.pin, nil
|
2022-11-27 23:06:09 +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
|
|
|
}
|
2022-12-06 01:28:57 +08:00
|
|
|
|
|
|
|
func (c *Adaptor) translatePWMPin(id string) (string, int, error) {
|
|
|
|
sysPin, ok := c.pinmap[id]
|
|
|
|
if !ok {
|
|
|
|
return "", -1, fmt.Errorf("'%s' is not a valid id for a pin", id)
|
|
|
|
}
|
|
|
|
if sysPin.pwmPin == -1 {
|
|
|
|
return "", -1, fmt.Errorf("'%s' is not a valid id for a PWM pin", id)
|
|
|
|
}
|
|
|
|
return "/sys/class/pwm/pwmchip0", sysPin.pwmPin, nil
|
|
|
|
}
|