2022-03-25 01:40:26 +08:00
|
|
|
package i2c
|
|
|
|
|
|
|
|
import (
|
2022-12-10 20:10:23 +08:00
|
|
|
"strings"
|
2022-03-25 01:40:26 +08:00
|
|
|
"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"
|
2022-03-25 01:40:26 +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
|
|
|
|
var _ gobot.Driver = (*PCF8591Driver)(nil)
|
|
|
|
|
2022-03-25 01:40:26 +08:00
|
|
|
func initTestPCF8591DriverWithStubbedAdaptor() (*PCF8591Driver, *i2cTestAdaptor) {
|
2022-12-10 20:10:23 +08:00
|
|
|
a := newI2cTestAdaptor()
|
|
|
|
d := NewPCF8591Driver(a, WithPCF8591With400kbitStabilization(0, 2))
|
|
|
|
d.lastCtrlByte = 0xFF // prevent skipping of write
|
|
|
|
if err := d.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return d, a
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewPCF8591Driver(t *testing.T) {
|
|
|
|
var di interface{} = NewPCF8591Driver(newI2cTestAdaptor())
|
|
|
|
d, ok := di.(*PCF8591Driver)
|
|
|
|
if !ok {
|
2024-02-13 17:33:46 +08:00
|
|
|
require.Fail(t, "NewPCF8591Driver() should have returned a *PCF8591Driver")
|
2022-12-10 20:10:23 +08:00
|
|
|
}
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.NotNil(t, d.Driver)
|
|
|
|
assert.True(t, strings.HasPrefix(d.Name(), "PCF8591"))
|
|
|
|
assert.Equal(t, 0x48, d.defaultAddress)
|
2022-12-10 20:10:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPCF8591Start(t *testing.T) {
|
|
|
|
d := NewPCF8591Driver(newI2cTestAdaptor())
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, d.Start())
|
2022-12-10 20:10:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPCF8591Halt(t *testing.T) {
|
|
|
|
d := NewPCF8591Driver(newI2cTestAdaptor())
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, d.Halt())
|
2022-03-25 01:40:26 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func TestPCF8591WithPCF8591With400kbitStabilization(t *testing.T) {
|
|
|
|
d := NewPCF8591Driver(newI2cTestAdaptor(), WithPCF8591With400kbitStabilization(5, 6))
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, uint8(5), d.additionalReadWrite)
|
|
|
|
assert.Equal(t, uint8(6), d.additionalRead)
|
2022-03-25 01:40:26 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func TestPCF8591AnalogReadSingle(t *testing.T) {
|
2022-03-25 01:40:26 +08:00
|
|
|
// sequence to read the input channel:
|
|
|
|
// * prepare value (with channel and mode) and write control register
|
|
|
|
// * read 3 values to drop (see description in implementation)
|
|
|
|
// * read the analog value
|
|
|
|
//
|
|
|
|
// arrange
|
2022-12-10 20:10:23 +08:00
|
|
|
d, a := initTestPCF8591DriverWithStubbedAdaptor()
|
|
|
|
a.written = []byte{} // reset writes of Start() and former test
|
2022-03-25 01:40:26 +08:00
|
|
|
description := "s.1"
|
2022-12-10 20:10:23 +08:00
|
|
|
d.lastCtrlByte = 0x00
|
2022-03-25 01:40:26 +08:00
|
|
|
ctrlByteOn := uint8(pcf8591_ALLSINGLE) | uint8(pcf8591_CHAN1)
|
|
|
|
returnRead := []uint8{0x01, 0x02, 0x03, 0xFF}
|
|
|
|
want := int(returnRead[3])
|
|
|
|
// arrange reads
|
|
|
|
numCallsRead := 0
|
2022-12-10 20:10:23 +08:00
|
|
|
a.i2cReadImpl = func(b []byte) (int, error) {
|
2022-03-25 01:40:26 +08:00
|
|
|
numCallsRead++
|
|
|
|
if numCallsRead == 1 {
|
|
|
|
b = returnRead[0:len(b)]
|
|
|
|
}
|
|
|
|
if numCallsRead == 2 {
|
|
|
|
b[0] = returnRead[len(returnRead)-1]
|
|
|
|
}
|
|
|
|
return len(b), nil
|
|
|
|
}
|
|
|
|
// act
|
2022-12-10 20:10:23 +08:00
|
|
|
got, err := d.AnalogRead(description)
|
2022-03-25 01:40:26 +08:00
|
|
|
// assert
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, a.written, 1)
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, ctrlByteOn, a.written[0])
|
|
|
|
assert.Equal(t, 2, numCallsRead)
|
|
|
|
assert.Equal(t, want, got)
|
2022-03-25 01:40:26 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func TestPCF8591AnalogReadDiff(t *testing.T) {
|
2022-03-25 01:40:26 +08:00
|
|
|
// sequence to read the input channel:
|
|
|
|
// * prepare value (with channel and mode) and write control register
|
|
|
|
// * read 3 values to drop (see description in implementation)
|
|
|
|
// * read the analog value
|
|
|
|
// * convert to 8-bit two's complement (-127...128)
|
|
|
|
//
|
|
|
|
// arrange
|
2022-12-10 20:10:23 +08:00
|
|
|
d, a := initTestPCF8591DriverWithStubbedAdaptor()
|
|
|
|
a.written = []byte{} // reset writes of Start() and former test
|
2022-03-25 01:40:26 +08:00
|
|
|
description := "m.2-3"
|
2022-12-10 20:10:23 +08:00
|
|
|
d.lastCtrlByte = 0x00
|
2022-03-25 01:40:26 +08:00
|
|
|
ctrlByteOn := uint8(pcf8591_MIXED) | uint8(pcf8591_CHAN2)
|
|
|
|
// some two' complements
|
|
|
|
// 0x80 => -128
|
|
|
|
// 0xFF => -1
|
|
|
|
// 0x00 => 0
|
|
|
|
// 0x7F => 127
|
|
|
|
returnRead := []uint8{0x01, 0x02, 0x03, 0xFF}
|
|
|
|
want := -1
|
|
|
|
// arrange reads
|
|
|
|
numCallsRead := 0
|
2022-12-10 20:10:23 +08:00
|
|
|
a.i2cReadImpl = func(b []byte) (int, error) {
|
2022-03-25 01:40:26 +08:00
|
|
|
numCallsRead++
|
|
|
|
if numCallsRead == 1 {
|
|
|
|
b = returnRead[0:len(b)]
|
|
|
|
}
|
|
|
|
if numCallsRead == 2 {
|
|
|
|
b[0] = returnRead[len(returnRead)-1]
|
|
|
|
}
|
|
|
|
return len(b), nil
|
|
|
|
}
|
|
|
|
// act
|
2022-12-10 20:10:23 +08:00
|
|
|
got, err := d.AnalogRead(description)
|
2022-03-25 01:40:26 +08:00
|
|
|
// assert
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, a.written, 1)
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, ctrlByteOn, a.written[0])
|
|
|
|
assert.Equal(t, 2, numCallsRead)
|
|
|
|
assert.Equal(t, want, got)
|
2022-03-25 01:40:26 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func TestPCF8591AnalogWrite(t *testing.T) {
|
2022-03-25 01:40:26 +08:00
|
|
|
// sequence to write the output:
|
|
|
|
// * create new value for the control register (ANAON)
|
|
|
|
// * write the control register and value
|
|
|
|
//
|
|
|
|
// arrange
|
2022-12-10 20:10:23 +08:00
|
|
|
d, a := initTestPCF8591DriverWithStubbedAdaptor()
|
|
|
|
a.written = []byte{} // reset writes of Start() and former test
|
|
|
|
d.lastCtrlByte = 0x00
|
|
|
|
d.lastAnaOut = 0x00
|
2022-03-25 01:40:26 +08:00
|
|
|
ctrlByteOn := uint8(pcf8591_ANAON)
|
|
|
|
want := uint8(0x15)
|
|
|
|
// arrange writes
|
2022-12-10 20:10:23 +08:00
|
|
|
a.i2cWriteImpl = func(b []byte) (int, error) {
|
2022-03-25 01:40:26 +08:00
|
|
|
return len(b), nil
|
|
|
|
}
|
|
|
|
// act
|
2022-12-10 20:10:23 +08:00
|
|
|
err := d.AnalogWrite("", int(want))
|
2022-03-25 01:40:26 +08:00
|
|
|
// assert
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, a.written, 2)
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, ctrlByteOn, a.written[0])
|
|
|
|
assert.Equal(t, want, a.written[1])
|
2022-03-25 01:40:26 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func TestPCF8591AnalogOutputState(t *testing.T) {
|
2022-03-25 01:40:26 +08:00
|
|
|
// sequence to set the state:
|
|
|
|
// * create the new value (ctrlByte) for the control register (ANAON)
|
|
|
|
// * write the register value
|
|
|
|
//
|
|
|
|
// arrange
|
2022-12-10 20:10:23 +08:00
|
|
|
d, a := initTestPCF8591DriverWithStubbedAdaptor()
|
2022-03-25 01:40:26 +08:00
|
|
|
for bitState := 0; bitState <= 1; bitState++ {
|
2022-12-10 20:10:23 +08:00
|
|
|
a.written = []byte{} // reset writes of Start() and former test
|
2022-03-25 01:40:26 +08:00
|
|
|
// arrange some values
|
2022-12-10 20:10:23 +08:00
|
|
|
d.lastCtrlByte = uint8(0x00)
|
2022-03-25 01:40:26 +08:00
|
|
|
wantCtrlByteVal := uint8(pcf8591_ANAON)
|
|
|
|
if bitState == 0 {
|
2022-12-10 20:10:23 +08:00
|
|
|
d.lastCtrlByte = uint8(0xFF)
|
2022-03-25 01:40:26 +08:00
|
|
|
wantCtrlByteVal = uint8(0xFF & ^pcf8591_ANAON)
|
|
|
|
}
|
|
|
|
// act
|
2022-12-10 20:10:23 +08:00
|
|
|
err := d.AnalogOutputState(bitState == 1)
|
2022-03-25 01:40:26 +08:00
|
|
|
// assert
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, a.written, 1)
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, wantCtrlByteVal, a.written[0])
|
2022-03-25 01:40:26 +08:00
|
|
|
}
|
|
|
|
}
|