refactor calctemp
This commit is contained in:
parent
761fd38a15
commit
9de432a84a
3 changed files with 27 additions and 33 deletions
|
|
@ -1020,27 +1020,19 @@ bool Adafruit_OPT4048::getCIE(double *CIEx, double *CIEy, double *lux) {
|
|||
* n = (x - 0.3320) / (0.1858 - y)
|
||||
* CCT = 437 * n^3 + 3601 * n^2 + 6861 * n + 5517
|
||||
*
|
||||
* @return The calculated color temperature in Kelvin, or 0 if calculation failed
|
||||
* @param CIEx The CIE x chromaticity coordinate
|
||||
* @param CIEy The CIE y chromaticity coordinate
|
||||
* @return The calculated color temperature in Kelvin
|
||||
*/
|
||||
double Adafruit_OPT4048::getColorTemperature(void) {
|
||||
if (!i2c_dev) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
// Get CIE coordinates
|
||||
double x, y, lux;
|
||||
if (!getCIE(&x, &y, &lux)) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
double Adafruit_OPT4048::calculateColorTemperature(double CIEx, double CIEy) {
|
||||
// Check for invalid coordinates
|
||||
if (x == 0 && y == 0) {
|
||||
if (CIEx == 0 && CIEy == 0) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
// Calculate using McCamy's formula from spreadsheet
|
||||
// n = (x - 0.3320) / (0.1858 - y)
|
||||
double n = (x - 0.3320) / (0.1858 - y);
|
||||
double n = (CIEx - 0.3320) / (0.1858 - CIEy);
|
||||
|
||||
// CCT = 437 * n^3 + 3601 * n^2 + 6861 * n + 5517
|
||||
double cct = (437.0 * n * n * n) + (3601.0 * n * n) + (6861.0 * n) + 5517.0;
|
||||
|
|
|
|||
|
|
@ -181,9 +181,11 @@ public:
|
|||
* Uses the McCamy's approximation formula to calculate CCT from CIE 1931 x,y coordinates.
|
||||
* This is accurate for color temperatures between 2000K and 30000K.
|
||||
*
|
||||
* @return The calculated color temperature in Kelvin, or 0 if calculation failed
|
||||
* @param CIEx The CIE x chromaticity coordinate
|
||||
* @param CIEy The CIE y chromaticity coordinate
|
||||
* @return The calculated color temperature in Kelvin
|
||||
*/
|
||||
double getColorTemperature(void);
|
||||
double calculateColorTemperature(double CIEx, double CIEy);
|
||||
|
||||
private:
|
||||
Adafruit_I2CDevice *i2c_dev;
|
||||
|
|
|
|||
|
|
@ -194,6 +194,23 @@ void loop() {
|
|||
Serial.print(F("Z (CH2): ")); Serial.println(z);
|
||||
Serial.print(F("W (CH3): ")); Serial.println(w);
|
||||
|
||||
// Calculate and display CIE chromaticity coordinates and lux
|
||||
double CIEx, CIEy, lux;
|
||||
if (sensor.getCIE(&CIEx, &CIEy, &lux)) {
|
||||
Serial.println(F("\nCIE Coordinates:"));
|
||||
Serial.print(F("CIE x: ")); Serial.println(CIEx, 8);
|
||||
Serial.print(F("CIE y: ")); Serial.println(CIEy, 8);
|
||||
Serial.print(F("Lux: ")); Serial.println(lux, 4);
|
||||
|
||||
// Calculate and display color temperature
|
||||
double colorTemp = sensor.calculateColorTemperature(CIEx, CIEy);
|
||||
Serial.print(F("Color Temperature: "));
|
||||
Serial.print(colorTemp, 2);
|
||||
Serial.println(F(" K"));
|
||||
} else {
|
||||
Serial.println(F("\nError calculating CIE coordinates"));
|
||||
}
|
||||
|
||||
// Read and print status flags
|
||||
uint8_t flags = sensor.getFlags();
|
||||
Serial.println(F("\nStatus Flags:"));
|
||||
|
|
@ -212,23 +229,6 @@ void loop() {
|
|||
if (flags == 0) {
|
||||
Serial.println(F("- No flags set"));
|
||||
}
|
||||
|
||||
// Calculate and display CIE chromaticity coordinates and lux
|
||||
double CIEx, CIEy, lux;
|
||||
if (sensor.getCIE(&CIEx, &CIEy, &lux)) {
|
||||
Serial.println(F("\nCIE Coordinates:"));
|
||||
Serial.print(F("CIE x: ")); Serial.println(CIEx, 8);
|
||||
Serial.print(F("CIE y: ")); Serial.println(CIEy, 8);
|
||||
Serial.print(F("Lux: ")); Serial.println(lux, 4);
|
||||
|
||||
// Calculate and display color temperature
|
||||
double colorTemp = sensor.getColorTemperature();
|
||||
Serial.print(F("Color Temperature: "));
|
||||
Serial.print(colorTemp, 2);
|
||||
Serial.println(F(" K"));
|
||||
} else {
|
||||
Serial.println(F("\nError calculating CIE coordinates"));
|
||||
}
|
||||
Serial.println();
|
||||
} else {
|
||||
Serial.println(F("Error reading sensor data"));
|
||||
|
|
|
|||
Loading…
Reference in a new issue