2016-01-30 02:00:27 -08:00
|
|
|
package chip
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2017-01-11 01:39:23 +01:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2016-11-27 21:56:09 +01:00
|
|
|
"strings"
|
2017-05-06 15:11:13 +02:00
|
|
|
"sync"
|
2016-01-30 02:00:27 -08:00
|
|
|
|
2016-11-07 17:43:55 +01:00
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
2017-02-02 15:57:45 +01:00
|
|
|
"gobot.io/x/gobot"
|
2017-01-20 00:17:41 +01:00
|
|
|
"gobot.io/x/gobot/drivers/i2c"
|
2022-12-01 17:33:33 +01:00
|
|
|
"gobot.io/x/gobot/platforms/adaptors"
|
2022-11-20 19:22:26 +01:00
|
|
|
"gobot.io/x/gobot/system"
|
2016-01-30 02:00:27 -08:00
|
|
|
)
|
|
|
|
|
2022-12-01 17:33:33 +01: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 10:41:47 +02:00
|
|
|
type sysfsPin struct {
|
|
|
|
pin int
|
|
|
|
pwmPin int
|
|
|
|
}
|
|
|
|
|
2016-11-27 21:56:09 +01:00
|
|
|
// Adaptor represents a Gobot Adaptor for a C.H.I.P.
|
2016-09-25 20:19:19 +02:00
|
|
|
type Adaptor struct {
|
2022-12-01 17:33:33 +01:00
|
|
|
name string
|
|
|
|
sys *system.Accesser
|
|
|
|
mutex sync.Mutex
|
|
|
|
pinmap map[string]sysfsPin
|
|
|
|
*adaptors.DigitalPinsAdaptor
|
|
|
|
pwmPins map[int]gobot.PWMPinner
|
|
|
|
i2cBuses [3]i2c.I2cDevice
|
2016-11-27 21:56:09 +01:00
|
|
|
}
|
|
|
|
|
2016-09-25 20:19:19 +02:00
|
|
|
// NewAdaptor creates a C.H.I.P. Adaptor
|
|
|
|
func NewAdaptor() *Adaptor {
|
2022-12-01 17:33:33 +01:00
|
|
|
sys := system.NewAccesser()
|
2016-09-25 20:19:19 +02:00
|
|
|
c := &Adaptor{
|
2022-12-01 17:33:33 +01:00
|
|
|
name: gobot.DefaultName("CHIP"),
|
|
|
|
sys: sys,
|
2017-04-27 10:04:54 +02:00
|
|
|
}
|
|
|
|
|
2022-12-01 17:33:33 +01:00
|
|
|
c.pwmPins = make(map[int]gobot.PWMPinner)
|
|
|
|
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}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.DigitalPinsAdaptor = adaptors.NewDigitalPinsAdaptor(sys, c.translateDigitalPin)
|
2017-04-27 10:04:54 +02:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
// NewProAdaptor creates a C.H.I.P. Pro Adaptor
|
2017-04-27 10:04:54 +02:00
|
|
|
func NewProAdaptor() *Adaptor {
|
2022-12-01 17:33:33 +01:00
|
|
|
c := NewAdaptor()
|
|
|
|
c.name = gobot.DefaultName("CHIP Pro")
|
|
|
|
c.pinmap = chipProPins
|
2016-01-30 02:00:27 -08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2016-09-25 20:19:19 +02: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 02:00:27 -08: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()
|
|
|
|
|
|
|
|
err := c.DigitalPinsAdaptor.Connect()
|
|
|
|
return err
|
2016-01-30 02:00:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finalize closes connection to board and pins
|
2022-12-01 17:33:33 +01:00
|
|
|
func (c *Adaptor) Finalize() error {
|
2017-05-06 15:11:13 +02:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
2022-12-01 17:33:33 +01:00
|
|
|
err := c.DigitalPinsAdaptor.Finalize()
|
|
|
|
|
2017-04-22 10:41:47 +02:00
|
|
|
for _, pin := range c.pwmPins {
|
|
|
|
if pin != nil {
|
|
|
|
if errs := pin.Enable(false); errs != nil {
|
|
|
|
err = multierror.Append(err, errs)
|
|
|
|
}
|
|
|
|
if errs := pin.Unexport(); errs != nil {
|
|
|
|
err = multierror.Append(err, errs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-20 00:17:41 +01:00
|
|
|
for _, bus := range c.i2cBuses {
|
|
|
|
if bus != nil {
|
|
|
|
if e := bus.Close(); e != nil {
|
|
|
|
err = multierror.Append(err, e)
|
|
|
|
}
|
2016-01-30 02:00:27 -08:00
|
|
|
}
|
|
|
|
}
|
2022-12-01 17:33:33 +01:00
|
|
|
return err
|
2016-01-30 02:00:27 -08:00
|
|
|
}
|
|
|
|
|
2017-02-10 11:08:32 +01:00
|
|
|
// GetConnection returns a connection to a device on a specified bus.
|
2017-01-20 00:17:41 +01:00
|
|
|
// Valid bus number is [0..2] which corresponds to /dev/i2c-0 through /dev/i2c-2.
|
2017-02-10 11:08:32 +01:00
|
|
|
func (c *Adaptor) GetConnection(address int, bus int) (connection i2c.Connection, err error) {
|
2017-05-06 15:11:13 +02:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
2017-01-20 00:17:41 +01:00
|
|
|
if (bus < 0) || (bus > 2) {
|
|
|
|
return nil, fmt.Errorf("Bus number %d out of range", bus)
|
|
|
|
}
|
|
|
|
if c.i2cBuses[bus] == nil {
|
2022-11-20 19:22:26 +01:00
|
|
|
c.i2cBuses[bus], err = c.sys.NewI2cDevice(fmt.Sprintf("/dev/i2c-%d", bus))
|
2017-01-20 00:17:41 +01:00
|
|
|
}
|
2017-02-10 11:08:32 +01:00
|
|
|
return i2c.NewConnection(c.i2cBuses[bus], address), err
|
2017-01-20 00:17:41 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 11:08:32 +01:00
|
|
|
// GetDefaultBus returns the default i2c bus for this platform
|
|
|
|
func (c *Adaptor) GetDefaultBus() int {
|
2017-02-06 14:50:14 +01:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
// PWMPin returns matched pwmPin for specified pin number
|
2022-11-20 19:22:26 +01:00
|
|
|
func (c *Adaptor) PWMPin(pin string) (gobot.PWMPinner, error) {
|
2017-05-06 15:11:13 +02:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
2017-04-28 12:52:08 +02:00
|
|
|
sysPin := c.pinmap[pin]
|
|
|
|
if sysPin.pwmPin != -1 {
|
|
|
|
if c.pwmPins[sysPin.pwmPin] == nil {
|
2022-11-20 19:22:26 +01:00
|
|
|
newPin := c.sys.NewPWMPin("/sys/class/pwm/pwmchip0", sysPin.pwmPin)
|
|
|
|
if err := newPin.Export(); err != nil {
|
|
|
|
return nil, err
|
2017-04-28 12:52:08 +02:00
|
|
|
}
|
2017-06-01 23:50:28 +02:00
|
|
|
// Make sure pwm is disabled when setting polarity
|
2022-11-20 19:22:26 +01:00
|
|
|
if err := newPin.Enable(false); err != nil {
|
|
|
|
return nil, err
|
2017-06-01 23:50:28 +02:00
|
|
|
}
|
2022-11-20 19:22:26 +01:00
|
|
|
if err := newPin.InvertPolarity(false); err != nil {
|
|
|
|
return nil, err
|
2017-04-28 12:52:08 +02:00
|
|
|
}
|
2022-11-20 19:22:26 +01:00
|
|
|
if err := newPin.Enable(true); err != nil {
|
|
|
|
return nil, err
|
2017-04-28 12:52:08 +02:00
|
|
|
}
|
2022-11-20 19:22:26 +01:00
|
|
|
if err := newPin.SetPeriod(10000000); err != nil {
|
|
|
|
return nil, err
|
2017-04-28 12:52:08 +02:00
|
|
|
}
|
|
|
|
c.pwmPins[sysPin.pwmPin] = newPin
|
|
|
|
}
|
|
|
|
|
2022-11-20 19:22:26 +01:00
|
|
|
return c.pwmPins[sysPin.pwmPin], nil
|
2017-04-28 12:52:08 +02:00
|
|
|
}
|
2022-11-20 19:22:26 +01:00
|
|
|
|
|
|
|
return nil, errors.New("Not a PWM pin")
|
2017-04-28 12:52:08 +02:00
|
|
|
}
|
|
|
|
|
2017-01-24 00:40:33 +01:00
|
|
|
// PwmWrite writes a PWM signal to the specified pin
|
|
|
|
func (c *Adaptor) PwmWrite(pin string, val byte) (err error) {
|
2017-04-28 12:52:08 +02:00
|
|
|
pwmPin, err := c.PWMPin(pin)
|
2017-04-22 11:50:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
2017-01-24 00:40:33 +01:00
|
|
|
}
|
2017-04-25 10:44:28 +02:00
|
|
|
period, err := pwmPin.Period()
|
2017-04-22 11:50:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
duty := gobot.FromScale(float64(val), 0, 255.0)
|
|
|
|
return pwmPin.SetDutyCycle(uint32(float64(period) * duty))
|
2017-01-24 00:40:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServoWrite writes a servo signal to the specified pin
|
2017-04-22 11:50:39 +02:00
|
|
|
func (c *Adaptor) ServoWrite(pin string, angle byte) (err error) {
|
2017-04-28 12:52:08 +02:00
|
|
|
pwmPin, err := c.PWMPin(pin)
|
2017-04-22 11:50:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 0.5 ms => -90
|
|
|
|
// 1.5 ms => 0
|
|
|
|
// 2.0 ms => 90
|
2017-05-31 00:04:21 +02:00
|
|
|
//
|
|
|
|
// Duty cycle is in nanos
|
|
|
|
const minDuty = 0.0005 * 1e9
|
|
|
|
const maxDuty = 0.0020 * 1e9
|
2017-04-22 11:50:39 +02:00
|
|
|
duty := uint32(gobot.ToScale(gobot.FromScale(float64(angle), 0, 180), minDuty, maxDuty))
|
|
|
|
return pwmPin.SetDutyCycle(duty)
|
|
|
|
}
|
2017-01-24 00:40:33 +01:00
|
|
|
|
2017-01-11 01:39:23 +01: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 {
|
|
|
|
label, err := ioutil.ReadFile(labelPath)
|
|
|
|
if err != nil {
|
|
|
|
return baseAddr, err
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(string(label), expanderID) {
|
|
|
|
expanderPath, _ := filepath.Split(labelPath)
|
|
|
|
basePath := filepath.Join(expanderPath, "base")
|
|
|
|
base, err := ioutil.ReadFile(basePath)
|
|
|
|
if err != nil {
|
|
|
|
return baseAddr, err
|
|
|
|
}
|
|
|
|
baseAddr, _ = strconv.Atoi(strings.TrimSpace(string(base)))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-11-27 21:56:09 +01:00
|
|
|
|
2017-01-11 01:39:23 +01:00
|
|
|
return baseAddr, nil
|
2016-11-27 21:56:09 +01:00
|
|
|
}
|
2017-04-22 11:50:39 +02:00
|
|
|
|
2022-12-01 17:33:33 +01:00
|
|
|
func (c *Adaptor) translateDigitalPin(id string) (string, int, error) {
|
2022-11-27 16:06:09 +01:00
|
|
|
if val, ok := c.pinmap[id]; ok {
|
2022-12-01 17:33:33 +01:00
|
|
|
return "", val.pin, nil
|
2022-11-27 16:06:09 +01:00
|
|
|
}
|
2022-12-01 17:33:33 +01:00
|
|
|
return "", -1, fmt.Errorf("'%s' is not a valid id for a digital pin", id)
|
2022-11-27 16:06:09 +01:00
|
|
|
}
|