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