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

     1  package i2c
     2  
     3  import (
     4  	"testing"
     5  
     6  	"gobot.io/x/gobot/gobottest"
     7  )
     8  
     9  func initTestPCF8591DriverWithStubbedAdaptor() (*PCF8591Driver, *i2cTestAdaptor) {
    10  	adaptor := newI2cTestAdaptor()
    11  	pcf := NewPCF8591Driver(adaptor, WithPCF8591With400kbitStabilization(0, 2))
    12  	pcf.lastCtrlByte = 0xFF // prevent skipping of write
    13  	pcf.Start()
    14  	return pcf, adaptor
    15  }
    16  
    17  func TestPCF8591DriverWithPCF8591With400kbitStabilization(t *testing.T) {
    18  	pcf := NewPCF8591Driver(newI2cTestAdaptor(), WithPCF8591With400kbitStabilization(5, 6))
    19  	gobottest.Assert(t, pcf.additionalReadWrite, uint8(5))
    20  	gobottest.Assert(t, pcf.additionalRead, uint8(6))
    21  }
    22  
    23  func TestPCF8591DriverAnalogReadSingle(t *testing.T) {
    24  	// sequence to read the input channel:
    25  	// * prepare value (with channel and mode) and write control register
    26  	// * read 3 values to drop (see description in implementation)
    27  	// * read the analog value
    28  	//
    29  	// arrange
    30  	pcf, adaptor := initTestPCF8591DriverWithStubbedAdaptor()
    31  	adaptor.written = []byte{} // reset writes of Start() and former test
    32  	description := "s.1"
    33  	pcf.lastCtrlByte = 0x00
    34  	ctrlByteOn := uint8(pcf8591_ALLSINGLE) | uint8(pcf8591_CHAN1)
    35  	returnRead := []uint8{0x01, 0x02, 0x03, 0xFF}
    36  	want := int(returnRead[3])
    37  	// arrange reads
    38  	numCallsRead := 0
    39  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
    40  		numCallsRead++
    41  		if numCallsRead == 1 {
    42  			b = returnRead[0:len(b)]
    43  		}
    44  		if numCallsRead == 2 {
    45  			b[0] = returnRead[len(returnRead)-1]
    46  		}
    47  		return len(b), nil
    48  	}
    49  	// act
    50  	got, err := pcf.AnalogRead(description)
    51  	// assert
    52  	gobottest.Assert(t, err, nil)
    53  	gobottest.Assert(t, len(adaptor.written), 1)
    54  	gobottest.Assert(t, adaptor.written[0], ctrlByteOn)
    55  	gobottest.Assert(t, numCallsRead, 2)
    56  	gobottest.Assert(t, got, want)
    57  }
    58  
    59  func TestPCF8591DriverAnalogReadDiff(t *testing.T) {
    60  	// sequence to read the input channel:
    61  	// * prepare value (with channel and mode) and write control register
    62  	// * read 3 values to drop (see description in implementation)
    63  	// * read the analog value
    64  	// * convert to 8-bit two's complement (-127...128)
    65  	//
    66  	// arrange
    67  	pcf, adaptor := initTestPCF8591DriverWithStubbedAdaptor()
    68  	adaptor.written = []byte{} // reset writes of Start() and former test
    69  	description := "m.2-3"
    70  	pcf.lastCtrlByte = 0x00
    71  	ctrlByteOn := uint8(pcf8591_MIXED) | uint8(pcf8591_CHAN2)
    72  	// some two' complements
    73  	// 0x80 => -128
    74  	// 0xFF => -1
    75  	// 0x00 => 0
    76  	// 0x7F => 127
    77  	returnRead := []uint8{0x01, 0x02, 0x03, 0xFF}
    78  	want := -1
    79  	// arrange reads
    80  	numCallsRead := 0
    81  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
    82  		numCallsRead++
    83  		if numCallsRead == 1 {
    84  			b = returnRead[0:len(b)]
    85  		}
    86  		if numCallsRead == 2 {
    87  			b[0] = returnRead[len(returnRead)-1]
    88  		}
    89  		return len(b), nil
    90  	}
    91  	// act
    92  	got, err := pcf.AnalogRead(description)
    93  	// assert
    94  	gobottest.Assert(t, err, nil)
    95  	gobottest.Assert(t, len(adaptor.written), 1)
    96  	gobottest.Assert(t, adaptor.written[0], ctrlByteOn)
    97  	gobottest.Assert(t, numCallsRead, 2)
    98  	gobottest.Assert(t, got, want)
    99  }
   100  
   101  func TestPCF8591DriverAnalogWrite(t *testing.T) {
   102  	// sequence to write the output:
   103  	// * create new value for the control register (ANAON)
   104  	// * write the control register and value
   105  	//
   106  	// arrange
   107  	pcf, adaptor := initTestPCF8591DriverWithStubbedAdaptor()
   108  	adaptor.written = []byte{} // reset writes of Start() and former test
   109  	pcf.lastCtrlByte = 0x00
   110  	pcf.lastAnaOut = 0x00
   111  	ctrlByteOn := uint8(pcf8591_ANAON)
   112  	want := uint8(0x15)
   113  	// arrange writes
   114  	adaptor.i2cWriteImpl = func(b []byte) (int, error) {
   115  		return len(b), nil
   116  	}
   117  	// act
   118  	err := pcf.AnalogWrite("", int(want))
   119  	// assert
   120  	gobottest.Assert(t, err, nil)
   121  	gobottest.Assert(t, len(adaptor.written), 2)
   122  	gobottest.Assert(t, adaptor.written[0], ctrlByteOn)
   123  	gobottest.Assert(t, adaptor.written[1], want)
   124  }
   125  
   126  func TestPCF8591DriverAnalogOutputState(t *testing.T) {
   127  	// sequence to set the state:
   128  	// * create the new value (ctrlByte) for the control register (ANAON)
   129  	// * write the register value
   130  	//
   131  	// arrange
   132  	pcf, adaptor := initTestPCF8591DriverWithStubbedAdaptor()
   133  	for bitState := 0; bitState <= 1; bitState++ {
   134  		adaptor.written = []byte{} // reset writes of Start() and former test
   135  		// arrange some values
   136  		pcf.lastCtrlByte = uint8(0x00)
   137  		wantCtrlByteVal := uint8(pcf8591_ANAON)
   138  		if bitState == 0 {
   139  			pcf.lastCtrlByte = uint8(0xFF)
   140  			wantCtrlByteVal = uint8(0xFF & ^pcf8591_ANAON)
   141  		}
   142  		// act
   143  		err := pcf.AnalogOutputState(bitState == 1)
   144  		// assert
   145  		gobottest.Assert(t, err, nil)
   146  		gobottest.Assert(t, len(adaptor.written), 1)
   147  		gobottest.Assert(t, adaptor.written[0], wantCtrlByteVal)
   148  	}
   149  }
   150  
   151  func TestPCF8591DriverStart(t *testing.T) {
   152  	yl := NewPCF8591Driver(newI2cTestAdaptor())
   153  	gobottest.Assert(t, yl.Start(), nil)
   154  }
   155  
   156  func TestPCF8591DriverHalt(t *testing.T) {
   157  	yl := NewPCF8591Driver(newI2cTestAdaptor())
   158  	gobottest.Assert(t, yl.Halt(), nil)
   159  }
   160  
   161  func TestPCF8591DriverSetName(t *testing.T) {
   162  	d := NewPCF8591Driver(newI2cTestAdaptor())
   163  	d.SetName("TESTME")
   164  	gobottest.Assert(t, d.Name(), "TESTME")
   165  }