gobot.io/x/gobot/v2@v2.1.0/drivers/i2c/bme280_driver_test.go (about) 1 package i2c 2 3 import ( 4 "bytes" 5 "errors" 6 "strings" 7 "testing" 8 9 "gobot.io/x/gobot/v2" 10 "gobot.io/x/gobot/v2/gobottest" 11 ) 12 13 // this ensures that the implementation is based on i2c.Driver, which implements the gobot.Driver 14 // and tests all implementations, so no further tests needed here for gobot.Driver interface 15 var _ gobot.Driver = (*BME280Driver)(nil) 16 17 func initTestBME280WithStubbedAdaptor() (*BME280Driver, *i2cTestAdaptor) { 18 adaptor := newI2cTestAdaptor() 19 return NewBME280Driver(adaptor), adaptor 20 } 21 22 func TestNewBME280Driver(t *testing.T) { 23 var di interface{} = NewBME280Driver(newI2cTestAdaptor()) 24 d, ok := di.(*BME280Driver) 25 if !ok { 26 t.Errorf("NewBME280Driver() should have returned a *BME280Driver") 27 } 28 gobottest.Refute(t, d.Driver, nil) 29 gobottest.Assert(t, strings.HasPrefix(d.Name(), "BMP280"), true) 30 gobottest.Assert(t, d.defaultAddress, 0x77) 31 gobottest.Assert(t, d.ctrlPwrMode, uint8(0x03)) 32 gobottest.Assert(t, d.ctrlPressOversamp, BMP280PressureOversampling(0x05)) 33 gobottest.Assert(t, d.ctrlTempOversamp, BMP280TemperatureOversampling(0x01)) 34 gobottest.Assert(t, d.ctrlHumOversamp, BME280HumidityOversampling(0x05)) 35 gobottest.Assert(t, d.confFilter, BMP280IIRFilter(0x00)) 36 gobottest.Refute(t, d.calCoeffs, nil) 37 } 38 39 func TestBME280Options(t *testing.T) { 40 // This is a general test, that options are applied in constructor by using the common WithBus() option and 41 // least one of this driver. Further tests for options can also be done by call of "WithOption(val)(d)". 42 d := NewBME280Driver(newI2cTestAdaptor(), WithBus(2), 43 WithBME280PressureOversampling(0x01), 44 WithBME280TemperatureOversampling(0x02), 45 WithBME280IIRFilter(0x03), 46 WithBME280HumidityOversampling(0x04)) 47 gobottest.Assert(t, d.GetBusOrDefault(1), 2) 48 gobottest.Assert(t, d.ctrlPressOversamp, BMP280PressureOversampling(0x01)) 49 gobottest.Assert(t, d.ctrlTempOversamp, BMP280TemperatureOversampling(0x02)) 50 gobottest.Assert(t, d.confFilter, BMP280IIRFilter(0x03)) 51 gobottest.Assert(t, d.ctrlHumOversamp, BME280HumidityOversampling(0x04)) 52 } 53 54 func TestBME280Measurements(t *testing.T) { 55 bme280, adaptor := initTestBME280WithStubbedAdaptor() 56 adaptor.i2cReadImpl = func(b []byte) (int, error) { 57 buf := new(bytes.Buffer) 58 // Values produced by dumping data from actual sensor 59 if adaptor.written[len(adaptor.written)-1] == bmp280RegCalib00 { 60 buf.Write([]byte{126, 109, 214, 102, 50, 0, 54, 149, 220, 213, 208, 11, 64, 30, 166, 255, 249, 255, 172, 38, 10, 216, 189, 16}) 61 } else if adaptor.written[len(adaptor.written)-1] == bme280RegCalibDigH1 { 62 buf.Write([]byte{75}) 63 } else if adaptor.written[len(adaptor.written)-1] == bmp280RegTempData { 64 buf.Write([]byte{129, 0, 0}) 65 } else if adaptor.written[len(adaptor.written)-1] == bme280RegCalibDigH2LSB { 66 buf.Write([]byte{112, 1, 0, 19, 1, 0, 30}) 67 } else if adaptor.written[len(adaptor.written)-1] == bme280RegHumidityMSB { 68 buf.Write([]byte{111, 83}) 69 } 70 copy(b, buf.Bytes()) 71 return buf.Len(), nil 72 } 73 bme280.Start() 74 hum, err := bme280.Humidity() 75 gobottest.Assert(t, err, nil) 76 gobottest.Assert(t, hum, float32(51.20179)) 77 } 78 79 func TestBME280InitH1Error(t *testing.T) { 80 bme280, adaptor := initTestBME280WithStubbedAdaptor() 81 adaptor.i2cReadImpl = func(b []byte) (int, error) { 82 buf := new(bytes.Buffer) 83 // Values produced by dumping data from actual sensor 84 if adaptor.written[len(adaptor.written)-1] == bmp280RegCalib00 { 85 buf.Write([]byte{126, 109, 214, 102, 50, 0, 54, 149, 220, 213, 208, 11, 64, 30, 166, 255, 249, 255, 172, 38, 10, 216, 189, 16}) 86 } else if adaptor.written[len(adaptor.written)-1] == bme280RegCalibDigH1 { 87 return 0, errors.New("h1 read error") 88 } else if adaptor.written[len(adaptor.written)-1] == bme280RegCalibDigH2LSB { 89 buf.Write([]byte{112, 1, 0, 19, 1, 0, 30}) 90 } 91 copy(b, buf.Bytes()) 92 return buf.Len(), nil 93 } 94 95 gobottest.Assert(t, bme280.Start(), errors.New("h1 read error")) 96 } 97 98 func TestBME280InitH2Error(t *testing.T) { 99 bme280, adaptor := initTestBME280WithStubbedAdaptor() 100 adaptor.i2cReadImpl = func(b []byte) (int, error) { 101 buf := new(bytes.Buffer) 102 // Values produced by dumping data from actual sensor 103 if adaptor.written[len(adaptor.written)-1] == bmp280RegCalib00 { 104 buf.Write([]byte{126, 109, 214, 102, 50, 0, 54, 149, 220, 213, 208, 11, 64, 30, 166, 255, 249, 255, 172, 38, 10, 216, 189, 16}) 105 } else if adaptor.written[len(adaptor.written)-1] == bme280RegCalibDigH1 { 106 buf.Write([]byte{75}) 107 } else if adaptor.written[len(adaptor.written)-1] == bme280RegCalibDigH2LSB { 108 return 0, errors.New("h2 read error") 109 } 110 copy(b, buf.Bytes()) 111 return buf.Len(), nil 112 } 113 114 gobottest.Assert(t, bme280.Start(), errors.New("h2 read error")) 115 } 116 117 func TestBME280HumidityWriteError(t *testing.T) { 118 bme280, adaptor := initTestBME280WithStubbedAdaptor() 119 bme280.Start() 120 121 adaptor.i2cWriteImpl = func([]byte) (int, error) { 122 return 0, errors.New("write error") 123 } 124 hum, err := bme280.Humidity() 125 gobottest.Assert(t, err, errors.New("write error")) 126 gobottest.Assert(t, hum, float32(0.0)) 127 } 128 129 func TestBME280HumidityReadError(t *testing.T) { 130 bme280, adaptor := initTestBME280WithStubbedAdaptor() 131 bme280.Start() 132 133 adaptor.i2cReadImpl = func([]byte) (int, error) { 134 return 0, errors.New("read error") 135 } 136 hum, err := bme280.Humidity() 137 gobottest.Assert(t, err, errors.New("read error")) 138 gobottest.Assert(t, hum, float32(0.0)) 139 } 140 141 func TestBME280HumidityNotEnabled(t *testing.T) { 142 bme280, adaptor := initTestBME280WithStubbedAdaptor() 143 adaptor.i2cReadImpl = func(b []byte) (int, error) { 144 buf := new(bytes.Buffer) 145 // Values produced by dumping data from actual sensor 146 if adaptor.written[len(adaptor.written)-1] == bmp280RegCalib00 { 147 buf.Write([]byte{126, 109, 214, 102, 50, 0, 54, 149, 220, 213, 208, 11, 64, 30, 166, 255, 249, 255, 172, 38, 10, 216, 189, 16}) 148 } else if adaptor.written[len(adaptor.written)-1] == bme280RegCalibDigH1 { 149 buf.Write([]byte{75}) 150 } else if adaptor.written[len(adaptor.written)-1] == bmp280RegTempData { 151 buf.Write([]byte{129, 0, 0}) 152 } else if adaptor.written[len(adaptor.written)-1] == bme280RegCalibDigH2LSB { 153 buf.Write([]byte{112, 1, 0, 19, 1, 0, 30}) 154 } else if adaptor.written[len(adaptor.written)-1] == bme280RegHumidityMSB { 155 buf.Write([]byte{0x80, 0x00}) 156 } 157 copy(b, buf.Bytes()) 158 return buf.Len(), nil 159 } 160 bme280.Start() 161 hum, err := bme280.Humidity() 162 gobottest.Assert(t, err, errors.New("Humidity disabled")) 163 gobottest.Assert(t, hum, float32(0.0)) 164 } 165 166 func TestBME280_initializationBME280(t *testing.T) { 167 bme280, adaptor := initTestBME280WithStubbedAdaptor() 168 readCallCounter := 0 169 adaptor.i2cReadImpl = func(b []byte) (int, error) { 170 readCallCounter++ 171 if readCallCounter == 1 { 172 // Simulate returning 24 bytes for the coefficients (register bmp280RegCalib00) 173 return 24, nil 174 } 175 if readCallCounter == 2 { 176 // Simulate returning a single byte for the hc.h1 value (register bme280RegCalibDigH1) 177 return 1, nil 178 } 179 if readCallCounter == 3 { 180 // Simulate returning 7 bytes for the coefficients (register bme280RegCalibDigH2LSB) 181 return 7, nil 182 } 183 if readCallCounter == 4 { 184 // Simulate returning 1 byte for the cmr (register bmp280RegControl) 185 return 1, nil 186 } 187 return 0, nil 188 } 189 gobottest.Assert(t, bme280.Start(), nil) 190 }