From 3d7bf121567027994f16fe9b69ff249ec969f3a6 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Sat, 1 Apr 2017 13:46:37 +0200 Subject: [PATCH] i2c: add test coverage for bme280 Signed-off-by: deadprogram --- drivers/i2c/bme280_driver_test.go | 25 +++++++++++++++++++++++-- drivers/i2c/bmp280_driver.go | 5 ----- drivers/i2c/bmp280_driver_test.go | 1 - 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/drivers/i2c/bme280_driver_test.go b/drivers/i2c/bme280_driver_test.go index 09db1563..14b0728f 100644 --- a/drivers/i2c/bme280_driver_test.go +++ b/drivers/i2c/bme280_driver_test.go @@ -1,6 +1,7 @@ package i2c import ( + "bytes" "testing" "gobot.io/x/gobot" @@ -47,9 +48,29 @@ func TestBME280DriverHalt(t *testing.T) { gobottest.Assert(t, bme280.Halt(), nil) } -// TODO: implement test func TestBME280DriverMeasurements(t *testing.T) { - + bme280, adaptor := initTestBME280DriverWithStubbedAdaptor() + 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] == bmp280RegisterCalib00 { + 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}) + } else if adaptor.written[len(adaptor.written)-1] == bme280RegisterCalibDigH1 { + buf.Write([]byte{75}) + } else if adaptor.written[len(adaptor.written)-1] == bmp280RegisterTempData { + buf.Write([]byte{129, 0, 0}) + } else if adaptor.written[len(adaptor.written)-1] == bme280RegisterCalibDigH2LSB { + buf.Write([]byte{112, 1, 0, 19, 1, 0, 30}) + } else if adaptor.written[len(adaptor.written)-1] == bme280RegisterHumidityMSB { + buf.Write([]byte{111, 83}) + } + copy(b, buf.Bytes()) + return buf.Len(), nil + } + bme280.Start() + hum, err := bme280.Humidity() + gobottest.Assert(t, err, nil) + gobottest.Assert(t, hum, float32(51.20179)) } func TestBME280DriverSetName(t *testing.T) { diff --git a/drivers/i2c/bmp280_driver.go b/drivers/i2c/bmp280_driver.go index ffe6190d..ca04fc81 100644 --- a/drivers/i2c/bmp280_driver.go +++ b/drivers/i2c/bmp280_driver.go @@ -140,8 +140,6 @@ func (d *BMP280Driver) Altitude() (alt float32, err error) { // initialization reads the calibration coefficients. func (d *BMP280Driver) initialization() (err error) { - // TODO: set sleep mode here... - var coefficients []byte if coefficients, err = d.read(bmp280RegisterCalib00, 24); err != nil { return err @@ -162,9 +160,6 @@ func (d *BMP280Driver) initialization() (err error) { d.connection.WriteByteData(bmp280RegisterControl, 0x3F) - // TODO: set usage mode here... - // TODO: set default sea level here - return nil } diff --git a/drivers/i2c/bmp280_driver_test.go b/drivers/i2c/bmp280_driver_test.go index c52dd108..b2c997db 100644 --- a/drivers/i2c/bmp280_driver_test.go +++ b/drivers/i2c/bmp280_driver_test.go @@ -48,7 +48,6 @@ func TestBMP280DriverHalt(t *testing.T) { gobottest.Assert(t, bmp280.Halt(), nil) } -// TODO: implement test func TestBMP280DriverMeasurements(t *testing.T) { bmp280, adaptor := initTestBMP280DriverWithStubbedAdaptor() adaptor.i2cReadImpl = func(b []byte) (int, error) {