Skip to content

Commit

Permalink
[test] Implement driver tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Cédric authored and deadprogram committed Oct 28, 2020
1 parent 1823ed9 commit cf368e9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
23 changes: 13 additions & 10 deletions drivers/i2c/bmp388.go → drivers/i2c/bmp388_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,23 @@ 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)

if d.connection, err = d.connector.GetConnection(address, bus); err != nil {
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
}
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand Down
34 changes: 24 additions & 10 deletions drivers/i2c/bmp388_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package i2c

import (
"bytes"
"encoding/binary"
"errors"
"testing"

Expand All @@ -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
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit cf368e9

Please sign in to comment.