2014-04-28 04:39:51 -07:00
|
|
|
package beaglebone
|
2014-04-26 03:11:51 -07:00
|
|
|
|
|
|
|
import (
|
2014-11-19 18:40:06 -08:00
|
|
|
"errors"
|
2014-11-09 14:35:36 -08:00
|
|
|
"strings"
|
2014-06-12 16:12:38 -07:00
|
|
|
"testing"
|
2014-11-03 19:41:34 -08:00
|
|
|
|
2016-12-08 13:24:03 +01:00
|
|
|
"gobot.io/x/gobot"
|
2016-12-21 15:54:00 +01:00
|
|
|
"gobot.io/x/gobot/drivers/aio"
|
2016-12-08 13:24:03 +01:00
|
|
|
"gobot.io/x/gobot/drivers/gpio"
|
|
|
|
"gobot.io/x/gobot/drivers/i2c"
|
2017-12-19 16:45:16 +01:00
|
|
|
"gobot.io/x/gobot/drivers/spi"
|
2016-12-08 13:24:03 +01:00
|
|
|
"gobot.io/x/gobot/gobottest"
|
2022-11-20 19:22:26 +01:00
|
|
|
"gobot.io/x/gobot/system"
|
2014-04-26 03:11:51 -07:00
|
|
|
)
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
// make sure that this Adaptor fulfills all the required interfaces
|
2016-09-25 14:12:52 +02:00
|
|
|
var _ gobot.Adaptor = (*Adaptor)(nil)
|
2022-11-20 19:22:26 +01:00
|
|
|
var _ gobot.DigitalPinnerProvider = (*Adaptor)(nil)
|
|
|
|
var _ gobot.PWMPinnerProvider = (*Adaptor)(nil)
|
2016-09-25 14:12:52 +02:00
|
|
|
var _ gpio.DigitalReader = (*Adaptor)(nil)
|
|
|
|
var _ gpio.DigitalWriter = (*Adaptor)(nil)
|
2016-12-21 15:54:00 +01:00
|
|
|
var _ aio.AnalogReader = (*Adaptor)(nil)
|
2016-09-25 14:12:52 +02:00
|
|
|
var _ gpio.PwmWriter = (*Adaptor)(nil)
|
|
|
|
var _ gpio.ServoWriter = (*Adaptor)(nil)
|
2017-02-10 11:08:32 +01:00
|
|
|
var _ i2c.Connector = (*Adaptor)(nil)
|
2017-12-19 16:45:16 +01:00
|
|
|
var _ spi.Connector = (*Adaptor)(nil)
|
2016-08-26 14:23:03 +02:00
|
|
|
|
2022-11-20 19:22:26 +01:00
|
|
|
func initTestAdaptorWithMockedFilesystem(mockPaths []string) (*Adaptor, *system.MockFilesystem) {
|
2017-12-16 16:15:41 +01:00
|
|
|
a := NewAdaptor()
|
2022-11-20 19:22:26 +01:00
|
|
|
fs := a.sys.UseMockFilesystem(mockPaths)
|
2022-11-05 07:42:28 +01:00
|
|
|
|
2017-12-19 12:03:41 +01:00
|
|
|
a.findPin = func(pinPath string) (string, error) {
|
2017-12-16 16:15:41 +01:00
|
|
|
switch pinPath {
|
|
|
|
case "/sys/devices/platform/ocp/48304000.epwmss/48304200.pwm/pwm/pwmchip*":
|
2017-12-19 12:03:41 +01:00
|
|
|
return "/sys/devices/platform/ocp/48304000.epwmss/48304200.pwm/pwm/pwmchip4", nil
|
2017-12-16 16:15:41 +01:00
|
|
|
case "/sys/devices/platform/ocp/48302000.epwmss/48302200.pwm/pwm/pwmchip*":
|
2017-12-19 12:03:41 +01:00
|
|
|
return "/sys/devices/platform/ocp/48302000.epwmss/48302200.pwm/pwm/pwmchip2", nil
|
2017-12-16 16:15:41 +01:00
|
|
|
case "/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip*":
|
2017-12-19 12:03:41 +01:00
|
|
|
return "/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0", nil
|
2017-12-16 16:15:41 +01:00
|
|
|
case "/sys/devices/platform/ocp/48300000.epwmss/48300100.ecap/pwm/pwmchip*":
|
2017-12-19 12:03:41 +01:00
|
|
|
return "/sys/devices/platform/ocp/48300000.epwmss/48300100.ecap/pwm/pwmchip0", nil
|
2017-12-16 16:15:41 +01:00
|
|
|
default:
|
2017-12-19 12:03:41 +01:00
|
|
|
return pinPath, nil
|
2017-12-16 16:15:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
return a, fs
|
2017-12-16 16:15:41 +01:00
|
|
|
}
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestGeneral(t *testing.T) {
|
|
|
|
mockPaths := []string{
|
2017-02-08 10:36:04 +01:00
|
|
|
"/dev/i2c-2",
|
2016-11-27 13:37:23 +01:00
|
|
|
"/sys/devices/platform/bone_capemgr",
|
2018-02-13 19:36:40 +01:00
|
|
|
"/sys/devices/platform/ocp/ocp:P8_07_pinmux/state",
|
2017-12-14 14:54:42 +01:00
|
|
|
"/sys/devices/platform/ocp/ocp:P9_11_pinmux/state",
|
|
|
|
"/sys/devices/platform/ocp/ocp:P9_12_pinmux/state",
|
2017-05-03 11:30:54 +02:00
|
|
|
"/sys/devices/platform/ocp/ocp:P9_22_pinmux/state",
|
2017-12-14 14:54:42 +01:00
|
|
|
"/sys/devices/platform/ocp/ocp:P9_21_pinmux/state",
|
2016-11-27 13:37:23 +01:00
|
|
|
"/sys/class/leds/beaglebone:green:usr1/brightness",
|
2016-12-03 13:17:31 +01:00
|
|
|
"/sys/bus/iio/devices/iio:device0/in_voltage1_raw",
|
2017-05-03 10:38:45 +02:00
|
|
|
"/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/export",
|
|
|
|
"/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/unexport",
|
2017-05-03 11:30:54 +02:00
|
|
|
"/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm0/enable",
|
|
|
|
"/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm0/period",
|
|
|
|
"/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm0/duty_cycle",
|
|
|
|
"/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm0/polarity",
|
2017-05-03 10:38:45 +02:00
|
|
|
"/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm1/enable",
|
|
|
|
"/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm1/period",
|
|
|
|
"/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm1/duty_cycle",
|
|
|
|
"/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm1/polarity",
|
2014-11-07 18:45:11 -08:00
|
|
|
"/sys/class/gpio/export",
|
|
|
|
"/sys/class/gpio/unexport",
|
|
|
|
"/sys/class/gpio/gpio60/value",
|
|
|
|
"/sys/class/gpio/gpio60/direction",
|
2017-05-03 10:38:45 +02:00
|
|
|
"/sys/class/gpio/gpio66/value",
|
|
|
|
"/sys/class/gpio/gpio66/direction",
|
2014-11-07 18:45:11 -08:00
|
|
|
"/sys/class/gpio/gpio10/value",
|
|
|
|
"/sys/class/gpio/gpio10/direction",
|
2017-05-03 11:30:54 +02:00
|
|
|
"/sys/class/gpio/gpio30/value",
|
|
|
|
"/sys/class/gpio/gpio30/direction",
|
2022-11-05 07:42:28 +01:00
|
|
|
}
|
2014-11-09 14:35:36 -08:00
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
a, fs := initTestAdaptorWithMockedFilesystem(mockPaths)
|
2014-11-09 14:35:36 -08:00
|
|
|
|
|
|
|
// PWM
|
2017-05-03 10:38:45 +02:00
|
|
|
gobottest.Assert(t, a.PwmWrite("P9_99", 175), errors.New("Not a valid PWM pin"))
|
|
|
|
a.PwmWrite("P9_21", 175)
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(
|
2014-11-09 14:35:36 -08:00
|
|
|
t,
|
2017-05-03 10:38:45 +02:00
|
|
|
fs.Files["/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm1/period"].Contents,
|
2014-11-09 14:35:36 -08:00
|
|
|
"500000",
|
|
|
|
)
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(
|
2014-11-09 14:35:36 -08:00
|
|
|
t,
|
2017-05-03 10:38:45 +02:00
|
|
|
fs.Files["/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm1/duty_cycle"].Contents,
|
2014-12-23 18:35:45 -08:00
|
|
|
"343137",
|
2014-11-09 14:35:36 -08:00
|
|
|
)
|
|
|
|
|
2017-05-03 10:38:45 +02:00
|
|
|
a.ServoWrite("P9_21", 100)
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(
|
2014-11-09 14:35:36 -08:00
|
|
|
t,
|
2017-05-03 10:38:45 +02:00
|
|
|
fs.Files["/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm1/period"].Contents,
|
|
|
|
"500000",
|
2014-11-09 14:35:36 -08:00
|
|
|
)
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(
|
2014-11-09 14:35:36 -08:00
|
|
|
t,
|
2017-05-03 10:38:45 +02:00
|
|
|
fs.Files["/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm1/duty_cycle"].Contents,
|
|
|
|
"66666",
|
2014-11-09 14:35:36 -08:00
|
|
|
)
|
2017-05-03 10:38:45 +02:00
|
|
|
gobottest.Assert(t, a.ServoWrite("P9_99", 175), errors.New("Not a valid PWM pin"))
|
2017-04-27 18:40:29 +02:00
|
|
|
|
2017-05-03 11:30:54 +02:00
|
|
|
fs.WithReadError = true
|
2022-09-25 14:26:57 +02:00
|
|
|
gobottest.Assert(t, strings.Contains(a.PwmWrite("P9_21", 175).Error(), "read error"), true)
|
2017-05-03 11:30:54 +02:00
|
|
|
fs.WithReadError = false
|
|
|
|
|
|
|
|
fs.WithWriteError = true
|
2022-09-25 14:26:57 +02:00
|
|
|
gobottest.Assert(t, strings.Contains(a.PwmWrite("P9_22", 175).Error(), "write error"), true)
|
2017-05-03 11:30:54 +02:00
|
|
|
fs.WithWriteError = false
|
|
|
|
|
2014-11-09 14:35:36 -08:00
|
|
|
// Analog
|
2016-12-03 13:17:31 +01:00
|
|
|
fs.Files["/sys/bus/iio/devices/iio:device0/in_voltage1_raw"].Contents = "567\n"
|
2017-05-03 10:38:45 +02:00
|
|
|
i, err := a.AnalogRead("P9_40")
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, i, 567)
|
2017-05-03 10:38:45 +02:00
|
|
|
gobottest.Assert(t, err, nil)
|
2014-11-09 14:35:36 -08:00
|
|
|
|
2017-05-03 10:38:45 +02:00
|
|
|
_, err = a.AnalogRead("P9_99")
|
|
|
|
gobottest.Assert(t, err, errors.New("Not a valid analog pin"))
|
2014-11-19 18:40:06 -08:00
|
|
|
|
2017-05-03 11:30:54 +02:00
|
|
|
fs.WithReadError = true
|
|
|
|
_, err = a.AnalogRead("P9_40")
|
|
|
|
gobottest.Assert(t, err, errors.New("read error"))
|
|
|
|
fs.WithReadError = false
|
|
|
|
|
2014-11-09 14:35:36 -08:00
|
|
|
// DigitalIO
|
|
|
|
a.DigitalWrite("usr1", 1)
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t,
|
2016-11-27 13:37:23 +01:00
|
|
|
fs.Files["/sys/class/leds/beaglebone:green:usr1/brightness"].Contents,
|
2014-11-09 14:35:36 -08:00
|
|
|
"1",
|
|
|
|
)
|
|
|
|
|
2017-04-11 18:29:31 +02:00
|
|
|
// no such LED
|
|
|
|
err = a.DigitalWrite("usr10101", 1)
|
2022-11-05 07:42:28 +01:00
|
|
|
gobottest.Assert(t, err.Error(), " : /sys/class/leds/beaglebone:green:usr10101/brightness: No such file.")
|
2017-04-11 18:29:31 +02:00
|
|
|
|
2014-11-03 19:41:34 -08:00
|
|
|
a.DigitalWrite("P9_12", 1)
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, fs.Files["/sys/class/gpio/gpio60/value"].Contents, "1")
|
2014-11-03 19:41:34 -08:00
|
|
|
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, a.DigitalWrite("P9_99", 1), errors.New("Not a valid pin"))
|
2014-11-19 18:40:06 -08:00
|
|
|
|
2017-04-29 10:59:48 +02:00
|
|
|
_, err = a.DigitalRead("P9_99")
|
|
|
|
gobottest.Assert(t, err, errors.New("Not a valid pin"))
|
|
|
|
|
2017-05-03 10:38:45 +02:00
|
|
|
fs.Files["/sys/class/gpio/gpio66/value"].Contents = "1"
|
2018-02-13 19:36:40 +01:00
|
|
|
i, err = a.DigitalRead("P8_07")
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, i, 1)
|
2017-05-03 10:38:45 +02:00
|
|
|
gobottest.Assert(t, err, nil)
|
2014-11-03 19:41:34 -08:00
|
|
|
|
2017-04-27 18:40:29 +02:00
|
|
|
fs.WithReadError = true
|
2018-02-13 19:36:40 +01:00
|
|
|
_, err = a.DigitalRead("P8_07")
|
2017-04-27 18:40:29 +02:00
|
|
|
gobottest.Assert(t, err, errors.New("read error"))
|
|
|
|
fs.WithReadError = false
|
|
|
|
|
2017-05-03 11:30:54 +02:00
|
|
|
fs.WithWriteError = true
|
|
|
|
_, err = a.DigitalRead("P9_11")
|
|
|
|
gobottest.Assert(t, err, errors.New("write error"))
|
|
|
|
fs.WithWriteError = false
|
|
|
|
|
2014-11-09 14:35:36 -08:00
|
|
|
// I2c
|
2022-11-20 19:22:26 +01:00
|
|
|
a.sys.UseMockSyscall()
|
2014-11-03 19:41:34 -08:00
|
|
|
|
2017-02-10 11:08:32 +01:00
|
|
|
con, err := a.GetConnection(0xff, 2)
|
2017-02-06 14:50:14 +01:00
|
|
|
gobottest.Assert(t, err, nil)
|
2015-07-03 18:57:29 -07:00
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
_, err = con.Write([]byte{0x00, 0x01})
|
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
|
2017-02-06 14:50:14 +01:00
|
|
|
data := []byte{42, 42}
|
2022-11-05 07:42:28 +01:00
|
|
|
_, err = con.Read(data)
|
|
|
|
gobottest.Assert(t, err, nil)
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, data, []byte{0x00, 0x01})
|
2014-11-09 14:35:36 -08:00
|
|
|
|
2016-11-07 17:33:11 +01:00
|
|
|
gobottest.Assert(t, a.Finalize(), nil)
|
2014-06-12 16:12:38 -07:00
|
|
|
}
|
2017-04-06 09:52:34 +02:00
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestSPI(t *testing.T) {
|
|
|
|
a := NewAdaptor()
|
|
|
|
|
|
|
|
gobottest.Assert(t, a.GetSpiDefaultBus(), 0)
|
|
|
|
gobottest.Assert(t, a.GetSpiDefaultChip(), 0)
|
|
|
|
gobottest.Assert(t, a.GetSpiDefaultMode(), 0)
|
|
|
|
gobottest.Assert(t, a.GetSpiDefaultBits(), 8)
|
|
|
|
gobottest.Assert(t, a.GetSpiDefaultMaxSpeed(), int64(500000))
|
|
|
|
|
|
|
|
_, err := a.GetSpiConnection(10, 0, 0, 8, 10000000)
|
|
|
|
gobottest.Assert(t, err.Error(), "Bus number 10 out of range")
|
|
|
|
|
2022-11-20 19:22:26 +01:00
|
|
|
// TODO: tests for real connection currently not possible, because not using system.Accessor using
|
2022-11-05 07:42:28 +01:00
|
|
|
// TODO: test tx/rx here...
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestName(t *testing.T) {
|
2017-04-06 09:52:34 +02:00
|
|
|
a := NewAdaptor()
|
|
|
|
gobottest.Assert(t, strings.HasPrefix(a.Name(), "Beaglebone"), true)
|
|
|
|
a.SetName("NewName")
|
|
|
|
gobottest.Assert(t, a.Name(), "NewName")
|
|
|
|
}
|
2017-04-11 18:29:31 +02:00
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestDefaultBus(t *testing.T) {
|
2017-04-11 18:29:31 +02:00
|
|
|
a := NewAdaptor()
|
|
|
|
gobottest.Assert(t, a.GetDefaultBus(), 2)
|
|
|
|
}
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestGetConnectionInvalidBus(t *testing.T) {
|
2017-04-11 18:29:31 +02:00
|
|
|
a := NewAdaptor()
|
|
|
|
_, err := a.GetConnection(0x01, 99)
|
|
|
|
gobottest.Assert(t, err, errors.New("Bus number 99 out of range"))
|
|
|
|
}
|
2017-05-03 12:59:49 +02:00
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestAnalogReadFileError(t *testing.T) {
|
|
|
|
mockPaths := []string{
|
2017-12-19 16:45:16 +01:00
|
|
|
"/sys/devices/platform/whatever",
|
2022-11-05 07:42:28 +01:00
|
|
|
}
|
2017-05-03 12:59:49 +02:00
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
a, _ := initTestAdaptorWithMockedFilesystem(mockPaths)
|
2017-05-03 12:59:49 +02:00
|
|
|
|
|
|
|
_, err := a.AnalogRead("P9_40")
|
|
|
|
gobottest.Assert(t, strings.Contains(err.Error(), "/sys/bus/iio/devices/iio:device0/in_voltage1_raw: No such file."), true)
|
|
|
|
}
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestDigitalPinDirectionFileError(t *testing.T) {
|
|
|
|
mockPaths := []string{
|
2017-05-03 12:59:49 +02:00
|
|
|
"/sys/class/gpio/export",
|
|
|
|
"/sys/class/gpio/gpio60/value",
|
2017-12-14 14:54:42 +01:00
|
|
|
"/sys/devices/platform/ocp/ocp:P9_12_pinmux/state",
|
2022-11-05 07:42:28 +01:00
|
|
|
}
|
2017-05-03 12:59:49 +02:00
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
a, _ := initTestAdaptorWithMockedFilesystem(mockPaths)
|
2017-05-03 12:59:49 +02:00
|
|
|
|
|
|
|
err := a.DigitalWrite("P9_12", 1)
|
|
|
|
gobottest.Assert(t, strings.Contains(err.Error(), "/sys/class/gpio/gpio60/direction: No such file."), true)
|
|
|
|
|
2022-11-27 16:06:09 +01:00
|
|
|
// no pin added after previous problem, so no pin to unexport in finalize
|
2017-05-03 12:59:49 +02:00
|
|
|
err = a.Finalize()
|
2022-11-27 16:06:09 +01:00
|
|
|
gobottest.Assert(t, nil, err)
|
2017-05-03 12:59:49 +02:00
|
|
|
}
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestDigitalPinFinalizeFileError(t *testing.T) {
|
|
|
|
mockPaths := []string{
|
2017-05-03 12:59:49 +02:00
|
|
|
"/sys/class/gpio/export",
|
|
|
|
"/sys/class/gpio/gpio60/value",
|
|
|
|
"/sys/class/gpio/gpio60/direction",
|
2017-12-14 14:54:42 +01:00
|
|
|
"/sys/devices/platform/ocp/ocp:P9_12_pinmux/state",
|
2022-11-05 07:42:28 +01:00
|
|
|
}
|
2017-05-03 12:59:49 +02:00
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
a, _ := initTestAdaptorWithMockedFilesystem(mockPaths)
|
2017-05-03 12:59:49 +02:00
|
|
|
|
|
|
|
err := a.DigitalWrite("P9_12", 1)
|
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
|
|
|
|
err = a.Finalize()
|
|
|
|
gobottest.Assert(t, strings.Contains(err.Error(), "/sys/class/gpio/unexport: No such file."), true)
|
|
|
|
}
|
2017-12-19 13:42:49 +01:00
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestPocketName(t *testing.T) {
|
2017-12-19 13:42:49 +01:00
|
|
|
a := NewPocketBeagleAdaptor()
|
|
|
|
gobottest.Assert(t, strings.HasPrefix(a.Name(), "PocketBeagle"), true)
|
|
|
|
}
|