-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathbmp388_driver_test.go
206 lines (187 loc) · 6.66 KB
/
bmp388_driver_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package i2c
import (
"bytes"
"encoding/binary"
"errors"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gobot.io/x/gobot/v2"
)
// 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 = (*BMP388Driver)(nil)
func initTestBMP388WithStubbedAdaptor() (*BMP388Driver, *i2cTestAdaptor) {
a := newI2cTestAdaptor()
readCallCounter := 0
a.i2cReadImpl = func(b []byte) (int, error) {
readCallCounter++
if readCallCounter == 1 {
buf := new(bytes.Buffer)
// Simulate returning of 0x50 for the
// ReadByteData(bmp388RegChipID) call in initialisation()
_ = binary.Write(buf, binary.LittleEndian, uint8(0x50))
copy(b, buf.Bytes())
return buf.Len(), nil
}
if readCallCounter == 2 {
// Simulate returning 24 bytes for the coefficients (register bmp388RegCalib00)
return 24, nil
}
return 0, nil
}
return NewBMP388Driver(a), a
}
func TestNewBMP388Driver(t *testing.T) {
var di interface{} = NewBMP388Driver(newI2cTestAdaptor())
d, ok := di.(*BMP388Driver)
if !ok {
require.Fail(t, "NewBMP388Driver() should have returned a *BMP388Driver")
}
assert.NotNil(t, d.Driver)
assert.True(t, strings.HasPrefix(d.Name(), "BMP388"))
assert.Equal(t, 0x77, d.defaultAddress)
assert.Equal(t, uint8(0x01), d.ctrlPwrMode) // forced mode
assert.Equal(t, BMP388IIRFilter(0x00), d.confFilter) // filter off
assert.NotNil(t, d.calCoeffs)
}
func TestBMP388Options(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 := NewBMP388Driver(newI2cTestAdaptor(), WithBus(2), WithBMP388IIRFilter(BMP388IIRFilter(0x03)))
assert.Equal(t, 2, d.GetBusOrDefault(1))
assert.Equal(t, BMP388IIRFilter(0x03), d.confFilter)
}
func TestBMP388Measurements(t *testing.T) {
d, a := initTestBMP388WithStubbedAdaptor()
a.i2cReadImpl = func(b []byte) (int, error) {
buf := new(bytes.Buffer)
lastWritten := a.written[len(a.written)-1]
switch lastWritten {
case bmp388RegChipID:
// Simulate returning of 0x50 for the
// ReadByteData(bmp388RegChipID) call in initialisation()
_ = binary.Write(buf, binary.LittleEndian, uint8(0x50))
case bmp388RegCalib00:
// Values produced by dumping data from actual sensor
buf.Write([]byte{
36, 107, 156, 73, 246, 104, 255, 189, 245, 35, 0, 151, 101, 184, 122, 243, 246, 211, 64, 14, 196, 0, 0, 0,
})
case bmp388RegTempData:
buf.Write([]byte{0, 28, 127})
case bmp388RegPressureData:
buf.Write([]byte{0, 66, 113})
}
copy(b, buf.Bytes())
return buf.Len(), nil
}
_ = d.Start()
temp, err := d.Temperature(2)
require.NoError(t, err)
assert.InDelta(t, float32(22.906143), temp, 0.0)
pressure, err := d.Pressure(2)
require.NoError(t, err)
assert.InDelta(t, float32(98874.85), pressure, 0.0)
alt, err := d.Altitude(2)
require.NoError(t, err)
assert.InDelta(t, float32(205.89395), alt, 0.0)
}
func TestBMP388TemperatureWriteError(t *testing.T) {
d, a := initTestBMP388WithStubbedAdaptor()
_ = d.Start()
a.i2cWriteImpl = func([]byte) (int, error) {
return 0, errors.New("write error")
}
temp, err := d.Temperature(2)
require.ErrorContains(t, err, "write error")
assert.InDelta(t, float32(0.0), temp, 0.0)
}
func TestBMP388TemperatureReadError(t *testing.T) {
d, a := initTestBMP388WithStubbedAdaptor()
_ = d.Start()
a.i2cReadImpl = func([]byte) (int, error) {
return 0, errors.New("read error")
}
temp, err := d.Temperature(2)
require.ErrorContains(t, err, "read error")
assert.InDelta(t, float32(0.0), temp, 0.0)
}
func TestBMP388PressureWriteError(t *testing.T) {
d, a := initTestBMP388WithStubbedAdaptor()
_ = d.Start()
a.i2cWriteImpl = func([]byte) (int, error) {
return 0, errors.New("write error")
}
press, err := d.Pressure(2)
require.ErrorContains(t, err, "write error")
assert.InDelta(t, float32(0.0), press, 0.0)
}
func TestBMP388PressureReadError(t *testing.T) {
d, a := initTestBMP388WithStubbedAdaptor()
_ = d.Start()
a.i2cReadImpl = func([]byte) (int, error) {
return 0, errors.New("read error")
}
press, err := d.Pressure(2)
require.ErrorContains(t, err, "read error")
assert.InDelta(t, float32(0.0), press, 0.0)
}
func TestBMP388_initialization(t *testing.T) {
// sequence to read and write in initialization():
// * read chip ID register (0x00) and compare
// * read 24 bytes (12 x 16 bit calibration data), starting from TC1 register (0x31)
// * fill calibration struct with data (LSByte read first)
// * perform a soft reset by command register (0x7E)
// * prepare the content of config register
// * write the config register (0x1F)
// arrange
d, a := initTestBMP388WithStubbedAdaptor()
a.written = []byte{} // reset writes of former test
const (
wantChipIDReg = uint8(0x00)
wantCalibReg = uint8(0x31)
wantCommandReg = uint8(0x7E)
wantCommandRegVal = uint8(0xB6) // soft reset
wantConfReg = uint8(0x1F)
wantConfRegVal = uint8(0x00) // no filter
)
// Values produced by dumping data from actual sensor
returnRead := []byte{
36, 107, 156, 73, 246, 104, 255, 189, 245, 35, 0, 151, 101, 184, 122, 243, 246, 211, 64, 14, 196, 0, 0, 0,
}
numCallsRead := 0
a.i2cReadImpl = func(b []byte) (int, error) {
numCallsRead++
if numCallsRead == 1 {
b[0] = 0x50
} else {
copy(b, returnRead)
}
return len(b), nil
}
// act, assert - initialization() must be called on Start()
err := d.Start()
// assert
require.NoError(t, err)
assert.Equal(t, 2, numCallsRead)
assert.Len(t, a.written, 6)
assert.Equal(t, wantChipIDReg, a.written[0])
assert.Equal(t, wantCalibReg, a.written[1])
assert.Equal(t, wantCommandReg, a.written[2])
assert.Equal(t, wantCommandRegVal, a.written[3])
assert.Equal(t, wantConfReg, a.written[4])
assert.Equal(t, wantConfRegVal, a.written[5])
assert.InDelta(t, float32(7.021568e+06), d.calCoeffs.t1, 0.0)
assert.InDelta(t, float32(1.7549843e-05), d.calCoeffs.t2, 0.0)
assert.InDelta(t, float32(-3.5527137e-14), d.calCoeffs.t3, 0.0)
assert.InDelta(t, float32(-0.015769958), d.calCoeffs.p1, 0.0)
assert.InDelta(t, float32(-3.5410747e-05), d.calCoeffs.p2, 0.0)
assert.InDelta(t, float32(8.1490725e-09), d.calCoeffs.p3, 0.0)
assert.InDelta(t, float32(0), d.calCoeffs.p4, 0.0)
assert.InDelta(t, float32(208056), d.calCoeffs.p5, 0.0)
assert.InDelta(t, float32(490.875), d.calCoeffs.p6, 0.0)
assert.InDelta(t, float32(-0.05078125), d.calCoeffs.p7, 0.0)
assert.InDelta(t, float32(-0.00030517578), d.calCoeffs.p8, 0.0)
assert.InDelta(t, float32(5.8957283e-11), d.calCoeffs.p9, 0.0)
}