2014-11-04 17:14:36 -08:00
|
|
|
package raspi
|
|
|
|
|
|
|
|
import (
|
2017-04-10 04:58:18 +02:00
|
|
|
"errors"
|
2015-01-03 02:21:35 -08:00
|
|
|
"strings"
|
2014-11-04 17:14:36 -08:00
|
|
|
"testing"
|
|
|
|
|
2017-12-13 13:17:12 +01:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
|
2016-12-08 13:24:03 +01:00
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/drivers/gpio"
|
|
|
|
"gobot.io/x/gobot/drivers/i2c"
|
2017-08-24 20:11:11 -06:00
|
|
|
"gobot.io/x/gobot/drivers/spi"
|
2016-12-08 13:24:03 +01:00
|
|
|
"gobot.io/x/gobot/gobottest"
|
|
|
|
"gobot.io/x/gobot/sysfs"
|
2014-11-04 17:14:36 -08:00
|
|
|
)
|
|
|
|
|
2017-02-10 11:08:32 +01:00
|
|
|
// make sure that this Adaptor fullfills all the required interfaces
|
2016-10-01 18:09:04 +02:00
|
|
|
var _ gobot.Adaptor = (*Adaptor)(nil)
|
|
|
|
var _ gpio.DigitalReader = (*Adaptor)(nil)
|
|
|
|
var _ gpio.DigitalWriter = (*Adaptor)(nil)
|
2017-05-01 09:24:34 +02:00
|
|
|
var _ gpio.PwmWriter = (*Adaptor)(nil)
|
|
|
|
var _ gpio.ServoWriter = (*Adaptor)(nil)
|
|
|
|
var _ sysfs.DigitalPinnerProvider = (*Adaptor)(nil)
|
|
|
|
var _ sysfs.PWMPinnerProvider = (*Adaptor)(nil)
|
2017-02-10 11:08:32 +01:00
|
|
|
var _ i2c.Connector = (*Adaptor)(nil)
|
2017-08-24 20:11:11 -06:00
|
|
|
var _ spi.Connector = (*Adaptor)(nil)
|
2016-08-27 11:56:01 +02:00
|
|
|
|
2016-10-01 18:09:04 +02:00
|
|
|
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
|
|
|
}
|
2016-10-01 18:09:04 +02:00
|
|
|
a := NewAdaptor()
|
2014-11-04 17:14:36 -08:00
|
|
|
a.Connect()
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2017-04-06 11:14:31 +02:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2016-10-01 18:09:04 +02:00
|
|
|
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
|
|
|
|
}
|
2016-10-01 18:09:04 +02:00
|
|
|
a := NewAdaptor()
|
2017-04-06 11:14:31 +02:00
|
|
|
gobottest.Assert(t, strings.HasPrefix(a.Name(), "RaspberryPi"), true)
|
2017-02-06 14:50:14 +01:00
|
|
|
gobottest.Assert(t, a.i2cDefaultBus, 1)
|
2016-02-22 00:21:24 -05:00
|
|
|
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
|
|
|
|
}
|
2016-10-01 18:09:04 +02:00
|
|
|
a = NewAdaptor()
|
2017-02-06 14:50:14 +01:00
|
|
|
gobottest.Assert(t, a.i2cDefaultBus, 1)
|
2016-02-22 00:21:24 -05:00
|
|
|
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
|
|
|
|
}
|
2016-10-01 18:09:04 +02:00
|
|
|
a = NewAdaptor()
|
2017-02-06 14:50:14 +01:00
|
|
|
gobottest.Assert(t, a.i2cDefaultBus, 0)
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, a.revision, "1")
|
2014-12-17 13:21:09 -08:00
|
|
|
|
|
|
|
}
|
2017-04-27 17:49:21 +02:00
|
|
|
|
2016-10-01 18:09:04 +02:00
|
|
|
func TestAdaptorFinalize(t *testing.T) {
|
|
|
|
a := initTestAdaptor()
|
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",
|
2017-08-24 20:11:11 -06:00
|
|
|
"/dev/spidev0.0",
|
|
|
|
"/dev/spidev0.1",
|
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
|
|
|
|
2017-02-10 11:08:32 +01:00
|
|
|
a.GetConnection(0xff, 0)
|
2016-11-07 20:11:31 +01:00
|
|
|
gobottest.Assert(t, a.Finalize(), nil)
|
2014-11-04 17:14:36 -08:00
|
|
|
}
|
|
|
|
|
2016-10-01 18:09:04 +02:00
|
|
|
func TestAdaptorDigitalPWM(t *testing.T) {
|
|
|
|
a := initTestAdaptor()
|
2015-01-03 02:21:35 -08:00
|
|
|
|
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
|
|
|
|
2017-05-01 08:37:14 +02:00
|
|
|
gobottest.Assert(t, a.ServoWrite("11", 90), nil)
|
2015-01-03 02:21:35 -08:00
|
|
|
|
2017-05-01 08:37:14 +02:00
|
|
|
gobottest.Assert(t, strings.Split(fs.Files["/dev/pi-blaster"].Contents, "\n")[0], "17=0.5")
|
2017-04-10 04:58:18 +02:00
|
|
|
|
|
|
|
gobottest.Assert(t, a.PwmWrite("notexist", 1), errors.New("Not a valid pin"))
|
2017-04-27 17:49:21 +02:00
|
|
|
gobottest.Assert(t, a.ServoWrite("notexist", 1), errors.New("Not a valid pin"))
|
2015-01-03 02:21:35 -08:00
|
|
|
}
|
|
|
|
|
2016-10-01 18:09:04 +02:00
|
|
|
func TestAdaptorDigitalIO(t *testing.T) {
|
|
|
|
a := initTestAdaptor()
|
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)
|
2017-04-10 04:58:18 +02:00
|
|
|
|
|
|
|
gobottest.Assert(t, a.DigitalWrite("notexist", 1), errors.New("Not a valid pin"))
|
2017-04-27 17:49:21 +02:00
|
|
|
|
|
|
|
fs.WithReadError = true
|
|
|
|
_, err := a.DigitalRead("13")
|
|
|
|
gobottest.Assert(t, err, errors.New("read error"))
|
2017-05-01 10:41:24 +02:00
|
|
|
|
|
|
|
fs.WithWriteError = true
|
|
|
|
_, err = a.DigitalRead("7")
|
|
|
|
gobottest.Assert(t, err, errors.New("write error"))
|
2014-11-04 17:14:36 -08:00
|
|
|
}
|
|
|
|
|
2016-10-01 18:09:04 +02:00
|
|
|
func TestAdaptorI2c(t *testing.T) {
|
|
|
|
a := initTestAdaptor()
|
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
|
|
|
|
2017-02-10 11:08:32 +01:00
|
|
|
con, err := a.GetConnection(0xff, 1)
|
2017-02-06 14:50:14 +01:00
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
|
|
|
|
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})
|
2017-04-10 04:58:18 +02:00
|
|
|
|
|
|
|
_, 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
|
|
|
}
|
2017-05-05 13:48:47 +02:00
|
|
|
|
2017-08-24 20:11:11 -06:00
|
|
|
func TestAdaptorSPI(t *testing.T) {
|
|
|
|
a := initTestAdaptor()
|
|
|
|
fs := sysfs.NewMockFilesystem([]string{
|
|
|
|
"/dev/spidev0.1",
|
|
|
|
})
|
|
|
|
sysfs.SetFilesystem(fs)
|
|
|
|
sysfs.SetSyscall(&sysfs.MockSyscall{})
|
2017-12-13 13:17:12 +01:00
|
|
|
|
2018-04-11 18:59:01 +02:00
|
|
|
gobottest.Assert(t, a.GetSpiDefaultBus(), 0)
|
|
|
|
gobottest.Assert(t, a.GetSpiDefaultChip(), 0)
|
2017-08-24 20:11:11 -06:00
|
|
|
gobottest.Assert(t, a.GetSpiDefaultMode(), 0)
|
|
|
|
gobottest.Assert(t, a.GetSpiDefaultMaxSpeed(), int64(500000))
|
|
|
|
|
2018-04-11 18:59:01 +02:00
|
|
|
_, err := a.GetSpiConnection(10, 0, 0, 8, 500000)
|
2017-12-13 13:17:12 +01:00
|
|
|
gobottest.Assert(t, err.Error(), "Bus number 10 out of range")
|
|
|
|
|
|
|
|
// TODO: test tx/rx here...
|
2017-08-24 20:11:11 -06:00
|
|
|
}
|
|
|
|
|
2017-05-05 13:48:47 +02:00
|
|
|
func TestAdaptorDigitalPinConcurrency(t *testing.T) {
|
|
|
|
|
|
|
|
oldProcs := runtime.GOMAXPROCS(0)
|
|
|
|
runtime.GOMAXPROCS(8)
|
|
|
|
|
|
|
|
for retry := 0; retry < 20; retry++ {
|
|
|
|
|
|
|
|
a := initTestAdaptor()
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
for i := 0; i < 20; i++ {
|
2017-12-21 22:29:11 +01:00
|
|
|
wg.Add(1)
|
2017-05-05 13:48:47 +02:00
|
|
|
pinAsString := strconv.Itoa(i)
|
2017-12-21 22:29:11 +01:00
|
|
|
go func(pin string) {
|
2017-05-05 13:48:47 +02:00
|
|
|
defer wg.Done()
|
2017-12-21 22:29:11 +01:00
|
|
|
a.DigitalPin(pin, sysfs.IN)
|
|
|
|
}(pinAsString)
|
2017-05-05 13:48:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime.GOMAXPROCS(oldProcs)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAdaptorPWMPin(t *testing.T) {
|
|
|
|
a := initTestAdaptor()
|
|
|
|
|
|
|
|
gobottest.Assert(t, len(a.pwmPins), 0)
|
|
|
|
|
|
|
|
firstSysPin, err := a.PWMPin("35")
|
|
|
|
|
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
gobottest.Assert(t, len(a.pwmPins), 1)
|
|
|
|
|
|
|
|
secondSysPin, err := a.PWMPin("35")
|
|
|
|
|
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
gobottest.Assert(t, len(a.pwmPins), 1)
|
|
|
|
gobottest.Assert(t, firstSysPin, secondSysPin)
|
|
|
|
|
|
|
|
otherSysPin, err := a.PWMPin("36")
|
|
|
|
|
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
gobottest.Assert(t, len(a.pwmPins), 2)
|
|
|
|
gobottest.Refute(t, firstSysPin, otherSysPin)
|
2017-05-05 14:51:33 +02:00
|
|
|
}
|