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