2017-04-20 19:32:38 +02:00
|
|
|
package tinkerboard
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-05-01 16:12:39 +02:00
|
|
|
"log"
|
2017-05-06 15:12:10 +02:00
|
|
|
"sync"
|
2017-04-20 19:32:38 +02:00
|
|
|
|
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/drivers/i2c"
|
2022-11-20 19:22:26 +01:00
|
|
|
"gobot.io/x/gobot/system"
|
2017-04-20 19:32:38 +02:00
|
|
|
)
|
|
|
|
|
2022-05-01 16:12:39 +02:00
|
|
|
const debug = false
|
|
|
|
|
|
|
|
const (
|
|
|
|
pwmNormal = "normal"
|
|
|
|
pwmInverted = "inversed"
|
|
|
|
pwmPeriodDefault = 10000000 // 10ms = 100Hz
|
|
|
|
)
|
|
|
|
|
|
|
|
type pwmPinDefinition struct {
|
|
|
|
channel int
|
|
|
|
dir string
|
|
|
|
dirRegexp string
|
2017-04-22 15:03:07 +02:00
|
|
|
}
|
|
|
|
|
2017-04-20 19:51:31 +02:00
|
|
|
// Adaptor represents a Gobot Adaptor for the ASUS Tinker Board
|
2017-04-20 19:32:38 +02:00
|
|
|
type Adaptor struct {
|
|
|
|
name string
|
2022-11-20 19:22:26 +01:00
|
|
|
sys *system.Accesser
|
|
|
|
mutex sync.Mutex
|
|
|
|
digitalPins map[string]gobot.DigitalPinner
|
|
|
|
pwmPins map[string]gobot.PWMPinner
|
2021-06-07 14:55:45 +02:00
|
|
|
i2cBuses [5]i2c.I2cDevice
|
2017-04-20 19:32:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAdaptor creates a Tinkerboard Adaptor
|
|
|
|
func NewAdaptor() *Adaptor {
|
|
|
|
c := &Adaptor{
|
2022-11-20 19:22:26 +01:00
|
|
|
name: gobot.DefaultName("Tinker Board"),
|
2022-11-27 16:06:09 +01:00
|
|
|
sys: system.NewAccesser("cdev"),
|
2017-04-20 19:32:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
c.setPins()
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 }
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
// Connect do nothing at the moment
|
|
|
|
func (c *Adaptor) Connect() error { return nil }
|
2017-04-20 19:32:38 +02:00
|
|
|
|
|
|
|
// Finalize closes connection to board and pins
|
|
|
|
func (c *Adaptor) Finalize() (err error) {
|
2017-05-06 15:12:10 +02:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
2017-04-20 19:32:38 +02:00
|
|
|
for _, pin := range c.digitalPins {
|
|
|
|
if pin != nil {
|
|
|
|
if e := pin.Unexport(); e != nil {
|
|
|
|
err = multierror.Append(err, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-22 15:03:07 +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-04-20 19:32:38 +02:00
|
|
|
for _, bus := range c.i2cBuses {
|
|
|
|
if bus != nil {
|
|
|
|
if e := bus.Close(); e != nil {
|
|
|
|
err = multierror.Append(err, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-22 15:03:07 +02:00
|
|
|
// DigitalRead reads digital value from the specified pin.
|
2022-11-27 16:06:09 +01:00
|
|
|
func (c *Adaptor) DigitalRead(id string) (int, error) {
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
|
|
|
pin, err := c.digitalPin(id, system.WithDirectionInput())
|
2017-04-22 15:03:07 +02:00
|
|
|
if err != nil {
|
2022-11-27 16:06:09 +01:00
|
|
|
return 0, err
|
2017-04-22 15:03:07 +02:00
|
|
|
}
|
2022-11-27 16:06:09 +01:00
|
|
|
return pin.Read()
|
2017-04-22 15:03:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DigitalWrite writes digital value to the specified pin.
|
2022-11-27 16:06:09 +01:00
|
|
|
func (c *Adaptor) DigitalWrite(id string, val byte) error {
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
|
|
|
pin, err := c.digitalPin(id, system.WithDirectionOutput(int(val)))
|
2017-04-22 15:03:07 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-27 16:06:09 +01:00
|
|
|
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 (c *Adaptor) DigitalPin(id string) (gobot.DigitalPinner, error) {
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
|
|
|
return c.digitalPin(id)
|
2017-04-22 15:03:07 +02:00
|
|
|
}
|
|
|
|
|
2022-05-01 16:12:39 +02:00
|
|
|
// PwmWrite writes a PWM signal to the specified pin.
|
2017-04-22 15:03:07 +02:00
|
|
|
func (c *Adaptor) PwmWrite(pin string, val byte) (err error) {
|
2022-05-01 16:12:39 +02:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
|
|
|
pwmPin, err := c.pwmPin(pin)
|
2017-04-22 15:03:07 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-04-25 11:57:25 +02:00
|
|
|
period, err := pwmPin.Period()
|
2017-04-22 15:03:07 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
duty := gobot.FromScale(float64(val), 0, 255.0)
|
2022-05-01 16:12:39 +02:00
|
|
|
if debug {
|
|
|
|
log.Printf("Tinkerboard PwmWrite - raw: %d, period: %d, duty: %.2f %%", val, period, duty*100)
|
|
|
|
}
|
2017-04-22 15:03:07 +02:00
|
|
|
return pwmPin.SetDutyCycle(uint32(float64(period) * duty))
|
|
|
|
}
|
|
|
|
|
2022-05-01 16:12:39 +02:00
|
|
|
// ServoWrite writes a servo signal to the specified pin.
|
2017-04-22 15:03:07 +02:00
|
|
|
func (c *Adaptor) ServoWrite(pin string, angle byte) (err error) {
|
2022-05-01 16:12:39 +02:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
|
|
|
pwmPin, err := c.pwmPin(pin)
|
2017-04-22 15:03:07 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-05-01 16:12:39 +02:00
|
|
|
period, err := pwmPin.Period()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-22 15:03:07 +02:00
|
|
|
|
|
|
|
// 0.5 ms => -90
|
|
|
|
// 1.5 ms => 0
|
|
|
|
// 2.0 ms => 90
|
2022-05-01 16:12:39 +02:00
|
|
|
minDuty := 100 * 0.0005 * float64(period)
|
|
|
|
maxDuty := 100 * 0.0020 * float64(period)
|
2017-04-22 15:03:07 +02:00
|
|
|
duty := uint32(gobot.ToScale(gobot.FromScale(float64(angle), 0, 180), minDuty, maxDuty))
|
|
|
|
return pwmPin.SetDutyCycle(duty)
|
|
|
|
}
|
|
|
|
|
2022-05-01 16:12:39 +02:00
|
|
|
// SetPeriod adjusts the period of the specified PWM pin.
|
|
|
|
// If duty cycle is already set, also this value will be adjusted in the same ratio.
|
|
|
|
func (c *Adaptor) SetPeriod(pin string, period uint32) error {
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
|
|
|
pwmPin, err := c.pwmPin(pin)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return setPeriod(pwmPin, period)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PWMPin initializes the pin for PWM and returns matched pwmPin for specified pin number.
|
|
|
|
// It implements the PWMPinnerProvider interface.
|
2022-11-20 19:22:26 +01:00
|
|
|
func (c *Adaptor) PWMPin(pin string) (gobot.PWMPinner, error) {
|
2017-05-06 15:12:10 +02:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
2022-05-01 16:12:39 +02:00
|
|
|
return c.pwmPin(pin)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetConnection returns a connection to a device on a specified i2c bus.
|
|
|
|
// Valid bus number is [0..4] which corresponds to /dev/i2c-0 through /dev/i2c-4.
|
|
|
|
// We don't support "/dev/i2c-6 DesignWare HDMI".
|
|
|
|
func (c *Adaptor) GetConnection(address int, bus int) (connection i2c.Connection, err error) {
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
|
|
|
if (bus < 0) || (bus > 4) {
|
|
|
|
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-04-27 12:21:38 +02:00
|
|
|
}
|
2022-05-01 16:12:39 +02:00
|
|
|
return i2c.NewConnection(c.i2cBuses[bus], address), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDefaultBus returns the default i2c bus for this platform.
|
|
|
|
func (c *Adaptor) GetDefaultBus() int {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// pwmPin initializes the pin for PWM and returns matched pwmPin for specified pin number.
|
2022-11-20 19:22:26 +01:00
|
|
|
func (c *Adaptor) pwmPin(pin string) (gobot.PWMPinner, error) {
|
2022-05-01 16:12:39 +02:00
|
|
|
var pwmPinData pwmPinDefinition
|
2022-11-20 19:22:26 +01:00
|
|
|
pwmPinData, err := c.translatePwmPin(pin)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-04-27 12:21:38 +02:00
|
|
|
}
|
2017-04-20 19:32:38 +02:00
|
|
|
|
2022-05-01 16:12:39 +02:00
|
|
|
if c.pwmPins[pin] == nil {
|
|
|
|
var path string
|
2022-11-20 19:22:26 +01:00
|
|
|
path, err := pwmPinData.findDir(*c.sys)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-05-01 16:12:39 +02:00
|
|
|
}
|
2022-11-20 19:22:26 +01:00
|
|
|
newPin := c.sys.NewPWMPin(path, pwmPinData.channel)
|
|
|
|
if err := newPin.Export(); err != nil {
|
|
|
|
return nil, err
|
2017-04-27 12:21:38 +02:00
|
|
|
}
|
2022-05-01 16:12:39 +02:00
|
|
|
// Make sure pwm is disabled before change anything
|
2022-11-20 19:22:26 +01:00
|
|
|
if err := newPin.Enable(false); err != nil {
|
|
|
|
return nil, err
|
2017-04-27 12:21:38 +02:00
|
|
|
}
|
2022-11-20 19:22:26 +01:00
|
|
|
if err := setPeriod(newPin, pwmPeriodDefault); err != nil {
|
|
|
|
return nil, err
|
2017-04-27 12:21:38 +02:00
|
|
|
}
|
2022-11-20 19:22:26 +01:00
|
|
|
if err := newPin.SetPolarity(pwmNormal); err != nil {
|
|
|
|
return nil, err
|
2017-06-04 00:13:49 +02:00
|
|
|
}
|
2022-11-20 19:22:26 +01:00
|
|
|
if err := newPin.Enable(true); err != nil {
|
|
|
|
return nil, err
|
2017-04-27 12:21:38 +02:00
|
|
|
}
|
2022-05-01 16:12:39 +02:00
|
|
|
if debug {
|
|
|
|
log.Printf("New PWMPin created for %s\n", pin)
|
|
|
|
}
|
|
|
|
c.pwmPins[pin] = newPin
|
2017-04-20 19:32:38 +02:00
|
|
|
}
|
2017-04-27 12:21:38 +02:00
|
|
|
|
2022-05-01 16:12:39 +02:00
|
|
|
return c.pwmPins[pin], nil
|
2017-04-20 19:32:38 +02:00
|
|
|
}
|
2017-04-29 10:26:38 +02:00
|
|
|
|
2022-05-01 16:12:39 +02:00
|
|
|
// setPeriod adjusts the PWM period of the given pin.
|
|
|
|
// If duty cycle is already set, also this value will be adjusted in the same ratio.
|
|
|
|
// The order in which the values are written must be observed, otherwise an error occur "write error: Invalid argument".
|
2022-11-20 19:22:26 +01:00
|
|
|
func setPeriod(pwmPin gobot.PWMPinner, period uint32) error {
|
2022-05-01 16:12:39 +02:00
|
|
|
var errorBase = fmt.Sprintf("tinkerboard.setPeriod(%v, %d) failed", pwmPin, period)
|
|
|
|
oldDuty, err := pwmPin.DutyCycle()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s with '%v'", errorBase, err)
|
2017-04-29 10:26:38 +02:00
|
|
|
}
|
2022-05-01 16:12:39 +02:00
|
|
|
|
|
|
|
if oldDuty == 0 {
|
|
|
|
if err := pwmPin.SetPeriod(period); err != nil {
|
|
|
|
log.Println(1, period)
|
|
|
|
return fmt.Errorf("%s with '%v'", errorBase, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// adjust duty cycle in the same ratio
|
|
|
|
oldPeriod, err := pwmPin.Period()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s with '%v'", errorBase, err)
|
|
|
|
}
|
|
|
|
duty := uint32(uint64(oldDuty) * uint64(period) / uint64(oldPeriod))
|
|
|
|
if debug {
|
|
|
|
log.Printf("oldPeriod: %d, oldDuty: %d, new period: %d, new duty: %d", oldPeriod, oldDuty, period, duty)
|
|
|
|
}
|
|
|
|
|
|
|
|
// the order depends on value (duty must not be bigger than period in any situation)
|
|
|
|
if duty > oldPeriod {
|
|
|
|
if err := pwmPin.SetPeriod(period); err != nil {
|
|
|
|
log.Println(2, period)
|
|
|
|
return fmt.Errorf("%s with '%v'", errorBase, err)
|
|
|
|
}
|
|
|
|
if err := pwmPin.SetDutyCycle(uint32(duty)); err != nil {
|
|
|
|
log.Println(2, duty)
|
|
|
|
return fmt.Errorf("%s with '%v'", errorBase, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := pwmPin.SetDutyCycle(uint32(duty)); err != nil {
|
|
|
|
log.Println(3, duty)
|
|
|
|
return fmt.Errorf("%s with '%v'", errorBase, err)
|
|
|
|
}
|
|
|
|
if err := pwmPin.SetPeriod(period); err != nil {
|
|
|
|
log.Println(3, period)
|
|
|
|
return fmt.Errorf("%s with '%v'", errorBase, err)
|
|
|
|
}
|
|
|
|
}
|
2017-04-29 10:26:38 +02:00
|
|
|
}
|
2022-05-01 16:12:39 +02:00
|
|
|
return nil
|
2017-04-29 10:26:38 +02:00
|
|
|
}
|
|
|
|
|
2022-05-01 16:12:39 +02:00
|
|
|
func (c *Adaptor) setPins() {
|
2022-11-20 19:22:26 +01:00
|
|
|
c.digitalPins = make(map[string]gobot.DigitalPinner)
|
|
|
|
c.pwmPins = make(map[string]gobot.PWMPinner)
|
2017-04-29 10:26:38 +02:00
|
|
|
}
|
|
|
|
|
2022-11-27 16:06:09 +01:00
|
|
|
func (c *Adaptor) translatePin(pin string) (chip string, line int, err error) {
|
|
|
|
pindef, ok := gpioPinDefinitions[pin]
|
2022-05-01 16:12:39 +02:00
|
|
|
if !ok {
|
2022-11-27 16:06:09 +01:00
|
|
|
return "", -1, fmt.Errorf("Not a valid pin")
|
2022-05-01 16:12:39 +02:00
|
|
|
}
|
2022-11-27 16:06:09 +01:00
|
|
|
|
|
|
|
if c.sys.IsSysfsDigitalPinAccess() {
|
|
|
|
return "", pindef.sysfs, nil
|
|
|
|
}
|
|
|
|
chip = fmt.Sprintf("gpiochip%d", pindef.cdev.chip)
|
|
|
|
line = int(pindef.cdev.line)
|
|
|
|
return chip, line, nil
|
2017-04-29 10:26:38 +02:00
|
|
|
}
|
|
|
|
|
2022-05-01 16:12:39 +02:00
|
|
|
func (c *Adaptor) translatePwmPin(pin string) (pwmPin pwmPinDefinition, err error) {
|
|
|
|
var ok bool
|
|
|
|
if pwmPin, ok = pwmPinDefinitions[pin]; !ok {
|
|
|
|
err = fmt.Errorf("Not a valid PWM pin")
|
2017-04-29 10:26:38 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-20 19:22:26 +01:00
|
|
|
func (p pwmPinDefinition) findDir(sys system.Accesser) (dir string, err error) {
|
|
|
|
items, _ := sys.Find(p.dir, p.dirRegexp)
|
2022-05-01 16:12:39 +02:00
|
|
|
if items == nil || len(items) == 0 {
|
2022-11-05 07:42:28 +01:00
|
|
|
return "", fmt.Errorf("No path found for PWM directory pattern, '%s' in path '%s'. See README.md for activation", p.dirRegexp, p.dir)
|
2017-04-29 10:26:38 +02:00
|
|
|
}
|
2022-05-01 16:12:39 +02:00
|
|
|
|
|
|
|
dir = items[0]
|
2022-11-20 19:22:26 +01:00
|
|
|
info, err := sys.Stat(dir)
|
2022-05-01 16:12:39 +02:00
|
|
|
if err != nil {
|
2022-11-05 07:42:28 +01:00
|
|
|
return "", fmt.Errorf("Error (%v) on access '%s'", err, dir)
|
2022-05-01 16:12:39 +02:00
|
|
|
}
|
|
|
|
if !info.IsDir() {
|
2022-11-05 07:42:28 +01:00
|
|
|
return "", fmt.Errorf("The item '%s' is not a directory, which is not expected", dir)
|
2022-05-01 16:12:39 +02:00
|
|
|
}
|
|
|
|
|
2017-04-29 10:26:38 +02:00
|
|
|
return
|
|
|
|
}
|
2022-11-27 16:06:09 +01:00
|
|
|
|
|
|
|
func (c *Adaptor) digitalPin(id string, o ...func(gobot.DigitalPinOptioner) bool) (gobot.DigitalPinner, error) {
|
|
|
|
chip, line, err := c.translatePin(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pin := c.digitalPins[id]
|
|
|
|
if pin == nil {
|
|
|
|
pin = c.sys.NewDigitalPin(chip, line, o...)
|
|
|
|
if err = pin.Export(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
c.digitalPins[id] = pin
|
|
|
|
} else {
|
|
|
|
if err := pin.ApplyOptions(o...); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pin, nil
|
|
|
|
}
|