@@ -14,23 +14,30 @@ bool AS726X::begin(TwoWire &wirePort, uint8_t gain, uint8_t measurementMode)
14
14
_sensorVersion = virtualReadRegister (AS726x_HW_VERSION);
15
15
16
16
// HW version for AS7262, AS7263 and AS7261
17
- if (_sensorVersion != 0x3E && _sensorVersion != 0x3F && _sensorVersion != 0x40 )
17
+ if (_sensorVersion != SENSORTYPE_AS7261 &&
18
+ _sensorVersion != SENSORTYPE_AS7262 &&
19
+ _sensorVersion != SENSORTYPE_AS7263)
18
20
{
19
21
return false ;
20
22
}
21
23
22
- setBulbCurrent ( 0b00 ); // Set to 12.5mA (minimum)
23
- disableBulb (); // Turn off to avoid heating the sensor
24
+ // Set to 12.5mA (minimum)
25
+ if ( setBulbCurrent ( 0b00 )) return false ;
24
26
25
- setIndicatorCurrent (0b11 ); // Set to 8mA (maximum)
26
- disableIndicator (); // Turn off lights to save power
27
+ if (disableBulb ()) return false ; // Turn off to avoid heating the sensor
27
28
28
- setIntegrationTime (50 ); // 50 * 2.8ms = 140ms. 0 to 255 is valid.
29
- // If you use Mode 2 or 3 (all the colors) then integration time is double. 140*2 = 280ms between readings.
29
+ if (setIndicatorCurrent (0b11 )) return false ; // Set to 8mA (maximum)
30
30
31
- setGain (gain) ; // Set gain to 64x
31
+ if ( disableIndicator ()) return false ; // Turn off lights to save power
32
32
33
- setMeasurementMode (measurementMode); // One-shot mode
33
+ if (setIntegrationTime (50 )) return false ; // 50 * 2.8ms = 140ms. 0 to 255 is valid.
34
+
35
+ // If you use Mode 2 or 3 (all the colors) then integration time is double.
36
+ // 140*2 = 280ms between readings.
37
+
38
+ if (setGain (gain)) return false ; // Set gain to 64x
39
+
40
+ if (setMeasurementMode (measurementMode)) return false ; // One-shot mode
34
41
35
42
return true ;
36
43
}
@@ -45,15 +52,15 @@ uint8_t AS726X::getVersion()
45
52
// Mode 1: Continuous reading of GYOR (7262) / RTUX (7263)
46
53
// Mode 2: Continuous reading of all channels (power-on default)
47
54
// Mode 3: One-shot reading of all channels
48
- void AS726X::setMeasurementMode (uint8_t mode)
55
+ int AS726X::setMeasurementMode (uint8_t mode)
49
56
{
50
57
if (mode > 0b11 ) mode = 0b11 ;
51
58
52
59
// Read, mask/set, write
53
60
uint8_t value = virtualReadRegister (AS726x_CONTROL_SETUP); // Read
54
61
value &= 0b11110011 ; // Clear BANK bits
55
62
value |= (mode << 2 ); // Set BANK bits with user's choice
56
- virtualWriteRegister (AS726x_CONTROL_SETUP, value); // Write
63
+ return virtualWriteRegister (AS726x_CONTROL_SETUP, value); // Write
57
64
}
58
65
59
66
uint8_t AS726X::getMeasurementMode ()
@@ -67,15 +74,15 @@ uint8_t AS726X::getMeasurementMode()
67
74
// Gain 1: 3.7x
68
75
// Gain 2: 16x
69
76
// Gain 3: 64x
70
- void AS726X::setGain (uint8_t gain)
77
+ int AS726X::setGain (uint8_t gain)
71
78
{
72
79
if (gain > 0b11 ) gain = 0b11 ;
73
80
74
81
// Read, mask/set, write
75
82
uint8_t value = virtualReadRegister (AS726x_CONTROL_SETUP); // Read
76
83
value &= 0b11001111 ; // Clear GAIN bits
77
84
value |= (gain << 4 ); // Set GAIN bits with user's choice
78
- virtualWriteRegister (AS726x_CONTROL_SETUP, value); // Write
85
+ return virtualWriteRegister (AS726x_CONTROL_SETUP, value); // Write
79
86
}
80
87
81
88
uint8_t AS726X::getGain ()
@@ -87,9 +94,9 @@ uint8_t AS726X::getGain()
87
94
// Sets the integration value
88
95
// Give this function a uint8_t from 0 to 255.
89
96
// Time will be 2.8ms * [integration value]
90
- void AS726X::setIntegrationTime (uint8_t integrationValue)
97
+ int AS726X::setIntegrationTime (uint8_t integrationValue)
91
98
{
92
- virtualWriteRegister (AS726x_INT_T, integrationValue); // Write
99
+ return virtualWriteRegister (AS726x_INT_T, integrationValue); // Write
93
100
}
94
101
95
102
uint8_t AS726X::getIntegrationTime ()
@@ -98,49 +105,59 @@ uint8_t AS726X::getIntegrationTime()
98
105
return value;
99
106
}
100
107
101
- void AS726X::enableInterrupt ()
108
+ int AS726X::enableInterrupt ()
102
109
{
103
110
// Read, mask/set, write
104
111
uint8_t value = virtualReadRegister (AS726x_CONTROL_SETUP); // Read
105
112
value |= 0b01000000 ; // Set INT bit
106
- virtualWriteRegister (AS726x_CONTROL_SETUP, value); // Write
113
+ return virtualWriteRegister (AS726x_CONTROL_SETUP, value); // Write
107
114
}
108
115
109
116
// Disables the interrupt pin
110
- void AS726X::disableInterrupt ()
117
+ int AS726X::disableInterrupt ()
111
118
{
112
119
// Read, mask/set, write
113
120
uint8_t value = virtualReadRegister (AS726x_CONTROL_SETUP); // Read
114
121
value &= 0b10111111 ; // Clear INT bit
115
- virtualWriteRegister (AS726x_CONTROL_SETUP, value); // Write
122
+ return virtualWriteRegister (AS726x_CONTROL_SETUP, value); // Write
116
123
}
117
124
118
125
// Tells IC to take measurements and polls for data ready flag
119
- void AS726X::takeMeasurements ()
126
+ int AS726X::takeMeasurements ()
120
127
{
121
- clearDataAvailable (); // Clear DATA_RDY flag when using Mode 3
128
+ // Clear DATA_RDY flag when using Mode 3
129
+ if (clearDataAvailable ()) return -1 ;
130
+
131
+ // Goto mode 3 for one shot measurement of all channels
132
+ if (setMeasurementMode (3 )) return -1 ;
122
133
123
- // Goto mode 3 for one shot measurement of all channels
124
- setMeasurementMode (3 );
134
+ uint32_t timeout = millis () + TIMEOUT;
125
135
126
136
// Wait for data to be ready
127
- while (dataAvailable () == false ) delay (POLLING_DELAY);
137
+ while (dataAvailable () == false )
138
+ {
139
+ delay (POLLING_DELAY);
140
+ if (millis () > timeout) return -1 ;
141
+ }
128
142
129
143
// Readings can now be accessed via getViolet(), getBlue(), etc
144
+ return 0 ;
130
145
}
131
146
132
147
// Turns on bulb, takes measurements, turns off bulb
133
- void AS726X::takeMeasurementsWithBulb ()
148
+ int AS726X::takeMeasurementsWithBulb ()
134
149
{
135
150
// enableIndicator(); //Tell the world we are taking a reading.
136
151
// The indicator LED is red and may corrupt the readings
137
152
138
- enableBulb (); // Turn on bulb to take measurement
153
+ if (enableBulb ()) return -1 ; // Turn on bulb to take measurement
154
+
155
+ if (takeMeasurements ()) return -1 ;
139
156
140
- takeMeasurements ();
157
+ if (disableBulb ()) return -1 ; // Turn off bulb to avoid heating sensor
158
+ // disableIndicator();
141
159
142
- disableBulb (); // Turn off bulb to avoid heating sensor
143
- // disableIndicator();
160
+ return 0 ;
144
161
}
145
162
146
163
// Get the various color readings
@@ -239,74 +256,74 @@ bool AS726X::dataAvailable()
239
256
240
257
// Clears the DRDY flag
241
258
// Normally this should clear when data registers are read
242
- void AS726X::clearDataAvailable ()
259
+ int AS726X::clearDataAvailable ()
243
260
{
244
261
uint8_t value = virtualReadRegister (AS726x_CONTROL_SETUP);
245
262
value &= ~(1 << 1 ); // Set the DATA_RDY bit
246
- virtualWriteRegister (AS726x_CONTROL_SETUP, value);
263
+ return virtualWriteRegister (AS726x_CONTROL_SETUP, value);
247
264
}
248
265
249
266
// Enable the onboard indicator LED
250
- void AS726X::enableIndicator ()
267
+ int AS726X::enableIndicator ()
251
268
{
252
269
// Read, mask/set, write
253
270
uint8_t value = virtualReadRegister (AS726x_LED_CONTROL);
254
271
value |= (1 << 0 ); // Set the bit
255
- virtualWriteRegister (AS726x_LED_CONTROL, value);
272
+ return virtualWriteRegister (AS726x_LED_CONTROL, value);
256
273
}
257
274
258
275
// Disable the onboard indicator LED
259
- void AS726X::disableIndicator ()
276
+ int AS726X::disableIndicator ()
260
277
{
261
278
// Read, mask/set, write
262
279
uint8_t value = virtualReadRegister (AS726x_LED_CONTROL);
263
280
value &= ~(1 << 0 ); // Clear the bit
264
- virtualWriteRegister (AS726x_LED_CONTROL, value);
281
+ return virtualWriteRegister (AS726x_LED_CONTROL, value);
265
282
}
266
283
267
284
// Set the current limit of onboard LED. Default is max 8mA = 0b11.
268
- void AS726X::setIndicatorCurrent (uint8_t current)
285
+ int AS726X::setIndicatorCurrent (uint8_t current)
269
286
{
270
287
if (current > 0b11 ) current = 0b11 ;
271
288
// Read, mask/set, write
272
289
uint8_t value = virtualReadRegister (AS726x_LED_CONTROL); // Read
273
290
value &= 0b11111001 ; // Clear ICL_IND bits
274
291
value |= (current << 1 ); // Set ICL_IND bits with user's choice
275
- virtualWriteRegister (AS726x_LED_CONTROL, value); // Write
292
+ return virtualWriteRegister (AS726x_LED_CONTROL, value); // Write
276
293
}
277
294
278
295
// Enable the onboard 5700k or external incandescent bulb
279
- void AS726X::enableBulb ()
296
+ int AS726X::enableBulb ()
280
297
{
281
298
// Read, mask/set, write
282
299
uint8_t value = virtualReadRegister (AS726x_LED_CONTROL);
283
300
value |= (1 << 3 ); // Set the bit
284
- virtualWriteRegister (AS726x_LED_CONTROL, value);
301
+ return virtualWriteRegister (AS726x_LED_CONTROL, value);
285
302
}
286
303
287
304
// Disable the onboard 5700k or external incandescent bulb
288
- void AS726X::disableBulb ()
305
+ int AS726X::disableBulb ()
289
306
{
290
307
// Read, mask/set, write
291
308
uint8_t value = virtualReadRegister (AS726x_LED_CONTROL);
292
309
value &= ~(1 << 3 ); // Clear the bit
293
- virtualWriteRegister (AS726x_LED_CONTROL, value);
310
+ return virtualWriteRegister (AS726x_LED_CONTROL, value);
294
311
}
295
312
296
313
// Set the current limit of bulb/LED.
297
314
// Current 0: 12.5mA
298
315
// Current 1: 25mA
299
316
// Current 2: 50mA
300
317
// Current 3: 100mA
301
- void AS726X::setBulbCurrent (uint8_t current)
318
+ int AS726X::setBulbCurrent (uint8_t current)
302
319
{
303
320
if (current > 0b11 ) current = 0b11 ; // Limit to two bits
304
321
305
322
// Read, mask/set, write
306
323
uint8_t value = virtualReadRegister (AS726x_LED_CONTROL); // Read
307
324
value &= 0b11001111 ; // Clear ICL_DRV bits
308
325
value |= (current << 4 ); // Set ICL_DRV bits with user's choice
309
- virtualWriteRegister (AS726x_LED_CONTROL, value); // Write
326
+ return virtualWriteRegister (AS726x_LED_CONTROL, value); // Write
310
327
}
311
328
312
329
// Returns the temperature in C
@@ -326,18 +343,19 @@ float AS726X::getTemperatureF()
326
343
327
344
// Does a soft reset
328
345
// Give sensor at least 1000ms to reset
329
- void AS726X::softReset ()
346
+ int AS726X::softReset ()
330
347
{
331
348
// Read, mask/set, write
332
349
uint8_t value = virtualReadRegister (AS726x_CONTROL_SETUP); // Read
333
350
value |= (1 << 7 ); // Set RST bit
334
- virtualWriteRegister (AS726x_CONTROL_SETUP, value); // Write
351
+ return virtualWriteRegister (AS726x_CONTROL_SETUP, value); // Write
335
352
}
336
353
337
354
// Read a virtual register from the AS726x
338
355
uint8_t AS726X::virtualReadRegister (uint8_t virtualAddr)
339
356
{
340
357
uint8_t status;
358
+ uint8_t retries = 0 ;
341
359
342
360
// Do a prelim check of the read register
343
361
status = readRegister (AS72XX_SLAVE_STATUS_REG);
@@ -351,75 +369,98 @@ uint8_t AS726X::virtualReadRegister(uint8_t virtualAddr)
351
369
while (1 )
352
370
{
353
371
status = readRegister (AS72XX_SLAVE_STATUS_REG);
372
+ if (status == 0xFF ) return status;
354
373
if ((status & AS72XX_SLAVE_TX_VALID) == 0 ) break ; // If TX bit is clear, it is ok to write
355
374
delay (POLLING_DELAY);
375
+ if (retries++ > retries) return 0xFF ;
356
376
}
357
377
358
378
// Send the virtual register address (bit 7 should be 0 to indicate we are reading a register).
359
- writeRegister (AS72XX_SLAVE_WRITE_REG, virtualAddr);
379
+ if (writeRegister (AS72XX_SLAVE_WRITE_REG, virtualAddr)) return 0xFF ;
380
+
381
+ retries = 0 ;
360
382
361
383
// Wait for READ flag to be set
362
384
while (1 )
363
385
{
364
386
status = readRegister (AS72XX_SLAVE_STATUS_REG);
387
+ if (status == 0xFF ) return status;
365
388
if ((status & AS72XX_SLAVE_RX_VALID) != 0 ) break ; // Read data is ready.
366
389
delay (POLLING_DELAY);
390
+ if (retries++ > retries) return 0xFF ;
367
391
}
368
392
369
393
uint8_t incoming = readRegister (AS72XX_SLAVE_READ_REG);
370
394
return (incoming);
371
395
}
372
396
373
397
// Write to a virtual register in the AS726x
374
- void AS726X::virtualWriteRegister (uint8_t virtualAddr, uint8_t dataToWrite)
398
+ int AS726X::virtualWriteRegister (uint8_t virtualAddr, uint8_t dataToWrite)
375
399
{
376
400
uint8_t status;
401
+ uint8_t retries = 0 ;
377
402
378
403
// Wait for WRITE register to be empty
379
404
while (1 )
380
405
{
381
406
status = readRegister (AS72XX_SLAVE_STATUS_REG);
407
+ if (status == 0xFF ) return -1 ;
382
408
if ((status & AS72XX_SLAVE_TX_VALID) == 0 ) break ; // No inbound TX pending at slave. Okay to write now.
383
409
delay (POLLING_DELAY);
410
+ if (retries++ > retries) return -1 ;
384
411
}
385
412
386
413
// Send the virtual register address (setting bit 7 to indicate we are writing to a register).
387
414
writeRegister (AS72XX_SLAVE_WRITE_REG, (virtualAddr | 0x80 ));
388
415
416
+ retries = 0 ;
417
+
389
418
// Wait for WRITE register to be empty
390
419
while (1 )
391
420
{
392
421
status = readRegister (AS72XX_SLAVE_STATUS_REG);
422
+ if (status == 0xFF ) return -1 ;
393
423
if ((status & AS72XX_SLAVE_TX_VALID) == 0 ) break ; // No inbound TX pending at slave. Okay to write now.
394
424
delay (POLLING_DELAY);
425
+ if (retries++ > retries) return -1 ;
395
426
}
396
427
397
428
// Send the data to complete the operation.
398
429
writeRegister (AS72XX_SLAVE_WRITE_REG, dataToWrite);
430
+
431
+ return 0 ;
399
432
}
400
433
401
434
// Reads from a give location from the AS726x
402
435
uint8_t AS726X::readRegister (uint8_t addr)
403
436
{
437
+ uint8_t err = 0xFF ;
438
+
404
439
_i2cPort->beginTransmission (AS726X_ADDR);
405
- _i2cPort->write (addr);
406
- _i2cPort->endTransmission ();
440
+ if ( _i2cPort->write (addr) == 0 ) return err ;
441
+ if ( _i2cPort->endTransmission ()) return err ;
407
442
408
- _i2cPort->requestFrom (AS726X_ADDR, 1 );
443
+ if ( _i2cPort->requestFrom (AS726X_ADDR, 1 ) == 0 ) return err ;
409
444
if (_i2cPort->available ()) {
410
445
return (_i2cPort->read ());
411
446
}
412
447
else {
413
448
Serial.println (" I2C Error" );
414
- return ( 0xFF ) ; // Error
449
+ return err ; // Error
415
450
}
451
+
452
+ return 0 ;
416
453
}
417
454
418
455
// Write a value to a spot in the AS726x
419
- void AS726X::writeRegister (uint8_t addr, uint8_t val)
456
+ int AS726X::writeRegister (uint8_t addr, uint8_t val)
420
457
{
458
+ uint8_t err = 0xFF ;
459
+
421
460
_i2cPort->beginTransmission (AS726X_ADDR);
422
- _i2cPort->write (addr);
423
- _i2cPort->write (val);
424
- _i2cPort->endTransmission ();
461
+ if (_i2cPort->write (addr) == 0 ) return (int )err;
462
+ if (_i2cPort->write (val) == 0 ) return (int )err;
463
+ if (_i2cPort->endTransmission ()) return (int )err;
464
+
465
+ return 0 ;
425
466
}
0 commit comments