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

     1  package i2c
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"gobot.io/x/gobot/v2"
     8  	"gobot.io/x/gobot/v2/drivers/aio"
     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 = (*ADS1x15Driver)(nil)
    15  
    16  // that supports the AnalogReader interface
    17  var _ aio.AnalogReader = (*ADS1x15Driver)(nil)
    18  
    19  func initTestADS1x15DriverWithStubbedAdaptor() (*ADS1x15Driver, *i2cTestAdaptor) {
    20  	a := newI2cTestAdaptor()
    21  	const defaultDataRate = 3
    22  	dataRates := map[int]uint16{defaultDataRate: 0x0003}
    23  	d := newADS1x15Driver(a, "ADS1x15", dataRates, defaultDataRate)
    24  	noConversion := []uint8{0x80, 0x00} // no conversion in progress
    25  	a.i2cReadImpl = func(b []byte) (int, error) {
    26  		copy(b, noConversion)
    27  		return 2, nil
    28  	}
    29  	if err := d.Start(); err != nil {
    30  		panic(err)
    31  	}
    32  	return d, a
    33  }
    34  
    35  var ads1x15TestChannel = map[string]interface{}{
    36  	"channel": int(2),
    37  }
    38  
    39  var ads1x15TestChannelGainDataRate = map[string]interface{}{
    40  	"channel":  int(1),
    41  	"gain":     int(2),
    42  	"dataRate": int(3),
    43  }
    44  
    45  func TestADS1x15CommandsReadDifferenceWithDefaults(t *testing.T) {
    46  	// arrange
    47  	d, _ := initTestADS1x15DriverWithStubbedAdaptor()
    48  	// act
    49  	result := d.Command("ReadDifferenceWithDefaults")(ads1x15TestChannel)
    50  	// assert
    51  	gobottest.Assert(t, result.(map[string]interface{})["err"], nil)
    52  	gobottest.Assert(t, result.(map[string]interface{})["val"], -4.096)
    53  }
    54  
    55  func TestADS1x15CommandsReadDifference(t *testing.T) {
    56  	// arrange
    57  	d, _ := initTestADS1x15DriverWithStubbedAdaptor()
    58  	// act
    59  	result := d.Command("ReadDifference")(ads1x15TestChannelGainDataRate)
    60  	// assert
    61  	gobottest.Assert(t, result.(map[string]interface{})["err"], nil)
    62  	gobottest.Assert(t, result.(map[string]interface{})["val"], -2.048)
    63  }
    64  
    65  func TestADS1x15CommandsReadWithDefaults(t *testing.T) {
    66  	// arrange
    67  	d, _ := initTestADS1x15DriverWithStubbedAdaptor()
    68  	// act
    69  	result := d.Command("ReadWithDefaults")(ads1x15TestChannel)
    70  	// assert
    71  	gobottest.Assert(t, result.(map[string]interface{})["err"], nil)
    72  	gobottest.Assert(t, result.(map[string]interface{})["val"], -4.096)
    73  }
    74  
    75  func TestADS1x15CommandsRead(t *testing.T) {
    76  	// arrange
    77  	d, _ := initTestADS1x15DriverWithStubbedAdaptor()
    78  	// act
    79  	result := d.Command("Read")(ads1x15TestChannelGainDataRate)
    80  	// assert
    81  	gobottest.Assert(t, result.(map[string]interface{})["err"], nil)
    82  	gobottest.Assert(t, result.(map[string]interface{})["val"], -2.048)
    83  }
    84  
    85  func TestADS1x15CommandsAnalogRead(t *testing.T) {
    86  	// arrange
    87  	d, _ := initTestADS1x15DriverWithStubbedAdaptor()
    88  	ads1x15TestPin := map[string]interface{}{
    89  		"pin": string("2"),
    90  	}
    91  	// act
    92  	result := d.Command("AnalogRead")(ads1x15TestPin)
    93  	// assert
    94  	gobottest.Assert(t, result.(map[string]interface{})["err"], nil)
    95  	gobottest.Assert(t, result.(map[string]interface{})["val"], -32768)
    96  }
    97  
    98  func TestADS1x15_ads1x15BestGainForVoltage(t *testing.T) {
    99  	g, _ := ads1x15BestGainForVoltage(1.5)
   100  	gobottest.Assert(t, g, 2)
   101  
   102  	_, err := ads1x15BestGainForVoltage(20.0)
   103  	gobottest.Assert(t, err, errors.New("The maximum voltage which can be read is 6.144000"))
   104  }