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-10-20 16:27:09 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-11-12 21:17:02 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-11-16 03:51:52 +08:00
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
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 {
|
2024-02-13 17:33:46 +08:00
|
|
|
require.Fail(t, "NewMMA7660Driver() should have returned a *MMA7660Driver")
|
2017-02-09 18:23:36 +08:00
|
|
|
}
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.NotNil(t, d.Driver)
|
|
|
|
assert.True(t, strings.HasPrefix(d.Name(), "MMA7660"))
|
|
|
|
assert.Equal(t, 0x4c, d.defaultAddress)
|
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))
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, 2, d.GetBusOrDefault(1))
|
2017-02-14 23:51:55 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func TestMMA7660Start(t *testing.T) {
|
|
|
|
d := NewMMA7660Driver(newI2cTestAdaptor())
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, d.Start())
|
2017-02-14 23:51:55 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func TestMMA7660Halt(t *testing.T) {
|
|
|
|
d, _ := initTestMMA7660DriverWithStubbedAdaptor()
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, d.Halt())
|
2017-02-14 23:51:55 +08:00
|
|
|
}
|
|
|
|
|
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)
|
2023-11-12 21:17:02 +08:00
|
|
|
assert.InDelta(t, 1.0, x, 0.0)
|
|
|
|
assert.InDelta(t, 1.0, y, 0.0)
|
|
|
|
assert.InDelta(t, 1.0, z, 0.0)
|
2017-02-14 23:51:55 +08:00
|
|
|
}
|
|
|
|
|
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()
|
2023-11-12 21:17:02 +08:00
|
|
|
assert.InDelta(t, 0.0, x, 0.0)
|
|
|
|
assert.InDelta(t, 0.0, y, 0.0)
|
|
|
|
assert.InDelta(t, 0.0, z, 0.0)
|
2017-02-14 23:51:55 +08:00
|
|
|
}
|
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()
|
2023-11-12 21:17:02 +08:00
|
|
|
assert.InDelta(t, 17.0, x, 0.0)
|
|
|
|
assert.InDelta(t, 18.0, y, 0.0)
|
|
|
|
assert.InDelta(t, 19.0, z, 0.0)
|
2017-04-04 17:50:31 +08:00
|
|
|
}
|
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")
|
|
|
|
}
|
|
|
|
|
2023-11-16 03:51:52 +08:00
|
|
|
x, y, z, err := d.XYZ()
|
2023-11-12 21:17:02 +08:00
|
|
|
require.ErrorContains(t, err, "read error")
|
2023-11-16 03:51:52 +08:00
|
|
|
assert.InDelta(t, 0.0, x, 0.0)
|
|
|
|
assert.InDelta(t, 0.0, y, 0.0)
|
|
|
|
assert.InDelta(t, 0.0, z, 0.0)
|
2017-04-04 18:34:42 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-11-16 03:51:52 +08:00
|
|
|
x, y, z, err := d.XYZ()
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, ErrNotReady, err)
|
2023-11-16 03:51:52 +08:00
|
|
|
assert.InDelta(t, 0.0, x, 0.0)
|
|
|
|
assert.InDelta(t, 0.0, y, 0.0)
|
|
|
|
assert.InDelta(t, 0.0, z, 0.0)
|
2017-04-04 18:34:42 +08:00
|
|
|
}
|