2014-11-04 17:14:36 -08:00
|
|
|
package raspi
|
|
|
|
|
|
|
|
import (
|
2015-01-03 02:21:35 -08:00
|
|
|
"strings"
|
2014-11-04 17:14:36 -08:00
|
|
|
"testing"
|
|
|
|
|
2016-08-27 11:56:01 +02:00
|
|
|
"github.com/hybridgroup/gobot"
|
2016-02-22 00:21:24 -05:00
|
|
|
"github.com/hybridgroup/gobot/gobottest"
|
2016-08-27 11:56:01 +02:00
|
|
|
"github.com/hybridgroup/gobot/platforms/gpio"
|
|
|
|
"github.com/hybridgroup/gobot/platforms/i2c"
|
2014-11-04 17:14:36 -08:00
|
|
|
"github.com/hybridgroup/gobot/sysfs"
|
|
|
|
)
|
|
|
|
|
2016-08-27 11:56:01 +02:00
|
|
|
var _ gobot.Adaptor = (*RaspiAdaptor)(nil)
|
|
|
|
|
|
|
|
var _ gpio.DigitalReader = (*RaspiAdaptor)(nil)
|
|
|
|
var _ gpio.DigitalWriter = (*RaspiAdaptor)(nil)
|
|
|
|
|
|
|
|
var _ i2c.I2c = (*RaspiAdaptor)(nil)
|
|
|
|
|
2015-07-03 18:57:29 -07:00
|
|
|
type NullReadWriteCloser struct {
|
|
|
|
contents []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NullReadWriteCloser) SetAddress(int) error {
|
|
|
|
return nil
|
|
|
|
}
|
2014-12-18 14:07:48 -08:00
|
|
|
|
2015-07-03 18:57:29 -07:00
|
|
|
func (n *NullReadWriteCloser) Write(b []byte) (int, error) {
|
|
|
|
n.contents = make([]byte, len(b))
|
|
|
|
copy(n.contents[:], b[:])
|
|
|
|
|
|
|
|
return len(b), nil
|
2014-12-18 14:07:48 -08:00
|
|
|
}
|
2015-07-03 18:57:29 -07:00
|
|
|
|
|
|
|
func (n *NullReadWriteCloser) Read(b []byte) (int, error) {
|
|
|
|
copy(b, n.contents)
|
2014-12-18 14:07:48 -08:00
|
|
|
return len(b), nil
|
|
|
|
}
|
2015-07-03 18:57:29 -07:00
|
|
|
|
|
|
|
var closeErr error = nil
|
|
|
|
|
|
|
|
func (n *NullReadWriteCloser) Close() error {
|
|
|
|
return closeErr
|
2014-12-18 14:07:48 -08:00
|
|
|
}
|
|
|
|
|
2014-11-04 17:14:36 -08:00
|
|
|
func initTestRaspiAdaptor() *RaspiAdaptor {
|
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 := NewRaspiAdaptor("myAdaptor")
|
|
|
|
a.Connect()
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2014-12-17 13:21:09 -08:00
|
|
|
func TestRaspiAdaptor(t *testing.T) {
|
|
|
|
readFile = func() ([]byte, error) {
|
|
|
|
return []byte(`
|
|
|
|
Hardware : BCM2708
|
|
|
|
Revision : 0010
|
|
|
|
Serial : 000000003bc748ea
|
|
|
|
`), nil
|
|
|
|
}
|
|
|
|
a := NewRaspiAdaptor("myAdaptor")
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, a.Name(), "myAdaptor")
|
|
|
|
gobottest.Assert(t, a.i2cLocation, "/dev/i2c-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 = NewRaspiAdaptor("myAdaptor")
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, a.i2cLocation, "/dev/i2c-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 = NewRaspiAdaptor("myAdaptor")
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, a.i2cLocation, "/dev/i2c-0")
|
|
|
|
gobottest.Assert(t, a.revision, "1")
|
2014-12-17 13:21:09 -08:00
|
|
|
|
|
|
|
}
|
2014-11-04 17:14:36 -08:00
|
|
|
func TestRaspiAdaptorFinalize(t *testing.T) {
|
|
|
|
a := initTestRaspiAdaptor()
|
2015-07-03 18:57:29 -07:00
|
|
|
|
2014-11-19 23:21:19 -08:00
|
|
|
fs := sysfs.NewMockFilesystem([]string{
|
|
|
|
"/sys/class/gpio/export",
|
|
|
|
"/sys/class/gpio/unexport",
|
2015-01-03 02:21:35 -08:00
|
|
|
"/dev/pi-blaster",
|
2015-07-03 18:57:29 -07:00
|
|
|
"/dev/i2c-1",
|
|
|
|
"/dev/i2c-0",
|
2014-11-19 23:21:19 -08:00
|
|
|
})
|
|
|
|
|
|
|
|
sysfs.SetFilesystem(fs)
|
2015-07-03 18:57:29 -07:00
|
|
|
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)
|
2015-07-03 18:57:29 -07:00
|
|
|
|
|
|
|
a.I2cStart(0xff)
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, len(a.Finalize()), 0)
|
2014-11-04 17:14:36 -08:00
|
|
|
}
|
|
|
|
|
2015-01-03 02:21:35 -08:00
|
|
|
func TestRaspiAdaptorDigitalPWM(t *testing.T) {
|
|
|
|
a := initTestRaspiAdaptor()
|
|
|
|
|
2016-02-22 00:21:24 -05: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)
|
|
|
|
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, a.PwmWrite("7", 255), nil)
|
2015-01-03 02:21:35 -08:00
|
|
|
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, strings.Split(fs.Files["/dev/pi-blaster"].Contents, "\n")[0], "4=1")
|
2015-01-03 02:21:35 -08:00
|
|
|
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, a.ServoWrite("11", 255), nil)
|
2015-01-03 02:21:35 -08:00
|
|
|
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, strings.Split(fs.Files["/dev/pi-blaster"].Contents, "\n")[0], "17=0.25")
|
2015-01-03 02:21:35 -08:00
|
|
|
}
|
|
|
|
|
2014-11-04 17:14:36 -08:00
|
|
|
func TestRaspiAdaptorDigitalIO(t *testing.T) {
|
|
|
|
a := initTestRaspiAdaptor()
|
2014-11-07 16:57:05 -08:00
|
|
|
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
|
|
|
|
2014-11-07 16:57:05 -08:00
|
|
|
sysfs.SetFilesystem(fs)
|
2014-11-04 17:14:36 -08:00
|
|
|
|
|
|
|
a.DigitalWrite("7", 1)
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, fs.Files["/sys/class/gpio/gpio4/value"].Contents, "1")
|
2014-11-04 17:14:36 -08:00
|
|
|
|
2014-11-07 16:57:05 -08:00
|
|
|
a.DigitalWrite("13", 1)
|
2014-11-19 12:19:43 -08:00
|
|
|
i, _ := a.DigitalRead("13")
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, i, 1)
|
2014-11-04 17:14:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRaspiAdaptorI2c(t *testing.T) {
|
|
|
|
a := initTestRaspiAdaptor()
|
2014-11-07 16:57:05 -08:00
|
|
|
fs := sysfs.NewMockFilesystem([]string{
|
|
|
|
"/dev/i2c-1",
|
|
|
|
})
|
|
|
|
sysfs.SetFilesystem(fs)
|
|
|
|
sysfs.SetSyscall(&sysfs.MockSyscall{})
|
2014-11-04 17:14:36 -08:00
|
|
|
a.I2cStart(0xff)
|
2015-07-03 18:57:29 -07:00
|
|
|
a.i2cDevice = &NullReadWriteCloser{}
|
2014-11-04 17:14:36 -08:00
|
|
|
|
2015-07-03 18:57:29 -07:00
|
|
|
a.I2cWrite(0xff, []byte{0x00, 0x01})
|
|
|
|
data, _ := a.I2cRead(0xff, 2)
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, data, []byte{0x00, 0x01})
|
2014-11-04 17:14:36 -08:00
|
|
|
}
|