2017-06-23 20:13:28 +02:00
|
|
|
package i2c
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2023-10-20 10:27:09 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-05-20 14:25:21 +02:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/drivers/gpio"
|
2017-06-23 20:13:28 +02:00
|
|
|
)
|
|
|
|
|
2022-12-10 13:10:23 +01: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-06-23 20:13:28 +02:00
|
|
|
var _ gobot.Driver = (*PCA9685Driver)(nil)
|
|
|
|
|
|
|
|
// and also the PwmWriter and ServoWriter interfaces
|
|
|
|
var _ gpio.PwmWriter = (*PCA9685Driver)(nil)
|
|
|
|
var _ gpio.ServoWriter = (*PCA9685Driver)(nil)
|
|
|
|
|
|
|
|
func initTestPCA9685DriverWithStubbedAdaptor() (*PCA9685Driver, *i2cTestAdaptor) {
|
2022-12-10 13:10:23 +01:00
|
|
|
a := newI2cTestAdaptor()
|
|
|
|
d := NewPCA9685Driver(a)
|
|
|
|
if err := d.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return d, a
|
2017-06-23 20:13:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewPCA9685Driver(t *testing.T) {
|
2022-12-10 13:10:23 +01:00
|
|
|
var di interface{} = NewPCA9685Driver(newI2cTestAdaptor())
|
|
|
|
d, ok := di.(*PCA9685Driver)
|
2017-06-23 20:13:28 +02:00
|
|
|
if !ok {
|
|
|
|
t.Errorf("NewPCA9685Driver() should have returned a *PCA9685Driver")
|
|
|
|
}
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.NotNil(t, d.Driver)
|
|
|
|
assert.True(t, strings.HasPrefix(d.Name(), "PCA9685"))
|
|
|
|
assert.Equal(t, 0x40, d.defaultAddress)
|
2017-06-23 20:13:28 +02:00
|
|
|
}
|
|
|
|
|
2022-12-10 13:10:23 +01:00
|
|
|
func TestPCA9685Options(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 := NewPCA9685Driver(newI2cTestAdaptor(), WithBus(2))
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Equal(t, 2, d.GetBusOrDefault(1))
|
2017-06-23 20:13:28 +02:00
|
|
|
}
|
|
|
|
|
2022-12-10 13:10:23 +01:00
|
|
|
func TestPCA9685Start(t *testing.T) {
|
|
|
|
a := newI2cTestAdaptor()
|
|
|
|
d := NewPCA9685Driver(a)
|
|
|
|
a.i2cReadImpl = func(b []byte) (int, error) {
|
2018-08-29 14:00:33 -06:00
|
|
|
copy(b, []byte{0x01})
|
|
|
|
return 1, nil
|
|
|
|
}
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Nil(t, d.Start())
|
2017-06-23 20:13:28 +02:00
|
|
|
}
|
|
|
|
|
2022-12-10 13:10:23 +01:00
|
|
|
func TestPCA9685Halt(t *testing.T) {
|
|
|
|
d, a := initTestPCA9685DriverWithStubbedAdaptor()
|
|
|
|
a.i2cReadImpl = func(b []byte) (int, error) {
|
2018-08-29 14:00:33 -06:00
|
|
|
copy(b, []byte{0x01})
|
|
|
|
return 1, nil
|
|
|
|
}
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Nil(t, d.Start())
|
|
|
|
assert.Nil(t, d.Halt())
|
2017-06-23 20:13:28 +02:00
|
|
|
}
|
|
|
|
|
2022-12-10 13:10:23 +01:00
|
|
|
func TestPCA9685SetPWM(t *testing.T) {
|
|
|
|
d, a := initTestPCA9685DriverWithStubbedAdaptor()
|
|
|
|
a.i2cReadImpl = func(b []byte) (int, error) {
|
2018-08-29 14:00:33 -06:00
|
|
|
copy(b, []byte{0x01})
|
|
|
|
return 1, nil
|
|
|
|
}
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Nil(t, d.Start())
|
|
|
|
assert.Nil(t, d.SetPWM(0, 0, 256))
|
2017-06-23 20:55:28 +02:00
|
|
|
}
|
|
|
|
|
2022-12-10 13:10:23 +01:00
|
|
|
func TestPCA9685SetPWMError(t *testing.T) {
|
|
|
|
d, a := initTestPCA9685DriverWithStubbedAdaptor()
|
|
|
|
a.i2cReadImpl = func(b []byte) (int, error) {
|
2018-08-29 14:00:33 -06:00
|
|
|
copy(b, []byte{0x01})
|
|
|
|
return 1, nil
|
|
|
|
}
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Nil(t, d.Start())
|
2022-12-10 13:10:23 +01:00
|
|
|
a.i2cWriteImpl = func([]byte) (int, error) {
|
2017-06-23 20:55:28 +02:00
|
|
|
return 0, errors.New("write error")
|
|
|
|
}
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Errorf(t, d.SetPWM(0, 0, 256), "write error")
|
2017-06-23 20:55:28 +02:00
|
|
|
}
|
|
|
|
|
2022-12-10 13:10:23 +01:00
|
|
|
func TestPCA9685SetPWMFreq(t *testing.T) {
|
|
|
|
d, a := initTestPCA9685DriverWithStubbedAdaptor()
|
|
|
|
a.i2cReadImpl = func(b []byte) (int, error) {
|
2018-08-29 14:00:33 -06:00
|
|
|
copy(b, []byte{0x01})
|
|
|
|
return 1, nil
|
|
|
|
}
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Nil(t, d.Start())
|
2017-06-23 20:55:28 +02:00
|
|
|
|
2022-12-10 13:10:23 +01:00
|
|
|
a.i2cReadImpl = func(b []byte) (int, error) {
|
2017-06-23 20:55:28 +02:00
|
|
|
copy(b, []byte{0x01})
|
|
|
|
return 1, nil
|
|
|
|
}
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Nil(t, d.SetPWMFreq(60))
|
2017-06-23 20:55:28 +02:00
|
|
|
}
|
|
|
|
|
2022-12-10 13:10:23 +01:00
|
|
|
func TestPCA9685SetPWMFreqReadError(t *testing.T) {
|
|
|
|
d, a := initTestPCA9685DriverWithStubbedAdaptor()
|
|
|
|
a.i2cReadImpl = func(b []byte) (int, error) {
|
2018-08-29 14:00:33 -06:00
|
|
|
copy(b, []byte{0x01})
|
|
|
|
return 1, nil
|
|
|
|
}
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Nil(t, d.Start())
|
2017-06-23 20:55:28 +02:00
|
|
|
|
2022-12-10 13:10:23 +01:00
|
|
|
a.i2cReadImpl = func(b []byte) (int, error) {
|
2017-06-23 20:55:28 +02:00
|
|
|
return 0, errors.New("read error")
|
|
|
|
}
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Errorf(t, d.SetPWMFreq(60), "read error")
|
2017-06-23 20:55:28 +02:00
|
|
|
}
|
|
|
|
|
2022-12-10 13:10:23 +01:00
|
|
|
func TestPCA9685SetPWMFreqWriteError(t *testing.T) {
|
|
|
|
d, a := initTestPCA9685DriverWithStubbedAdaptor()
|
|
|
|
a.i2cReadImpl = func(b []byte) (int, error) {
|
2018-08-29 14:00:33 -06:00
|
|
|
copy(b, []byte{0x01})
|
|
|
|
return 1, nil
|
|
|
|
}
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Nil(t, d.Start())
|
2017-06-23 20:55:28 +02:00
|
|
|
|
2022-12-10 13:10:23 +01:00
|
|
|
a.i2cWriteImpl = func([]byte) (int, error) {
|
2017-06-23 20:55:28 +02:00
|
|
|
return 0, errors.New("write error")
|
|
|
|
}
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Errorf(t, d.SetPWMFreq(60), "write error")
|
2017-06-23 20:13:28 +02:00
|
|
|
}
|
2018-08-15 12:05:10 +02:00
|
|
|
|
2022-12-10 13:10:23 +01:00
|
|
|
func TestPCA9685Commands(t *testing.T) {
|
|
|
|
d, a := initTestPCA9685DriverWithStubbedAdaptor()
|
|
|
|
a.i2cReadImpl = func(b []byte) (int, error) {
|
2018-08-29 14:00:33 -06:00
|
|
|
copy(b, []byte{0x01})
|
|
|
|
return 1, nil
|
|
|
|
}
|
2023-06-12 19:51:25 +02:00
|
|
|
_ = d.Start()
|
2018-08-15 12:05:10 +02:00
|
|
|
|
2022-12-10 13:10:23 +01:00
|
|
|
err := d.Command("PwmWrite")(map[string]interface{}{"pin": "1", "val": "1"})
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Nil(t, err)
|
2018-08-15 12:05:10 +02:00
|
|
|
|
2022-12-10 13:10:23 +01:00
|
|
|
err = d.Command("ServoWrite")(map[string]interface{}{"pin": "1", "val": "1"})
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Nil(t, err)
|
2018-08-15 12:05:10 +02:00
|
|
|
|
2022-12-10 13:10:23 +01:00
|
|
|
err = d.Command("SetPWM")(map[string]interface{}{"channel": "1", "on": "0", "off": "1024"})
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Nil(t, err)
|
2018-08-15 12:05:10 +02:00
|
|
|
|
2022-12-10 13:10:23 +01:00
|
|
|
err = d.Command("SetPWMFreq")(map[string]interface{}{"freq": "60"})
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Nil(t, err)
|
2018-08-15 12:05:10 +02:00
|
|
|
}
|