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