hybridgroup.gobot/platforms/dragonboard/dragonboard_adaptor_test.go

69 lines
1.7 KiB
Go
Raw Normal View History

2017-02-23 18:12:17 +08:00
package dragonboard
import (
"errors"
"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-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-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)
}