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

     1  package i2c
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/binary"
     6  	"errors"
     7  	"strings"
     8  	"testing"
     9  
    10  	"gobot.io/x/gobot/v2"
    11  	"gobot.io/x/gobot/v2/gobottest"
    12  )
    13  
    14  // this ensures that the implementation is based on i2c.Driver, which implements the gobot.Driver
    15  // and tests all implementations, so no further tests needed here for gobot.Driver interface
    16  var _ gobot.Driver = (*DRV2605LDriver)(nil)
    17  
    18  func initTestDRV2605LDriverWithStubbedAdaptor() (*DRV2605LDriver, *i2cTestAdaptor) {
    19  	a := newI2cTestAdaptor()
    20  	// Prime adapter reader to make "Start()" call happy
    21  	a.i2cReadImpl = func(b []byte) (int, error) {
    22  		buf := new(bytes.Buffer)
    23  		binary.Write(buf, binary.LittleEndian, uint8(42))
    24  		copy(b, buf.Bytes())
    25  		return buf.Len(), nil
    26  	}
    27  	d := NewDRV2605LDriver(a)
    28  	if err := d.Start(); err != nil {
    29  		panic(err)
    30  	}
    31  	return d, a
    32  }
    33  
    34  func TestNewDRV2605LDriver(t *testing.T) {
    35  	var di interface{} = NewDRV2605LDriver(newI2cTestAdaptor())
    36  	d, ok := di.(*DRV2605LDriver)
    37  	if !ok {
    38  		t.Errorf("NewDRV2605LDriver() should have returned a *DRV2605LDriver")
    39  	}
    40  	gobottest.Refute(t, d.Driver, nil)
    41  	gobottest.Assert(t, strings.HasPrefix(d.Name(), "DRV2605L"), true)
    42  	gobottest.Assert(t, d.defaultAddress, 0x5a)
    43  }
    44  
    45  func TestDRV2605LOptions(t *testing.T) {
    46  	// This is a general test, that options are applied in constructor by using the common WithBus() option and
    47  	// least one of this driver. Further tests for options can also be done by call of "WithOption(val)(d)".
    48  	d := NewDRV2605LDriver(newI2cTestAdaptor(), WithBus(2))
    49  	gobottest.Assert(t, d.GetBusOrDefault(1), 2)
    50  }
    51  
    52  func TestDRV2605LStart(t *testing.T) {
    53  	d := NewDRV2605LDriver(newI2cTestAdaptor())
    54  	gobottest.Assert(t, d.Start(), nil)
    55  }
    56  
    57  func TestDRV2605LHalt(t *testing.T) {
    58  	writeStopPlaybackData := []byte{drv2605RegGo, 0}
    59  	// single-byte-read starts with a write operation to set the register for reading
    60  	// see section 8.5.3.5 of data sheet
    61  	readCurrentStandbyModeData := byte(drv2605RegMode)
    62  	writeNewStandbyModeData := []byte{drv2605RegMode, 42 | drv2605Standby}
    63  	d, a := initTestDRV2605LDriverWithStubbedAdaptor()
    64  	a.written = []byte{}
    65  	gobottest.Assert(t, d.Halt(), nil)
    66  	gobottest.Assert(t, a.written, append(append(writeStopPlaybackData, readCurrentStandbyModeData), writeNewStandbyModeData...))
    67  }
    68  
    69  func TestDRV2605LGetPause(t *testing.T) {
    70  	d, _ := initTestDRV2605LDriverWithStubbedAdaptor()
    71  	gobottest.Assert(t, d.GetPauseWaveform(0), uint8(0x80))
    72  	gobottest.Assert(t, d.GetPauseWaveform(1), uint8(0x81))
    73  	gobottest.Assert(t, d.GetPauseWaveform(128), d.GetPauseWaveform(127))
    74  }
    75  
    76  func TestDRV2605LSequenceTermination(t *testing.T) {
    77  	d, a := initTestDRV2605LDriverWithStubbedAdaptor()
    78  	a.written = []byte{}
    79  	gobottest.Assert(t, d.SetSequence([]byte{1, 2}), nil)
    80  	gobottest.Assert(t, a.written, []byte{
    81  		drv2605RegWaveSeq1, 1,
    82  		drv2605RegWaveSeq2, 2,
    83  		drv2605RegWaveSeq3, 0,
    84  	})
    85  }
    86  
    87  func TestDRV2605LSequenceTruncation(t *testing.T) {
    88  	d, a := initTestDRV2605LDriverWithStubbedAdaptor()
    89  	a.written = []byte{}
    90  	gobottest.Assert(t, d.SetSequence([]byte{1, 2, 3, 4, 5, 6, 7, 8, 99, 100}), nil)
    91  	gobottest.Assert(t, a.written, []byte{
    92  		drv2605RegWaveSeq1, 1,
    93  		drv2605RegWaveSeq2, 2,
    94  		drv2605RegWaveSeq3, 3,
    95  		drv2605RegWaveSeq4, 4,
    96  		drv2605RegWaveSeq5, 5,
    97  		drv2605RegWaveSeq6, 6,
    98  		drv2605RegWaveSeq7, 7,
    99  		drv2605RegWaveSeq8, 8,
   100  	})
   101  }
   102  
   103  func TestDRV2605LSetMode(t *testing.T) {
   104  	d, _ := initTestDRV2605LDriverWithStubbedAdaptor()
   105  	gobottest.Assert(t, d.SetMode(DRV2605ModeIntTrig), nil)
   106  }
   107  
   108  func TestDRV2605LSetModeReadError(t *testing.T) {
   109  	d, a := initTestDRV2605LDriverWithStubbedAdaptor()
   110  	a.i2cReadImpl = func(b []byte) (int, error) {
   111  		return 0, errors.New("read error")
   112  	}
   113  	gobottest.Assert(t, d.SetMode(DRV2605ModeIntTrig), errors.New("read error"))
   114  }
   115  
   116  func TestDRV2605LSetStandbyMode(t *testing.T) {
   117  	d, _ := initTestDRV2605LDriverWithStubbedAdaptor()
   118  	gobottest.Assert(t, d.SetStandbyMode(true), nil)
   119  }
   120  
   121  func TestDRV2605LSetStandbyModeReadError(t *testing.T) {
   122  	d, a := initTestDRV2605LDriverWithStubbedAdaptor()
   123  	a.i2cReadImpl = func(b []byte) (int, error) {
   124  		return 0, errors.New("read error")
   125  	}
   126  	gobottest.Assert(t, d.SetStandbyMode(true), errors.New("read error"))
   127  }
   128  
   129  func TestDRV2605LSelectLibrary(t *testing.T) {
   130  	d, _ := initTestDRV2605LDriverWithStubbedAdaptor()
   131  	gobottest.Assert(t, d.SelectLibrary(1), nil)
   132  }
   133  
   134  func TestDRV2605LGo(t *testing.T) {
   135  	d, _ := initTestDRV2605LDriverWithStubbedAdaptor()
   136  	gobottest.Assert(t, d.Go(), nil)
   137  }