For use with STM32L746RG . Class which provides functions to control a TAOS TCS3472 Color Light-to-Digital Converter with IR Filter via I2C.
Fork of TCS3472_I2C by
Diff: TCS3472_I2C.cpp
- Revision:
- 7:ab9ff8738826
- Parent:
- 6:6d5bb4ad7d6e
- Child:
- 8:764a98777c11
diff -r 6d5bb4ad7d6e -r ab9ff8738826 TCS3472_I2C.cpp --- a/TCS3472_I2C.cpp Thu Apr 24 09:14:25 2014 +0000 +++ b/TCS3472_I2C.cpp Sun Apr 30 08:00:52 2017 +0000 @@ -43,12 +43,15 @@ void TCS3472_I2C::getAllColors( int* readings ){ char buffer[8] = { 0 }; - readMultipleRegisters( CDATA, buffer, 8 ); - - readings[0] = (int)buffer[1] << 8 | (int)buffer[0]; - readings[1] = (int)buffer[3] << 8 | (int)buffer[2]; - readings[2] = (int)buffer[5] << 8 | (int)buffer[4]; - readings[3] = (int)buffer[7] << 8 | (int)buffer[6]; + if (readMultipleRegisters( CDATA, buffer, 8 ) ==0){ + readings[0] = (int)buffer[1] << 8 | (int)buffer[0];//c + readings[1] = (int)buffer[3] << 8 | (int)buffer[2];//r + readings[2] = (int)buffer[5] << 8 | (int)buffer[4];//g + readings[3] = (int)buffer[7] << 8 | (int)buffer[6];//b + } + else{ + readings[0] = 0;readings[1] = 0;readings[2] = 0;readings[3] = 0; + } } int TCS3472_I2C::getClearData(){ @@ -178,7 +181,7 @@ } int TCS3472_I2C::setIntegrationTime( const float itime ){ - char atime = 256 - roundTowardsZero( itime / 2.4 ); // rounding ensures nearest value of atime is used + char atime = 256 - (int)roundTowardsZero( itime / 2.4 ); // rounding ensures nearest value of atime is used int ack = writeSingleRegister( ATIME, atime ); return ack; } @@ -447,4 +450,85 @@ result = ceil(value); } return result; +} + +int TCS3472_I2C::calculateColorTemperature(int r, int g, int b) +{ + 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 * r) + (1.54924F * g) + (-0.95641F * b); + Y = (-0.32466F * r) + (1.57837F * g) + (-0.73191F * b); + Z = (-0.68202F * r) + (0.77073F * g) + ( 0.56332F * 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 */ + cct = -(449.0F * powf(n, 3)) + (3525.0F * powf(n, 2)) - (6823.3F * n) + 5520.33F; + + /* Return the results in degrees Kelvin */ + return (int)cct; +} + +float TCS3472_I2C::calculateChromaticityX(int r, int g, int b) +{ + float X, Y, Z; /* RGB to XYZ correlation */ + float xc, yc; /* Chromaticity co-ordinates */ + + /* 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 * r) + (1.54924F * g) + (-0.95641F * b); + Y = (-0.32466F * r) + (1.57837F * g) + (-0.73191F * b); + Z = (-0.68202F * r) + (0.77073F * g) + ( 0.56332F * b); + + /* 2. Calculate the chromaticity co-ordinates */ + xc = (X) / (X + Y + Z); + yc = (Y) / (X + Y + Z); + + /* Return the results in degrees Kelvin */ + return (float)xc; +} +float TCS3472_I2C::calculateChromaticityY(int r, int g, int b) +{ + float X, Y, Z; /* RGB to XYZ correlation */ + float xc, yc; /* Chromaticity co-ordinates */ + + /* 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 * r) + (1.54924F * g) + (-0.95641F * b); + Y = (-0.32466F * r) + (1.57837F * g) + (-0.73191F * b); + Z = (-0.68202F * r) + (0.77073F * g) + ( 0.56332F * b); + + /* 2. Calculate the chromaticity co-ordinates */ + xc = (X) / (X + Y + Z); + yc = (Y) / (X + Y + Z); + + /* Return the results in degrees Kelvin */ + return (float)yc; +} + +int TCS3472_I2C::calculateLux(int r, int g, int b) +{ + float illuminance; + + /* This only uses RGB ... how can we integrate clear or calculate lux */ + /* based exclusively on clear since this might be more reliable? */ + illuminance = (-0.32466F * r) + (1.57837F * g) + (-0.73191F * b); + + return (int)illuminance; } \ No newline at end of file