gobot.io/x/gobot/v2@v2.1.0/drivers/i2c/bmp180_driver_test.go (about)

     1  package i2c
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/binary"
     6  	"errors"
     7  	"strings"
     8  	"testing"
     9  	"time"
    10  
    11  	"gobot.io/x/gobot/v2"
    12  	"gobot.io/x/gobot/v2/gobottest"
    13  )
    14  
    15  // this ensures that the implementation is based on i2c.Driver, which implements the gobot.Driver
    16  // and tests all implementations, so no further tests needed here for gobot.Driver interface
    17  var _ gobot.Driver = (*BMP180Driver)(nil)
    18  
    19  func initTestBMP180WithStubbedAdaptor() (*BMP180Driver, *i2cTestAdaptor) {
    20  	adaptor := newI2cTestAdaptor()
    21  	return NewBMP180Driver(adaptor), adaptor
    22  }
    23  
    24  func TestNewBMP180Driver(t *testing.T) {
    25  	// Does it return a pointer to an instance of BMP180Driver?
    26  	var di interface{} = NewBMP180Driver(newI2cTestAdaptor())
    27  	d, ok := di.(*BMP180Driver)
    28  	if !ok {
    29  		t.Errorf("NewBMP180Driver() should have returned a *BMP180Driver")
    30  	}
    31  	gobottest.Refute(t, d.Driver, nil)
    32  	gobottest.Assert(t, strings.HasPrefix(d.Name(), "BMP180"), true)
    33  	gobottest.Assert(t, d.defaultAddress, 0x77)
    34  	gobottest.Assert(t, d.oversampling, BMP180OversamplingMode(0x00))
    35  	gobottest.Refute(t, d.calCoeffs, nil)
    36  }
    37  
    38  func TestBMP180Options(t *testing.T) {
    39  	// This is a general test, that options are applied in constructor by using the common WithBus() option and
    40  	// least one of this driver. Further tests for options can also be done by call of "WithOption(val)(d)".
    41  	d := NewBMP180Driver(newI2cTestAdaptor(), WithBus(2), WithBMP180OversamplingMode(0x01))
    42  	gobottest.Assert(t, d.GetBusOrDefault(1), 2)
    43  	gobottest.Assert(t, d.oversampling, BMP180OversamplingMode(0x01))
    44  }
    45  
    46  func TestBMP180Measurements(t *testing.T) {
    47  	bmp180, adaptor := initTestBMP180WithStubbedAdaptor()
    48  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
    49  		buf := new(bytes.Buffer)
    50  		// Values from the datasheet example.
    51  		if adaptor.written[len(adaptor.written)-1] == bmp180RegisterAC1MSB {
    52  			binary.Write(buf, binary.BigEndian, int16(408))
    53  			binary.Write(buf, binary.BigEndian, int16(-72))
    54  			binary.Write(buf, binary.BigEndian, int16(-14383))
    55  			binary.Write(buf, binary.BigEndian, uint16(32741))
    56  			binary.Write(buf, binary.BigEndian, uint16(32757))
    57  			binary.Write(buf, binary.BigEndian, uint16(23153))
    58  			binary.Write(buf, binary.BigEndian, int16(6190))
    59  			binary.Write(buf, binary.BigEndian, int16(4))
    60  			binary.Write(buf, binary.BigEndian, int16(-32768))
    61  			binary.Write(buf, binary.BigEndian, int16(-8711))
    62  			binary.Write(buf, binary.BigEndian, int16(2868))
    63  		} else if adaptor.written[len(adaptor.written)-2] == bmp180CtlTemp && adaptor.written[len(adaptor.written)-1] == bmp180RegisterDataMSB {
    64  			binary.Write(buf, binary.BigEndian, int16(27898))
    65  		} else if adaptor.written[len(adaptor.written)-2] == bmp180CtlPressure && adaptor.written[len(adaptor.written)-1] == bmp180RegisterDataMSB {
    66  			binary.Write(buf, binary.BigEndian, int16(23843))
    67  			// XLSB, not used in this test.
    68  			buf.WriteByte(0)
    69  		}
    70  		copy(b, buf.Bytes())
    71  		return buf.Len(), nil
    72  	}
    73  	bmp180.Start()
    74  	temp, err := bmp180.Temperature()
    75  	gobottest.Assert(t, err, nil)
    76  	gobottest.Assert(t, temp, float32(15.0))
    77  	pressure, err := bmp180.Pressure()
    78  	gobottest.Assert(t, err, nil)
    79  	gobottest.Assert(t, pressure, float32(69964))
    80  }
    81  
    82  func TestBMP180TemperatureError(t *testing.T) {
    83  	bmp180, adaptor := initTestBMP180WithStubbedAdaptor()
    84  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
    85  		buf := new(bytes.Buffer)
    86  		// Values from the datasheet example.
    87  		if adaptor.written[len(adaptor.written)-1] == bmp180RegisterAC1MSB {
    88  			binary.Write(buf, binary.BigEndian, int16(408))
    89  			binary.Write(buf, binary.BigEndian, int16(-72))
    90  			binary.Write(buf, binary.BigEndian, int16(-14383))
    91  			binary.Write(buf, binary.BigEndian, uint16(32741))
    92  			binary.Write(buf, binary.BigEndian, uint16(32757))
    93  			binary.Write(buf, binary.BigEndian, uint16(23153))
    94  			binary.Write(buf, binary.BigEndian, int16(6190))
    95  			binary.Write(buf, binary.BigEndian, int16(4))
    96  			binary.Write(buf, binary.BigEndian, int16(-32768))
    97  			binary.Write(buf, binary.BigEndian, int16(-8711))
    98  			binary.Write(buf, binary.BigEndian, int16(2868))
    99  		} else if adaptor.written[len(adaptor.written)-2] == bmp180CtlTemp && adaptor.written[len(adaptor.written)-1] == bmp180RegisterDataMSB {
   100  			return 0, errors.New("temp error")
   101  		} else if adaptor.written[len(adaptor.written)-2] == bmp180CtlPressure && adaptor.written[len(adaptor.written)-1] == bmp180RegisterDataMSB {
   102  			binary.Write(buf, binary.BigEndian, int16(23843))
   103  			// XLSB, not used in this test.
   104  			buf.WriteByte(0)
   105  		}
   106  		copy(b, buf.Bytes())
   107  		return buf.Len(), nil
   108  	}
   109  	bmp180.Start()
   110  	_, err := bmp180.Temperature()
   111  	gobottest.Assert(t, err, errors.New("temp error"))
   112  }
   113  
   114  func TestBMP180PressureError(t *testing.T) {
   115  	bmp180, adaptor := initTestBMP180WithStubbedAdaptor()
   116  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
   117  		buf := new(bytes.Buffer)
   118  		// Values from the datasheet example.
   119  		if adaptor.written[len(adaptor.written)-1] == bmp180RegisterAC1MSB {
   120  			binary.Write(buf, binary.BigEndian, int16(408))
   121  			binary.Write(buf, binary.BigEndian, int16(-72))
   122  			binary.Write(buf, binary.BigEndian, int16(-14383))
   123  			binary.Write(buf, binary.BigEndian, uint16(32741))
   124  			binary.Write(buf, binary.BigEndian, uint16(32757))
   125  			binary.Write(buf, binary.BigEndian, uint16(23153))
   126  			binary.Write(buf, binary.BigEndian, int16(6190))
   127  			binary.Write(buf, binary.BigEndian, int16(4))
   128  			binary.Write(buf, binary.BigEndian, int16(-32768))
   129  			binary.Write(buf, binary.BigEndian, int16(-8711))
   130  			binary.Write(buf, binary.BigEndian, int16(2868))
   131  		} else if adaptor.written[len(adaptor.written)-2] == bmp180CtlTemp && adaptor.written[len(adaptor.written)-1] == bmp180RegisterDataMSB {
   132  			binary.Write(buf, binary.BigEndian, int16(27898))
   133  		} else if adaptor.written[len(adaptor.written)-2] == bmp180CtlPressure && adaptor.written[len(adaptor.written)-1] == bmp180RegisterDataMSB {
   134  			return 0, errors.New("press error")
   135  		}
   136  		copy(b, buf.Bytes())
   137  		return buf.Len(), nil
   138  	}
   139  	bmp180.Start()
   140  	_, err := bmp180.Pressure()
   141  	gobottest.Assert(t, err, errors.New("press error"))
   142  }
   143  
   144  func TestBMP180PressureWriteError(t *testing.T) {
   145  	bmp180, adaptor := initTestBMP180WithStubbedAdaptor()
   146  	bmp180.Start()
   147  
   148  	adaptor.i2cWriteImpl = func([]byte) (int, error) {
   149  		return 0, errors.New("write error")
   150  	}
   151  
   152  	_, err := bmp180.Pressure()
   153  	gobottest.Assert(t, err, errors.New("write error"))
   154  }
   155  
   156  func TestBMP180_initialization(t *testing.T) {
   157  	// sequence to read in initialization():
   158  	// * read 22 bytes (11 x 16 bit calibration data), starting from AC1 register (0xAA)
   159  	// * fill calibration struct with data (MSByte read first)
   160  	// arrange
   161  	d, a := initTestBMP180WithStubbedAdaptor()
   162  	a.written = []byte{} // reset writes of former test
   163  	// Values from the datasheet example.
   164  	ac1 := []uint8{0x01, 0x98}
   165  	ac2 := []uint8{0xFF, 0xB8}
   166  	ac3 := []uint8{0xC7, 0xD1}
   167  	ac4 := []uint8{0x7F, 0xE5}
   168  	ac5 := []uint8{0x7F, 0xF5}
   169  	ac6 := []uint8{0x5A, 0x71}
   170  	b1 := []uint8{0x18, 0x2E}
   171  	b2 := []uint8{0x00, 0x04}
   172  	mb := []uint8{0x80, 0x00}
   173  	mc := []uint8{0xDD, 0xF9}
   174  	md := []uint8{0x0B, 0x34}
   175  	returnRead := append(append(append(append(append(ac1, ac2...), ac3...), ac4...), ac5...), ac6...)
   176  	returnRead = append(append(append(append(append(returnRead, b1...), b2...), mb...), mc...), md...)
   177  	numCallsRead := 0
   178  	a.i2cReadImpl = func(b []byte) (int, error) {
   179  		numCallsRead++
   180  		copy(b, returnRead)
   181  		return len(b), nil
   182  	}
   183  	// act, assert - initialization() must be called on Start()
   184  	err := d.Start()
   185  	// assert
   186  	gobottest.Assert(t, err, nil)
   187  	gobottest.Assert(t, numCallsRead, 1)
   188  	gobottest.Assert(t, len(a.written), 1)
   189  	gobottest.Assert(t, a.written[0], uint8(0xAA))
   190  	gobottest.Assert(t, d.calCoeffs.ac1, int16(408))
   191  	gobottest.Assert(t, d.calCoeffs.ac2, int16(-72))
   192  	gobottest.Assert(t, d.calCoeffs.ac3, int16(-14383))
   193  	gobottest.Assert(t, d.calCoeffs.ac4, uint16(32741))
   194  	gobottest.Assert(t, d.calCoeffs.ac5, uint16(32757))
   195  	gobottest.Assert(t, d.calCoeffs.ac6, uint16(23153))
   196  	gobottest.Assert(t, d.calCoeffs.b1, int16(6190))
   197  	gobottest.Assert(t, d.calCoeffs.b2, int16(4))
   198  	gobottest.Assert(t, d.calCoeffs.mb, int16(-32768))
   199  	gobottest.Assert(t, d.calCoeffs.mc, int16(-8711))
   200  	gobottest.Assert(t, d.calCoeffs.md, int16(2868))
   201  }
   202  
   203  func TestBMP180_bmp180PauseForReading(t *testing.T) {
   204  	gobottest.Assert(t, bmp180PauseForReading(BMP180UltraLowPower), time.Duration(5*time.Millisecond))
   205  	gobottest.Assert(t, bmp180PauseForReading(BMP180Standard), time.Duration(8*time.Millisecond))
   206  	gobottest.Assert(t, bmp180PauseForReading(BMP180HighResolution), time.Duration(14*time.Millisecond))
   207  	gobottest.Assert(t, bmp180PauseForReading(BMP180UltraHighResolution), time.Duration(26*time.Millisecond))
   208  }