Skip to content

Commit

Permalink
calc Color struct only when required
Browse files Browse the repository at this point in the history
  • Loading branch information
hideakitai committed Jun 5, 2021
1 parent 75b0276 commit 5b6066b
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions TCS34725.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,21 @@ class TCS34725_
return b;
}

const Color& color() const { return clr; }
Color color() const
{
Color clr;
if (raw_data.c == 0) clr.r = clr.g = clr.b = 0;
else
{
clr.r = pow((float)raw_data.r / (float)raw_data.c, scaling) * 255.f;
clr.g = pow((float)raw_data.g / (float)raw_data.c, scaling) * 255.f;
clr.b = pow((float)raw_data.b / (float)raw_data.c, scaling) * 255.f;
if (clr.r > 255.f) clr.r = 255.f;
if (clr.g > 255.f) clr.g = 255.f;
if (clr.b > 255.f) clr.b = 255.f;
}
return clr;
}
const RawData& raw() const { return raw_data; }
float lux() const { return lx; }
float colorTemperature() const { return color_temp; }
Expand Down Expand Up @@ -214,17 +228,6 @@ class TCS34725_
wire->requestFrom(I2C_ADDR, sizeof(RawData));
for (uint8_t i = 0; i < sizeof(RawData); i++)
raw_data.raw[i] = wire->read();

if (raw_data.c == 0) clr.r = clr.g = clr.b = 0;
else
{
clr.r = pow((float)raw_data.r / (float)raw_data.c, scaling) * 255.f;
clr.g = pow((float)raw_data.g / (float)raw_data.c, scaling) * 255.f;
clr.b = pow((float)raw_data.b / (float)raw_data.c, scaling) * 255.f;
if (clr.r > 255.f) clr.r = 255.f;
if (clr.g > 255.f) clr.g = 255.f;
if (clr.b > 255.f) clr.b = 255.f;
}
}

// https://github.com/adafruit/Adafruit_CircuitPython_TCS34725/blob/master/adafruit_tcs34725.py
Expand Down Expand Up @@ -276,7 +279,6 @@ class TCS34725_
float lx;
float color_temp;
RawData raw_data;
Color clr;
float gain_value {1.f};
uint8_t atime {0xFF};
float integration_time {2.4f}; // [ms]
Expand Down

0 comments on commit 5b6066b

Please sign in to comment.