gobot.io/x/gobot/v2@v2.1.0/drivers/i2c/tsl2561_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 = (*TSL2561Driver)(nil) 17 18 func testIDReader(b []byte) (int, error) { 19 buf := new(bytes.Buffer) 20 // Mock device responding 0xA 21 binary.Write(buf, binary.LittleEndian, uint8(0x0A)) 22 copy(b, buf.Bytes()) 23 return buf.Len(), nil 24 } 25 26 func initTestTSL2561Driver() (*TSL2561Driver, *i2cTestAdaptor) { 27 a := newI2cTestAdaptor() 28 d := NewTSL2561Driver(a) 29 a.i2cReadImpl = testIDReader 30 if err := d.Start(); err != nil { 31 panic(err) 32 } 33 return d, a 34 } 35 36 func TestNewTSL2561Driver(t *testing.T) { 37 var di interface{} = NewTSL2561Driver(newI2cTestAdaptor()) 38 d, ok := di.(*TSL2561Driver) 39 if !ok { 40 t.Errorf("NewTSL2561Driver() should have returned a *TSL2561Driver") 41 } 42 gobottest.Refute(t, d.Driver, nil) 43 gobottest.Assert(t, strings.HasPrefix(d.Name(), "TSL2561"), true) 44 gobottest.Assert(t, d.defaultAddress, 0x39) 45 gobottest.Assert(t, d.autoGain, false) 46 gobottest.Assert(t, d.gain, TSL2561Gain(0)) 47 gobottest.Assert(t, d.integrationTime, TSL2561IntegrationTime(2)) 48 } 49 50 func TestTSL2561DriverOptions(t *testing.T) { 51 // This is a general test, that options are applied in constructor by using the common WithBus() option and 52 // least one of this driver. Further tests for options can also be done by call of "WithOption(val)(d)". 53 d := NewTSL2561Driver(newI2cTestAdaptor(), WithBus(2), WithTSL2561AutoGain) 54 gobottest.Assert(t, d.GetBusOrDefault(1), 2) 55 gobottest.Assert(t, d.autoGain, true) 56 } 57 58 func TestTSL2561DriverStart(t *testing.T) { 59 a := newI2cTestAdaptor() 60 d := NewTSL2561Driver(a) 61 a.i2cReadImpl = testIDReader 62 63 gobottest.Assert(t, d.Start(), nil) 64 } 65 66 func TestTSL2561DriverStartNotFound(t *testing.T) { 67 a := newI2cTestAdaptor() 68 d := NewTSL2561Driver(a) 69 a.i2cReadImpl = func(b []byte) (int, error) { 70 buf := new(bytes.Buffer) 71 buf.Write([]byte{1}) 72 copy(b, buf.Bytes()) 73 return buf.Len(), nil 74 } 75 gobottest.Assert(t, d.Start(), errors.New("TSL2561 device not found (0x1)")) 76 } 77 78 func TestTSL2561DriverHalt(t *testing.T) { 79 d, _ := initTestTSL2561Driver() 80 gobottest.Assert(t, d.Halt(), nil) 81 } 82 83 func TestTSL2561DriverRead16(t *testing.T) { 84 d, a := initTestTSL2561Driver() 85 a.i2cReadImpl = testIDReader 86 a.i2cReadImpl = func(b []byte) (int, error) { 87 buf := new(bytes.Buffer) 88 // send low 89 binary.Write(buf, binary.LittleEndian, uint8(0xEA)) 90 // send high 91 binary.Write(buf, binary.LittleEndian, uint8(0xAE)) 92 copy(b, buf.Bytes()) 93 return buf.Len(), nil 94 } 95 val, err := d.connection.ReadWordData(1) 96 gobottest.Assert(t, err, nil) 97 gobottest.Assert(t, val, uint16(0xAEEA)) 98 } 99 100 func TestTSL2561DriverValidOptions(t *testing.T) { 101 a := newI2cTestAdaptor() 102 103 d := NewTSL2561Driver(a, 104 WithTSL2561IntegrationTime101MS, 105 WithAddress(TSL2561AddressLow), 106 WithTSL2561AutoGain) 107 108 gobottest.Refute(t, d, nil) 109 gobottest.Assert(t, d.autoGain, true) 110 gobottest.Assert(t, d.integrationTime, TSL2561IntegrationTime101MS) 111 } 112 113 func TestTSL2561DriverMoreOptions(t *testing.T) { 114 a := newI2cTestAdaptor() 115 116 d := NewTSL2561Driver(a, 117 WithTSL2561IntegrationTime101MS, 118 WithAddress(TSL2561AddressLow), 119 WithTSL2561Gain16X) 120 121 gobottest.Refute(t, d, nil) 122 gobottest.Assert(t, d.autoGain, false) 123 gobottest.Assert(t, d.gain, TSL2561Gain(TSL2561Gain16X)) 124 } 125 126 func TestTSL2561DriverEvenMoreOptions(t *testing.T) { 127 a := newI2cTestAdaptor() 128 129 d := NewTSL2561Driver(a, 130 WithTSL2561IntegrationTime13MS, 131 WithAddress(TSL2561AddressLow), 132 WithTSL2561Gain1X) 133 134 gobottest.Refute(t, d, nil) 135 gobottest.Assert(t, d.autoGain, false) 136 gobottest.Assert(t, d.gain, TSL2561Gain(TSL2561Gain1X)) 137 gobottest.Assert(t, d.integrationTime, TSL2561IntegrationTime13MS) 138 } 139 140 func TestTSL2561DriverYetEvenMoreOptions(t *testing.T) { 141 a := newI2cTestAdaptor() 142 143 d := NewTSL2561Driver(a, 144 WithTSL2561IntegrationTime402MS, 145 WithAddress(TSL2561AddressLow), 146 WithTSL2561AutoGain) 147 148 gobottest.Refute(t, d, nil) 149 gobottest.Assert(t, d.autoGain, true) 150 gobottest.Assert(t, d.integrationTime, TSL2561IntegrationTime402MS) 151 } 152 153 func TestTSL2561DriverGetDataWriteError(t *testing.T) { 154 d, a := initTestTSL2561Driver() 155 a.i2cWriteImpl = func([]byte) (int, error) { 156 return 0, errors.New("write error") 157 } 158 159 _, _, err := d.getData() 160 gobottest.Assert(t, err, errors.New("write error")) 161 } 162 163 func TestTSL2561DriverGetDataReadError(t *testing.T) { 164 d, a := initTestTSL2561Driver() 165 a.i2cReadImpl = func([]byte) (int, error) { 166 return 0, errors.New("read error") 167 } 168 169 _, _, err := d.getData() 170 gobottest.Assert(t, err, errors.New("read error")) 171 } 172 173 func TestTSL2561DriverGetLuminocity(t *testing.T) { 174 d, a := initTestTSL2561Driver() 175 // TODO: obtain real sensor data here for testing 176 a.i2cReadImpl = func(b []byte) (int, error) { 177 buf := new(bytes.Buffer) 178 buf.Write([]byte{77, 48}) 179 copy(b, buf.Bytes()) 180 return buf.Len(), nil 181 } 182 bb, ir, err := d.GetLuminocity() 183 gobottest.Assert(t, err, nil) 184 gobottest.Assert(t, bb, uint16(12365)) 185 gobottest.Assert(t, ir, uint16(12365)) 186 gobottest.Assert(t, d.CalculateLux(bb, ir), uint32(72)) 187 } 188 189 func TestTSL2561DriverGetLuminocityAutoGain(t *testing.T) { 190 a := newI2cTestAdaptor() 191 d := NewTSL2561Driver(a, 192 WithTSL2561IntegrationTime402MS, 193 WithAddress(TSL2561AddressLow), 194 WithTSL2561AutoGain) 195 // TODO: obtain real sensor data here for testing 196 a.i2cReadImpl = func(b []byte) (int, error) { 197 buf := new(bytes.Buffer) 198 buf.Write([]byte{77, 48}) 199 copy(b, buf.Bytes()) 200 return buf.Len(), nil 201 } 202 203 d.Start() 204 bb, ir, err := d.GetLuminocity() 205 gobottest.Assert(t, err, nil) 206 gobottest.Assert(t, bb, uint16(12365)) 207 gobottest.Assert(t, ir, uint16(12365)) 208 gobottest.Assert(t, d.CalculateLux(bb, ir), uint32(72)) 209 } 210 211 func TestTSL2561SetIntegrationTimeError(t *testing.T) { 212 d, a := initTestTSL2561Driver() 213 a.i2cWriteImpl = func([]byte) (int, error) { 214 return 0, errors.New("write error") 215 } 216 gobottest.Assert(t, d.SetIntegrationTime(TSL2561IntegrationTime101MS), errors.New("write error")) 217 } 218 219 func TestTSL2561SetGainError(t *testing.T) { 220 d, a := initTestTSL2561Driver() 221 a.i2cWriteImpl = func([]byte) (int, error) { 222 return 0, errors.New("write error") 223 } 224 gobottest.Assert(t, d.SetGain(TSL2561Gain16X), errors.New("write error")) 225 } 226 227 func TestTSL2561getHiLo13MS(t *testing.T) { 228 a := newI2cTestAdaptor() 229 d := NewTSL2561Driver(a, 230 WithTSL2561IntegrationTime13MS, 231 WithTSL2561AutoGain) 232 233 hi, lo := d.getHiLo() 234 gobottest.Assert(t, hi, uint16(tsl2561AgcTHi13MS)) 235 gobottest.Assert(t, lo, uint16(tsl2561AgcTLo13MS)) 236 } 237 238 func TestTSL2561getHiLo101MS(t *testing.T) { 239 a := newI2cTestAdaptor() 240 d := NewTSL2561Driver(a, 241 WithTSL2561IntegrationTime101MS, 242 WithTSL2561AutoGain) 243 244 hi, lo := d.getHiLo() 245 gobottest.Assert(t, hi, uint16(tsl2561AgcTHi101MS)) 246 gobottest.Assert(t, lo, uint16(tsl2561AgcTLo101MS)) 247 } 248 249 func TestTSL2561getHiLo402MS(t *testing.T) { 250 a := newI2cTestAdaptor() 251 d := NewTSL2561Driver(a, 252 WithTSL2561IntegrationTime402MS, 253 WithTSL2561AutoGain) 254 255 hi, lo := d.getHiLo() 256 gobottest.Assert(t, hi, uint16(tsl2561AgcTHi402MS)) 257 gobottest.Assert(t, lo, uint16(tsl2561AgcTLo402MS)) 258 } 259 260 func TestTSL2561getClipScaling13MS(t *testing.T) { 261 a := newI2cTestAdaptor() 262 d := NewTSL2561Driver(a, 263 WithTSL2561IntegrationTime13MS, 264 WithTSL2561AutoGain) 265 266 c, s := d.getClipScaling() 267 d.waitForADC() 268 gobottest.Assert(t, c, uint16(tsl2561Clipping13MS)) 269 gobottest.Assert(t, s, uint32(tsl2561LuxCHScaleTInt0)) 270 } 271 272 func TestTSL2561getClipScaling101MS(t *testing.T) { 273 a := newI2cTestAdaptor() 274 d := NewTSL2561Driver(a, 275 WithTSL2561IntegrationTime101MS, 276 WithTSL2561AutoGain) 277 278 c, s := d.getClipScaling() 279 d.waitForADC() 280 gobottest.Assert(t, c, uint16(tsl2561Clipping101MS)) 281 gobottest.Assert(t, s, uint32(tsl2561LuxChScaleTInt1)) 282 } 283 284 func TestTSL2561getClipScaling402MS(t *testing.T) { 285 a := newI2cTestAdaptor() 286 d := NewTSL2561Driver(a, 287 WithTSL2561IntegrationTime402MS, 288 WithTSL2561AutoGain) 289 290 c, s := d.getClipScaling() 291 d.waitForADC() 292 gobottest.Assert(t, c, uint16(tsl2561Clipping402MS)) 293 gobottest.Assert(t, s, uint32(1<<tsl2561LuxChScale)) 294 } 295 296 func TestTSL2561getBM(t *testing.T) { 297 a := newI2cTestAdaptor() 298 d := NewTSL2561Driver(a, 299 WithTSL2561IntegrationTime13MS, 300 WithTSL2561AutoGain) 301 302 b, m := d.getBM(tsl2561LuxK1T) 303 gobottest.Assert(t, b, uint32(tsl2561LuxB1T)) 304 gobottest.Assert(t, m, uint32(tsl2561LuxM1T)) 305 306 b, m = d.getBM(tsl2561LuxK2T) 307 gobottest.Assert(t, b, uint32(tsl2561LuxB2T)) 308 gobottest.Assert(t, m, uint32(tsl2561LuxM2T)) 309 310 b, m = d.getBM(tsl2561LuxK3T) 311 gobottest.Assert(t, b, uint32(tsl2561LuxB3T)) 312 gobottest.Assert(t, m, uint32(tsl2561LuxM3T)) 313 314 b, m = d.getBM(tsl2561LuxK4T) 315 gobottest.Assert(t, b, uint32(tsl2561LuxB4T)) 316 gobottest.Assert(t, m, uint32(tsl2561LuxM4T)) 317 318 b, m = d.getBM(tsl2561LuxK5T) 319 gobottest.Assert(t, b, uint32(tsl2561LuxB5T)) 320 gobottest.Assert(t, m, uint32(tsl2561LuxM5T)) 321 322 b, m = d.getBM(tsl2561LuxK6T) 323 gobottest.Assert(t, b, uint32(tsl2561LuxB6T)) 324 gobottest.Assert(t, m, uint32(tsl2561LuxM6T)) 325 326 b, m = d.getBM(tsl2561LuxK7T) 327 gobottest.Assert(t, b, uint32(tsl2561LuxB7T)) 328 gobottest.Assert(t, m, uint32(tsl2561LuxM7T)) 329 330 b, m = d.getBM(tsl2561LuxK8T + 1) 331 gobottest.Assert(t, b, uint32(tsl2561LuxB8T)) 332 gobottest.Assert(t, m, uint32(tsl2561LuxM8T)) 333 }