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

     1  package i2c
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  	"testing"
     7  
     8  	"gobot.io/x/gobot/v2"
     9  	"gobot.io/x/gobot/v2/gobottest"
    10  )
    11  
    12  // this ensures that the implementation is based on i2c.Driver, which implements the gobot.Driver
    13  // and tests all implementations, so no further tests needed here for gobot.Driver interface
    14  var _ gobot.Driver = (*MPL115A2Driver)(nil)
    15  
    16  func initTestMPL115A2DriverWithStubbedAdaptor() (*MPL115A2Driver, *i2cTestAdaptor) {
    17  	a := newI2cTestAdaptor()
    18  	return NewMPL115A2Driver(a), a
    19  }
    20  
    21  func TestNewMPL115A2Driver(t *testing.T) {
    22  	var di interface{} = NewMPL115A2Driver(newI2cTestAdaptor())
    23  	d, ok := di.(*MPL115A2Driver)
    24  	if !ok {
    25  		t.Errorf("NewMPL115A2Driver() should have returned a *MPL115A2Driver")
    26  	}
    27  	gobottest.Refute(t, d.Connection(), nil)
    28  	gobottest.Assert(t, strings.HasPrefix(d.Name(), "MPL115A2"), true)
    29  	gobottest.Assert(t, d.defaultAddress, 0x60)
    30  }
    31  
    32  func TestMPL115A2Options(t *testing.T) {
    33  	// This is a general test, that options are applied in constructor by using the common WithBus() option and
    34  	// least one of this driver. Further tests for options can also be done by call of "WithOption(val)(d)".
    35  	d := NewMPL115A2Driver(newI2cTestAdaptor(), WithBus(2))
    36  	gobottest.Assert(t, d.GetBusOrDefault(1), 2)
    37  }
    38  
    39  func TestMPL115A2ReadData(t *testing.T) {
    40  	// sequence for read data
    41  	// * retrieve the coefficients for temperature compensation of pressure - see test for Start()
    42  	// * write start conversion register address (0x12)
    43  	// * write start value - 0x00
    44  	// * wait at least 3 ms according to data sheet (tc - conversion time)
    45  	// * write pressure MSB register address (0x00)
    46  	// * read pressure (16 bit, order MSB-LSB)
    47  	// * read temperature (16 bit, order MSB-LSB)
    48  	// * calculate temperature compensated pressure in kPa according to data sheet
    49  	//   * shift the temperature value right for 6 bits (resolution is 10 bit)
    50  	//   * shift the pressure value right for 6 bits (resolution is 10 bit)
    51  	// * calculate temperature in °C according to this implementation:
    52  	//   https://github.com/adafruit/Adafruit_MPL115A2/blob/master/Adafruit_MPL115A2.cpp
    53  	//
    54  	// arrange
    55  	d, a := initTestMPL115A2DriverWithStubbedAdaptor()
    56  	d.Start()
    57  	a.written = []byte{}
    58  	// arrange coefficients according the example from data sheet
    59  	d.a0 = 2009.75
    60  	d.b1 = -2.37585
    61  	d.b2 = -0.92047
    62  	d.c12 = 0.00079
    63  	readReturnP := []byte{0x66, 0x80, 0x7E, 0xC0} // use example from data sheet
    64  	readReturnT := []byte{0x00, 0x00, 0x7E, 0xC0} // use example from data sheet
    65  	readCallCounter := 0
    66  	a.i2cReadImpl = func(b []byte) (int, error) {
    67  		readCallCounter++
    68  		if readCallCounter == 1 {
    69  			copy(b, readReturnP)
    70  		}
    71  		if readCallCounter == 2 {
    72  			copy(b, readReturnT)
    73  		}
    74  		return len(b), nil
    75  	}
    76  
    77  	// act
    78  	press, errP := d.Pressure()
    79  	temp, errT := d.Temperature()
    80  	// assert
    81  	gobottest.Assert(t, errP, nil)
    82  	gobottest.Assert(t, errT, nil)
    83  	gobottest.Assert(t, readCallCounter, 2)
    84  	gobottest.Assert(t, len(a.written), 6)
    85  	gobottest.Assert(t, a.written[0], uint8(0x12))
    86  	gobottest.Assert(t, a.written[1], uint8(0x00))
    87  	gobottest.Assert(t, a.written[2], uint8(0x00))
    88  	gobottest.Assert(t, a.written[3], uint8(0x12))
    89  	gobottest.Assert(t, a.written[4], uint8(0x00))
    90  	gobottest.Assert(t, a.written[5], uint8(0x00))
    91  	gobottest.Assert(t, press, float32(96.585915))
    92  	gobottest.Assert(t, temp, float32(23.317757))
    93  }
    94  
    95  func TestMPL115A2ReadDataError(t *testing.T) {
    96  	d, a := initTestMPL115A2DriverWithStubbedAdaptor()
    97  	d.Start()
    98  
    99  	a.i2cWriteImpl = func([]byte) (int, error) {
   100  		return 0, errors.New("write error")
   101  	}
   102  	_, err := d.Pressure()
   103  
   104  	gobottest.Assert(t, err, errors.New("write error"))
   105  }
   106  
   107  func TestMPL115A2_initialization(t *testing.T) {
   108  	// sequence for initialization the device on Start(), which calculates
   109  	// the coefficients for temperature compensation of pressure
   110  	// * write coefficient A0 MSB register address (0x04)
   111  	// * read all 4 coefficients (16 bit, order MSB-LSB)
   112  	// * write signal path reset register address (0x68)
   113  	// * calculate A0, B1, B2, C12 according to data sheet
   114  	//
   115  	// arrange
   116  	d, a := initTestMPL115A2DriverWithStubbedAdaptor()
   117  	readCallCounter := 0
   118  	readReturn := []byte{0x3E, 0xCE, 0xB3, 0xF9, 0xC5, 0x17, 0x33, 0xC8} // use example from data sheet
   119  	a.i2cReadImpl = func(b []byte) (int, error) {
   120  		readCallCounter++
   121  		copy(b, readReturn)
   122  		return len(b), nil
   123  	}
   124  	// act, assert - initialization() must be called on Start()
   125  	err := d.Start()
   126  	// assert
   127  	gobottest.Assert(t, err, nil)
   128  	gobottest.Assert(t, readCallCounter, 1)
   129  	gobottest.Assert(t, len(a.written), 1)
   130  	gobottest.Assert(t, a.written[0], uint8(0x04))
   131  	gobottest.Assert(t, d.a0, float32(2009.75))
   132  	gobottest.Assert(t, d.b1, float32(-2.3758545))
   133  	gobottest.Assert(t, d.b2, float32(-0.9204712))
   134  	gobottest.Assert(t, d.c12, float32(0.0007901192))
   135  }