1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-27 13:48:56 +08:00
hybridgroup.gobot/platforms/beaglebone/beaglebone_adaptor.go

364 lines
9.0 KiB
Go
Raw Normal View History

2014-04-28 04:39:51 -07:00
package beaglebone
import (
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
2014-10-16 10:59:23 -05:00
multierror "github.com/hashicorp/go-multierror"
"gobot.io/x/gobot"
"gobot.io/x/gobot/drivers/i2c"
"gobot.io/x/gobot/drivers/spi"
2022-11-20 19:22:26 +01:00
"gobot.io/x/gobot/system"
)
type pwmPinData struct {
channel int
path string
}
const pwmDefaultPeriod = 500000
// Adaptor is the gobot.Adaptor representation for the Beaglebone Black/Green
type Adaptor struct {
2022-11-05 07:42:28 +01:00
name string
2022-11-20 19:22:26 +01:00
sys *system.Accesser
mutex sync.Mutex
digitalPins map[string]gobot.DigitalPinner
2022-11-20 19:22:26 +01:00
pwmPins map[string]gobot.PWMPinner
2022-11-05 07:42:28 +01:00
i2cBuses map[int]i2c.I2cDevice
usrLed string
analogPath string
pinMap map[string]int
pwmPinMap map[string]pwmPinData
analogPinMap map[string]string
findPin func(pinPath string) (string, error)
spiBuses [2]spi.Connection
}
// NewAdaptor returns a new Beaglebone Black/Green Adaptor
func NewAdaptor() *Adaptor {
b := &Adaptor{
name: gobot.DefaultName("BeagleboneBlack"),
2022-11-20 19:22:26 +01:00
sys: system.NewAccesser(),
digitalPins: make(map[string]gobot.DigitalPinner, 120),
2022-11-20 19:22:26 +01:00
pwmPins: make(map[string]gobot.PWMPinner),
i2cBuses: make(map[int]i2c.I2cDevice),
pinMap: bbbPinMap,
pwmPinMap: bbbPwmPinMap,
analogPinMap: bbbAnalogPinMap,
findPin: func(pinPath string) (string, error) {
files, err := filepath.Glob(pinPath)
return files[0], err
},
2022-11-05 07:42:28 +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
return b
}
// 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
2022-11-05 07:42:28 +01:00
// Connect do nothing at the moment
func (b *Adaptor) Connect() error { return nil }
// Finalize releases all i2c devices and exported analog, digital, pwm pins.
func (b *Adaptor) Finalize() (err error) {
b.mutex.Lock()
defer b.mutex.Unlock()
for _, pin := range b.digitalPins {
if pin != nil {
if e := pin.Unexport(); e != nil {
err = multierror.Append(err, e)
}
}
}
for _, pin := range b.pwmPins {
if pin != nil {
if e := pin.Unexport(); e != nil {
err = multierror.Append(err, e)
}
}
}
for _, bus := range b.i2cBuses {
if bus != nil {
if e := bus.Close(); e != nil {
err = multierror.Append(err, e)
}
}
}
for _, bus := range b.spiBuses {
if bus != nil {
if e := bus.Close(); e != nil {
err = multierror.Append(err, e)
}
}
}
return
}
2014-10-16 10:59:23 -05:00
// PwmWrite writes the 0-254 value to the specified pin
func (b *Adaptor) PwmWrite(pin string, val byte) (err error) {
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))
}
// ServoWrite writes a servo signal to the specified pin
func (b *Adaptor) ServoWrite(pin string, angle byte) (err error) {
pwmPin, err := b.PWMPin(pin)
if err != nil {
return
}
// 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-10-16 10:59:23 -05:00
// DigitalRead returns a digital value from specified pin
func (b *Adaptor) DigitalRead(id string) (int, error) {
b.mutex.Lock()
defer b.mutex.Unlock()
pin, err := b.digitalPin(id, system.WithDirectionInput())
if err != nil {
return 0, err
}
return pin.Read()
}
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
func (b *Adaptor) DigitalWrite(id string, val byte) error {
b.mutex.Lock()
defer b.mutex.Unlock()
if strings.Contains(id, "usr") {
fi, e := b.sys.OpenFile(b.usrLed+id+"/brightness", os.O_WRONLY|os.O_APPEND, 0666)
2014-10-30 15:26:31 -07:00
defer fi.Close()
if e != nil {
return e
2014-09-17 15:20:08 -07:00
}
_, err := fi.WriteString(strconv.Itoa(int(val)))
return err
2014-09-17 15:20:08 -07:00
}
pin, err := b.digitalPin(id, system.WithDirectionOutput(int(val)))
2014-11-19 18:40:06 -08:00
if err != nil {
return err
}
return pin.Write(int(val))
}
// DigitalPin returns a digital pin. If the pin is initially acquired, it is an input.
// Pin direction and other options can be changed afterwards by pin.ApplyOptions() at any time.
func (b *Adaptor) DigitalPin(id string) (gobot.DigitalPinner, error) {
b.mutex.Lock()
defer b.mutex.Unlock()
return b.digitalPin(id)
}
// PWMPin returns matched pwmPin for specified pin number
2022-11-20 19:22:26 +01:00
func (b *Adaptor) PWMPin(pin string) (gobot.PWMPinner, error) {
b.mutex.Lock()
defer b.mutex.Unlock()
pinInfo, err := b.translatePwmPin(pin)
if err != nil {
return nil, err
}
if b.pwmPins[pin] == nil {
2022-11-20 19:22:26 +01:00
newPath, err := b.findPin(pinInfo.path)
if err != nil {
return nil, err
}
2022-11-20 19:22:26 +01:00
newPin := b.sys.NewPWMPin(newPath, pinInfo.channel)
if err := b.muxPin(pin, "pwm"); err != nil {
return nil, err
}
if err = newPin.Export(); err != nil {
2022-11-20 19:22:26 +01:00
return nil, err
}
if err = newPin.SetPeriod(pwmDefaultPeriod); err != nil {
2022-11-20 19:22:26 +01:00
return nil, err
}
if err = newPin.Enable(true); err != nil {
2022-11-20 19:22:26 +01:00
return nil, err
}
b.pwmPins[pin] = newPin
}
2022-11-20 19:22:26 +01:00
return b.pwmPins[pin], nil
}
2014-10-16 10:59:23 -05:00
// AnalogRead returns an analog value from specified pin
func (b *Adaptor) AnalogRead(pin string) (val int, err error) {
analogPin, err := b.translateAnalogPin(pin)
if err != nil {
return
}
2022-11-20 19:22:26 +01:00
fi, err := b.sys.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-09 14:35:36 -08:00
if err != nil {
return
2014-11-09 14:35:36 -08:00
}
var buf = make([]byte, 1024)
_, err = fi.Read(buf)
if err != nil {
return
}
2014-11-09 14:35:36 -08:00
val, _ = strconv.Atoi(strings.Split(string(buf), "\n")[0])
return
}
// GetConnection returns a connection to a device on a specified bus.
// Valid bus number is either 0 or 2 which corresponds to /dev/i2c-0 or /dev/i2c-2.
func (b *Adaptor) GetConnection(address int, bus int) (connection i2c.Connection, err error) {
b.mutex.Lock()
defer b.mutex.Unlock()
if (bus != 0) && (bus != 2) {
return nil, fmt.Errorf("Bus number %d out of range", bus)
}
if b.i2cBuses[bus] == nil {
2022-11-20 19:22:26 +01:00
b.i2cBuses[bus], err = b.sys.NewI2cDevice(fmt.Sprintf("/dev/i2c-%d", bus))
}
return i2c.NewConnection(b.i2cBuses[bus], address), err
}
// GetDefaultBus returns the default i2c bus for this platform
func (b *Adaptor) GetDefaultBus() int {
return 2
}
// GetSpiConnection returns an spi connection to a device on a specified bus.
// Valid bus number is [0..1] which corresponds to /dev/spidev0.0 through /dev/spidev0.1.
func (b *Adaptor) GetSpiConnection(busNum, chipNum, mode, bits int, maxSpeed int64) (connection spi.Connection, err error) {
b.mutex.Lock()
defer b.mutex.Unlock()
if (busNum < 0) || (busNum > 1) {
return nil, fmt.Errorf("Bus number %d out of range", busNum)
}
if b.spiBuses[busNum] == nil {
b.spiBuses[busNum], err = spi.GetSpiConnection(busNum, chipNum, mode, bits, maxSpeed)
}
return b.spiBuses[busNum], err
}
// GetSpiDefaultBus returns the default spi bus for this platform.
func (b *Adaptor) GetSpiDefaultBus() int {
2022-11-05 07:42:28 +01:00
return 0
}
// GetSpiDefaultChip returns the default spi chip for this platform.
func (b *Adaptor) GetSpiDefaultChip() int {
2022-11-05 07:42:28 +01:00
return 0
}
// GetSpiDefaultMode returns the default spi mode for this platform.
func (b *Adaptor) GetSpiDefaultMode() int {
2022-11-05 07:42:28 +01:00
return 0
}
// GetSpiDefaultBits returns the default spi number of bits for this platform.
func (b *Adaptor) GetSpiDefaultBits() int {
return 8
}
// GetSpiDefaultMaxSpeed returns the default spi bus for this platform.
func (b *Adaptor) GetSpiDefaultMaxSpeed() int64 {
2022-11-05 07:42:28 +01:00
return 500000
}
2014-10-16 10:59:23 -05:00
// translatePin converts digital pin name to pin position
func (b *Adaptor) translateDigitalPin(pin string) (value int, err error) {
if val, ok := b.pinMap[pin]; ok {
value = val
} else {
err = errors.New("Not a valid pin")
}
return
}
func (b *Adaptor) translatePwmPin(pin string) (p pwmPinData, err error) {
if val, ok := b.pwmPinMap[pin]; ok {
p = val
} else {
err = errors.New("Not a valid PWM pin")
}
return
}
2014-10-16 10:59:23 -05:00
// translateAnalogPin converts analog pin name to pin position
func (b *Adaptor) translateAnalogPin(pin string) (value string, err error) {
if val, ok := b.analogPinMap[pin]; ok {
value = val
} else {
err = errors.New("Not a valid analog pin")
}
return
}
func (b *Adaptor) digitalPin(id string, o ...func(gobot.DigitalPinOptioner) bool) (gobot.DigitalPinner, error) {
pin := b.digitalPins[id]
if pin == nil {
i, err := b.translateDigitalPin(id)
if err != nil {
return nil, err
}
pin = b.sys.NewDigitalPin("", i, o...)
if err := b.muxPin(id, "gpio"); err != nil {
return nil, err
}
if err := pin.Export(); err != nil {
return nil, err
}
b.digitalPins[id] = pin
} else {
if err := pin.ApplyOptions(o...); err != nil {
return nil, err
}
}
return pin, nil
}
2022-11-05 07:42:28 +01:00
func (b *Adaptor) muxPin(pin, cmd string) error {
path := fmt.Sprintf("/sys/devices/platform/ocp/ocp:%s_pinmux/state", pin)
2022-11-20 19:22:26 +01:00
fi, e := b.sys.OpenFile(path, os.O_WRONLY, 0666)
defer fi.Close()
if e != nil {
return e
}
_, e = fi.WriteString(cmd)
return e
}