2015-02-18 04:11:02 +08:00
|
|
|
package i2c
|
|
|
|
|
|
|
|
import (
|
2017-04-04 20:34:06 +08:00
|
|
|
"bytes"
|
2015-02-18 04:11:02 +08:00
|
|
|
"errors"
|
2022-12-10 20:10:23 +08:00
|
|
|
"strings"
|
2015-02-18 04:11:02 +08:00
|
|
|
"testing"
|
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/gobottest"
|
2015-02-18 04:11:02 +08:00
|
|
|
)
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
// this ensures that the implementation is based on i2c.Driver, which implements the gobot.Driver
|
|
|
|
// and tests all implementations, so no further tests needed here for gobot.Driver interface
|
2016-12-21 01:59:26 +08:00
|
|
|
var _ gobot.Driver = (*LIDARLiteDriver)(nil)
|
|
|
|
|
2015-02-18 04:11:02 +08:00
|
|
|
func initTestLIDARLiteDriver() (driver *LIDARLiteDriver) {
|
|
|
|
driver, _ = initTestLIDARLiteDriverWithStubbedAdaptor()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func initTestLIDARLiteDriverWithStubbedAdaptor() (*LIDARLiteDriver, *i2cTestAdaptor) {
|
2022-12-10 20:10:23 +08:00
|
|
|
a := newI2cTestAdaptor()
|
|
|
|
d := NewLIDARLiteDriver(a)
|
|
|
|
if err := d.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return d, a
|
2015-02-18 04:11:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewLIDARLiteDriver(t *testing.T) {
|
2022-12-10 20:10:23 +08:00
|
|
|
var di interface{} = NewLIDARLiteDriver(newI2cTestAdaptor())
|
|
|
|
d, ok := di.(*LIDARLiteDriver)
|
2015-02-18 04:11:02 +08:00
|
|
|
if !ok {
|
|
|
|
t.Errorf("NewLIDARLiteDriver() should have returned a *LIDARLiteDriver")
|
|
|
|
}
|
2022-12-10 20:10:23 +08:00
|
|
|
gobottest.Refute(t, d.Driver, nil)
|
|
|
|
gobottest.Assert(t, strings.HasPrefix(d.Name(), "LIDARLite"), true)
|
|
|
|
gobottest.Assert(t, d.defaultAddress, 0x62)
|
2015-02-18 04:11:02 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func TestLIDARLiteDriverOptions(t *testing.T) {
|
|
|
|
// This is a general test, that options are applied in constructor by using the common WithBus() option and
|
|
|
|
// least one of this driver. Further tests for options can also be done by call of "WithOption(val)(d)".
|
|
|
|
d := NewLIDARLiteDriver(newI2cTestAdaptor(), WithBus(2))
|
|
|
|
gobottest.Assert(t, d.GetBusOrDefault(1), 2)
|
2015-02-18 04:11:02 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func TestLIDARLiteDriverStart(t *testing.T) {
|
|
|
|
d := NewLIDARLiteDriver(newI2cTestAdaptor())
|
|
|
|
gobottest.Assert(t, d.Start(), nil)
|
2017-04-12 17:47:42 +08:00
|
|
|
}
|
|
|
|
|
2015-02-18 04:11:02 +08:00
|
|
|
func TestLIDARLiteDriverHalt(t *testing.T) {
|
2022-12-10 20:10:23 +08:00
|
|
|
d := initTestLIDARLiteDriver()
|
|
|
|
gobottest.Assert(t, d.Halt(), nil)
|
2015-02-18 04:11:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLIDARLiteDriverDistance(t *testing.T) {
|
2015-07-13 06:22:23 +08:00
|
|
|
// when everything is happy
|
2022-12-10 20:10:23 +08:00
|
|
|
d, a := initTestLIDARLiteDriverWithStubbedAdaptor()
|
2015-07-13 06:22:23 +08:00
|
|
|
first := true
|
2022-12-10 20:10:23 +08:00
|
|
|
a.i2cReadImpl = func(b []byte) (int, error) {
|
2015-07-13 06:22:23 +08:00
|
|
|
if first {
|
|
|
|
first = false
|
2017-02-06 07:19:42 +08:00
|
|
|
copy(b, []byte{99})
|
|
|
|
return 1, nil
|
2015-07-13 06:22:23 +08:00
|
|
|
}
|
2017-02-06 07:19:42 +08:00
|
|
|
copy(b, []byte{1})
|
|
|
|
return 1, nil
|
2015-02-18 04:11:02 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
distance, err := d.Distance()
|
2015-07-13 06:22:23 +08:00
|
|
|
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
gobottest.Assert(t, distance, int(25345))
|
2015-02-18 04:11:02 +08:00
|
|
|
|
2015-07-13 06:22:23 +08:00
|
|
|
// when insufficient bytes have been read
|
2022-12-10 20:10:23 +08:00
|
|
|
d, a = initTestLIDARLiteDriverWithStubbedAdaptor()
|
|
|
|
a.i2cReadImpl = func([]byte) (int, error) {
|
2017-02-06 07:19:42 +08:00
|
|
|
return 0, nil
|
2015-02-18 04:11:02 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
distance, err = d.Distance()
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, distance, int(0))
|
|
|
|
gobottest.Assert(t, err, ErrNotEnoughBytes)
|
2015-02-18 04:11:02 +08:00
|
|
|
|
|
|
|
// when read error
|
2022-12-10 20:10:23 +08:00
|
|
|
d, a = initTestLIDARLiteDriverWithStubbedAdaptor()
|
|
|
|
a.i2cReadImpl = func([]byte) (int, error) {
|
2017-02-06 07:19:42 +08:00
|
|
|
return 0, errors.New("read error")
|
2015-02-18 04:11:02 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
distance, err = d.Distance()
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, distance, int(0))
|
|
|
|
gobottest.Assert(t, err, errors.New("read error"))
|
2017-04-04 20:34:06 +08:00
|
|
|
}
|
2015-02-18 04:11:02 +08:00
|
|
|
|
2017-04-04 20:34:06 +08:00
|
|
|
func TestLIDARLiteDriverDistanceError1(t *testing.T) {
|
2022-12-10 20:10:23 +08:00
|
|
|
d, a := initTestLIDARLiteDriverWithStubbedAdaptor()
|
|
|
|
a.i2cWriteImpl = func([]byte) (int, error) {
|
2017-02-06 07:19:42 +08:00
|
|
|
return 0, errors.New("write error")
|
2015-02-18 04:11:02 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
distance, err := d.Distance()
|
2017-04-04 20:34:06 +08:00
|
|
|
gobottest.Assert(t, distance, int(0))
|
|
|
|
gobottest.Assert(t, err, errors.New("write error"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLIDARLiteDriverDistanceError2(t *testing.T) {
|
2022-12-10 20:10:23 +08:00
|
|
|
d, a := initTestLIDARLiteDriverWithStubbedAdaptor()
|
|
|
|
a.i2cWriteImpl = func(b []byte) (int, error) {
|
2017-04-04 20:34:06 +08:00
|
|
|
if b[0] == 0x0f {
|
|
|
|
return 0, errors.New("write error")
|
|
|
|
}
|
|
|
|
return len(b), nil
|
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
distance, err := d.Distance()
|
2017-04-04 20:34:06 +08:00
|
|
|
gobottest.Assert(t, distance, int(0))
|
|
|
|
gobottest.Assert(t, err, errors.New("write error"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLIDARLiteDriverDistanceError3(t *testing.T) {
|
2022-12-10 20:10:23 +08:00
|
|
|
d, a := initTestLIDARLiteDriverWithStubbedAdaptor()
|
|
|
|
a.i2cWriteImpl = func(b []byte) (int, error) {
|
2017-04-04 20:34:06 +08:00
|
|
|
if b[0] == 0x10 {
|
|
|
|
return 0, errors.New("write error")
|
|
|
|
}
|
|
|
|
return len(b), nil
|
|
|
|
}
|
2022-12-10 20:10:23 +08:00
|
|
|
a.i2cReadImpl = func(b []byte) (int, error) {
|
2017-04-04 20:34:06 +08:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.Write([]byte{0x03})
|
|
|
|
copy(b, buf.Bytes())
|
|
|
|
return buf.Len(), nil
|
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
distance, err := d.Distance()
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, distance, int(0))
|
|
|
|
gobottest.Assert(t, err, errors.New("write error"))
|
2015-02-18 04:11:02 +08:00
|
|
|
}
|