2017-02-09 18:23:36 +08:00
|
|
|
package i2c
|
|
|
|
|
|
|
|
import (
|
2017-04-04 17:50:31 +08:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
2017-02-09 18:23:36 +08:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/gobottest"
|
2017-02-09 18:23:36 +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
|
2017-02-09 18:23:36 +08:00
|
|
|
var _ gobot.Driver = (*MMA7660Driver)(nil)
|
|
|
|
|
|
|
|
func initTestMMA7660DriverWithStubbedAdaptor() (*MMA7660Driver, *i2cTestAdaptor) {
|
2022-12-10 20:10:23 +08:00
|
|
|
a := newI2cTestAdaptor()
|
|
|
|
d := NewMMA7660Driver(a)
|
|
|
|
if err := d.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return d, a
|
2017-02-09 18:23:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewMMA7660Driver(t *testing.T) {
|
2022-12-10 20:10:23 +08:00
|
|
|
var di interface{} = NewMMA7660Driver(newI2cTestAdaptor())
|
|
|
|
d, ok := di.(*MMA7660Driver)
|
2017-02-09 18:23:36 +08:00
|
|
|
if !ok {
|
|
|
|
t.Errorf("NewMMA7660Driver() should have returned a *MMA7660Driver")
|
|
|
|
}
|
2022-12-10 20:10:23 +08:00
|
|
|
gobottest.Refute(t, d.Driver, nil)
|
|
|
|
gobottest.Assert(t, strings.HasPrefix(d.Name(), "MMA7660"), true)
|
|
|
|
gobottest.Assert(t, d.defaultAddress, 0x4c)
|
2017-02-09 18:23:36 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func TestMMA7660Options(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)".
|
2017-02-14 23:51:55 +08:00
|
|
|
d := NewMMA7660Driver(newI2cTestAdaptor(), WithBus(2))
|
|
|
|
gobottest.Assert(t, d.GetBusOrDefault(1), 2)
|
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func TestMMA7660Start(t *testing.T) {
|
|
|
|
d := NewMMA7660Driver(newI2cTestAdaptor())
|
2017-02-14 23:51:55 +08:00
|
|
|
gobottest.Assert(t, d.Start(), nil)
|
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func TestMMA7660Halt(t *testing.T) {
|
|
|
|
d, _ := initTestMMA7660DriverWithStubbedAdaptor()
|
2017-02-14 23:51:55 +08:00
|
|
|
gobottest.Assert(t, d.Halt(), nil)
|
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func TestMMA7660Acceleration(t *testing.T) {
|
|
|
|
d, _ := initTestMMA7660DriverWithStubbedAdaptor()
|
2017-02-14 23:51:55 +08:00
|
|
|
x, y, z := d.Acceleration(21.0, 21.0, 21.0)
|
|
|
|
gobottest.Assert(t, x, 1.0)
|
|
|
|
gobottest.Assert(t, y, 1.0)
|
|
|
|
gobottest.Assert(t, z, 1.0)
|
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func TestMMA7660NullXYZ(t *testing.T) {
|
2017-02-14 23:51:55 +08:00
|
|
|
d, _ := initTestMMA7660DriverWithStubbedAdaptor()
|
2022-12-10 20:10:23 +08:00
|
|
|
|
2017-02-14 23:51:55 +08:00
|
|
|
x, y, z, _ := d.XYZ()
|
|
|
|
gobottest.Assert(t, x, 0.0)
|
|
|
|
gobottest.Assert(t, y, 0.0)
|
|
|
|
gobottest.Assert(t, z, 0.0)
|
|
|
|
}
|
2017-04-04 17:50:31 +08:00
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func TestMMA7660XYZ(t *testing.T) {
|
|
|
|
d, a := initTestMMA7660DriverWithStubbedAdaptor()
|
|
|
|
a.i2cReadImpl = func(b []byte) (int, error) {
|
2017-04-04 17:50:31 +08:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.Write([]byte{0x11, 0x12, 0x13})
|
|
|
|
copy(b, buf.Bytes())
|
|
|
|
return buf.Len(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
x, y, z, _ := d.XYZ()
|
|
|
|
gobottest.Assert(t, x, 17.0)
|
|
|
|
gobottest.Assert(t, y, 18.0)
|
|
|
|
gobottest.Assert(t, z, 19.0)
|
|
|
|
}
|
2017-04-04 18:34:42 +08:00
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func TestMMA7660XYZError(t *testing.T) {
|
|
|
|
d, a := initTestMMA7660DriverWithStubbedAdaptor()
|
|
|
|
a.i2cReadImpl = func(b []byte) (int, error) {
|
2017-04-04 18:34:42 +08:00
|
|
|
return 0, errors.New("read error")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, _, err := d.XYZ()
|
|
|
|
gobottest.Assert(t, err, errors.New("read error"))
|
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func TestMMA7660XYZNotReady(t *testing.T) {
|
|
|
|
d, a := initTestMMA7660DriverWithStubbedAdaptor()
|
|
|
|
a.i2cReadImpl = func(b []byte) (int, error) {
|
2017-04-04 18:34:42 +08:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.Write([]byte{0x40, 0x40, 0x40})
|
|
|
|
copy(b, buf.Bytes())
|
|
|
|
return buf.Len(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, _, err := d.XYZ()
|
|
|
|
gobottest.Assert(t, err, ErrNotReady)
|
|
|
|
}
|