gobot.io/x/gobot@v1.16.0/drivers/i2c/ads1x15_driver_test.go (about)

     1  package i2c
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  	"testing"
     7  
     8  	"gobot.io/x/gobot"
     9  	"gobot.io/x/gobot/drivers/aio"
    10  	"gobot.io/x/gobot/gobottest"
    11  )
    12  
    13  // the ADS1x15Driver is a Driver
    14  var _ gobot.Driver = (*ADS1x15Driver)(nil)
    15  
    16  // that supports the AnalogReader interface
    17  var _ aio.AnalogReader = (*ADS1x15Driver)(nil)
    18  
    19  // --------- HELPERS
    20  func initTestADS1015Driver() (driver *ADS1x15Driver) {
    21  	driver, _ = initTestADS1015DriverWithStubbedAdaptor()
    22  	return
    23  }
    24  
    25  func initTestADS1115Driver() (driver *ADS1x15Driver) {
    26  	driver, _ = initTestADS1115DriverWithStubbedAdaptor()
    27  	return
    28  }
    29  
    30  func initTestADS1015DriverWithStubbedAdaptor() (*ADS1x15Driver, *i2cTestAdaptor) {
    31  	adaptor := newI2cTestAdaptor()
    32  	return NewADS1015Driver(adaptor), adaptor
    33  }
    34  
    35  func initTestADS1115DriverWithStubbedAdaptor() (*ADS1x15Driver, *i2cTestAdaptor) {
    36  	adaptor := newI2cTestAdaptor()
    37  	return NewADS1115Driver(adaptor), adaptor
    38  }
    39  
    40  // --------- BASE TESTS
    41  func TestNewADS1015Driver(t *testing.T) {
    42  	// Does it return a pointer to an instance of ADS1x15Driver?
    43  	var bm interface{} = NewADS1015Driver(newI2cTestAdaptor())
    44  	_, ok := bm.(*ADS1x15Driver)
    45  	if !ok {
    46  		t.Errorf("NewADS1015Driver() should have returned a *ADS1x15Driver")
    47  	}
    48  }
    49  
    50  func TestNewADS1115Driver(t *testing.T) {
    51  	// Does it return a pointer to an instance of ADS1x15Driver?
    52  	var bm interface{} = NewADS1115Driver(newI2cTestAdaptor())
    53  	_, ok := bm.(*ADS1x15Driver)
    54  	if !ok {
    55  		t.Errorf("NewADS1115Driver() should have returned a *ADS1x15Driver")
    56  	}
    57  }
    58  
    59  func TestADS1x15DriverSetName(t *testing.T) {
    60  	d := initTestADS1015Driver()
    61  	d.SetName("TESTME")
    62  	gobottest.Assert(t, d.Name(), "TESTME")
    63  }
    64  
    65  func TestADS1x15DriverOptions(t *testing.T) {
    66  	d := NewADS1015Driver(newI2cTestAdaptor(), WithBus(2), WithADS1x15Gain(2), WithADS1x15DataRate(920))
    67  	gobottest.Assert(t, d.GetBusOrDefault(1), 2)
    68  	gobottest.Assert(t, d.DefaultGain, 2)
    69  	gobottest.Assert(t, d.DefaultDataRate, 920)
    70  }
    71  
    72  func TestADS1x15StartAndHalt(t *testing.T) {
    73  	d, _ := initTestADS1015DriverWithStubbedAdaptor()
    74  	gobottest.Assert(t, d.Start(), nil)
    75  	gobottest.Refute(t, d.Connection(), nil)
    76  	gobottest.Assert(t, d.Halt(), nil)
    77  }
    78  
    79  func TestADS1x15StartConnectError(t *testing.T) {
    80  	d, adaptor := initTestADS1015DriverWithStubbedAdaptor()
    81  	adaptor.Testi2cConnectErr(true)
    82  	gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
    83  }
    84  
    85  // --------- DRIVER SPECIFIC TESTS
    86  
    87  func TestADS1015DriverAnalogRead(t *testing.T) {
    88  	d, adaptor := initTestADS1015DriverWithStubbedAdaptor()
    89  	d.Start()
    90  
    91  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
    92  		copy(b, []byte{0x7F, 0xFF})
    93  		return 2, nil
    94  	}
    95  
    96  	val, err := d.AnalogRead("0")
    97  	gobottest.Assert(t, val, 1022)
    98  	gobottest.Assert(t, err, nil)
    99  
   100  	val, err = d.AnalogRead("1")
   101  	gobottest.Assert(t, val, 1022)
   102  	gobottest.Assert(t, err, nil)
   103  
   104  	val, err = d.AnalogRead("2")
   105  	gobottest.Assert(t, val, 1022)
   106  	gobottest.Assert(t, err, nil)
   107  
   108  	val, err = d.AnalogRead("3")
   109  	gobottest.Assert(t, val, 1022)
   110  	gobottest.Assert(t, err, nil)
   111  
   112  	val, err = d.AnalogRead("0-1")
   113  	gobottest.Assert(t, val, 1022)
   114  	gobottest.Assert(t, err, nil)
   115  
   116  	val, err = d.AnalogRead("0-3")
   117  	gobottest.Assert(t, val, 1022)
   118  	gobottest.Assert(t, err, nil)
   119  
   120  	val, err = d.AnalogRead("1-3")
   121  	gobottest.Assert(t, val, 1022)
   122  	gobottest.Assert(t, err, nil)
   123  
   124  	val, err = d.AnalogRead("2-3")
   125  	gobottest.Assert(t, val, 1022)
   126  	gobottest.Assert(t, err, nil)
   127  
   128  	val, err = d.AnalogRead("3-2")
   129  	gobottest.Refute(t, err.Error(), nil)
   130  }
   131  
   132  func TestADS1115DriverAnalogRead(t *testing.T) {
   133  	d, adaptor := initTestADS1115DriverWithStubbedAdaptor()
   134  	d.Start()
   135  
   136  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
   137  		copy(b, []byte{0x7F, 0xFF})
   138  		return 2, nil
   139  	}
   140  
   141  	val, err := d.AnalogRead("0")
   142  	gobottest.Assert(t, val, 1022)
   143  	gobottest.Assert(t, err, nil)
   144  
   145  	val, err = d.AnalogRead("1")
   146  	gobottest.Assert(t, val, 1022)
   147  	gobottest.Assert(t, err, nil)
   148  
   149  	val, err = d.AnalogRead("2")
   150  	gobottest.Assert(t, val, 1022)
   151  	gobottest.Assert(t, err, nil)
   152  
   153  	val, err = d.AnalogRead("3")
   154  	gobottest.Assert(t, val, 1022)
   155  	gobottest.Assert(t, err, nil)
   156  
   157  	val, err = d.AnalogRead("0-1")
   158  	gobottest.Assert(t, val, 1022)
   159  	gobottest.Assert(t, err, nil)
   160  
   161  	val, err = d.AnalogRead("0-3")
   162  	gobottest.Assert(t, val, 1022)
   163  	gobottest.Assert(t, err, nil)
   164  
   165  	val, err = d.AnalogRead("1-3")
   166  	gobottest.Assert(t, val, 1022)
   167  	gobottest.Assert(t, err, nil)
   168  
   169  	val, err = d.AnalogRead("2-3")
   170  	gobottest.Assert(t, val, 1022)
   171  	gobottest.Assert(t, err, nil)
   172  
   173  	val, err = d.AnalogRead("3-2")
   174  	gobottest.Refute(t, err.Error(), nil)
   175  }
   176  
   177  func TestADS1x15DriverAnalogReadError(t *testing.T) {
   178  	d, a := initTestADS1015DriverWithStubbedAdaptor()
   179  	d.Start()
   180  
   181  	a.i2cReadImpl = func(b []byte) (int, error) {
   182  		return 0, errors.New("read error")
   183  	}
   184  
   185  	_, err := d.AnalogRead("0")
   186  	gobottest.Assert(t, err, errors.New("read error"))
   187  }
   188  
   189  func TestADS1x15DriverAnalogReadInvalidPin(t *testing.T) {
   190  	d, _ := initTestADS1015DriverWithStubbedAdaptor()
   191  
   192  	_, err := d.AnalogRead("99")
   193  	gobottest.Assert(t, err, errors.New("Invalid channel, must be between 0 and 3"))
   194  }
   195  
   196  func TestADS1x15DriverAnalogReadWriteError(t *testing.T) {
   197  	d, a := initTestADS1015DriverWithStubbedAdaptor()
   198  	d.Start()
   199  
   200  	a.i2cWriteImpl = func([]byte) (int, error) {
   201  		return 0, errors.New("write error")
   202  	}
   203  
   204  	_, err := d.AnalogRead("0")
   205  	gobottest.Assert(t, err, errors.New("write error"))
   206  
   207  	_, err = d.AnalogRead("0-1")
   208  	gobottest.Assert(t, err, errors.New("write error"))
   209  
   210  	_, err = d.AnalogRead("2-3")
   211  	gobottest.Assert(t, err, errors.New("write error"))
   212  }
   213  
   214  func TestADS1x15DriverBestGainForVoltage(t *testing.T) {
   215  	d, _ := initTestADS1015DriverWithStubbedAdaptor()
   216  
   217  	g, err := d.BestGainForVoltage(1.5)
   218  	gobottest.Assert(t, g, 2)
   219  
   220  	g, err = d.BestGainForVoltage(20.0)
   221  	gobottest.Assert(t, err, errors.New("The maximum voltage which can be read is 6.144000"))
   222  }
   223  
   224  func TestADS1x15DriverReadInvalidChannel(t *testing.T) {
   225  	d, _ := initTestADS1015DriverWithStubbedAdaptor()
   226  
   227  	_, err := d.Read(9, d.DefaultGain, d.DefaultDataRate)
   228  	gobottest.Assert(t, err, errors.New("Invalid channel, must be between 0 and 3"))
   229  }
   230  
   231  func TestADS1x15DriverReadInvalidGain(t *testing.T) {
   232  	d, _ := initTestADS1015DriverWithStubbedAdaptor()
   233  
   234  	_, err := d.Read(0, 21, d.DefaultDataRate)
   235  	gobottest.Assert(t, err, errors.New("Gain must be one of: 2/3, 1, 2, 4, 8, 16"))
   236  }
   237  
   238  func TestADS1x15DriverReadInvalidDataRate(t *testing.T) {
   239  	d, _ := initTestADS1015DriverWithStubbedAdaptor()
   240  
   241  	_, err := d.Read(0, d.DefaultGain, 666)
   242  	gobottest.Assert(t, strings.Contains(err.Error(), "Invalid data rate."), true)
   243  }
   244  
   245  func TestADS1x15DriverReadDifferenceInvalidChannel(t *testing.T) {
   246  	d, _ := initTestADS1015DriverWithStubbedAdaptor()
   247  
   248  	_, err := d.ReadDifference(9, d.DefaultGain, d.DefaultDataRate)
   249  	gobottest.Assert(t, err, errors.New("Invalid channel, must be between 0 and 3"))
   250  }