-
Notifications
You must be signed in to change notification settings - Fork 0
/
xsns_27_apds9960.ino
2073 lines (1756 loc) · 56.6 KB
/
xsns_27_apds9960.ino
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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
xsns_27_apds9960.ino - Support for I2C APDS9960 Proximity Sensor for Sonoff-Tasmota
Copyright (C) 2019 Shawn Hymel/Sparkfun and Theo Arends
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- 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.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 OR CONTRIBUTORS 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 USE_I2C
#ifdef USE_APDS9960
/*********************************************************************************************\
* APDS9960 - Digital Proximity Ambient Light RGB and Gesture Sensor
*
* Source: Shawn Hymel (SparkFun Electronics)
* Adaption for TASMOTA: Christian Baars
*
* I2C Address: 0x39
\*********************************************************************************************/
#define XSNS_27 27
#if defined(USE_SHT) || defined(USE_VEML6070) || defined(USE_TSL2561)
#warning **** Turned off conflicting drivers SHT and VEML6070 ****
#ifdef USE_SHT
#undef USE_SHT // SHT-Driver blocks gesture sensor
#endif
#ifdef USE_VEML6070
#undef USE_VEML6070 // address conflict on the I2C-bus
#endif
#ifdef USE_TSL2561
#undef USE_TSL2561 // possible address conflict on the I2C-bus
#endif
#endif
#define APDS9960_I2C_ADDR 0x39
#define APDS9960_CHIPID_1 0xAB
#define APDS9960_CHIPID_2 0x9C
#define APDS9930_CHIPID_1 0x12 // we will check, if someone got an incorrect sensor
#define APDS9930_CHIPID_2 0x39 // there are case reports about "accidentially bought" 9930's
/* Gesture parameters */
#define GESTURE_THRESHOLD_OUT 10
#define GESTURE_SENSITIVITY_1 50
#define GESTURE_SENSITIVITY_2 20
uint8_t APDS9960addr;
uint8_t APDS9960type = 0;
char APDS9960stype[9];
char currentGesture[6];
uint8_t gesture_mode = 1;
volatile uint8_t recovery_loop_counter = 0; //count number of stateloops to switch the sensor off, if needed
#define APDS9960_LONG_RECOVERY 50 //long pause after sensor overload in loops
#define APDS9960_MAX_GESTURE_CYCLES 50 //how many FIFO-reads are allowed to prevent crash
bool APDS9960_overload = false;
#ifdef USE_WEBSERVER
const char HTTP_APDS_9960_SNS[] PROGMEM = "%s"
"{s}" "Red" "{m}%s{e}"
"{s}" "Green" "{m}%s{e}"
"{s}" "Blue" "{m}%s{e}"
"{s}" "Ambient" "{m}%s " D_UNIT_LUX "{e}"
"{s}" "CCT" "{m}%s " "K" "{e}" // calculated color temperature in Kelvin
"{s}" "Proximity" "{m}%s{e}"; // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr>
#endif // USE_WEBSERVER
/*********************************************************************************************\
* APDS9960
*
* Programmer : APDS9960 Datasheet and Sparkfun
\*********************************************************************************************/
/* Misc parameters */
#define FIFO_PAUSE_TIME 30 // Wait period (ms) between FIFO reads
/* APDS-9960 register addresses */
#define APDS9960_ENABLE 0x80
#define APDS9960_ATIME 0x81
#define APDS9960_WTIME 0x83
#define APDS9960_AILTL 0x84
#define APDS9960_AILTH 0x85
#define APDS9960_AIHTL 0x86
#define APDS9960_AIHTH 0x87
#define APDS9960_PILT 0x89
#define APDS9960_PIHT 0x8B
#define APDS9960_PERS 0x8C
#define APDS9960_CONFIG1 0x8D
#define APDS9960_PPULSE 0x8E
#define APDS9960_CONTROL 0x8F
#define APDS9960_CONFIG2 0x90
#define APDS9960_ID 0x92
#define APDS9960_STATUS 0x93
#define APDS9960_CDATAL 0x94
#define APDS9960_CDATAH 0x95
#define APDS9960_RDATAL 0x96
#define APDS9960_RDATAH 0x97
#define APDS9960_GDATAL 0x98
#define APDS9960_GDATAH 0x99
#define APDS9960_BDATAL 0x9A
#define APDS9960_BDATAH 0x9B
#define APDS9960_PDATA 0x9C
#define APDS9960_POFFSET_UR 0x9D
#define APDS9960_POFFSET_DL 0x9E
#define APDS9960_CONFIG3 0x9F
#define APDS9960_GPENTH 0xA0
#define APDS9960_GEXTH 0xA1
#define APDS9960_GCONF1 0xA2
#define APDS9960_GCONF2 0xA3
#define APDS9960_GOFFSET_U 0xA4
#define APDS9960_GOFFSET_D 0xA5
#define APDS9960_GOFFSET_L 0xA7
#define APDS9960_GOFFSET_R 0xA9
#define APDS9960_GPULSE 0xA6
#define APDS9960_GCONF3 0xAA
#define APDS9960_GCONF4 0xAB
#define APDS9960_GFLVL 0xAE
#define APDS9960_GSTATUS 0xAF
#define APDS9960_IFORCE 0xE4
#define APDS9960_PICLEAR 0xE5
#define APDS9960_CICLEAR 0xE6
#define APDS9960_AICLEAR 0xE7
#define APDS9960_GFIFO_U 0xFC
#define APDS9960_GFIFO_D 0xFD
#define APDS9960_GFIFO_L 0xFE
#define APDS9960_GFIFO_R 0xFF
/* Bit fields */
#define APDS9960_PON 0b00000001
#define APDS9960_AEN 0b00000010
#define APDS9960_PEN 0b00000100
#define APDS9960_WEN 0b00001000
#define APSD9960_AIEN 0b00010000
#define APDS9960_PIEN 0b00100000
#define APDS9960_GEN 0b01000000
#define APDS9960_GVALID 0b00000001
/* On/Off definitions */
#define OFF 0
#define ON 1
/* Acceptable parameters for setMode */
#define POWER 0
#define AMBIENT_LIGHT 1
#define PROXIMITY 2
#define WAIT 3
#define AMBIENT_LIGHT_INT 4
#define PROXIMITY_INT 5
#define GESTURE 6
#define ALL 7
/* LED Drive values */
#define LED_DRIVE_100MA 0
#define LED_DRIVE_50MA 1
#define LED_DRIVE_25MA 2
#define LED_DRIVE_12_5MA 3
/* Proximity Gain (PGAIN) values */
#define PGAIN_1X 0
#define PGAIN_2X 1
#define PGAIN_4X 2
#define PGAIN_8X 3
/* ALS Gain (AGAIN) values */
#define AGAIN_1X 0
#define AGAIN_4X 1
#define AGAIN_16X 2
#define AGAIN_64X 3
/* Gesture Gain (GGAIN) values */
#define GGAIN_1X 0
#define GGAIN_2X 1
#define GGAIN_4X 2
#define GGAIN_8X 3
/* LED Boost values */
#define LED_BOOST_100 0
#define LED_BOOST_150 1
#define LED_BOOST_200 2
#define LED_BOOST_300 3
/* Gesture wait time values */
#define GWTIME_0MS 0
#define GWTIME_2_8MS 1
#define GWTIME_5_6MS 2
#define GWTIME_8_4MS 3
#define GWTIME_14_0MS 4
#define GWTIME_22_4MS 5
#define GWTIME_30_8MS 6
#define GWTIME_39_2MS 7
/* Default values */
#define DEFAULT_ATIME 0xdb // 103ms = 0xdb
#define DEFAULT_WTIME 246 // 27ms
#define DEFAULT_PROX_PPULSE 0x87 // 16us, 8 pulses
#define DEFAULT_GESTURE_PPULSE 0x89 // 16us, 10 pulses ---89
#define DEFAULT_POFFSET_UR 0 // 0 offset
#define DEFAULT_POFFSET_DL 0 // 0 offset
#define DEFAULT_CONFIG1 0x60 // No 12x wait (WTIME) factor
#define DEFAULT_LDRIVE LED_DRIVE_100MA
#define DEFAULT_PGAIN PGAIN_4X
#define DEFAULT_AGAIN AGAIN_4X // we have to divide by the same facot at the end
#define DEFAULT_PILT 0 // Low proximity threshold
#define DEFAULT_PIHT 50 // High proximity threshold
#define DEFAULT_AILT 0xFFFF // Force interrupt for calibration
#define DEFAULT_AIHT 0
#define DEFAULT_PERS 0x11 // 2 consecutive prox or ALS for int.
#define DEFAULT_CONFIG2 0x01 // No saturation interrupts or LED boost
#define DEFAULT_CONFIG3 0 // Enable all photodiodes, no SAI
#define DEFAULT_GPENTH 40 // Threshold for entering gesture mode
#define DEFAULT_GEXTH 30 // Threshold for exiting gesture mode
#define DEFAULT_GCONF1 0x40 // 4 gesture events for int., 1 for exit
#define DEFAULT_GGAIN GGAIN_4X
#define DEFAULT_GLDRIVE LED_DRIVE_100MA // default 100ma
#define DEFAULT_GWTIME GWTIME_2_8MS // default 2_8MS
#define DEFAULT_GOFFSET 0 // No offset scaling for gesture mode
#define DEFAULT_GPULSE 0xC9 // 32us, 10 pulses
#define DEFAULT_GCONF3 0 // All photodiodes active during gesture
#define DEFAULT_GIEN 0 // Disable gesture interrupts
#define ERROR 0xFF
/* Direction definitions */
enum {
DIR_NONE,
DIR_LEFT,
DIR_RIGHT,
DIR_UP,
DIR_DOWN,
DIR_ALL
};
/* State definitions*/
enum {
APDS9960_NA_STATE,
APDS9960_ALL_STATE
};
/* Container for gesture data */
typedef struct gesture_data_type {
uint8_t u_data[32];
uint8_t d_data[32];
uint8_t l_data[32];
uint8_t r_data[32];
uint8_t index;
uint8_t total_gestures;
uint8_t in_threshold;
uint8_t out_threshold;
} gesture_data_type;
/*Members*/
gesture_data_type gesture_data_;
int16_t gesture_ud_delta_ = 0;
int16_t gesture_lr_delta_ = 0;
int16_t gesture_ud_count_ = 0;
int16_t gesture_lr_count_ = 0;
int16_t gesture_state_ = 0;
int16_t gesture_motion_ = DIR_NONE;
typedef struct color_data_type {
uint16_t a; // measured ambient
uint16_t r;
uint16_t g;
uint16_t b;
uint8_t p; // proximity
uint16_t cct; // calculated color temperature
uint16_t lux; // calculated illuminance - atm only from rgb
} color_data_type;
color_data_type color_data;
uint8_t APDS9960_aTime = DEFAULT_ATIME;
/*******************************************************************************
* Helper functions
******************************************************************************/
/**
* @brief Writes a single byte to the I2C device (no register)
*
* @param[in] val the 1-byte value to write to the I2C device
* @return True if successful write operation. False otherwise.
*/
bool wireWriteByte(uint8_t val)
{
Wire.beginTransmission(APDS9960_I2C_ADDR);
Wire.write(val);
if( Wire.endTransmission() != 0 ) {
return false;
}
return true;
}
/**
* @brief Reads a block (array) of bytes from the I2C device and register
*
* @param[in] reg the register to read from
* @param[out] val pointer to the beginning of the data
* @param[in] len number of bytes to read
* @return Number of bytes read. -1 on read error.
*/
int8_t wireReadDataBlock( uint8_t reg,
uint8_t *val,
uint16_t len)
{
unsigned char i = 0;
/* Indicate which register we want to read from */
if (!wireWriteByte(reg)) {
return -1;
}
/* Read block data */
Wire.requestFrom(APDS9960_I2C_ADDR, len);
while (Wire.available()) {
if (i >= len) {
return -1;
}
val[i] = Wire.read();
i++;
}
return i;
}
/**
* Taken from the Adafruit-library
* @brief Converts the raw R/G/B values to color temperature in degrees
* Kelvin
*/
void calculateColorTemperature(void)
{
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 * color_data.r) + (1.54924F * color_data.g) + (-0.95641F * color_data.b);
Y = (-0.32466F * color_data.r) + (1.57837F * color_data.g) + (-0.73191F * color_data.b); // this is Lux ... under certain circumstances
Z = (-0.68202F * color_data.r) + (0.77073F * color_data.g) + ( 0.56332F * color_data.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 */
color_data.cct = (449.0F * powf(n, 3)) + (3525.0F * powf(n, 2)) + (6823.3F * n) + 5520.33F;
return;
}
/**
* Taken from the Adafruit-Library
* @brief Implements missing powf function
*/
float powf(const float x, const float y)
{
return (float)(pow((double)x, (double)y));
}
/*******************************************************************************
* Getters and setters for register values
******************************************************************************/
/**
* @brief Returns the lower threshold for proximity detection
*
* @return lower threshold
*/
uint8_t getProxIntLowThresh(void)
{
uint8_t val;
/* Read value from PILT register */
val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_PILT) ;
return val;
}
/**
* @brief Sets the lower threshold for proximity detection
*
* @param[in] threshold the lower proximity threshold
*/
void setProxIntLowThresh(uint8_t threshold)
{
I2cWrite8(APDS9960_I2C_ADDR, APDS9960_PILT, threshold);
}
/**
* @brief Returns the high threshold for proximity detection
*
* @return high threshold
*/
uint8_t getProxIntHighThresh(void)
{
uint8_t val;
/* Read value from PIHT register */
val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_PIHT) ;
return val;
}
/**
* @brief Sets the high threshold for proximity detection
*
* @param[in] threshold the high proximity threshold
*/
void setProxIntHighThresh(uint8_t threshold)
{
I2cWrite8(APDS9960_I2C_ADDR, APDS9960_PIHT, threshold);
}
/**
* @brief Returns LED drive strength for proximity and ALS
*
* Value LED Current
* 0 100 mA
* 1 50 mA
* 2 25 mA
* 3 12.5 mA
*
* @return the value of the LED drive strength. 0xFF on failure.
*/
uint8_t getLEDDrive(void)
{
uint8_t val;
/* Read value from CONTROL register */
val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONTROL) ;
/* Shift and mask out LED drive bits */
val = (val >> 6) & 0b00000011;
return val;
}
/**
* @brief Sets the LED drive strength for proximity and ALS
*
* Value LED Current
* 0 100 mA
* 1 50 mA
* 2 25 mA
* 3 12.5 mA
*
* @param[in] drive the value (0-3) for the LED drive strength
*/
void setLEDDrive(uint8_t drive)
{
uint8_t val;
/* Read value from CONTROL register */
val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONTROL);
/* Set bits in register to given value */
drive &= 0b00000011;
drive = drive << 6;
val &= 0b00111111;
val |= drive;
/* Write register value back into CONTROL register */
I2cWrite8(APDS9960_I2C_ADDR, APDS9960_CONTROL, val);
}
/**
* @brief Returns receiver gain for proximity detection
*
* Value Gain
* 0 1x
* 1 2x
* 2 4x
* 3 8x
*
* @return the value of the proximity gain. 0xFF on failure.
*/
uint8_t getProximityGain(void)
{
uint8_t val;
/* Read value from CONTROL register */
val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONTROL) ;
/* Shift and mask out PDRIVE bits */
val = (val >> 2) & 0b00000011;
return val;
}
/**
* @brief Sets the receiver gain for proximity detection
*
* Value Gain
* 0 1x
* 1 2x
* 2 4x
* 3 8x
*
* @param[in] drive the value (0-3) for the gain
*/
void setProximityGain(uint8_t drive)
{
uint8_t val;
/* Read value from CONTROL register */
val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONTROL);
/* Set bits in register to given value */
drive &= 0b00000011;
drive = drive << 2;
val &= 0b11110011;
val |= drive;
/* Write register value back into CONTROL register */
I2cWrite8(APDS9960_I2C_ADDR, APDS9960_CONTROL, val);
}
/**
* @brief Returns receiver gain for the ambient light sensor (ALS)
*
* Value Gain
* 0 1x
* 1 4x
* 2 16x
* 3 64x
*
* @return the value of the ALS gain. 0xFF on failure.
*/
/**
* @brief Sets the receiver gain for the ambient light sensor (ALS)
*
* Value Gain
* 0 1x
* 1 4x
* 2 16x
* 3 64x
*
* @param[in] drive the value (0-3) for the gain
*/
void setAmbientLightGain(uint8_t drive)
{
uint8_t val;
/* Read value from CONTROL register */
val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONTROL);
/* Set bits in register to given value */
drive &= 0b00000011;
val &= 0b11111100;
val |= drive;
/* Write register value back into CONTROL register */
I2cWrite8(APDS9960_I2C_ADDR, APDS9960_CONTROL, val);
}
/**
* @brief Get the current LED boost value
*
* Value Boost Current
* 0 100%
* 1 150%
* 2 200%
* 3 300%
*
* @return The LED boost value. 0xFF on failure.
*/
uint8_t getLEDBoost(void)
{
uint8_t val;
/* Read value from CONFIG2 register */
val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONFIG2) ;
/* Shift and mask out LED_BOOST bits */
val = (val >> 4) & 0b00000011;
return val;
}
/**
* @brief Sets the LED current boost value
*
* Value Boost Current
* 0 100%
* 1 150%
* 2 200%
* 3 300%
*
* @param[in] drive the value (0-3) for current boost (100-300%)
*/
void setLEDBoost(uint8_t boost)
{
uint8_t val;
/* Read value from CONFIG2 register */
val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONFIG2) ;
/* Set bits in register to given value */
boost &= 0b00000011;
boost = boost << 4;
val &= 0b11001111;
val |= boost;
/* Write register value back into CONFIG2 register */
I2cWrite8(APDS9960_I2C_ADDR, APDS9960_CONFIG2, val) ;
}
/**
* @brief Gets proximity gain compensation enable
*
* @return 1 if compensation is enabled. 0 if not. 0xFF on error.
*/
uint8_t getProxGainCompEnable(void)
{
uint8_t val;
/* Read value from CONFIG3 register */
val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONFIG3) ;
/* Shift and mask out PCMP bits */
val = (val >> 5) & 0b00000001;
return val;
}
/**
* @brief Sets the proximity gain compensation enable
*
* @param[in] enable 1 to enable compensation. 0 to disable compensation.
*/
void setProxGainCompEnable(uint8_t enable)
{
uint8_t val;
/* Read value from CONFIG3 register */
val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONFIG3) ;
/* Set bits in register to given value */
enable &= 0b00000001;
enable = enable << 5;
val &= 0b11011111;
val |= enable;
/* Write register value back into CONFIG3 register */
I2cWrite8(APDS9960_I2C_ADDR, APDS9960_CONFIG3, val) ;
}
/**
* @brief Gets the current mask for enabled/disabled proximity photodiodes
*
* 1 = disabled, 0 = enabled
* Bit Photodiode
* 3 UP
* 2 DOWN
* 1 LEFT
* 0 RIGHT
*
* @return Current proximity mask for photodiodes. 0xFF on error.
*/
uint8_t getProxPhotoMask(void)
{
uint8_t val;
/* Read value from CONFIG3 register */
val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONFIG3) ;
/* Mask out photodiode enable mask bits */
val &= 0b00001111;
return val;
}
/**
* @brief Sets the mask for enabling/disabling proximity photodiodes
*
* 1 = disabled, 0 = enabled
* Bit Photodiode
* 3 UP
* 2 DOWN
* 1 LEFT
* 0 RIGHT
*
* @param[in] mask 4-bit mask value
*/
void setProxPhotoMask(uint8_t mask)
{
uint8_t val;
/* Read value from CONFIG3 register */
val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONFIG3) ;
/* Set bits in register to given value */
mask &= 0b00001111;
val &= 0b11110000;
val |= mask;
/* Write register value back into CONFIG3 register */
I2cWrite8(APDS9960_I2C_ADDR, APDS9960_CONFIG3, val) ;
}
/**
* @brief Gets the entry proximity threshold for gesture sensing
*
* @return Current entry proximity threshold.
*/
uint8_t getGestureEnterThresh(void)
{
uint8_t val;
/* Read value from GPENTH register */
val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GPENTH) ;
return val;
}
/**
* @brief Sets the entry proximity threshold for gesture sensing
*
* @param[in] threshold proximity value needed to start gesture mode
*/
void setGestureEnterThresh(uint8_t threshold)
{
I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GPENTH, threshold) ;
}
/**
* @brief Gets the exit proximity threshold for gesture sensing
*
* @return Current exit proximity threshold.
*/
uint8_t getGestureExitThresh(void)
{
uint8_t val;
/* Read value from GEXTH register */
val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GEXTH) ;
return val;
}
/**
* @brief Sets the exit proximity threshold for gesture sensing
*
* @param[in] threshold proximity value needed to end gesture mode
*/
void setGestureExitThresh(uint8_t threshold)
{
I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GEXTH, threshold) ;
}
/**
* @brief Gets the gain of the photodiode during gesture mode
*
* Value Gain
* 0 1x
* 1 2x
* 2 4x
* 3 8x
*
* @return the current photodiode gain. 0xFF on error.
*/
uint8_t getGestureGain(void)
{
uint8_t val;
/* Read value from GCONF2 register */
val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GCONF2) ;
/* Shift and mask out GGAIN bits */
val = (val >> 5) & 0b00000011;
return val;
}
/**
* @brief Sets the gain of the photodiode during gesture mode
*
* Value Gain
* 0 1x
* 1 2x
* 2 4x
* 3 8x
*
* @param[in] gain the value for the photodiode gain
*/
void setGestureGain(uint8_t gain)
{
uint8_t val;
/* Read value from GCONF2 register */
val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GCONF2) ;
/* Set bits in register to given value */
gain &= 0b00000011;
gain = gain << 5;
val &= 0b10011111;
val |= gain;
/* Write register value back into GCONF2 register */
I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GCONF2, val) ;
}
/**
* @brief Gets the drive current of the LED during gesture mode
*
* Value LED Current
* 0 100 mA
* 1 50 mA
* 2 25 mA
* 3 12.5 mA
*
* @return the LED drive current value. 0xFF on error.
*/
uint8_t getGestureLEDDrive(void)
{
uint8_t val;
/* Read value from GCONF2 register */
val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GCONF2) ;
/* Shift and mask out GLDRIVE bits */
val = (val >> 3) & 0b00000011;
return val;
}
/**
* @brief Sets the LED drive current during gesture mode
*
* Value LED Current
* 0 100 mA
* 1 50 mA
* 2 25 mA
* 3 12.5 mA
*
* @param[in] drive the value for the LED drive current
*/
void setGestureLEDDrive(uint8_t drive)
{
uint8_t val;
/* Read value from GCONF2 register */
val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GCONF2) ;
/* Set bits in register to given value */
drive &= 0b00000011;
drive = drive << 3;
val &= 0b11100111;
val |= drive;
/* Write register value back into GCONF2 register */
I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GCONF2, val) ;
}
/**
* @brief Gets the time in low power mode between gesture detections
*
* Value Wait time
* 0 0 ms
* 1 2.8 ms
* 2 5.6 ms
* 3 8.4 ms
* 4 14.0 ms
* 5 22.4 ms
* 6 30.8 ms
* 7 39.2 ms
*
* @return the current wait time between gestures. 0xFF on error.
*/
uint8_t getGestureWaitTime(void)
{
uint8_t val;
/* Read value from GCONF2 register */
val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GCONF2) ;
/* Mask out GWTIME bits */
val &= 0b00000111;
return val;
}
/**
* @brief Sets the time in low power mode between gesture detections
*
* Value Wait time
* 0 0 ms
* 1 2.8 ms
* 2 5.6 ms
* 3 8.4 ms
* 4 14.0 ms
* 5 22.4 ms
* 6 30.8 ms
* 7 39.2 ms
*
* @param[in] the value for the wait time
*/
void setGestureWaitTime(uint8_t time)
{
uint8_t val;
/* Read value from GCONF2 register */
val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GCONF2) ;
/* Set bits in register to given value */
time &= 0b00000111;
val &= 0b11111000;
val |= time;
/* Write register value back into GCONF2 register */
I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GCONF2, val) ;
}
/**
* @brief Gets the low threshold for ambient light interrupts
*
* @param[out] threshold current low threshold stored on the APDS-9960
*/
void getLightIntLowThreshold(uint16_t &threshold)
{
uint8_t val_byte;
threshold = 0;
/* Read value from ambient light low threshold, low byte register */
val_byte = I2cRead8(APDS9960_I2C_ADDR, APDS9960_AILTL) ;
threshold = val_byte;
/* Read value from ambient light low threshold, high byte register */
I2cWrite8(APDS9960_I2C_ADDR, APDS9960_AILTH, val_byte) ;
threshold = threshold + ((uint16_t)val_byte << 8);
}
/**
* @brief Sets the low threshold for ambient light interrupts
*
* @param[in] threshold low threshold value for interrupt to trigger
*/
void setLightIntLowThreshold(uint16_t threshold)
{
uint8_t val_low;
uint8_t val_high;
/* Break 16-bit threshold into 2 8-bit values */
val_low = threshold & 0x00FF;
val_high = (threshold & 0xFF00) >> 8;
/* Write low byte */
I2cWrite8(APDS9960_I2C_ADDR, APDS9960_AILTL, val_low) ;
/* Write high byte */
I2cWrite8(APDS9960_I2C_ADDR, APDS9960_AILTH, val_high) ;
}
/**
* @brief Gets the high threshold for ambient light interrupts
*
* @param[out] threshold current low threshold stored on the APDS-9960
*/
void getLightIntHighThreshold(uint16_t &threshold)
{
uint8_t val_byte;
threshold = 0;
/* Read value from ambient light high threshold, low byte register */