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
|
|
|
"bufio"
|
2014-11-19 17:18:01 -08:00
|
|
|
"errors"
|
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"
|
2016-12-08 13:24:03 +01:00
|
|
|
"gobot.io/x/gobot"
|
2017-02-06 14:50:14 +01:00
|
|
|
"gobot.io/x/gobot/drivers/i2c"
|
2017-02-08 10:36:04 +01:00
|
|
|
"gobot.io/x/gobot/sysfs"
|
2014-04-26 03:11:51 -07:00
|
|
|
)
|
|
|
|
|
2017-12-14 14:54:42 +01:00
|
|
|
type pwmPinData struct {
|
|
|
|
channel int
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
2017-05-03 10:38:45 +02:00
|
|
|
const pwmDefaultPeriod = 500000
|
|
|
|
|
2016-09-25 14:12:52 +02:00
|
|
|
// Adaptor is the gobot.Adaptor representation for the Beaglebone
|
|
|
|
type Adaptor struct {
|
2017-12-14 14:54:42 +01:00
|
|
|
name string
|
|
|
|
digitalPins []*sysfs.DigitalPin
|
|
|
|
pwmPins map[string]*sysfs.PWMPin
|
|
|
|
i2cBuses map[int]i2c.I2cDevice
|
|
|
|
usrLed string
|
|
|
|
analogPath string
|
|
|
|
slots string
|
|
|
|
pinMap map[string]int
|
|
|
|
pwmPinMap map[string]pwmPinData
|
|
|
|
analogPinMap map[string]string
|
|
|
|
mutex *sync.Mutex
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2016-09-25 14:12:52 +02:00
|
|
|
// NewAdaptor returns a new Beaglebone Adaptor
|
|
|
|
func NewAdaptor() *Adaptor {
|
|
|
|
b := &Adaptor{
|
2017-12-14 14:54:42 +01:00
|
|
|
name: gobot.DefaultName("Beaglebone"),
|
|
|
|
digitalPins: make([]*sysfs.DigitalPin, 120),
|
|
|
|
pwmPins: make(map[string]*sysfs.PWMPin),
|
|
|
|
i2cBuses: make(map[int]i2c.I2cDevice),
|
|
|
|
mutex: &sync.Mutex{},
|
|
|
|
pinMap: bbbPinMap,
|
|
|
|
pwmPinMap: bbbPwmPinMap,
|
|
|
|
analogPinMap: bbbAnalogPinMap,
|
2014-05-22 19:32:09 -07:00
|
|
|
}
|
2014-11-09 14:35:36 -08:00
|
|
|
|
2016-11-26 21:48:10 +01:00
|
|
|
b.setSlots()
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Adaptor) setSlots() {
|
2017-05-03 10:38:45 +02:00
|
|
|
b.slots = "/sys/devices/platform/bone_capemgr/slots"
|
2016-11-27 20:24:24 +01:00
|
|
|
b.usrLed = "/sys/class/leds/beaglebone:green:"
|
2017-05-03 11:30:54 +02:00
|
|
|
b.analogPath = "/sys/bus/iio/devices/iio:device0"
|
2014-04-28 04:39:51 -07:00
|
|
|
}
|
2014-12-31 06:46:47 -08:00
|
|
|
|
2016-09-25 14:12:52 +02:00
|
|
|
// Name returns the Adaptor name
|
|
|
|
func (b *Adaptor) Name() string { return b.name }
|
|
|
|
|
|
|
|
// SetName sets the Adaptor name
|
|
|
|
func (b *Adaptor) SetName(n string) { b.name = n }
|
2014-04-28 04:39:51 -07:00
|
|
|
|
2014-12-31 06:46:47 -08:00
|
|
|
// Connect initializes the pwm and analog dts.
|
2016-11-07 17:33:11 +01:00
|
|
|
func (b *Adaptor) Connect() error {
|
2017-05-06 15:10:59 +02:00
|
|
|
b.mutex.Lock()
|
|
|
|
defer b.mutex.Unlock()
|
|
|
|
|
2017-05-03 10:38:45 +02:00
|
|
|
if err := ensureSlot(b.slots, "BB-ADC"); err != nil {
|
2016-11-07 17:33:11 +01:00
|
|
|
return err
|
2014-11-16 14:18:29 -08:00
|
|
|
}
|
2014-11-10 12:48:08 -08:00
|
|
|
|
2016-11-07 17:33:11 +01:00
|
|
|
return nil
|
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.
|
2016-11-07 17:33:11 +01:00
|
|
|
func (b *Adaptor) Finalize() (err error) {
|
2017-05-06 15:10:59 +02:00
|
|
|
b.mutex.Lock()
|
|
|
|
defer b.mutex.Unlock()
|
|
|
|
|
2017-05-03 11:30:54 +02:00
|
|
|
for _, pin := range b.digitalPins {
|
2014-04-26 03:11:51 -07:00
|
|
|
if pin != nil {
|
2017-05-03 10:38:45 +02:00
|
|
|
if e := pin.Unexport(); e != nil {
|
2016-11-07 17:33:11 +01:00
|
|
|
err = multierror.Append(err, e)
|
2014-11-19 23:21:19 -08:00
|
|
|
}
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
}
|
2017-05-03 11:30:54 +02:00
|
|
|
for _, pin := range b.pwmPins {
|
2014-04-26 03:11:51 -07:00
|
|
|
if pin != nil {
|
2016-11-07 17:33:11 +01:00
|
|
|
if e := pin.Unexport(); e != nil {
|
|
|
|
err = multierror.Append(err, e)
|
2014-11-19 23:21:19 -08:00
|
|
|
}
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
}
|
2017-02-06 14:50:14 +01:00
|
|
|
for _, bus := range b.i2cBuses {
|
|
|
|
if bus != nil {
|
|
|
|
if e := bus.Close(); e != nil {
|
|
|
|
err = multierror.Append(err, e)
|
|
|
|
}
|
2014-11-19 23:21:19 -08:00
|
|
|
}
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
2014-11-19 23:21:19 -08:00
|
|
|
return
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
2014-10-16 10:59:23 -05:00
|
|
|
|
2014-12-31 06:46:47 -08:00
|
|
|
// PwmWrite writes the 0-254 value to the specified pin
|
2016-09-25 14:12:52 +02:00
|
|
|
func (b *Adaptor) PwmWrite(pin string, val byte) (err error) {
|
2017-05-03 10:38:45 +02:00
|
|
|
pwmPin, err := b.PWMPin(pin)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
period, err := pwmPin.Period()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
duty := gobot.FromScale(float64(val), 0, 255.0)
|
|
|
|
return pwmPin.SetDutyCycle(uint32(float64(period) * duty))
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2017-05-03 10:38:45 +02:00
|
|
|
// ServoWrite writes a servo signal to the specified pin
|
|
|
|
func (b *Adaptor) ServoWrite(pin string, angle byte) (err error) {
|
|
|
|
pwmPin, err := b.PWMPin(pin)
|
2014-11-16 14:18:29 -08:00
|
|
|
if err != nil {
|
2017-05-03 10:38:45 +02:00
|
|
|
return
|
2014-11-16 14:18:29 -08:00
|
|
|
}
|
2017-05-03 10:38:45 +02:00
|
|
|
|
|
|
|
// TODO: take into account the actual period setting, not just assume default
|
|
|
|
const minDuty = 100 * 0.0005 * pwmDefaultPeriod
|
|
|
|
const maxDuty = 100 * 0.0020 * pwmDefaultPeriod
|
|
|
|
duty := uint32(gobot.ToScale(gobot.FromScale(float64(angle), 0, 180), minDuty, maxDuty))
|
|
|
|
return pwmPin.SetDutyCycle(duty)
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2014-10-16 10:59:23 -05:00
|
|
|
// DigitalRead returns a digital value from specified pin
|
2016-09-25 14:12:52 +02:00
|
|
|
func (b *Adaptor) DigitalRead(pin string) (val int, err error) {
|
2017-04-29 10:48:12 +02:00
|
|
|
sysfsPin, err := b.DigitalPin(pin, sysfs.IN)
|
2014-11-19 17:18:01 -08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return sysfsPin.Read()
|
2014-06-28 12:05:17 -06: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
|
2016-09-25 14:12:52 +02:00
|
|
|
func (b *Adaptor) DigitalWrite(pin string, val byte) (err error) {
|
2014-09-17 15:20:08 -07:00
|
|
|
if strings.Contains(pin, "usr") {
|
2017-05-03 10:38:45 +02:00
|
|
|
fi, e := sysfs.OpenFile(b.usrLed+pin+"/brightness", os.O_WRONLY|os.O_APPEND, 0666)
|
2014-10-30 15:26:31 -07:00
|
|
|
defer fi.Close()
|
2017-05-03 10:38:45 +02:00
|
|
|
if e != nil {
|
|
|
|
return e
|
2014-09-17 15:20:08 -07:00
|
|
|
}
|
2014-11-16 14:18:29 -08:00
|
|
|
_, err = fi.WriteString(strconv.Itoa(int(val)))
|
|
|
|
return err
|
2014-09-17 15:20:08 -07:00
|
|
|
}
|
2017-04-29 10:48:12 +02:00
|
|
|
sysfsPin, err := b.DigitalPin(pin, sysfs.OUT)
|
2014-11-19 18:40:06 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-11-19 17:18:01 -08:00
|
|
|
return sysfsPin.Write(int(val))
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2017-04-29 10:48:12 +02:00
|
|
|
// DigitalPin retrieves digital pin value by name
|
2017-05-03 10:38:45 +02:00
|
|
|
func (b *Adaptor) DigitalPin(pin string, dir string) (sysfsPin sysfs.DigitalPinner, err error) {
|
2017-05-06 15:10:59 +02:00
|
|
|
b.mutex.Lock()
|
|
|
|
defer b.mutex.Unlock()
|
|
|
|
|
2017-04-29 10:48:12 +02:00
|
|
|
i, err := b.translatePin(pin)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if b.digitalPins[i] == nil {
|
|
|
|
b.digitalPins[i] = sysfs.NewDigitalPin(i)
|
2017-12-14 14:54:42 +01:00
|
|
|
if err = muxPin(pin, "gpio"); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-29 10:48:12 +02:00
|
|
|
err := b.digitalPins[i].Export()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err = b.digitalPins[i].Direction(dir); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return b.digitalPins[i], nil
|
|
|
|
}
|
|
|
|
|
2017-05-03 10:38:45 +02:00
|
|
|
// PWMPin returns matched pwmPin for specified pin number
|
|
|
|
func (b *Adaptor) PWMPin(pin string) (sysfsPin sysfs.PWMPinner, err error) {
|
2017-05-06 15:10:59 +02:00
|
|
|
b.mutex.Lock()
|
|
|
|
defer b.mutex.Unlock()
|
|
|
|
|
2017-05-03 10:38:45 +02:00
|
|
|
pinInfo, err := b.translatePwmPin(pin)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.pwmPins[pin] == nil {
|
|
|
|
newPin := sysfs.NewPWMPin(pinInfo.channel)
|
|
|
|
newPin.Path = pinInfo.path
|
|
|
|
|
2017-12-14 14:54:42 +01:00
|
|
|
if err = muxPin(pin, "pwm"); err != nil {
|
2017-05-03 10:38:45 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = newPin.Export(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = newPin.SetPeriod(pwmDefaultPeriod); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// if err = newPin.InvertPolarity(false); err != nil {
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
if err = newPin.Enable(true); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
b.pwmPins[pin] = newPin
|
|
|
|
}
|
|
|
|
|
|
|
|
sysfsPin = b.pwmPins[pin]
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-10-16 10:59:23 -05:00
|
|
|
// AnalogRead returns an analog value from specified pin
|
2016-09-25 14:12:52 +02:00
|
|
|
func (b *Adaptor) AnalogRead(pin string) (val int, err error) {
|
2014-11-19 17:18:01 -08:00
|
|
|
analogPin, err := b.translateAnalogPin(pin)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-12-03 13:17:31 +01:00
|
|
|
fi, err := sysfs.OpenFile(fmt.Sprintf("%v/%v", b.analogPath, analogPin), os.O_RDONLY, 0644)
|
2014-11-09 14:35:36 -08:00
|
|
|
defer fi.Close()
|
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
|
|
|
}
|
|
|
|
|
|
|
|
var 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
|
|
|
}
|
|
|
|
|
2017-02-10 11:08:32 +01:00
|
|
|
// GetConnection returns a connection to a device on a specified bus.
|
2017-02-08 10:36:04 +01:00
|
|
|
// Valid bus number is either 0 or 2 which corresponds to /dev/i2c-0 or /dev/i2c-2.
|
2017-02-10 11:08:32 +01:00
|
|
|
func (b *Adaptor) GetConnection(address int, bus int) (connection i2c.Connection, err error) {
|
2017-05-06 15:10:59 +02:00
|
|
|
b.mutex.Lock()
|
|
|
|
defer b.mutex.Unlock()
|
|
|
|
|
2017-02-08 10:36:04 +01:00
|
|
|
if (bus != 0) && (bus != 2) {
|
2017-02-06 14:50:14 +01:00
|
|
|
return nil, fmt.Errorf("Bus number %d out of range", bus)
|
2015-07-03 18:57:29 -07:00
|
|
|
}
|
2017-02-10 11:08:32 +01:00
|
|
|
if b.i2cBuses[bus] == nil {
|
|
|
|
b.i2cBuses[bus], err = sysfs.NewI2cDevice(fmt.Sprintf("/dev/i2c-%d", bus))
|
2017-02-06 14:50:14 +01:00
|
|
|
}
|
2017-02-10 11:08:32 +01:00
|
|
|
return i2c.NewConnection(b.i2cBuses[bus], address), err
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2017-02-10 11:08:32 +01:00
|
|
|
// GetDefaultBus returns the default i2c bus for this platform
|
|
|
|
func (b *Adaptor) GetDefaultBus() int {
|
2017-02-08 10:36:04 +01:00
|
|
|
return 2
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2014-10-16 10:59:23 -05:00
|
|
|
// translatePin converts digital pin name to pin position
|
2016-09-25 14:12:52 +02:00
|
|
|
func (b *Adaptor) translatePin(pin string) (value int, err error) {
|
2017-12-14 14:54:42 +01:00
|
|
|
if val, ok := b.pinMap[pin]; ok {
|
2017-05-03 10:38:45 +02:00
|
|
|
value = val
|
|
|
|
} else {
|
|
|
|
err = errors.New("Not a valid pin")
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
2014-11-19 17:18:01 -08:00
|
|
|
return
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2017-05-03 10:38:45 +02:00
|
|
|
func (b *Adaptor) translatePwmPin(pin string) (p pwmPinData, err error) {
|
2017-12-14 14:54:42 +01:00
|
|
|
if val, ok := b.pwmPinMap[pin]; ok {
|
2017-05-03 10:38:45 +02:00
|
|
|
p = val
|
|
|
|
} else {
|
|
|
|
err = errors.New("Not a valid PWM pin")
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
2014-11-19 17:18:01 -08:00
|
|
|
return
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2014-10-16 10:59:23 -05:00
|
|
|
// translateAnalogPin converts analog pin name to pin position
|
2016-09-25 14:12:52 +02:00
|
|
|
func (b *Adaptor) translateAnalogPin(pin string) (value string, err error) {
|
2017-12-14 14:54:42 +01:00
|
|
|
if val, ok := b.analogPinMap[pin]; ok {
|
2017-05-03 10:38:45 +02:00
|
|
|
value = val
|
|
|
|
} else {
|
|
|
|
err = errors.New("Not a valid analog pin")
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
2014-11-16 14:18:29 -08:00
|
|
|
return
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
2014-06-28 12:05:17 -06:00
|
|
|
|
2014-11-16 14:18:29 -08:00
|
|
|
func ensureSlot(slots, item string) (err error) {
|
2014-11-09 14:35:36 -08:00
|
|
|
fi, err := sysfs.OpenFile(slots, os.O_RDWR|os.O_APPEND, 0666)
|
2014-11-16 14:18:29 -08:00
|
|
|
defer fi.Close()
|
2014-06-11 13:59:06 -06:00
|
|
|
if err != nil {
|
2014-11-16 14:18:29 -08:00
|
|
|
return
|
2014-06-11 13:59:06 -06:00
|
|
|
}
|
|
|
|
|
2014-07-08 18:36:14 -07:00
|
|
|
// ensure the slot is not already written into the capemanager
|
|
|
|
// (from: https://github.com/mrmorphic/hwio/blob/master/module_bb_pwm.go#L190)
|
2014-06-11 13:59:06 -06:00
|
|
|
scanner := bufio.NewScanner(fi)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if strings.Index(line, item) > 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-16 14:18:29 -08:00
|
|
|
_, err = fi.WriteString(item)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-23 18:35:45 -08:00
|
|
|
fi.Sync()
|
2014-06-11 13:59:06 -06:00
|
|
|
|
2014-06-30 19:50:28 -07:00
|
|
|
scanner = bufio.NewScanner(fi)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if strings.Index(line, item) > 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2014-11-16 14:18:29 -08:00
|
|
|
return
|
2014-06-11 13:59:06 -06:00
|
|
|
}
|
2016-11-27 20:24:24 +01:00
|
|
|
|
2017-12-14 14:54:42 +01:00
|
|
|
func 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)
|
|
|
|
fi, e := sysfs.OpenFile(path, os.O_WRONLY, 0666)
|
|
|
|
defer fi.Close()
|
|
|
|
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
|
|
|
}
|