2020-03-17 09:08:06 +08:00
|
|
|
package i2c
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-12-10 20:10:23 +08:00
|
|
|
"strings"
|
2020-03-17 09:08:06 +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"
|
2020-03-17 09:08:06 +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
|
2020-03-17 09:08:06 +08:00
|
|
|
var _ gobot.Driver = (*SHT2xDriver)(nil)
|
|
|
|
|
|
|
|
func initTestSHT2xDriverWithStubbedAdaptor() (*SHT2xDriver, *i2cTestAdaptor) {
|
2022-12-10 20:10:23 +08:00
|
|
|
a := newI2cTestAdaptor()
|
|
|
|
d := NewSHT2xDriver(a)
|
|
|
|
if err := d.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return d, a
|
2020-03-17 09:08:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewSHT2xDriver(t *testing.T) {
|
2022-12-10 20:10:23 +08:00
|
|
|
var di interface{} = NewSHT2xDriver(newI2cTestAdaptor())
|
|
|
|
d, ok := di.(*SHT2xDriver)
|
2020-03-17 09:08:06 +08:00
|
|
|
if !ok {
|
|
|
|
t.Errorf("NewSHT2xDriver() should have returned a *SHT2xDriver")
|
|
|
|
}
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.NotNil(t, d.Driver)
|
|
|
|
assert.True(t, strings.HasPrefix(d.Name(), "SHT2x"))
|
|
|
|
assert.Equal(t, 0x40, d.defaultAddress)
|
2020-03-17 09:08:06 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func TestSHT2xOptions(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)".
|
|
|
|
b := NewSHT2xDriver(newI2cTestAdaptor(), WithBus(2))
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, 2, b.GetBusOrDefault(1))
|
2020-03-17 09:08:06 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func TestSHT2xStart(t *testing.T) {
|
|
|
|
d := NewSHT2xDriver(newI2cTestAdaptor())
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, d.Start())
|
2020-03-17 09:08:06 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func TestSHT2xHalt(t *testing.T) {
|
|
|
|
d, _ := initTestSHT2xDriverWithStubbedAdaptor()
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, d.Halt())
|
2020-03-17 09:08:06 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func TestSHT2xReset(t *testing.T) {
|
|
|
|
d, a := initTestSHT2xDriverWithStubbedAdaptor()
|
|
|
|
a.i2cReadImpl = func(b []byte) (int, error) {
|
2020-03-17 09:08:06 +08:00
|
|
|
return 0, nil
|
|
|
|
}
|
2023-06-13 01:51:25 +08:00
|
|
|
_ = d.Start()
|
2022-12-10 20:10:23 +08:00
|
|
|
err := d.Reset()
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, err)
|
2020-03-17 09:08:06 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func TestSHT2xMeasurements(t *testing.T) {
|
|
|
|
d, a := initTestSHT2xDriverWithStubbedAdaptor()
|
|
|
|
a.i2cReadImpl = func(b []byte) (int, error) {
|
2020-03-17 09:08:06 +08:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
// Values produced by dumping data from actual sensor
|
2022-12-10 20:10:23 +08:00
|
|
|
if a.written[len(a.written)-1] == SHT2xTriggerTempMeasureNohold {
|
2020-03-17 09:08:06 +08:00
|
|
|
buf.Write([]byte{95, 168, 59})
|
2022-12-10 20:10:23 +08:00
|
|
|
} else if a.written[len(a.written)-1] == SHT2xTriggerHumdMeasureNohold {
|
2020-03-17 09:08:06 +08:00
|
|
|
buf.Write([]byte{94, 202, 22})
|
|
|
|
}
|
|
|
|
copy(b, buf.Bytes())
|
|
|
|
return buf.Len(), nil
|
|
|
|
}
|
2023-06-13 01:51:25 +08:00
|
|
|
_ = d.Start()
|
2022-12-10 20:10:23 +08:00
|
|
|
temp, err := d.Temperature()
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.InDelta(t, float32(18.809052), temp, 0.0)
|
2022-12-10 20:10:23 +08:00
|
|
|
hum, err := d.Humidity()
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.InDelta(t, float32(40.279907), hum, 0.0)
|
2020-03-17 09:08:06 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func TestSHT2xAccuracy(t *testing.T) {
|
|
|
|
d, a := initTestSHT2xDriverWithStubbedAdaptor()
|
|
|
|
a.i2cReadImpl = func(b []byte) (int, error) {
|
2020-03-17 09:08:06 +08:00
|
|
|
buf := new(bytes.Buffer)
|
2023-11-16 03:51:52 +08:00
|
|
|
switch {
|
|
|
|
case a.written[len(a.written)-1] == SHT2xReadUserReg:
|
2020-03-17 09:08:06 +08:00
|
|
|
buf.Write([]byte{0x3a})
|
2023-11-16 03:51:52 +08:00
|
|
|
case a.written[len(a.written)-2] == SHT2xWriteUserReg:
|
2022-12-10 20:10:23 +08:00
|
|
|
buf.Write([]byte{a.written[len(a.written)-1]})
|
2023-11-16 03:51:52 +08:00
|
|
|
default:
|
2020-03-17 09:08:06 +08:00
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
copy(b, buf.Bytes())
|
|
|
|
return buf.Len(), nil
|
|
|
|
}
|
2023-06-13 01:51:25 +08:00
|
|
|
_ = d.Start()
|
|
|
|
_ = d.SetAccuracy(SHT2xAccuracyLow)
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, SHT2xAccuracyLow, d.Accuracy())
|
2022-12-10 20:10:23 +08:00
|
|
|
err := d.sendAccuracy()
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, err)
|
2020-03-17 09:08:06 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func TestSHT2xTemperatureCrcError(t *testing.T) {
|
|
|
|
d, a := initTestSHT2xDriverWithStubbedAdaptor()
|
2023-06-13 01:51:25 +08:00
|
|
|
_ = d.Start()
|
2020-03-17 09:08:06 +08:00
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
a.i2cReadImpl = func(b []byte) (int, error) {
|
2020-03-17 09:08:06 +08:00
|
|
|
buf := new(bytes.Buffer)
|
2022-12-10 20:10:23 +08:00
|
|
|
if a.written[len(a.written)-1] == SHT2xTriggerTempMeasureNohold {
|
2020-03-17 09:08:06 +08:00
|
|
|
buf.Write([]byte{95, 168, 0})
|
|
|
|
}
|
|
|
|
copy(b, buf.Bytes())
|
|
|
|
return buf.Len(), nil
|
|
|
|
}
|
2022-12-10 20:10:23 +08:00
|
|
|
temp, err := d.Temperature()
|
2023-11-12 21:17:02 +08:00
|
|
|
require.ErrorContains(t, err, "Invalid crc")
|
|
|
|
assert.InDelta(t, float32(0.0), temp, 0.0)
|
2020-03-17 09:08:06 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func TestSHT2xHumidityCrcError(t *testing.T) {
|
|
|
|
d, a := initTestSHT2xDriverWithStubbedAdaptor()
|
2023-06-13 01:51:25 +08:00
|
|
|
_ = d.Start()
|
2020-03-17 09:08:06 +08:00
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
a.i2cReadImpl = func(b []byte) (int, error) {
|
2020-03-17 09:08:06 +08:00
|
|
|
buf := new(bytes.Buffer)
|
2022-12-10 20:10:23 +08:00
|
|
|
if a.written[len(a.written)-1] == SHT2xTriggerHumdMeasureNohold {
|
2020-03-17 09:08:06 +08:00
|
|
|
buf.Write([]byte{94, 202, 0})
|
|
|
|
}
|
|
|
|
copy(b, buf.Bytes())
|
|
|
|
return buf.Len(), nil
|
|
|
|
}
|
2022-12-10 20:10:23 +08:00
|
|
|
hum, err := d.Humidity()
|
2023-11-12 21:17:02 +08:00
|
|
|
require.ErrorContains(t, err, "Invalid crc")
|
|
|
|
assert.InDelta(t, float32(0.0), hum, 0.0)
|
2020-03-17 09:08:06 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func TestSHT2xTemperatureLengthError(t *testing.T) {
|
|
|
|
d, a := initTestSHT2xDriverWithStubbedAdaptor()
|
2023-06-13 01:51:25 +08:00
|
|
|
_ = d.Start()
|
2020-03-17 09:08:06 +08:00
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
a.i2cReadImpl = func(b []byte) (int, error) {
|
2020-03-17 09:08:06 +08:00
|
|
|
buf := new(bytes.Buffer)
|
2022-12-10 20:10:23 +08:00
|
|
|
if a.written[len(a.written)-1] == SHT2xTriggerTempMeasureNohold {
|
2020-03-17 09:08:06 +08:00
|
|
|
buf.Write([]byte{95, 168})
|
|
|
|
}
|
|
|
|
copy(b, buf.Bytes())
|
|
|
|
return buf.Len(), nil
|
|
|
|
}
|
2022-12-10 20:10:23 +08:00
|
|
|
temp, err := d.Temperature()
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, ErrNotEnoughBytes, err)
|
2023-11-12 21:17:02 +08:00
|
|
|
assert.InDelta(t, float32(0.0), temp, 0.0)
|
2020-03-17 09:08:06 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func TestSHT2xHumidityLengthError(t *testing.T) {
|
|
|
|
d, a := initTestSHT2xDriverWithStubbedAdaptor()
|
2023-06-13 01:51:25 +08:00
|
|
|
_ = d.Start()
|
2020-03-17 09:08:06 +08:00
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
a.i2cReadImpl = func(b []byte) (int, error) {
|
2020-03-17 09:08:06 +08:00
|
|
|
buf := new(bytes.Buffer)
|
2022-12-10 20:10:23 +08:00
|
|
|
if a.written[len(a.written)-1] == SHT2xTriggerHumdMeasureNohold {
|
2020-03-17 09:08:06 +08:00
|
|
|
buf.Write([]byte{94, 202})
|
|
|
|
}
|
|
|
|
copy(b, buf.Bytes())
|
|
|
|
return buf.Len(), nil
|
|
|
|
}
|
2022-12-10 20:10:23 +08:00
|
|
|
hum, err := d.Humidity()
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, ErrNotEnoughBytes, err)
|
2023-11-12 21:17:02 +08:00
|
|
|
assert.InDelta(t, float32(0.0), hum, 0.0)
|
2020-03-17 09:08:06 +08:00
|
|
|
}
|