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

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