-
Notifications
You must be signed in to change notification settings - Fork 45
/
Adafruit_APDS9960.cpp
716 lines (632 loc) · 17.7 KB
/
Adafruit_APDS9960.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
/*!
* @file Adafruit_APDS9960.cpp
*
* @mainpage Adafruit APDS9960 Proximity, Light, RGB, and Gesture Sensor
*
* @section author Author
*
* Ladyada, Dean Miller (Adafruit Industries)
*
* @section license License
*
* Software License Agreement (BSD License)
*
* Copyright (c) 2017, Adafruit Industries
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef __AVR
#include <avr/pgmspace.h>
#elif defined(ESP8266)
#include <pgmspace.h>
#endif
#include <math.h>
#include <stdlib.h>
#include "Adafruit_APDS9960.h"
/*!
* @brief Implements missing powf function
* @param x
* Base number
* @param y
* Exponent
* @return x raised to the power of y
*/
float powf(const float x, const float y) {
return (float)(pow((double)x, (double)y));
}
Adafruit_APDS9960::~Adafruit_APDS9960() {
if (i2c_dev)
delete i2c_dev;
}
/*!
* @brief Enables the device
* Disables the device (putting it in lower power sleep mode)
* @param en
* Enable (True/False)
*/
void Adafruit_APDS9960::enable(boolean en) {
_enable.PON = en;
this->write8(APDS9960_ENABLE, _enable.get());
}
/*!
* @brief Initializes I2C and configures the sensor
* @param iTimeMS
* Integration time
* @param aGain
* Gain
* @param addr
* I2C address
* @param *theWire
* Wire object
* @return True if initialization was successful, otherwise false.
*/
boolean Adafruit_APDS9960::begin(uint16_t iTimeMS, apds9960AGain_t aGain,
uint8_t addr, TwoWire *theWire) {
if (i2c_dev)
delete i2c_dev;
i2c_dev = new Adafruit_I2CDevice(addr, theWire);
if (!i2c_dev->begin()) {
return false;
}
/* Make sure we're actually connected */
uint8_t x = read8(APDS9960_ID);
if (x != 0xAB) {
return false;
}
/* Set default integration time and gain */
setADCIntegrationTime(iTimeMS);
setADCGain(aGain);
// disable everything to start
enableGesture(false);
enableProximity(false);
enableColor(false);
disableColorInterrupt();
disableProximityInterrupt();
clearInterrupt();
/* Note: by default, the device is in power down mode on bootup */
enable(false);
delay(10);
enable(true);
delay(10);
// default to all gesture dimensions
setGestureDimensions(APDS9960_DIMENSIONS_ALL);
setGestureFIFOThreshold(APDS9960_GFIFO_4);
setGestureGain(APDS9960_GGAIN_4);
setGestureProximityThreshold(50);
resetCounts();
_gpulse.GPLEN = APDS9960_GPULSE_32US;
_gpulse.GPULSE = 9; // 10 pulses
this->write8(APDS9960_GPULSE, _gpulse.get());
return true;
}
/*!
* @brief Sets the integration time for the ADC of the APDS9960, in millis
* @param iTimeMS
* Integration time
*/
void Adafruit_APDS9960::setADCIntegrationTime(uint16_t iTimeMS) {
float temp;
// convert ms into 2.78ms increments
temp = iTimeMS;
temp /= 2.78;
temp = 256 - temp;
if (temp > 255)
temp = 255;
if (temp < 0)
temp = 0;
/* Update the timing register */
write8(APDS9960_ATIME, (uint8_t)temp);
}
/*!
* @brief Returns the integration time for the ADC of the APDS9960, in millis
* @return Integration time
*/
float Adafruit_APDS9960::getADCIntegrationTime() {
float temp;
temp = read8(APDS9960_ATIME);
// convert to units of 2.78 ms
temp = 256 - temp;
temp *= 2.78;
return temp;
}
/*!
* @brief Adjusts the color/ALS gain on the APDS9960 (adjusts the sensitivity
* to light)
* @param aGain
* Gain
*/
void Adafruit_APDS9960::setADCGain(apds9960AGain_t aGain) {
_control.AGAIN = aGain;
/* Update the timing register */
write8(APDS9960_CONTROL, _control.get());
}
/*!
* @brief Returns the ADC gain
* @return ADC gain
*/
apds9960AGain_t Adafruit_APDS9960::getADCGain() {
return (apds9960AGain_t)(read8(APDS9960_CONTROL) & 0x03);
}
/*!
* @brief Adjusts the Proximity gain on the APDS9960
* @param pGain
* Gain
*/
void Adafruit_APDS9960::setProxGain(apds9960PGain_t pGain) {
_control.PGAIN = pGain;
/* Update the timing register */
write8(APDS9960_CONTROL, _control.get());
}
/*!
* @brief Returns the Proximity gain on the APDS9960
* @return Proxmity gain
*/
apds9960PGain_t Adafruit_APDS9960::getProxGain() {
return (apds9960PGain_t)((read8(APDS9960_CONTROL) & 0x0C) >> 2);
}
/*!
* @brief Sets number of proxmity pulses
* @param pLen
* Pulse Length
* @param pulses
* Number of pulses
*/
void Adafruit_APDS9960::setProxPulse(apds9960PPulseLen_t pLen, uint8_t pulses) {
if (pulses < 1)
pulses = 1;
if (pulses > 64)
pulses = 64;
pulses--;
_ppulse.PPLEN = pLen;
_ppulse.PPULSE = pulses;
write8(APDS9960_PPULSE, _ppulse.get());
}
/*!
* @brief Enable proximity readings on APDS9960
* @param en
* Enable (True/False)
*/
void Adafruit_APDS9960::enableProximity(boolean en) {
_enable.PEN = en;
write8(APDS9960_ENABLE, _enable.get());
}
/*!
* @brief Enable proximity interrupts
*/
void Adafruit_APDS9960::enableProximityInterrupt() {
_enable.PIEN = 1;
write8(APDS9960_ENABLE, _enable.get());
clearInterrupt();
}
/*!
* @brief Disable proximity interrupts
*/
void Adafruit_APDS9960::disableProximityInterrupt() {
_enable.PIEN = 0;
write8(APDS9960_ENABLE, _enable.get());
}
/*!
* @brief Set proxmity interrupt thresholds
* @param low
* Low threshold
* @param high
* High threshold
* @param persistence
* Persistence
*/
void Adafruit_APDS9960::setProximityInterruptThreshold(uint8_t low,
uint8_t high,
uint8_t persistence) {
write8(APDS9960_PILT, low);
write8(APDS9960_PIHT, high);
if (persistence > 7)
persistence = 7;
_pers.PPERS = persistence;
write8(APDS9960_PERS, _pers.get());
}
/*!
* @brief Returns proximity interrupt status
* @return True if enabled, false otherwise.
*/
bool Adafruit_APDS9960::getProximityInterrupt() {
_status.set(this->read8(APDS9960_STATUS));
return _status.PINT;
};
/*!
* @brief Read proximity data
* @return Proximity
*/
uint8_t Adafruit_APDS9960::readProximity() { return read8(APDS9960_PDATA); }
/*!
* @brief Returns validity status of a gesture
* @return Status (True/False)
*/
bool Adafruit_APDS9960::gestureValid() {
_gstatus.set(this->read8(APDS9960_GSTATUS));
return _gstatus.GVALID;
}
/*!
* @brief Sets gesture dimensions
* @param dims
* Dimensions (APDS9960_DIMENSIONS_ALL, APDS9960_DIMENSIONS_UP_DOWN,
* APGS9960_DIMENSIONS_LEFT_RIGHT)
*/
void Adafruit_APDS9960::setGestureDimensions(uint8_t dims) {
_gconf3.GDIMS = dims;
this->write8(APDS9960_GCONF3, _gconf3.get());
}
/*!
* @brief Sets gesture FIFO Threshold
* @param thresh
* Threshold (APDS9960_GFIFO_1, APDS9960_GFIFO_4, APDS9960_GFIFO_8,
* APDS9960_GFIFO_16)
*/
void Adafruit_APDS9960::setGestureFIFOThreshold(uint8_t thresh) {
_gconf1.GFIFOTH = thresh;
this->write8(APDS9960_GCONF1, _gconf1.get());
}
/*!
* @brief Sets gesture sensor gain
* @param gain
* Gain (APDS9960_GAIN_1, APDS9960_GAIN_2, APDS9960_GAIN_4,
* APDS9960_GAIN_8)
*/
void Adafruit_APDS9960::setGestureGain(uint8_t gain) {
_gconf2.GGAIN = gain;
this->write8(APDS9960_GCONF2, _gconf2.get());
}
/*!
* @brief Sets gesture sensor threshold
* @param thresh
* Threshold
*/
void Adafruit_APDS9960::setGestureProximityThreshold(uint8_t thresh) {
this->write8(APDS9960_GPENTH, thresh);
}
/*!
* @brief Sets gesture sensor offset
* @param offset_up
* Up offset
* @param offset_down
* Down offset
* @param offset_left
* Left offset
* @param offset_right
* Right offset
*/
void Adafruit_APDS9960::setGestureOffset(uint8_t offset_up, uint8_t offset_down,
uint8_t offset_left,
uint8_t offset_right) {
this->write8(APDS9960_GOFFSET_U, offset_up);
this->write8(APDS9960_GOFFSET_D, offset_down);
this->write8(APDS9960_GOFFSET_L, offset_left);
this->write8(APDS9960_GOFFSET_R, offset_right);
}
/*!
* @brief Enable gesture readings on APDS9960
* @param en
* Enable (True/False)
*/
void Adafruit_APDS9960::enableGesture(boolean en) {
if (!en) {
_gconf4.GMODE = 0;
write8(APDS9960_GCONF4, _gconf4.get());
}
_enable.GEN = en;
write8(APDS9960_ENABLE, _enable.get());
resetCounts();
// update to enable gesture sensing without proximity sensing
if (en) { // start the gesture engine here without a prox. gesture
write8(APDS9960_GCONF4, 0x01);
}
}
/*!
* @brief Resets gesture counts
*/
void Adafruit_APDS9960::resetCounts() {
gestCnt = 0;
UCount = 0;
DCount = 0;
LCount = 0;
RCount = 0;
}
/*!
* @brief Reads gesture
* @return Received gesture (APDS9960_DOWN APDS9960_UP, APDS9960_LEFT
* APDS9960_RIGHT)
*/
uint8_t Adafruit_APDS9960::readGesture() {
uint8_t toRead;
uint8_t buf[256];
unsigned long t = 0;
uint8_t gestureReceived;
while (1) {
int up_down_diff = 0;
int left_right_diff = 0;
gestureReceived = 0;
if (!gestureValid())
return 0;
delay(30);
toRead = this->read8(APDS9960_GFLVL);
// produces sideffects needed for readGesture to work
this->read(APDS9960_GFIFO_U, buf, toRead);
if (abs((int)buf[0] - (int)buf[1]) > 13)
up_down_diff += (int)buf[0] - (int)buf[1];
if (abs((int)buf[2] - (int)buf[3]) > 13)
left_right_diff += (int)buf[2] - (int)buf[3];
if (up_down_diff != 0) {
if (up_down_diff < 0) {
if (DCount > 0) {
gestureReceived = APDS9960_UP;
} else
UCount++;
} else if (up_down_diff > 0) {
if (UCount > 0) {
gestureReceived = APDS9960_DOWN;
} else
DCount++;
}
}
if (left_right_diff != 0) {
if (left_right_diff < 0) {
if (RCount > 0) {
gestureReceived = APDS9960_LEFT;
} else
LCount++;
} else if (left_right_diff > 0) {
if (LCount > 0) {
gestureReceived = APDS9960_RIGHT;
} else
RCount++;
}
}
if (up_down_diff != 0 || left_right_diff != 0)
t = millis();
if (gestureReceived || millis() - t > 300) {
resetCounts();
return gestureReceived;
}
}
}
/*!
* @brief Set LED brightness for proximity/gesture
* @param drive
* LED Drive
* @param boost
* LED Boost
*/
void Adafruit_APDS9960::setLED(apds9960LedDrive_t drive,
apds9960LedBoost_t boost) {
// set BOOST
_config2.LED_BOOST = boost;
write8(APDS9960_CONFIG2, _config2.get());
_control.LDRIVE = drive;
write8(APDS9960_CONTROL, _control.get());
}
/*!
* @brief Enable color readings on APDS9960
* @param en
* Enable (True/False)
*/
void Adafruit_APDS9960::enableColor(boolean en) {
_enable.AEN = en;
write8(APDS9960_ENABLE, _enable.get());
}
/*!
* @brief Returns status of color data
* @return True if color data ready, False otherwise
*/
bool Adafruit_APDS9960::colorDataReady() {
_status.set(this->read8(APDS9960_STATUS));
return _status.AVALID;
}
/*!
* @brief Reads the raw red, green, blue and clear channel values
* @param *r
* Red value
* @param *g
* Green value
* @param *b
* Blue value
* @param *c
* Clear channel value
*/
void Adafruit_APDS9960::getColorData(uint16_t *r, uint16_t *g, uint16_t *b,
uint16_t *c) {
*c = read16R(APDS9960_CDATAL);
*r = read16R(APDS9960_RDATAL);
*g = read16R(APDS9960_GDATAL);
*b = read16R(APDS9960_BDATAL);
}
/*!
* @brief Converts the raw R/G/B values to color temperature in degrees Kelvin
* @param r
* Red value
* @param g
* Green value
* @param b
* Blue value
* @return Color temperature
*/
uint16_t Adafruit_APDS9960::calculateColorTemperature(uint16_t r, uint16_t g,
uint16_t b) {
float X, Y, Z; /* RGB to XYZ correlation */
float xc, yc; /* Chromaticity co-ordinates */
float n; /* McCamy's formula */
float cct;
/* 1. Map RGB values to their XYZ counterparts. */
/* Based on 6500K fluorescent, 3000K fluorescent */
/* and 60W incandescent values for a wide range. */
/* Note: Y = Illuminance or lux */
X = (-0.14282F * r) + (1.54924F * g) + (-0.95641F * b);
Y = (-0.32466F * r) + (1.57837F * g) + (-0.73191F * b);
Z = (-0.68202F * r) + (0.77073F * g) + (0.56332F * b);
/* 2. Calculate the chromaticity co-ordinates */
xc = (X) / (X + Y + Z);
yc = (Y) / (X + Y + Z);
/* 3. Use McCamy's formula to determine the CCT */
n = (xc - 0.3320F) / (0.1858F - yc);
/* Calculate the final CCT */
cct =
(449.0F * powf(n, 3)) + (3525.0F * powf(n, 2)) + (6823.3F * n) + 5520.33F;
/* Return the results in degrees Kelvin */
return (uint16_t)cct;
}
/*!
* @brief Calculate ambient light values
* @param r
* Red value
* @param g
* Green value
* @param b
* Blue value
* @return LUX value
*/
uint16_t Adafruit_APDS9960::calculateLux(uint16_t r, uint16_t g, uint16_t b) {
float illuminance;
/* This only uses RGB ... how can we integrate clear or calculate lux */
/* based exclusively on clear since this might be more reliable? */
illuminance = (-0.32466F * r) + (1.57837F * g) + (-0.73191F * b);
return (uint16_t)illuminance;
}
/*!
* @brief Enables color interrupt
*/
void Adafruit_APDS9960::enableColorInterrupt() {
_enable.AIEN = 1;
write8(APDS9960_ENABLE, _enable.get());
}
/*!
* @brief Disables color interrupt
*/
void Adafruit_APDS9960::disableColorInterrupt() {
_enable.AIEN = 0;
write8(APDS9960_ENABLE, _enable.get());
}
/*!
* @brief Clears interrupt
*/
void Adafruit_APDS9960::clearInterrupt() {
this->write(APDS9960_AICLEAR, NULL, 0);
}
/*!
* @brief Sets interrupt limits
* @param low
* Low limit
* @param high
* High limit
*/
void Adafruit_APDS9960::setIntLimits(uint16_t low, uint16_t high) {
write8(APDS9960_AILTIL, low & 0xFF);
write8(APDS9960_AILTH, low >> 8);
write8(APDS9960_AIHTL, high & 0xFF);
write8(APDS9960_AIHTH, high >> 8);
}
/*!
* @brief Writes specified value to given register
* @param reg
* Register to write to
* @param value
* Value to write
*/
void Adafruit_APDS9960::write8(byte reg, byte value) {
this->write(reg, &value, 1);
}
/*!
* @brief Reads 8 bits from specified register
* @param reg
* Register to write to
* @return Value in register
*/
uint8_t Adafruit_APDS9960::read8(byte reg) {
uint8_t ret;
this->read(reg, &ret, 1);
return ret;
}
/*!
* @brief Reads 32 bits from specified register
* @param reg
* Register to write to
* @return Value in register
*/
uint32_t Adafruit_APDS9960::read32(uint8_t reg) {
uint8_t ret[4];
uint32_t ret32;
this->read(reg, ret, 4);
ret32 = ret[3];
ret32 |= (uint32_t)ret[2] << 8;
ret32 |= (uint32_t)ret[1] << 16;
ret32 |= (uint32_t)ret[0] << 24;
return ret32;
}
/*!
* @brief Reads 16 bites from specified register
* @param reg
* Register to write to
* @return Value in register
*/
uint16_t Adafruit_APDS9960::read16(uint8_t reg) {
uint8_t ret[2];
this->read(reg, ret, 2);
return (ret[0] << 8) | ret[1];
}
/*!
* @brief Reads 16 bites from specified register
* @param reg
* Register to write to
* @return Value in register
*/
uint16_t Adafruit_APDS9960::read16R(uint8_t reg) {
uint8_t ret[2];
this->read(reg, ret, 2);
return (ret[1] << 8) | ret[0];
}
/*!
* @brief Reads num bytes from specified register into a given buffer
* @param reg
* Register
* @param *buf
* Buffer
* @param num
* Number of bytes
* @return Position after reading
*/
uint8_t Adafruit_APDS9960::read(uint8_t reg, uint8_t *buf, uint8_t num) {
buf[0] = reg;
i2c_dev->write_then_read(buf, 1, buf, num);
return num;
}
/*!
* @brief Writes num bytes from specified buffer into a given register
* @param reg
* Register
* @param *buf
* Buffer
* @param num
* Number of bytes
*/
void Adafruit_APDS9960::write(uint8_t reg, uint8_t *buf, uint8_t num) {
uint8_t prefix[1] = {reg};
i2c_dev->write(buf, num, true, prefix, 1);
}