gobot.io/x/gobot/v2@v2.1.0/drivers/spi/spi_connection_test.go (about)

     1  package spi
     2  
     3  import (
     4  	"testing"
     5  
     6  	"gobot.io/x/gobot/v2"
     7  	"gobot.io/x/gobot/v2/gobottest"
     8  	"gobot.io/x/gobot/v2/system"
     9  )
    10  
    11  var _ gobot.SpiOperations = (*spiConnection)(nil)
    12  
    13  func initTestConnectionWithMockedSystem() (Connection, *system.MockSpiAccess) {
    14  	a := system.NewAccesser()
    15  	sysdev := a.UseMockSpi()
    16  	const (
    17  		busNum   = 15
    18  		chipNum  = 14
    19  		mode     = 13
    20  		bits     = 12
    21  		maxSpeed = int64(11)
    22  	)
    23  	d, err := a.NewSpiDevice(busNum, chipNum, mode, bits, maxSpeed)
    24  	if err != nil {
    25  		panic(err)
    26  	}
    27  	c := NewConnection(d)
    28  	return c, sysdev
    29  }
    30  
    31  func TestReadCommandData(t *testing.T) {
    32  	// arrange
    33  	command := []byte{0x11, 0x12}
    34  	want := []byte{0x31, 0x32}
    35  	c, sysdev := initTestConnectionWithMockedSystem()
    36  	sysdev.SetSimRead(want)
    37  	// act
    38  	got := []byte{0x01, 0x02}
    39  	err := c.ReadCommandData(command, got)
    40  	// assert
    41  	gobottest.Assert(t, err, nil)
    42  	gobottest.Assert(t, sysdev.Written(), command)
    43  	gobottest.Assert(t, got, want)
    44  }
    45  
    46  func TestReadByteData(t *testing.T) {
    47  	// arrange
    48  	const (
    49  		reg  = 0x15
    50  		want = uint8(0x41)
    51  	)
    52  	c, sysdev := initTestConnectionWithMockedSystem()
    53  	sysdev.SetSimRead([]byte{0x00, want}) // the answer is one cycle behind
    54  	// act
    55  	got, err := c.ReadByteData(reg)
    56  	// assert
    57  	gobottest.Assert(t, err, nil)
    58  	gobottest.Assert(t, sysdev.Written(), []byte{reg, 0x00}) // for read register we need n+1 bytes
    59  	gobottest.Assert(t, got, want)
    60  }
    61  
    62  func TestReadBlockData(t *testing.T) {
    63  	// arrange
    64  	const (
    65  		reg = 0x16
    66  	)
    67  	want := []byte{42, 24, 56, 65}
    68  	c, sysdev := initTestConnectionWithMockedSystem()
    69  	sysdev.SetSimRead(append([]byte{0x00}, want...)) // the answer is one cycle behind
    70  	// act
    71  	got := make([]byte, 4)
    72  	err := c.ReadBlockData(reg, got)
    73  	// assert
    74  	gobottest.Assert(t, err, nil)
    75  	gobottest.Assert(t, sysdev.Written(), []byte{reg, 0x00, 0x00, 0x00, 0x00}) // for read registers we need n+1 bytes
    76  	gobottest.Assert(t, got, want)
    77  }
    78  
    79  func TestWriteByte(t *testing.T) {
    80  	// arrange
    81  	const want = 0x02
    82  	c, sysdev := initTestConnectionWithMockedSystem()
    83  	// act
    84  	err := c.WriteByte(want)
    85  	// assert
    86  	gobottest.Assert(t, err, nil)
    87  	gobottest.Assert(t, sysdev.Written(), []byte{want})
    88  }
    89  
    90  func TestWriteByteData(t *testing.T) {
    91  	// arrange
    92  	const (
    93  		reg = 0x22
    94  		val = 0x33
    95  	)
    96  	c, sysdev := initTestConnectionWithMockedSystem()
    97  	// act
    98  	err := c.WriteByteData(reg, val)
    99  	// assert
   100  	gobottest.Assert(t, err, nil)
   101  	gobottest.Assert(t, sysdev.Written(), []byte{reg, val})
   102  }
   103  
   104  func TestWriteBlockData(t *testing.T) {
   105  	// arrange
   106  	const reg = 0x33
   107  	data := []byte{0x22, 0x11}
   108  	c, sysdev := initTestConnectionWithMockedSystem()
   109  	// act
   110  	err := c.WriteBlockData(reg, data)
   111  	// assert
   112  	gobottest.Assert(t, err, nil)
   113  	gobottest.Assert(t, sysdev.Written(), append([]byte{reg}, data...))
   114  }
   115  
   116  func TestWriteBytes(t *testing.T) {
   117  	// arrange
   118  	want := []byte{0x03}
   119  	c, sysdev := initTestConnectionWithMockedSystem()
   120  	// act
   121  	err := c.WriteBytes(want)
   122  	// assert
   123  	gobottest.Assert(t, err, nil)
   124  	gobottest.Assert(t, sysdev.Written(), want)
   125  }