Skip to content

Commit 4f11d55

Browse files
committed
added hsv and hsl api and fixed wrong check in begin
1 parent 0f05445 commit 4f11d55

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

src/LTR381RGB.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,16 @@ int LTR381RGBClass::begin(int gain, int rate, int resolution, int pin, int ut, i
5353
#endif
5454
return 0;
5555
}
56-
56+
// Clear the power-on event bit after the first initialization
5757
res = readRegister(LTR381RGB_MAIN_STATUS);
58-
// Check if Power-On event happens
59-
if ((res & 0x20) != 0) {
60-
return 0;
61-
}
58+
6259
if(pin > -1) {
6360
pinMode(pin, INPUT);
6461
attachInterrupt(digitalPinToInterrupt(pin), _callback, FALLING);
6562
enableALSInterrupt();
6663
_irq = true;
6764
}
65+
6866
setGain(gain);
6967
setMeasurementRate(rate);
7068
setADCResolution(resolution);
@@ -330,7 +328,7 @@ void LTR381RGBClass::setCallback(callback_f callback) {
330328
_callback = callback;
331329
}
332330

333-
void LTR381RGBClass::getHSV(int r, int g, int b, int& h, int& s, int& v) {
331+
void LTR381RGBClass::getHSV(int r, int g, int b, float& h, float& s, float& v) {
334332
float red = r/255.0;
335333
float green = g/255.0;
336334
float blue = b/255.0;
@@ -340,11 +338,11 @@ void LTR381RGBClass::getHSV(int r, int g, int b, int& h, int& s, int& v) {
340338
if(delta == 0) {
341339
h = 0;
342340
} else if (cmax == red) {
343-
h = fmod((60*((green - blue) / delta)),6);
341+
h = 60*fmod((((green - blue) / delta)),6);
344342
} else if(cmax == green) {
345-
h = (60*((blue - red) / delta)) + 2;
343+
h = 60*(((blue - red) / delta) + 2);
346344
} else {
347-
h = (60*((red - green) / delta)) + 4;
345+
h = 60*(((red - green) / delta) + 4);
348346
}
349347

350348
if(cmax == 0) {
@@ -353,10 +351,11 @@ void LTR381RGBClass::getHSV(int r, int g, int b, int& h, int& s, int& v) {
353351
s = delta/cmax;
354352
}
355353

356-
v = cmax;
354+
v = cmax * 100.0;
355+
s = s*100;
357356
}
358357

359-
void LTR381RGBClass::getHSL(int r, int g, int b, int& h, int& s, int& l) {
358+
void LTR381RGBClass::getHSL(int r, int g, int b, float& h, float& s, float& l) {
360359
float red = r/255.0;
361360
float green = g/255.0;
362361
float blue = b/255.0;
@@ -367,18 +366,20 @@ void LTR381RGBClass::getHSL(int r, int g, int b, int& h, int& s, int& l) {
367366
if(delta == 0) {
368367
h = 0;
369368
} else if (cmax == red) {
370-
h = fmod((60*((green - blue) / delta)),6);
369+
h = 60*fmod((((green - blue) / delta)),6);
371370
} else if(cmax == green) {
372-
h = (60*((blue - red) / delta)) + 2;
371+
h = 60* (( (blue - red) / delta) + 2);
373372
} else {
374-
h = (60*((red - green) / delta)) + 4;
373+
h = 60*(((red - green) / delta) + 4);
375374
}
376375

377376
if(delta == 0) {
378-
s = 0;
377+
s = 0.0;
379378
} else {
380-
s = delta/(1 - abs(2*l -1));
379+
s = (delta/(1 - abs(2*l -1)));
381380
}
381+
l = l*100;
382+
s= s*100;
382383
}
383384

384385
void LTR381RGBClass::getALS(uint8_t * buf, int& ir, int& rawlux, int& lux) {

src/LTR381RGB.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ class LTR381RGBClass {
6464
void resetSW();
6565
void setCalibrations(int rmax, int gmax, int bmax, int rmin, int gmin, int bmin);
6666
void setCallback(callback_f callback);
67-
void getHSV(int r, int g, int b, int& h, int& s, int& v);
68-
void getHSL(int r, int g, int b, int& h, int& s, int& l);
67+
void getHSV(int r, int g, int b, float& h, float& s, float& v);
68+
void getHSL(int r, int g, int b, float& h, float& s, float& l);
6969
private:
7070
void getALS(uint8_t * buf, int& ir, int& rawlux, int& lux);
7171
void getColors(uint8_t * buf, int& r, int& g, int& b);

0 commit comments

Comments
 (0)