1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-29 13:49:14 +08:00
hybridgroup.gobot/platforms/raspi/raspi_adaptor_test.go

163 lines
3.7 KiB
Go
Raw Normal View History

2014-11-04 17:14:36 -08:00
package raspi
import (
"errors"
2015-01-03 02:21:35 -08:00
"strings"
2014-11-04 17:14:36 -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"
2014-11-04 17:14:36 -08:00
)
// 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)
func initTestAdaptor() *Adaptor {
2014-12-17 13:21:09 -08:00
readFile = func() ([]byte, error) {
return []byte(`
Hardware : BCM2708
Revision : 0010
Serial : 000000003bc748ea
`), nil
2014-11-04 17:14:36 -08:00
}
a := NewAdaptor()
2014-11-04 17:14:36 -08:00
a.Connect()
return a
}
func TestRaspiAdaptorName(t *testing.T) {
a := initTestAdaptor()
gobottest.Assert(t, strings.HasPrefix(a.Name(), "RaspberryPi"), true)
a.SetName("NewName")
gobottest.Assert(t, a.Name(), "NewName")
}
func TestAdaptor(t *testing.T) {
2014-12-17 13:21:09 -08:00
readFile = func() ([]byte, error) {
return []byte(`
Hardware : BCM2708
Revision : 0010
Serial : 000000003bc748ea
`), nil
}
a := NewAdaptor()
gobottest.Assert(t, strings.HasPrefix(a.Name(), "RaspberryPi"), true)
gobottest.Assert(t, a.i2cDefaultBus, 1)
gobottest.Assert(t, a.revision, "3")
2014-12-17 13:21:09 -08:00
readFile = func() ([]byte, error) {
return []byte(`
Hardware : BCM2708
Revision : 000D
Serial : 000000003bc748ea
`), nil
}
a = NewAdaptor()
gobottest.Assert(t, a.i2cDefaultBus, 1)
gobottest.Assert(t, a.revision, "2")
2014-12-17 13:21:09 -08:00
readFile = func() ([]byte, error) {
return []byte(`
Hardware : BCM2708
Revision : 0002
Serial : 000000003bc748ea
`), nil
}
a = NewAdaptor()
gobottest.Assert(t, a.i2cDefaultBus, 0)
gobottest.Assert(t, a.revision, "1")
2014-12-17 13:21:09 -08:00
}
func TestAdaptorFinalize(t *testing.T) {
a := initTestAdaptor()
fs := sysfs.NewMockFilesystem([]string{
"/sys/class/gpio/export",
"/sys/class/gpio/unexport",
2015-01-03 02:21:35 -08:00
"/dev/pi-blaster",
"/dev/i2c-1",
"/dev/i2c-0",
})
sysfs.SetFilesystem(fs)
sysfs.SetSyscall(&sysfs.MockSyscall{})
2014-11-04 17:14:36 -08:00
a.DigitalWrite("3", 1)
2015-01-03 02:21:35 -08:00
a.PwmWrite("7", 255)
a.GetConnection(0xff, 0)
gobottest.Assert(t, a.Finalize(), nil)
2014-11-04 17:14:36 -08:00
}
func TestAdaptorDigitalPWM(t *testing.T) {
a := initTestAdaptor()
2015-01-03 02:21:35 -08:00
gobottest.Assert(t, a.PwmWrite("7", 4), nil)
2015-01-03 02:21:35 -08:00
fs := sysfs.NewMockFilesystem([]string{
"/dev/pi-blaster",
})
sysfs.SetFilesystem(fs)
gobottest.Assert(t, a.PwmWrite("7", 255), nil)
2015-01-03 02:21:35 -08:00
gobottest.Assert(t, strings.Split(fs.Files["/dev/pi-blaster"].Contents, "\n")[0], "4=1")
2015-01-03 02:21:35 -08:00
gobottest.Assert(t, a.ServoWrite("11", 255), nil)
2015-01-03 02:21:35 -08:00
gobottest.Assert(t, strings.Split(fs.Files["/dev/pi-blaster"].Contents, "\n")[0], "17=0.25")
gobottest.Assert(t, a.PwmWrite("notexist", 1), errors.New("Not a valid pin"))
2015-01-03 02:21:35 -08:00
}
func TestAdaptorDigitalIO(t *testing.T) {
a := initTestAdaptor()
fs := sysfs.NewMockFilesystem([]string{
"/sys/class/gpio/export",
"/sys/class/gpio/unexport",
"/sys/class/gpio/gpio4/value",
"/sys/class/gpio/gpio4/direction",
"/sys/class/gpio/gpio27/value",
"/sys/class/gpio/gpio27/direction",
})
2014-11-04 17:14:36 -08:00
sysfs.SetFilesystem(fs)
2014-11-04 17:14:36 -08:00
a.DigitalWrite("7", 1)
gobottest.Assert(t, fs.Files["/sys/class/gpio/gpio4/value"].Contents, "1")
2014-11-04 17:14:36 -08:00
a.DigitalWrite("13", 1)
i, _ := a.DigitalRead("13")
gobottest.Assert(t, i, 1)
gobottest.Assert(t, a.DigitalWrite("notexist", 1), errors.New("Not a valid pin"))
2014-11-04 17:14:36 -08:00
}
func TestAdaptorI2c(t *testing.T) {
a := initTestAdaptor()
fs := sysfs.NewMockFilesystem([]string{
"/dev/i2c-1",
})
sysfs.SetFilesystem(fs)
sysfs.SetSyscall(&sysfs.MockSyscall{})
2014-11-04 17:14:36 -08:00
con, err := a.GetConnection(0xff, 1)
gobottest.Assert(t, err, nil)
con.Write([]byte{0x00, 0x01})
data := []byte{42, 42}
con.Read(data)
gobottest.Assert(t, data, []byte{0x00, 0x01})
_, err = a.GetConnection(0xff, 51)
gobottest.Assert(t, err, errors.New("Bus number 51 out of range"))
gobottest.Assert(t, a.GetDefaultBus(), 1)
2014-11-04 17:14:36 -08:00
}