2017-02-23 18:12:17 +08:00
|
|
|
package dragonboard
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2017-04-06 17:10:58 +08:00
|
|
|
"strings"
|
2017-02-23 18:12:17 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/drivers/gpio"
|
|
|
|
"gobot.io/x/gobot/drivers/i2c"
|
|
|
|
"gobot.io/x/gobot/gobottest"
|
|
|
|
"gobot.io/x/gobot/sysfs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// make sure that this Adaptor fullfills all the required interfaces
|
|
|
|
var _ gobot.Adaptor = (*Adaptor)(nil)
|
|
|
|
var _ gpio.DigitalReader = (*Adaptor)(nil)
|
|
|
|
var _ gpio.DigitalWriter = (*Adaptor)(nil)
|
|
|
|
var _ i2c.Connector = (*Adaptor)(nil)
|
|
|
|
|
2017-02-23 23:54:13 +08:00
|
|
|
func initTestDragonBoardAdaptor(t *testing.T) *Adaptor {
|
2017-02-23 18:12:17 +08:00
|
|
|
a := NewAdaptor()
|
2017-02-23 23:54:13 +08:00
|
|
|
if err := a.Connect(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2017-02-23 18:12:17 +08:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2017-04-06 17:10:58 +08:00
|
|
|
func TestDragonBoardAdaptorName(t *testing.T) {
|
|
|
|
a := initTestDragonBoardAdaptor(t)
|
|
|
|
gobottest.Assert(t, strings.HasPrefix(a.Name(), "DragonBoard"), true)
|
|
|
|
a.SetName("NewName")
|
|
|
|
gobottest.Assert(t, a.Name(), "NewName")
|
|
|
|
}
|
|
|
|
|
2017-02-23 23:54:13 +08:00
|
|
|
func TestDragonBoardAdaptorDigitalIO(t *testing.T) {
|
|
|
|
a := initTestDragonBoardAdaptor(t)
|
2017-02-23 18:12:17 +08:00
|
|
|
fs := sysfs.NewMockFilesystem([]string{
|
|
|
|
"/sys/class/gpio/export",
|
|
|
|
"/sys/class/gpio/unexport",
|
2017-02-23 23:54:13 +08:00
|
|
|
"/sys/class/gpio/gpio36/value",
|
|
|
|
"/sys/class/gpio/gpio36/direction",
|
|
|
|
"/sys/class/gpio/gpio12/value",
|
|
|
|
"/sys/class/gpio/gpio12/direction",
|
2017-02-23 18:12:17 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
sysfs.SetFilesystem(fs)
|
|
|
|
|
2017-02-23 23:54:13 +08:00
|
|
|
_ = a.DigitalWrite("GPIO_B", 1)
|
|
|
|
gobottest.Assert(t, fs.Files["/sys/class/gpio/gpio12/value"].Contents, "1")
|
2017-02-23 18:12:17 +08:00
|
|
|
|
2017-02-23 23:54:13 +08:00
|
|
|
fs.Files["/sys/class/gpio/gpio36/value"].Contents = "1"
|
|
|
|
i, _ := a.DigitalRead("GPIO_A")
|
2017-02-23 18:12:17 +08:00
|
|
|
gobottest.Assert(t, i, 1)
|
|
|
|
|
2017-02-23 23:54:13 +08:00
|
|
|
gobottest.Assert(t, a.DigitalWrite("GPIO_M", 1), errors.New("Not a valid pin"))
|
2017-04-29 18:43:55 +08:00
|
|
|
gobottest.Assert(t, a.Finalize(), nil)
|
2017-02-23 18:12:17 +08:00
|
|
|
}
|
|
|
|
|
2017-02-23 23:54:13 +08:00
|
|
|
func TestDragonBoardAdaptorI2c(t *testing.T) {
|
|
|
|
a := initTestDragonBoardAdaptor(t)
|
2017-02-23 18:12:17 +08:00
|
|
|
fs := sysfs.NewMockFilesystem([]string{
|
|
|
|
"/dev/i2c-1",
|
|
|
|
})
|
|
|
|
sysfs.SetFilesystem(fs)
|
|
|
|
sysfs.SetSyscall(&sysfs.MockSyscall{})
|
|
|
|
|
|
|
|
con, err := a.GetConnection(0xff, 1)
|
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
|
2017-02-23 23:54:13 +08:00
|
|
|
_, _ = con.Write([]byte{0x00, 0x01})
|
2017-02-23 18:12:17 +08:00
|
|
|
data := []byte{42, 42}
|
2017-02-23 23:54:13 +08:00
|
|
|
_, _ = con.Read(data)
|
2017-02-23 18:12:17 +08:00
|
|
|
gobottest.Assert(t, data, []byte{0x00, 0x01})
|
|
|
|
|
|
|
|
gobottest.Assert(t, a.Finalize(), nil)
|
|
|
|
}
|
2017-04-12 00:37:28 +08:00
|
|
|
|
|
|
|
func TestDragonBoardDefaultBus(t *testing.T) {
|
|
|
|
a := initTestDragonBoardAdaptor(t)
|
|
|
|
gobottest.Assert(t, a.GetDefaultBus(), 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDragonBoardGetConnectionInvalidBus(t *testing.T) {
|
|
|
|
a := initTestDragonBoardAdaptor(t)
|
|
|
|
_, err := a.GetConnection(0x01, 99)
|
|
|
|
gobottest.Assert(t, err, errors.New("Bus number 99 out of range"))
|
|
|
|
}
|
2017-04-29 18:43:55 +08:00
|
|
|
|
|
|
|
func TestAdaptorFinalizeErrorAfterGPIO(t *testing.T) {
|
|
|
|
a := initTestDragonBoardAdaptor(t)
|
|
|
|
fs := sysfs.NewMockFilesystem([]string{
|
|
|
|
"/sys/class/gpio/export",
|
|
|
|
"/sys/class/gpio/unexport",
|
|
|
|
"/sys/class/gpio/gpio36/value",
|
|
|
|
"/sys/class/gpio/gpio36/direction",
|
|
|
|
"/sys/class/gpio/gpio12/value",
|
|
|
|
"/sys/class/gpio/gpio12/direction",
|
|
|
|
})
|
|
|
|
|
|
|
|
sysfs.SetFilesystem(fs)
|
|
|
|
|
|
|
|
gobottest.Assert(t, a.Connect(), nil)
|
|
|
|
gobottest.Assert(t, a.DigitalWrite("GPIO_B", 1), nil)
|
|
|
|
|
|
|
|
fs.WithWriteError = true
|
|
|
|
|
|
|
|
err := a.Finalize()
|
|
|
|
gobottest.Assert(t, strings.Contains(err.Error(), "write error"), true)
|
|
|
|
}
|