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

     1  package i2c
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/binary"
     6  	"errors"
     7  	"testing"
     8  
     9  	"gobot.io/x/gobot"
    10  	"gobot.io/x/gobot/gobottest"
    11  )
    12  
    13  var _ gobot.Driver = (*DRV2605LDriver)(nil)
    14  
    15  // --------- HELPERS
    16  
    17  func initTestDriverAndAdaptor() (*DRV2605LDriver, *i2cTestAdaptor) {
    18  	adaptor := newI2cTestAdaptor()
    19  	// Prime adapter reader to make "Start()" call happy
    20  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
    21  		buf := new(bytes.Buffer)
    22  		binary.Write(buf, binary.LittleEndian, uint8(42))
    23  		copy(b, buf.Bytes())
    24  		return buf.Len(), nil
    25  	}
    26  	return NewDRV2605LDriver(adaptor), adaptor
    27  }
    28  
    29  // --------- TESTS
    30  
    31  func TestDRV2605LDriver(t *testing.T) {
    32  	d, _ := initTestDriverAndAdaptor()
    33  	gobottest.Refute(t, d.Connection(), nil)
    34  }
    35  
    36  func TestDRV2605LDriverStart(t *testing.T) {
    37  	d, _ := initTestDriverAndAdaptor()
    38  	gobottest.Assert(t, d.Start(), nil)
    39  }
    40  
    41  func TestDRV2605LStartConnectError(t *testing.T) {
    42  	d, adaptor := initTestDriverAndAdaptor()
    43  	adaptor.Testi2cConnectErr(true)
    44  	gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
    45  }
    46  
    47  func TestDRVD2605DriverStartWriteError(t *testing.T) {
    48  	d, a := initTestDriverAndAdaptor()
    49  	a.i2cWriteImpl = func(b []byte) (int, error) {
    50  		return 0, errors.New("write error")
    51  	}
    52  	gobottest.Assert(t, d.Start(), errors.New("write error"))
    53  }
    54  
    55  func TestDRVD2605DriverStartReadError(t *testing.T) {
    56  	d, a := initTestDriverAndAdaptor()
    57  	a.i2cReadImpl = func(b []byte) (int, error) {
    58  		return 0, errors.New("read error")
    59  	}
    60  	gobottest.Assert(t, d.Start(), errors.New("read error"))
    61  }
    62  
    63  func TestDRV2605LDriverHalt(t *testing.T) {
    64  	writeStopPlaybackData := []byte{drv2605RegGo, 0}
    65  	// single-byte-read starts with a write operation to set the register for reading
    66  	// see section 8.5.3.5 of data sheet
    67  	readCurrentStandbyModeData := byte(drv2605RegMode)
    68  	writeNewStandbyModeData := []byte{drv2605RegMode, 42 | drv2605Standby}
    69  	d, adaptor := initTestDriverAndAdaptor()
    70  	gobottest.Assert(t, d.Start(), nil)
    71  	adaptor.written = []byte{}
    72  	gobottest.Assert(t, d.Halt(), nil)
    73  	gobottest.Assert(t, adaptor.written, append(append(writeStopPlaybackData, readCurrentStandbyModeData), writeNewStandbyModeData...))
    74  }
    75  
    76  func TestDRVD2605DriverHaltWriteError(t *testing.T) {
    77  	d, a := initTestDriverAndAdaptor()
    78  	d.Start()
    79  	a.i2cWriteImpl = func(b []byte) (int, error) {
    80  		return 0, errors.New("write error")
    81  	}
    82  	gobottest.Assert(t, d.Halt(), errors.New("write error"))
    83  }
    84  
    85  func TestDRV2605LDriverGetPause(t *testing.T) {
    86  	d, _ := initTestDriverAndAdaptor()
    87  	gobottest.Assert(t, d.Start(), nil)
    88  	gobottest.Assert(t, d.GetPauseWaveform(0), uint8(0x80))
    89  	gobottest.Assert(t, d.GetPauseWaveform(1), uint8(0x81))
    90  	gobottest.Assert(t, d.GetPauseWaveform(128), d.GetPauseWaveform(127))
    91  }
    92  
    93  func TestDRV2605LDriverSequenceTermination(t *testing.T) {
    94  	d, adaptor := initTestDriverAndAdaptor()
    95  	gobottest.Assert(t, d.Start(), nil)
    96  	adaptor.written = []byte{}
    97  	gobottest.Assert(t, d.SetSequence([]byte{1, 2}), nil)
    98  	gobottest.Assert(t, adaptor.written, []byte{
    99  		drv2605RegWaveSeq1, 1,
   100  		drv2605RegWaveSeq2, 2,
   101  		drv2605RegWaveSeq3, 0,
   102  	})
   103  }
   104  
   105  func TestDRV2605LDriverSequenceTruncation(t *testing.T) {
   106  	d, adaptor := initTestDriverAndAdaptor()
   107  	gobottest.Assert(t, d.Start(), nil)
   108  	adaptor.written = []byte{}
   109  	gobottest.Assert(t, d.SetSequence([]byte{1, 2, 3, 4, 5, 6, 7, 8, 99, 100}), nil)
   110  	gobottest.Assert(t, adaptor.written, []byte{
   111  		drv2605RegWaveSeq1, 1,
   112  		drv2605RegWaveSeq2, 2,
   113  		drv2605RegWaveSeq3, 3,
   114  		drv2605RegWaveSeq4, 4,
   115  		drv2605RegWaveSeq5, 5,
   116  		drv2605RegWaveSeq6, 6,
   117  		drv2605RegWaveSeq7, 7,
   118  		drv2605RegWaveSeq8, 8,
   119  	})
   120  }
   121  
   122  func TestDRV2605LDriverSetName(t *testing.T) {
   123  	d, _ := initTestDriverAndAdaptor()
   124  	d.SetName("TESTME")
   125  	gobottest.Assert(t, d.Name(), "TESTME")
   126  }
   127  
   128  func TestDRV2605DriverOptions(t *testing.T) {
   129  	d := NewDRV2605LDriver(newI2cTestAdaptor(), WithBus(2))
   130  	gobottest.Assert(t, d.GetBusOrDefault(1), 2)
   131  }
   132  
   133  func TestDRV2605LDriverSetMode(t *testing.T) {
   134  	d, _ := initTestDriverAndAdaptor()
   135  	d.Start()
   136  	gobottest.Assert(t, d.SetMode(DRV2605ModeIntTrig), nil)
   137  }
   138  
   139  func TestDRV2605LDriverSetModeReadError(t *testing.T) {
   140  	d, a := initTestDriverAndAdaptor()
   141  	d.Start()
   142  
   143  	a.i2cReadImpl = func(b []byte) (int, error) {
   144  		return 0, errors.New("read error")
   145  	}
   146  	gobottest.Assert(t, d.SetMode(DRV2605ModeIntTrig), errors.New("read error"))
   147  }
   148  
   149  func TestDRV2605LDriverSetStandbyMode(t *testing.T) {
   150  	d, _ := initTestDriverAndAdaptor()
   151  	d.Start()
   152  	gobottest.Assert(t, d.SetStandbyMode(true), nil)
   153  }
   154  
   155  func TestDRV2605LDriverSetStandbyModeReadError(t *testing.T) {
   156  	d, a := initTestDriverAndAdaptor()
   157  	d.Start()
   158  
   159  	a.i2cReadImpl = func(b []byte) (int, error) {
   160  		return 0, errors.New("read error")
   161  	}
   162  	gobottest.Assert(t, d.SetStandbyMode(true), errors.New("read error"))
   163  }
   164  
   165  func TestDRV2605LDriverSelectLibrary(t *testing.T) {
   166  	d, _ := initTestDriverAndAdaptor()
   167  	d.Start()
   168  	gobottest.Assert(t, d.SelectLibrary(1), nil)
   169  }
   170  
   171  func TestDRV2605LDriverGo(t *testing.T) {
   172  	d, _ := initTestDriverAndAdaptor()
   173  	d.Start()
   174  	gobottest.Assert(t, d.Go(), nil)
   175  }