2018-08-13 17:03:47 +02:00
|
|
|
package i2c
|
|
|
|
|
|
|
|
import (
|
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/drivers/aio"
|
|
|
|
"gobot.io/x/gobot/drivers/gpio"
|
|
|
|
"gobot.io/x/gobot/gobottest"
|
2019-11-26 23:45:00 +01:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2018-08-13 17:03:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ gobot.Driver = (*GrovePiDriver)(nil)
|
|
|
|
|
2018-08-14 00:21:46 +02:00
|
|
|
// must implement the DigitalReader interface
|
|
|
|
var _ gpio.DigitalReader = (*GrovePiDriver)(nil)
|
|
|
|
|
2018-08-13 17:03:47 +02:00
|
|
|
// must implement the DigitalWriter interface
|
|
|
|
var _ gpio.DigitalWriter = (*GrovePiDriver)(nil)
|
|
|
|
|
|
|
|
// must implement the AnalogReader interface
|
|
|
|
var _ aio.AnalogReader = (*GrovePiDriver)(nil)
|
|
|
|
|
2018-08-15 00:12:48 +02:00
|
|
|
// must implement the Adaptor interface
|
|
|
|
var _ gobot.Adaptor = (*GrovePiDriver)(nil)
|
|
|
|
|
2019-11-26 23:45:00 +01:00
|
|
|
func initTestGrovePiDriver() (driver *GrovePiDriver, adaptor *i2cTestAdaptor) {
|
|
|
|
return initGrovePiDriverWithStubbedAdaptor()
|
2018-08-13 17:03:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func initGrovePiDriverWithStubbedAdaptor() (*GrovePiDriver, *i2cTestAdaptor) {
|
|
|
|
adaptor := newI2cTestAdaptor()
|
|
|
|
return NewGrovePiDriver(adaptor), adaptor
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGrovePiDriverName(t *testing.T) {
|
2019-11-26 23:45:00 +01:00
|
|
|
g, _ := initTestGrovePiDriver()
|
2018-08-13 17:03:47 +02:00
|
|
|
gobottest.Refute(t, g.Connection(), nil)
|
|
|
|
gobottest.Assert(t, strings.HasPrefix(g.Name(), "GrovePi"), true)
|
|
|
|
}
|
2018-08-14 10:24:07 +02:00
|
|
|
|
|
|
|
func TestGrovePiDriverOptions(t *testing.T) {
|
|
|
|
g := NewGrovePiDriver(newI2cTestAdaptor(), WithBus(2))
|
|
|
|
gobottest.Assert(t, g.GetBusOrDefault(1), 2)
|
|
|
|
}
|
|
|
|
|
2019-11-26 23:45:00 +01:00
|
|
|
func TestGrovePiDriver_UltrasonicRead(t *testing.T) {
|
|
|
|
g, a := initTestGrovePiDriver()
|
|
|
|
g.Start()
|
|
|
|
|
|
|
|
fakePin := byte(1)
|
|
|
|
fakeI2cResponse := []byte{CommandReadUltrasonic, 1, 2}
|
|
|
|
|
|
|
|
expectedCommand := []byte{CommandReadUltrasonic, fakePin, 0, 0}
|
|
|
|
expectedResult := 257
|
|
|
|
|
|
|
|
resultCommand := make([]byte, 3)
|
|
|
|
|
|
|
|
// capture i2c command
|
|
|
|
a.i2cWriteImpl = func(bytes []byte) (i int, e error) {
|
|
|
|
resultCommand = bytes
|
|
|
|
return len(bytes), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fake i2c response
|
|
|
|
a.i2cReadImpl = func(bytes []byte) (i int, e error) {
|
|
|
|
bytes[0] = fakeI2cResponse[0]
|
|
|
|
bytes[1] = fakeI2cResponse[1]
|
|
|
|
bytes[2] = fakeI2cResponse[2]
|
|
|
|
return len(bytes), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
result, _ := g.readUltrasonic(fakePin, 10)
|
|
|
|
|
|
|
|
gobottest.Assert(t, resultCommand, expectedCommand)
|
|
|
|
gobottest.Assert(t, result, expectedResult)
|
|
|
|
}
|
|
|
|
|
2018-08-14 10:24:07 +02:00
|
|
|
// Methods
|
|
|
|
func TestGrovePiDriverStart(t *testing.T) {
|
2019-11-26 23:45:00 +01:00
|
|
|
g, _ := initTestGrovePiDriver()
|
2018-08-14 10:24:07 +02:00
|
|
|
|
|
|
|
gobottest.Assert(t, g.Start(), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGrovePiDrivergetPin(t *testing.T) {
|
|
|
|
gobottest.Assert(t, getPin("a1"), "1")
|
|
|
|
gobottest.Assert(t, getPin("A16"), "16")
|
|
|
|
gobottest.Assert(t, getPin("D3"), "3")
|
|
|
|
gobottest.Assert(t, getPin("d22"), "22")
|
|
|
|
gobottest.Assert(t, getPin("22"), "22")
|
|
|
|
}
|