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"
|
|
|
|
"gobot.io/x/gobot/gobottest"
|
|
|
|
"gobot.io/x/gobot/sysfs"
|
2014-04-26 03:11:51 -07:00
|
|
|
)
|
|
|
|
|
2017-02-10 11:08:32 +01:00
|
|
|
// make sure that this Adaptor fullfills all the required interfaces
|
2016-09-25 14:12:52 +02:00
|
|
|
var _ gobot.Adaptor = (*Adaptor)(nil)
|
|
|
|
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)
|
2016-08-26 14:23:03 +02:00
|
|
|
|
2014-11-09 14:35:36 -08:00
|
|
|
func TestBeagleboneAdaptor(t *testing.T) {
|
|
|
|
glob = func(pattern string) (matches []string, err error) {
|
|
|
|
return make([]string, 2), nil
|
|
|
|
}
|
2014-11-07 18:45:11 -08:00
|
|
|
fs := sysfs.NewMockFilesystem([]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",
|
|
|
|
"/sys/devices/platform/ocp/ocp4",
|
|
|
|
"/sys/class/leds/beaglebone:green:usr1/brightness",
|
2016-12-03 13:17:31 +01:00
|
|
|
"/sys/bus/iio/devices/iio:device0/in_voltage1_raw",
|
2016-11-27 13:37:23 +01:00
|
|
|
"/sys/devices/platform/ocp/ocp4/pwm_test_P9_14.5",
|
|
|
|
"/sys/devices/platform/ocp/ocp4/pwm_test_P9_14.5/run",
|
|
|
|
"/sys/devices/platform/ocp/ocp4/pwm_test_P9_14.5/period",
|
|
|
|
"/sys/devices/platform/ocp/ocp4/pwm_test_P9_14.5/polarity",
|
|
|
|
"/sys/devices/platform/ocp/ocp4/pwm_test_P9_14.5/duty",
|
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",
|
|
|
|
"/sys/class/gpio/gpio10/value",
|
|
|
|
"/sys/class/gpio/gpio10/direction",
|
|
|
|
})
|
|
|
|
|
|
|
|
sysfs.SetFilesystem(fs)
|
2016-09-25 14:12:52 +02:00
|
|
|
a := NewAdaptor()
|
2016-11-27 13:37:23 +01:00
|
|
|
a.slots = "/sys/devices/platform/bone_capemgr"
|
|
|
|
a.ocp = "/sys/devices/platform/ocp/ocp4"
|
2016-12-03 14:09:38 +01:00
|
|
|
a.kernel = "4.4"
|
2014-11-09 14:35:36 -08:00
|
|
|
|
|
|
|
a.Connect()
|
|
|
|
|
2016-12-03 13:17:31 +01:00
|
|
|
a.analogPath = "/sys/bus/iio/devices/iio:device0"
|
2014-11-10 12:48:08 -08:00
|
|
|
|
2014-11-09 14:35:36 -08:00
|
|
|
// PWM
|
|
|
|
glob = func(pattern string) (matches []string, err error) {
|
|
|
|
pattern = strings.TrimSuffix(pattern, "*")
|
|
|
|
return []string{pattern + "5"}, nil
|
|
|
|
}
|
|
|
|
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, a.PwmWrite("P9_99", 175), errors.New("Not a valid pin"))
|
2014-11-09 14:35:36 -08:00
|
|
|
a.PwmWrite("P9_14", 175)
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(
|
2014-11-09 14:35:36 -08:00
|
|
|
t,
|
2016-11-27 13:37:23 +01:00
|
|
|
fs.Files["/sys/devices/platform/ocp/ocp4/pwm_test_P9_14.5/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,
|
2016-11-27 13:37:23 +01:00
|
|
|
fs.Files["/sys/devices/platform/ocp/ocp4/pwm_test_P9_14.5/duty"].Contents,
|
2014-12-23 18:35:45 -08:00
|
|
|
"343137",
|
2014-11-09 14:35:36 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
a.ServoWrite("P9_14", 100)
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(
|
2014-11-09 14:35:36 -08:00
|
|
|
t,
|
2016-11-27 13:37:23 +01:00
|
|
|
fs.Files["/sys/devices/platform/ocp/ocp4/pwm_test_P9_14.5/period"].Contents,
|
2014-12-23 18:35:45 -08:00
|
|
|
"16666666",
|
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,
|
2016-11-27 13:37:23 +01:00
|
|
|
fs.Files["/sys/devices/platform/ocp/ocp4/pwm_test_P9_14.5/duty"].Contents,
|
2014-12-23 18:35:45 -08:00
|
|
|
"1898148",
|
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"
|
2014-11-16 14:18:29 -08:00
|
|
|
i, _ := a.AnalogRead("P9_40")
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, i, 567)
|
2014-11-09 14:35:36 -08:00
|
|
|
|
2016-12-03 13:17:31 +01:00
|
|
|
_, err := a.AnalogRead("P9_99")
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, err, errors.New("Not a valid pin"))
|
2014-11-19 18:40:06 -08:00
|
|
|
|
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",
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2014-11-07 18:45:11 -08:00
|
|
|
fs.Files["/sys/class/gpio/gpio10/value"].Contents = "1"
|
2014-11-16 14:18:29 -08:00
|
|
|
i, _ = a.DigitalRead("P8_31")
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, i, 1)
|
2014-11-03 19:41:34 -08:00
|
|
|
|
2014-11-09 14:35:36 -08:00
|
|
|
// I2c
|
2014-11-07 18:45:11 -08:00
|
|
|
sysfs.SetSyscall(&sysfs.MockSyscall{})
|
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
|
|
|
|
2017-02-06 14:50:14 +01:00
|
|
|
con.Write([]byte{0x00, 0x01})
|
|
|
|
data := []byte{42, 42}
|
|
|
|
con.Read(data)
|
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
|
|
|
}
|