3232#define LTR381RGB_ALS_THRES_LTV_2 0X25
3333#define LTR381RGB_ALS_THRES_LTV_3 0X26
3434
35- enum {
36- ADC_RES_20BIT = 0x00 ,
37- ADC_RES_19BIT = 0x01 ,
38- ADC_RES_18BIT = 0x02 ,
39- ADC_RES_17BIT = 0x03 ,
40- ADC_RES_16BIT = 0x04 ,
41- };
42-
43- enum {
44- ADC_MEAS_RATE_25MS = 0x00 ,
45- ADC_MEAS_RATE_50MS = 0x01 ,
46- ADC_MEAS_RATE_100MS = 0x02 ,
47- ADC_MEAS_RATE_200MS = 0x03 ,
48- ADC_MEAS_RATE_400MS = 0x04 ,
49- ADC_MEAS_RATE_500MS = 0x05 ,
50- ADC_MEAS_RATE_1000MS = 0x06 ,
51- ADC_MEAS_RATE_2000MS = 0x07 ,
52- };
53-
54- enum {
55- ALS_CS_GAIN_1 = 0x00 ,
56- ALS_CS_GAIN_3 = 0x01 ,
57- ALS_CS_GAIN_6 = 0x02 ,
58- ALS_CS_GAIN_9 = 0x03 ,
59- ALS_CS_GAIN_18 = 0x04 ,
60- };
6135
6236LTR381RGBClass::LTR381RGBClass (TwoWire& wire, uint8_t slaveAddress) :
6337 _wire(&wire),
@@ -69,7 +43,7 @@ LTR381RGBClass::~LTR381RGBClass()
6943{
7044}
7145
72- int LTR381RGBClass::begin () {
46+ int LTR381RGBClass::begin (int gain, int rate, int resolution, int pin, int ut, int lt ) {
7347 _wire->begin ();
7448
7549 uint8_t res = readRegister (LTR381RGB_PART_ID);
@@ -85,20 +59,52 @@ int LTR381RGBClass::begin() {
8559 if ((res & 0x20 ) != 0 ) {
8660 return 0 ;
8761 }
88- setCalibrations (158 , 137 , 155 , 1 , 1 , 0 );
89- enableALSInterrupt ();
90- setGain (ALS_CS_GAIN_18);
91- setMeasurementRate (ADC_MEAS_RATE_25MS);
92- setADCResolution (ADC_RES_16BIT);
93- setUpperThreshold (_upperThreshold);
94- setLowerThreshold (lowerThreshold);
62+ if (pin > -1 ) {
63+ pinMode (pin, INPUT);
64+ attachInterrupt (digitalPinToInterrupt (pin), _callback, FALLING);
65+ enableALSInterrupt ();
66+ _irq = true ;
67+ }
68+ setGain (gain);
69+ setMeasurementRate (rate);
70+ setADCResolution (resolution);
71+ setUpperThreshold (ut);
72+ setLowerThreshold (lt);
9573 return 1 ;
9674}
9775
9876void LTR381RGBClass::end () {
9977 _wire->end ();
10078}
10179
80+
81+ int LTR381RGBClass::readAllSensors (int & r, int & g, int & b, int & rawlux, int & lux, int & ir) {
82+ enableRGB ();
83+ unsigned long start = millis ();
84+ while (!available () && (millis () - start) < _timeout) {
85+ delay (50 );
86+ }
87+ uint8_t cs_buf[9 ] = {0 };
88+ int res = readRegisters (LTR381RGB_CS_DATA_GREEN, cs_buf, 9 );
89+ if (res != 1 ) {
90+ return 0 ;
91+ }
92+
93+ enableALS ();
94+ while (!available () && (millis () - start) < _timeout) {
95+ delay (50 );
96+ }
97+ disableMeas ();
98+ uint8_t als_buf[6 ] = {0 };
99+ res = readRegisters (LTR381RGB_CS_DATA_IR, als_buf, 6 );
100+ if (res != 1 ) {
101+ return 0 ;
102+ }
103+ getColors (cs_buf, r, g, b);
104+ getALS (als_buf, ir, rawlux, lux);
105+ return 1 ;
106+ }
107+
102108int LTR381RGBClass::readColors (int & r, int & g, int & b) {
103109 enableRGB ();
104110 unsigned long start = millis ();
@@ -111,19 +117,7 @@ int LTR381RGBClass::readColors(int& r, int& g, int& b) {
111117 if (res != 1 ) {
112118 return 0 ;
113119 }
114- if (_calibrated) {
115- int lg = adcToValue ((buf[2 ] << 16 | buf[1 ] << 8 | buf[0 ]));
116- int lr = adcToValue ((buf[5 ] << 16 | buf[4 ] << 8 | buf[3 ]));
117- int lb = adcToValue ((buf[8 ] << 16 | buf[7 ] << 8 | buf[6 ]));
118- r = normAndTrim (lr, _minR, _maxR);
119- g = normAndTrim (lg, _minG, _maxG);
120- b = normAndTrim (lb, _minB, _maxB);
121-
122- } else {
123- g = adcToValue ((buf[2 ] << 16 | buf[1 ] << 8 | buf[0 ]));
124- r = adcToValue ((buf[5 ] << 16 | buf[4 ] << 8 | buf[3 ]));
125- b = adcToValue ((buf[8 ] << 16 | buf[7 ] << 8 | buf[6 ]));
126- }
120+ getColors (buf, r, g, b);
127121
128122 return 1 ;
129123}
@@ -217,18 +211,18 @@ void LTR381RGBClass::setADCResolution(int resolution) {
217211 }
218212 uint8_t res = readRegister (LTR381RGB_ALS_CS_MEAS_RATE);
219213 writeRegister (LTR381RGB_ALS_CS_MEAS_RATE, ((res & 0x8F ) | (resolution << 4 )));
220- _adcResolution = resolution;
221214 setTimeout (getADCResTime (resolution) + getADCRate (_rate));
215+ _adcResolution = resolution;
222216}
223217
224218void LTR381RGBClass::setMeasurementRate (int rate) {
225219 if (rate > 0x08 ) {
226220 rate = 0x08 ;
227221 }
228- _rate = rate;
229222 uint8_t res = readRegister (LTR381RGB_ALS_CS_MEAS_RATE);
230223 writeRegister (LTR381RGB_ALS_CS_MEAS_RATE, ((res & 0xF8 ) | rate));
231224 setTimeout (getADCResTime (_adcResolution) + getADCRate (rate));
225+ _rate = rate;
232226}
233227
234228void LTR381RGBClass::setUpperThreshold (int utv) {
@@ -303,7 +297,7 @@ int LTR381RGBClass::getADCRate(int rate) {
303297 }
304298}
305299
306- void LTR381RGBClass::setInteruptPersistence (int persistence) {
300+ void LTR381RGBClass::setInterruptPersistence (int persistence) {
307301 writeRegister (LTR381RGB_INT_PST, (persistence << 4 ));
308302}
309303
@@ -332,6 +326,88 @@ void LTR381RGBClass::setCalibrations(int rmax, int gmax, int bmax, int rmin, int
332326 _calibrated = true ;
333327}
334328
329+ void LTR381RGBClass::setCallback (callback_f callback) {
330+ _callback = callback;
331+ }
332+
333+ void LTR381RGBClass::getHSV (int r, int g, int b, int & h, int & s, int & v) {
334+ float red = r/255.0 ;
335+ float green = g/255.0 ;
336+ float blue = b/255.0 ;
337+ float cmax = max (red, max (blue, green));
338+ float cmin = min (red, min (blue, green));
339+ float delta = cmax - cmin;
340+ if (delta == 0 ) {
341+ h = 0 ;
342+ } else if (cmax == red) {
343+ h = fmod ((60 *((green - blue) / delta)),6 );
344+ } else if (cmax == green) {
345+ h = (60 *((blue - red) / delta)) + 2 ;
346+ } else {
347+ h = (60 *((red - green) / delta)) + 4 ;
348+ }
349+
350+ if (cmax == 0 ) {
351+ s = 0 ;
352+ } else {
353+ s = delta/cmax;
354+ }
355+
356+ v = cmax;
357+ }
358+
359+ void LTR381RGBClass::getHSL (int r, int g, int b, int & h, int & s, int & l) {
360+ float red = r/255.0 ;
361+ float green = g/255.0 ;
362+ float blue = b/255.0 ;
363+ float cmax = max (red, max (blue, green));
364+ float cmin = min (red, min (blue, green));
365+ float delta = cmax - cmin;
366+ l = (cmax + cmin)/2 ;
367+ if (delta == 0 ) {
368+ h = 0 ;
369+ } else if (cmax == red) {
370+ h = fmod ((60 *((green - blue) / delta)),6 );
371+ } else if (cmax == green) {
372+ h = (60 *((blue - red) / delta)) + 2 ;
373+ } else {
374+ h = (60 *((red - green) / delta)) + 4 ;
375+ }
376+
377+ if (delta == 0 ) {
378+ s = 0 ;
379+ } else {
380+ s = delta/(1 - abs (2 *l -1 ));
381+ }
382+ }
383+
384+ void LTR381RGBClass::getALS (uint8_t * buf, int & ir, int & rawlux, int & lux) {
385+ int resolution = getADCResolution (_adcResolution);
386+ int gain = getLuxGain (_gain);
387+ float intTime = getLuxIntTime (_adcResolution);
388+
389+ ir = resolution & buf[2 ] << 16 | buf[1 ] << 8 | buf[0 ];
390+ rawlux = resolution & buf[5 ] << 16 | buf[4 ] << 8 | buf[3 ];
391+ lux = ((0 .8f *rawlux)/(gain*intTime))*(1 - (0 .033f *(ir/rawlux)));
392+ }
393+
394+ void LTR381RGBClass::getColors (uint8_t * buf, int & r, int & g, int & b) {
395+ if (_calibrated) {
396+ int lg = adcToValue ((buf[2 ] << 16 | buf[1 ] << 8 | buf[0 ]));
397+ int lr = adcToValue ((buf[5 ] << 16 | buf[4 ] << 8 | buf[3 ]));
398+ int lb = adcToValue ((buf[8 ] << 16 | buf[7 ] << 8 | buf[6 ]));
399+ r = normAndTrim (lr, _minR, _maxR);
400+ g = normAndTrim (lg, _minG, _maxG);
401+ b = normAndTrim (lb, _minB, _maxB);
402+
403+ } else {
404+ g = adcToValue ((buf[2 ] << 16 | buf[1 ] << 8 | buf[0 ]));
405+ r = adcToValue ((buf[5 ] << 16 | buf[4 ] << 8 | buf[3 ]));
406+ b = adcToValue ((buf[8 ] << 16 | buf[7 ] << 8 | buf[6 ]));
407+ }
408+ }
409+
410+
335411void LTR381RGBClass::dumpReg () {
336412 for (int i = 0x00 ; i < 0x27 ; i++) {
337413 if (i == 0x01 || i == 0x02 || i ==0x03 || i == 0x08 || i == 0x09
@@ -413,13 +489,17 @@ int LTR381RGBClass::available() {
413489 dumpReg ();
414490#endif
415491 // clear the reading after the reset
416- res = readRegister (LTR381RGB_MAIN_STATUS);
417- setADCResolution (_adcResolution);
418- setGain (_gain);
492+ begin ();
419493 return 0 ;
420494 }
421- if ((res & 0x08 ) == 0x08 ) {
422- return 1 ;
495+ if (_irq) {
496+ if ((res & 0x18 ) == 0x18 ) {
497+ return 1 ;
498+ }
499+ } else {
500+ if ((res & 0x08 ) == 0x08 ) {
501+ return 1 ;
502+ }
423503 }
424504 return 0 ;
425505}
0 commit comments