2014-04-28 04:39:51 -07:00
|
|
|
package beaglebone
|
2014-04-26 03:11:51 -07:00
|
|
|
|
|
|
|
import (
|
2014-06-11 13:59:06 -06:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2014-04-26 03:11:51 -07:00
|
|
|
"strconv"
|
2014-06-11 13:59:06 -06:00
|
|
|
"strings"
|
2017-05-06 15:10:59 +02:00
|
|
|
"sync"
|
2014-10-16 10:59:23 -05:00
|
|
|
|
2016-11-07 17:33:11 +01:00
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
2023-05-20 14:25:21 +02:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/adaptors"
|
|
|
|
"gobot.io/x/gobot/v2/system"
|
2014-04-26 03:11:51 -07:00
|
|
|
)
|
|
|
|
|
2017-12-14 14:54:42 +01:00
|
|
|
type pwmPinData struct {
|
2022-12-05 18:28:57 +01:00
|
|
|
channel int
|
|
|
|
dir string
|
|
|
|
dirRegexp string
|
2017-12-14 14:54:42 +01:00
|
|
|
}
|
|
|
|
|
2022-12-08 19:29:51 +01:00
|
|
|
const (
|
2022-12-17 11:56:11 +01:00
|
|
|
pwmPeriodDefault = 500000 // 0.5 ms = 2 kHz
|
|
|
|
|
2022-12-08 19:29:51 +01:00
|
|
|
defaultI2cBusNumber = 2
|
2022-12-17 11:56:11 +01:00
|
|
|
|
|
|
|
defaultSpiBusNumber = 0
|
|
|
|
defaultSpiChipNumber = 0
|
|
|
|
defaultSpiMode = 0
|
|
|
|
defaultSpiBitsNumber = 8
|
|
|
|
defaultSpiMaxSpeed = 500000
|
2022-12-08 19:29:51 +01:00
|
|
|
)
|
2017-05-03 10:38:45 +02:00
|
|
|
|
2017-12-19 13:42:49 +01:00
|
|
|
// Adaptor is the gobot.Adaptor representation for the Beaglebone Black/Green
|
2016-09-25 14:12:52 +02:00
|
|
|
type Adaptor struct {
|
2022-12-01 17:33:33 +01:00
|
|
|
name string
|
|
|
|
sys *system.Accesser
|
|
|
|
mutex sync.Mutex
|
|
|
|
*adaptors.DigitalPinsAdaptor
|
2022-12-05 18:28:57 +01:00
|
|
|
*adaptors.PWMPinsAdaptor
|
2022-12-08 19:29:51 +01:00
|
|
|
*adaptors.I2cBusAdaptor
|
2022-12-17 11:56:11 +01:00
|
|
|
*adaptors.SpiBusAdaptor
|
2022-11-05 07:42:28 +01:00
|
|
|
usrLed string
|
|
|
|
analogPath string
|
|
|
|
pinMap map[string]int
|
|
|
|
pwmPinMap map[string]pwmPinData
|
|
|
|
analogPinMap map[string]string
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2017-12-19 13:42:49 +01:00
|
|
|
// NewAdaptor returns a new Beaglebone Black/Green Adaptor
|
2023-01-05 19:04:32 +01:00
|
|
|
//
|
|
|
|
// Optional parameters:
|
2023-05-19 14:16:22 +02: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-05 19:04:32 +01:00
|
|
|
func NewAdaptor(opts ...func(adaptors.Optioner)) *Adaptor {
|
2022-12-01 17:33:33 +01:00
|
|
|
sys := system.NewAccesser()
|
|
|
|
c := &Adaptor{
|
2017-12-19 13:42:49 +01:00
|
|
|
name: gobot.DefaultName("BeagleboneBlack"),
|
2022-12-01 17:33:33 +01:00
|
|
|
sys: sys,
|
2017-12-14 14:54:42 +01:00
|
|
|
pinMap: bbbPinMap,
|
|
|
|
pwmPinMap: bbbPwmPinMap,
|
|
|
|
analogPinMap: bbbAnalogPinMap,
|
2022-12-05 18:28:57 +01:00
|
|
|
usrLed: "/sys/class/leds/beaglebone:green:",
|
|
|
|
analogPath: "/sys/bus/iio/devices/iio:device0",
|
2014-05-22 19:32:09 -07:00
|
|
|
}
|
2014-11-09 14:35:36 -08:00
|
|
|
|
2023-01-05 19:04:32 +01:00
|
|
|
c.DigitalPinsAdaptor = adaptors.NewDigitalPinsAdaptor(sys, c.translateAndMuxDigitalPin, opts...)
|
2022-12-05 18:28:57 +01:00
|
|
|
c.PWMPinsAdaptor = adaptors.NewPWMPinsAdaptor(sys, c.translateAndMuxPWMPin,
|
|
|
|
adaptors.WithPWMPinDefaultPeriod(pwmPeriodDefault))
|
2022-12-08 19:29:51 +01:00
|
|
|
c.I2cBusAdaptor = adaptors.NewI2cBusAdaptor(sys, c.validateI2cBusNumber, defaultI2cBusNumber)
|
2022-12-17 11:56:11 +01:00
|
|
|
c.SpiBusAdaptor = adaptors.NewSpiBusAdaptor(sys, c.validateSpiBusNumber, defaultSpiBusNumber, defaultSpiChipNumber,
|
|
|
|
defaultSpiMode, defaultSpiBitsNumber, defaultSpiMaxSpeed)
|
2022-12-01 17:33:33 +01:00
|
|
|
return c
|
2016-11-26 21:48:10 +01:00
|
|
|
}
|
|
|
|
|
2016-09-25 14:12:52 +02:00
|
|
|
// Name returns the Adaptor name
|
2022-12-01 17:33:33 +01:00
|
|
|
func (c *Adaptor) Name() string { return c.name }
|
2016-09-25 14:12:52 +02:00
|
|
|
|
|
|
|
// SetName sets the Adaptor name
|
2022-12-01 17:33:33 +01:00
|
|
|
func (c *Adaptor) SetName(n string) { c.name = n }
|
2014-04-28 04:39:51 -07:00
|
|
|
|
2022-12-01 17:33:33 +01:00
|
|
|
// Connect create new connection to board and pins.
|
|
|
|
func (c *Adaptor) Connect() error {
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
2022-12-17 11:56:11 +01:00
|
|
|
if err := c.SpiBusAdaptor.Connect(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-08 19:29:51 +01:00
|
|
|
if err := c.I2cBusAdaptor.Connect(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-05 18:28:57 +01:00
|
|
|
if err := c.PWMPinsAdaptor.Connect(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.DigitalPinsAdaptor.Connect()
|
2022-12-01 17:33:33 +01:00
|
|
|
}
|
2014-04-26 03:11:51 -07:00
|
|
|
|
2014-12-31 06:46:47 -08:00
|
|
|
// Finalize releases all i2c devices and exported analog, digital, pwm pins.
|
2022-12-01 17:33:33 +01:00
|
|
|
func (c *Adaptor) Finalize() error {
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
2017-05-06 15:10:59 +02:00
|
|
|
|
2022-12-01 17:33:33 +01:00
|
|
|
err := c.DigitalPinsAdaptor.Finalize()
|
|
|
|
|
2022-12-05 18:28:57 +01:00
|
|
|
if e := c.PWMPinsAdaptor.Finalize(); e != nil {
|
|
|
|
err = multierror.Append(err, e)
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
2022-12-05 18:28:57 +01:00
|
|
|
|
2022-12-08 19:29:51 +01:00
|
|
|
if e := c.I2cBusAdaptor.Finalize(); e != nil {
|
|
|
|
err = multierror.Append(err, e)
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
2022-12-08 19:29:51 +01:00
|
|
|
|
2022-12-17 11:56:11 +01:00
|
|
|
if e := c.SpiBusAdaptor.Finalize(); e != nil {
|
|
|
|
err = multierror.Append(err, e)
|
2017-12-19 16:45:16 +01:00
|
|
|
}
|
2022-12-01 17:33:33 +01:00
|
|
|
return err
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
2014-10-16 10:59:23 -05:00
|
|
|
|
|
|
|
// DigitalWrite writes a digital value to specified pin.
|
|
|
|
// valid usr pin values are usr0, usr1, usr2 and usr3
|
2022-12-01 17:33:33 +01:00
|
|
|
func (c *Adaptor) DigitalWrite(id string, val byte) error {
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
2022-11-27 16:06:09 +01:00
|
|
|
|
|
|
|
if strings.Contains(id, "usr") {
|
2023-10-20 20:50:42 +02:00
|
|
|
fi, e := c.sys.OpenFile(c.usrLed+id+"/brightness", os.O_WRONLY|os.O_APPEND, 0o666)
|
2023-05-19 14:16:22 +02:00
|
|
|
defer fi.Close() //nolint:staticcheck // for historical reasons
|
2017-05-03 10:38:45 +02:00
|
|
|
if e != nil {
|
|
|
|
return e
|
2014-09-17 15:20:08 -07:00
|
|
|
}
|
2022-11-27 16:06:09 +01:00
|
|
|
_, err := fi.WriteString(strconv.Itoa(int(val)))
|
2014-11-16 14:18:29 -08:00
|
|
|
return err
|
2014-09-17 15:20:08 -07:00
|
|
|
}
|
2022-11-27 16:06:09 +01:00
|
|
|
|
2022-12-01 17:33:33 +01:00
|
|
|
return c.DigitalPinsAdaptor.DigitalWrite(id, val)
|
2017-04-29 10:48:12 +02:00
|
|
|
}
|
|
|
|
|
2014-10-16 10:59:23 -05:00
|
|
|
// AnalogRead returns an analog value from specified pin
|
2022-12-01 17:33:33 +01:00
|
|
|
func (c *Adaptor) AnalogRead(pin string) (val int, err error) {
|
|
|
|
analogPin, err := c.translateAnalogPin(pin)
|
2014-11-19 17:18:01 -08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-10-20 20:50:42 +02:00
|
|
|
fi, err := c.sys.OpenFile(fmt.Sprintf("%v/%v", c.analogPath, analogPin), os.O_RDONLY, 0o644)
|
2023-05-19 14:16:22 +02:00
|
|
|
defer fi.Close() //nolint:staticcheck // for historical reasons
|
2014-11-16 14:18:29 -08:00
|
|
|
|
2014-11-09 14:35:36 -08:00
|
|
|
if err != nil {
|
2014-11-16 14:18:29 -08:00
|
|
|
return
|
2014-11-09 14:35:36 -08:00
|
|
|
}
|
|
|
|
|
2023-10-20 20:50:42 +02:00
|
|
|
buf := make([]byte, 1024)
|
2014-11-16 14:18:29 -08:00
|
|
|
_, err = fi.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-11-09 14:35:36 -08:00
|
|
|
|
2014-11-16 14:18:29 -08:00
|
|
|
val, _ = strconv.Atoi(strings.Split(string(buf), "\n")[0])
|
|
|
|
return
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2022-12-17 11:56:11 +01:00
|
|
|
func (c *Adaptor) validateSpiBusNumber(busNr int) error {
|
|
|
|
// Valid bus numbers are [0,1] which corresponds to /dev/spidev0.x through /dev/spidev1.x.
|
|
|
|
// x is the chip number <255
|
|
|
|
if (busNr < 0) || (busNr > 1) {
|
|
|
|
return fmt.Errorf("Bus number %d out of range", busNr)
|
2017-12-19 16:45:16 +01:00
|
|
|
}
|
2022-12-17 11:56:11 +01:00
|
|
|
return nil
|
2017-12-19 16:45:16 +01:00
|
|
|
}
|
|
|
|
|
2022-12-08 19:29:51 +01:00
|
|
|
func (c *Adaptor) validateI2cBusNumber(busNr int) error {
|
|
|
|
// Valid bus number is either 0 or 2 which corresponds to /dev/i2c-0 or /dev/i2c-2.
|
|
|
|
if (busNr != 0) && (busNr != 2) {
|
|
|
|
return fmt.Errorf("Bus number %d out of range", busNr)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-05 18:28:57 +01:00
|
|
|
// translateAnalogPin converts analog pin name to pin position
|
|
|
|
func (c *Adaptor) translateAnalogPin(pin string) (string, error) {
|
|
|
|
if val, ok := c.analogPinMap[pin]; ok {
|
|
|
|
return val, nil
|
|
|
|
}
|
2022-12-08 19:29:51 +01:00
|
|
|
return "", fmt.Errorf("Not a valid analog pin")
|
2022-12-05 18:28:57 +01:00
|
|
|
}
|
|
|
|
|
2014-10-16 10:59:23 -05:00
|
|
|
// translatePin converts digital pin name to pin position
|
2022-12-01 17:33:33 +01:00
|
|
|
func (c *Adaptor) translateAndMuxDigitalPin(id string) (string, int, error) {
|
|
|
|
line, ok := c.pinMap[id]
|
|
|
|
if !ok {
|
|
|
|
return "", -1, fmt.Errorf("'%s' is not a valid id for a digital pin", id)
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
2022-12-01 17:33:33 +01:00
|
|
|
// mux is done by id, not by line
|
|
|
|
if err := c.muxPin(id, "gpio"); err != nil {
|
|
|
|
return "", -1, err
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
2022-12-01 17:33:33 +01:00
|
|
|
return "", line, nil
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2022-12-05 18:28:57 +01:00
|
|
|
func (c *Adaptor) translateAndMuxPWMPin(id string) (string, int, error) {
|
|
|
|
pinInfo, ok := c.pwmPinMap[id]
|
|
|
|
if !ok {
|
|
|
|
return "", -1, fmt.Errorf("'%s' is not a valid id for a PWM pin", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
path, err := pinInfo.findPWMDir(c.sys)
|
|
|
|
if err != nil {
|
|
|
|
return "", -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.muxPin(id, "pwm"); err != nil {
|
|
|
|
return "", -1, err
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
2022-12-05 18:28:57 +01:00
|
|
|
|
|
|
|
return path, pinInfo.channel, nil
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
2014-06-28 12:05:17 -06:00
|
|
|
|
2022-12-05 18:28:57 +01:00
|
|
|
func (p pwmPinData) findPWMDir(sys *system.Accesser) (dir string, err error) {
|
|
|
|
items, _ := sys.Find(p.dir, p.dirRegexp)
|
2023-05-19 14:16:22 +02:00
|
|
|
if len(items) == 0 {
|
2022-12-05 18:28:57 +01:00
|
|
|
return "", fmt.Errorf("No path found for PWM directory pattern, '%s' in path '%s'", p.dirRegexp, p.dir)
|
2022-11-27 16:06:09 +01:00
|
|
|
}
|
2022-12-05 18:28:57 +01:00
|
|
|
|
|
|
|
dir = items[0]
|
|
|
|
info, err := sys.Stat(dir)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Error (%v) on access '%s'", err, dir)
|
|
|
|
}
|
|
|
|
if !info.IsDir() {
|
|
|
|
return "", fmt.Errorf("The item '%s' is not a directory, which is not expected", dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2022-11-27 16:06:09 +01:00
|
|
|
}
|
|
|
|
|
2022-12-01 17:33:33 +01:00
|
|
|
func (c *Adaptor) muxPin(pin, cmd string) error {
|
2017-05-03 10:38:45 +02:00
|
|
|
path := fmt.Sprintf("/sys/devices/platform/ocp/ocp:%s_pinmux/state", pin)
|
2023-10-20 20:50:42 +02:00
|
|
|
fi, e := c.sys.OpenFile(path, os.O_WRONLY, 0o666)
|
2023-05-19 14:16:22 +02:00
|
|
|
defer fi.Close() //nolint:staticcheck // for historical reasons
|
2017-05-03 10:38:45 +02:00
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
2017-12-14 14:54:42 +01:00
|
|
|
_, e = fi.WriteString(cmd)
|
2017-05-03 10:38:45 +02:00
|
|
|
return e
|
2017-04-29 10:48:12 +02:00
|
|
|
}
|