diff --git a/drivers/i2c/bmp388.go b/drivers/i2c/bmp388_driver.go similarity index 99% rename from drivers/i2c/bmp388.go rename to drivers/i2c/bmp388_driver.go index fcedb75fd..732ec1efc 100644 --- a/drivers/i2c/bmp388.go +++ b/drivers/i2c/bmp388_driver.go @@ -125,6 +125,8 @@ func (d *BMP388Driver) Connection() gobot.Connection { // Start initializes the BMP388 and loads the calibration coefficients. func (d *BMP388Driver) Start() (err error) { + var chipID uint8 + bus := d.GetBusOrDefault(d.connector.GetDefaultBus()) address := d.GetAddressOrDefault(bmp180Address) @@ -132,6 +134,14 @@ func (d *BMP388Driver) Start() (err error) { return err } + if chipID, err = d.connection.ReadByteData(bmp388RegisterChipID); err != nil { + return err + } + + if bmp388ChipID != chipID { + return fmt.Errorf("Incorrect BMP388 chip ID '0%x' Expected 0x%x", chipID, bmp388ChipID) + } + if err := d.initialization(); err != nil { return err } @@ -206,7 +216,6 @@ func (d *BMP388Driver) Altitude(accuracy BMP388Accuracy) (alt float32, err error // initialization reads the calibration coefficients. func (d *BMP388Driver) initialization() (err error) { var ( - chipID uint8 coefficients []byte t1 uint16 t2 uint16 @@ -224,18 +233,11 @@ func (d *BMP388Driver) initialization() (err error) { p11 int8 ) - if chipID, err = d.connection.ReadByteData(bmp388RegisterChipID); err != nil { - return err - } - - if bmp388ChipID != chipID { - return fmt.Errorf("Incorrect BMP388 chip ID '0%x' Expected 0x%x", chipID, bmp388ChipID) - } - if coefficients, err = d.read(bmp388RegisterCalib00, 24); err != nil { return err } buf := bytes.NewBuffer(coefficients) + binary.Read(buf, binary.LittleEndian, &t1) binary.Read(buf, binary.LittleEndian, &t2) binary.Read(buf, binary.LittleEndian, &t3) @@ -288,12 +290,12 @@ func (d *BMP388Driver) rawTemp() (temp int32, err error) { return 0, err } buf := bytes.NewBuffer(data) + binary.Read(buf, binary.LittleEndian, &tp0) // XLSB binary.Read(buf, binary.LittleEndian, &tp1) // LSB binary.Read(buf, binary.LittleEndian, &tp2) // MSB temp = ((int32(tp2) << 16) | (int32(tp1) << 8) | int32(tp0)) - return } @@ -305,6 +307,7 @@ func (d *BMP388Driver) rawPressure() (press int32, err error) { return 0, err } buf := bytes.NewBuffer(data) + binary.Read(buf, binary.LittleEndian, &tp0) // XLSB binary.Read(buf, binary.LittleEndian, &tp1) // LSB binary.Read(buf, binary.LittleEndian, &tp2) // MSB diff --git a/drivers/i2c/bmp388_driver_test.go b/drivers/i2c/bmp388_driver_test.go index 21463c254..e128bf9f6 100644 --- a/drivers/i2c/bmp388_driver_test.go +++ b/drivers/i2c/bmp388_driver_test.go @@ -2,6 +2,7 @@ package i2c import ( "bytes" + "encoding/binary" "errors" "testing" @@ -19,6 +20,14 @@ func initTestBMP388Driver() (driver *BMP388Driver) { func initTestBMP388DriverWithStubbedAdaptor() (*BMP388Driver, *i2cTestAdaptor) { adaptor := newI2cTestAdaptor() + adaptor.i2cReadImpl = func(b []byte) (int, error) { + buf := new(bytes.Buffer) + // Simulate returning of 0x50 for the + // ReadByteData(bmp388RegisterChipID) call in initialisation() + binary.Write(buf, binary.LittleEndian, uint8(0x50)) + copy(b, buf.Bytes()) + return buf.Len(), nil + } return NewBMP388Driver(adaptor), adaptor } @@ -44,9 +53,9 @@ func TestBMP388DriverStart(t *testing.T) { } func TestBMP388StartConnectError(t *testing.T) { - d, adaptor := initTestBMP388DriverWithStubbedAdaptor() + bmp388, adaptor := initTestBMP388DriverWithStubbedAdaptor() adaptor.Testi2cConnectErr(true) - gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection")) + gobottest.Assert(t, bmp388.Start(), errors.New("Invalid i2c connection")) } func TestBMP388DriverStartWriteError(t *testing.T) { @@ -75,27 +84,32 @@ func TestBMP388DriverMeasurements(t *testing.T) { bmp388, adaptor := initTestBMP388DriverWithStubbedAdaptor() adaptor.i2cReadImpl = func(b []byte) (int, error) { buf := new(bytes.Buffer) - // Values produced by dumping data from actual sensor - if adaptor.written[len(adaptor.written)-1] == bmp388RegisterCalib00 { - 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}) + if len(adaptor.written) == 0 { + // Simulate returning of 0x50 for the + // ReadByteData(bmp388RegisterChipID) call in initialisation() + binary.Write(buf, binary.LittleEndian, uint8(0x50)) + } else if adaptor.written[len(adaptor.written)-1] == bmp388RegisterCalib00 { + // 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}) } else if adaptor.written[len(adaptor.written)-1] == bmp388RegisterTempData { - buf.Write([]byte{128, 243, 0}) + buf.Write([]byte{0, 28, 127}) } else if adaptor.written[len(adaptor.written)-1] == bmp388RegisterPressureData { - buf.Write([]byte{77, 23, 48}) + buf.Write([]byte{0, 66, 113}) } + copy(b, buf.Bytes()) return buf.Len(), nil } bmp388.Start() temp, err := bmp388.Temperature(2) gobottest.Assert(t, err, nil) - gobottest.Assert(t, temp, float32(25.014637)) + gobottest.Assert(t, temp, float32(22.906143)) pressure, err := bmp388.Pressure(2) gobottest.Assert(t, err, nil) - gobottest.Assert(t, pressure, float32(99545.414)) + gobottest.Assert(t, pressure, float32(98874.85)) alt, err := bmp388.Altitude(2) gobottest.Assert(t, err, nil) - gobottest.Assert(t, alt, float32(149.22713)) + gobottest.Assert(t, alt, float32(205.89395)) } func TestBMP388DriverTemperatureWriteError(t *testing.T) {