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

     1  package i2c
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"gobot.io/x/gobot/gobottest"
     9  )
    10  
    11  func initTestYL40DriverWithStubbedAdaptor() (*YL40Driver, *i2cTestAdaptor) {
    12  	adaptor := newI2cTestAdaptor()
    13  	yl := NewYL40Driver(adaptor, WithPCF8591With400kbitStabilization(0, 2))
    14  	WithPCF8591ForceRefresh(1)(yl.PCF8591Driver)
    15  	yl.Start()
    16  	return yl, adaptor
    17  }
    18  
    19  func TestYL40Driver(t *testing.T) {
    20  	// arrange, act
    21  	yl := NewYL40Driver(newI2cTestAdaptor())
    22  	//assert
    23  	gobottest.Refute(t, yl.PCF8591Driver, nil)
    24  	gobottest.Assert(t, yl.conf.sensors[YL40Bri].interval, time.Duration(0))
    25  	gobottest.Refute(t, yl.conf.sensors[YL40Bri].scaler, nil)
    26  	gobottest.Assert(t, yl.conf.sensors[YL40Temp].interval, time.Duration(0))
    27  	gobottest.Refute(t, yl.conf.sensors[YL40Temp].scaler, nil)
    28  	gobottest.Assert(t, yl.conf.sensors[YL40AIN2].interval, time.Duration(0))
    29  	gobottest.Refute(t, yl.conf.sensors[YL40AIN2].scaler, nil)
    30  	gobottest.Assert(t, yl.conf.sensors[YL40Poti].interval, time.Duration(0))
    31  	gobottest.Refute(t, yl.conf.sensors[YL40Poti].scaler, nil)
    32  	gobottest.Refute(t, yl.conf.aOutScaler, nil)
    33  	gobottest.Refute(t, yl.aBri, nil)
    34  	gobottest.Refute(t, yl.aTemp, nil)
    35  	gobottest.Refute(t, yl.aAIN2, nil)
    36  	gobottest.Refute(t, yl.aPoti, nil)
    37  	gobottest.Refute(t, yl.aOut, nil)
    38  }
    39  
    40  func TestYL40DriverWithYL40Interval(t *testing.T) {
    41  	// arrange, act
    42  	yl := NewYL40Driver(newI2cTestAdaptor(),
    43  		WithYL40Interval(YL40Bri, 100),
    44  		WithYL40Interval(YL40Temp, 101),
    45  		WithYL40Interval(YL40AIN2, 102),
    46  		WithYL40Interval(YL40Poti, 103),
    47  	)
    48  	// assert
    49  	gobottest.Assert(t, yl.conf.sensors[YL40Bri].interval, time.Duration(100))
    50  	gobottest.Assert(t, yl.conf.sensors[YL40Temp].interval, time.Duration(101))
    51  	gobottest.Assert(t, yl.conf.sensors[YL40AIN2].interval, time.Duration(102))
    52  	gobottest.Assert(t, yl.conf.sensors[YL40Poti].interval, time.Duration(103))
    53  }
    54  
    55  func TestYL40DriverWithYL40InputScaler(t *testing.T) {
    56  	// arrange
    57  	yl := NewYL40Driver(newI2cTestAdaptor())
    58  	f1 := func(input int) (value float64) { return 0.1 }
    59  	f2 := func(input int) (value float64) { return 0.2 }
    60  	f3 := func(input int) (value float64) { return 0.3 }
    61  	f4 := func(input int) (value float64) { return 0.4 }
    62  	//act
    63  	WithYL40InputScaler(YL40Bri, f1)(yl)
    64  	WithYL40InputScaler(YL40Temp, f2)(yl)
    65  	WithYL40InputScaler(YL40AIN2, f3)(yl)
    66  	WithYL40InputScaler(YL40Poti, f4)(yl)
    67  	// assert
    68  	gobottest.Assert(t, fEqual(yl.conf.sensors[YL40Bri].scaler, f1), true)
    69  	gobottest.Assert(t, fEqual(yl.conf.sensors[YL40Temp].scaler, f2), true)
    70  	gobottest.Assert(t, fEqual(yl.conf.sensors[YL40AIN2].scaler, f3), true)
    71  	gobottest.Assert(t, fEqual(yl.conf.sensors[YL40Poti].scaler, f4), true)
    72  }
    73  
    74  func TestYL40DriverWithYL40WithYL40OutputScaler(t *testing.T) {
    75  	// arrange
    76  	yl := NewYL40Driver(newI2cTestAdaptor())
    77  	fo := func(input float64) (value int) { return 123 }
    78  	//act
    79  	WithYL40OutputScaler(fo)(yl)
    80  	// assert
    81  	gobottest.Assert(t, fEqual(yl.conf.aOutScaler, fo), true)
    82  }
    83  
    84  func TestYL40DriverReadBrightness(t *testing.T) {
    85  	// sequence to read the input with PCF8591, see there tests
    86  	// arrange
    87  	yl, adaptor := initTestYL40DriverWithStubbedAdaptor()
    88  	adaptor.written = []byte{} // reset writes of Start() and former test
    89  	// ANAOUT was switched on by Start()
    90  	ctrlByteOn := uint8(pcf8591_ANAON) | uint8(pcf8591_ALLSINGLE) | uint8(pcf8591_CHAN0)
    91  	returnRead := []uint8{0x01, 0x02, 0x03, 73}
    92  	// scaler for brightness is 255..0 => 0..1000
    93  	want := float64(255-returnRead[3]) * 1000 / 255
    94  	// arrange reads
    95  	numCallsRead := 0
    96  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
    97  		numCallsRead++
    98  		if numCallsRead == 1 {
    99  			b = returnRead[0:len(b)]
   100  		}
   101  		if numCallsRead == 2 {
   102  			b[0] = returnRead[len(returnRead)-1]
   103  		}
   104  		return len(b), nil
   105  	}
   106  	// act
   107  	got, err := yl.ReadBrightness()
   108  	got2, err2 := yl.Brightness()
   109  	// assert
   110  	gobottest.Assert(t, err, nil)
   111  	gobottest.Assert(t, len(adaptor.written), 1)
   112  	gobottest.Assert(t, adaptor.written[0], ctrlByteOn)
   113  	gobottest.Assert(t, numCallsRead, 2)
   114  	gobottest.Assert(t, got, want)
   115  	gobottest.Assert(t, err2, nil)
   116  	gobottest.Assert(t, got2, want)
   117  }
   118  
   119  func TestYL40DriverReadTemperature(t *testing.T) {
   120  	// sequence to read the input with PCF8591, see there tests
   121  	// arrange
   122  	yl, adaptor := initTestYL40DriverWithStubbedAdaptor()
   123  	adaptor.written = []byte{} // reset writes of Start() and former test
   124  	// ANAOUT was switched on by Start()
   125  	ctrlByteOn := uint8(pcf8591_ANAON) | uint8(pcf8591_ALLSINGLE) | uint8(pcf8591_CHAN1)
   126  	returnRead := []uint8{0x01, 0x02, 0x03, 232}
   127  	// scaler for temperature is 255..0 => NTC °C, 232 relates to nearly 25°C
   128  	// in TestTemperatureSensorDriverNtcScaling we have already used this NTC values
   129  	want := 24.805280460718336
   130  	// arrange reads
   131  	numCallsRead := 0
   132  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
   133  		numCallsRead++
   134  		if numCallsRead == 1 {
   135  			b = returnRead[0:len(b)]
   136  		}
   137  		if numCallsRead == 2 {
   138  			b[0] = returnRead[len(returnRead)-1]
   139  		}
   140  		return len(b), nil
   141  	}
   142  	// act
   143  	got, err := yl.ReadTemperature()
   144  	got2, err2 := yl.Temperature()
   145  	// assert
   146  	gobottest.Assert(t, err, nil)
   147  	gobottest.Assert(t, len(adaptor.written), 1)
   148  	gobottest.Assert(t, adaptor.written[0], ctrlByteOn)
   149  	gobottest.Assert(t, numCallsRead, 2)
   150  	gobottest.Assert(t, got, want)
   151  	gobottest.Assert(t, err2, nil)
   152  	gobottest.Assert(t, got2, want)
   153  }
   154  
   155  func TestYL40DriverReadAIN2(t *testing.T) {
   156  	// sequence to read the input with PCF8591, see there tests
   157  	// arrange
   158  	yl, adaptor := initTestYL40DriverWithStubbedAdaptor()
   159  	adaptor.written = []byte{} // reset writes of Start() and former test
   160  	// ANAOUT was switched on by Start()
   161  	ctrlByteOn := uint8(pcf8591_ANAON) | uint8(pcf8591_ALLSINGLE) | uint8(pcf8591_CHAN2)
   162  	returnRead := []uint8{0x01, 0x02, 0x03, 72}
   163  	// scaler for analog input 2 is 0..255 => 0..3.3
   164  	want := float64(returnRead[3]) * 33 / 2550
   165  	// arrange reads
   166  	numCallsRead := 0
   167  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
   168  		numCallsRead++
   169  		if numCallsRead == 1 {
   170  			b = returnRead[0:len(b)]
   171  		}
   172  		if numCallsRead == 2 {
   173  			b[0] = returnRead[len(returnRead)-1]
   174  		}
   175  		return len(b), nil
   176  	}
   177  	// act
   178  	got, err := yl.ReadAIN2()
   179  	got2, err2 := yl.AIN2()
   180  	// assert
   181  	gobottest.Assert(t, err, nil)
   182  	gobottest.Assert(t, len(adaptor.written), 1)
   183  	gobottest.Assert(t, adaptor.written[0], ctrlByteOn)
   184  	gobottest.Assert(t, numCallsRead, 2)
   185  	gobottest.Assert(t, got, want)
   186  	gobottest.Assert(t, err2, nil)
   187  	gobottest.Assert(t, got2, want)
   188  }
   189  
   190  func TestYL40DriverReadPotentiometer(t *testing.T) {
   191  	// sequence to read the input with PCF8591, see there tests
   192  	// arrange
   193  	yl, adaptor := initTestYL40DriverWithStubbedAdaptor()
   194  	adaptor.written = []byte{} // reset writes of Start() and former test
   195  	// ANAOUT was switched on by Start()
   196  	ctrlByteOn := uint8(pcf8591_ANAON) | uint8(pcf8591_ALLSINGLE) | uint8(pcf8591_CHAN3)
   197  	returnRead := []uint8{0x01, 0x02, 0x03, 63}
   198  	// scaler for potentiometer is 255..0 => -100..100
   199  	want := float64(returnRead[3])*-200/255 + 100
   200  	// arrange reads
   201  	numCallsRead := 0
   202  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
   203  		numCallsRead++
   204  		if numCallsRead == 1 {
   205  			b = returnRead[0:len(b)]
   206  		}
   207  		if numCallsRead == 2 {
   208  			b[0] = returnRead[len(returnRead)-1]
   209  		}
   210  		return len(b), nil
   211  	}
   212  	// act
   213  	got, err := yl.ReadPotentiometer()
   214  	got2, err2 := yl.Potentiometer()
   215  	// assert
   216  	gobottest.Assert(t, err, nil)
   217  	gobottest.Assert(t, len(adaptor.written), 1)
   218  	gobottest.Assert(t, adaptor.written[0], ctrlByteOn)
   219  	gobottest.Assert(t, numCallsRead, 2)
   220  	gobottest.Assert(t, got, want)
   221  	gobottest.Assert(t, err2, nil)
   222  	gobottest.Assert(t, got2, want)
   223  }
   224  
   225  func TestYL40DriverAnalogWrite(t *testing.T) {
   226  	// sequence to write the output of PCF8591, see there
   227  	// arrange
   228  	pcf, adaptor := initTestYL40DriverWithStubbedAdaptor()
   229  	adaptor.written = []byte{} // reset writes of Start() and former test
   230  	ctrlByteOn := uint8(pcf8591_ANAON)
   231  	want := uint8(175)
   232  	// write is scaled by 0..3.3V => 0..255
   233  	write := float64(want) * 33 / 2550
   234  	// arrange writes
   235  	adaptor.i2cWriteImpl = func(b []byte) (int, error) {
   236  		return len(b), nil
   237  	}
   238  	// act
   239  	err := pcf.Write(write)
   240  	// assert
   241  	gobottest.Assert(t, err, nil)
   242  	gobottest.Assert(t, len(adaptor.written), 2)
   243  	gobottest.Assert(t, adaptor.written[0], ctrlByteOn)
   244  	gobottest.Assert(t, adaptor.written[1], want)
   245  }
   246  
   247  func TestYL40DriverStart(t *testing.T) {
   248  	yl := NewYL40Driver(newI2cTestAdaptor())
   249  	gobottest.Assert(t, yl.Start(), nil)
   250  }
   251  
   252  func TestYL40DriverHalt(t *testing.T) {
   253  	yl := NewYL40Driver(newI2cTestAdaptor())
   254  	gobottest.Assert(t, yl.Halt(), nil)
   255  }
   256  
   257  func fEqual(want interface{}, got interface{}) bool {
   258  	return fmt.Sprintf("%v", want) == fmt.Sprintf("%v", got)
   259  }