GROVE i2c color sensor Library. based on https://github.com/Seeed-Studio/Grove_I2C_Color_Sensor_TCS3472
Dependents: PROJ 2PA2S 2PA2S_v2 2PA2S-interrupteur
Adafruit_TCS34725.cpp
00001 #include "Adafruit_TCS34725.h " 00002 00003 /*example***************************************************************** 00004 00005 #include "mbed.h" 00006 #include "Adafruit_TCS34725.h" 00007 00008 #define commonAnode true 00009 00010 I2C i2c(p28, p27); 00011 Adafruit_TCS34725 tcs = Adafruit_TCS34725(&i2c, TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X); 00012 00013 int main() 00014 { 00015 char gammatable[256]; 00016 if (tcs.begin()) 00017 { 00018 printf("Found sensor"); 00019 } 00020 else 00021 { 00022 printf("No TCS34725 found ... check your connections"); 00023 while (1); // halt! 00024 } 00025 for (int i=0; i<256; i++) 00026 { 00027 float x = i; 00028 x /= 255; 00029 x = pow((double)x, 2.5); 00030 x *= 255; 00031 if (commonAnode) 00032 { 00033 gammatable[i] = 255 - x; 00034 } 00035 else 00036 { 00037 gammatable[i] = x; 00038 } 00039 printf("%d\r\n", gammatable[i]); 00040 } 00041 while(1) 00042 { 00043 uint16_t clear, red, green, blue; 00044 tcs.setInterrupt(false); // turn on LED 00045 tcs.getRawData(&red, &green, &blue, &clear); 00046 tcs.setInterrupt(true); // turn off LED 00047 printf("%d, %d, %d, %d\r\n", clear, red, green, blue); 00048 // Figure out some basic hex code for visualization 00049 uint32_t sum = clear; 00050 float r, g, b; 00051 r = red; r /= sum; 00052 g = green; g /= sum; 00053 b = blue; b /= sum; 00054 r *= 256; g *= 256; b *= 256; 00055 } 00056 } 00057 00058 00059 *************************************************************************/ 00060 00061 void Adafruit_TCS34725::write8 (uint8_t reg, uint32_t value) 00062 { 00063 char data[2] = {TCS34725_COMMAND_BIT | reg, value & 0xFF}; 00064 _i2c->write(TCS34725_ADDRESS, data, 2); 00065 } 00066 00067 uint8_t Adafruit_TCS34725::read8(uint8_t reg) 00068 { 00069 char data[2] = {TCS34725_COMMAND_BIT | reg, 0}; 00070 char r_data = 0; 00071 _i2c->write(TCS34725_ADDRESS, data, 1); 00072 _i2c->read(TCS34725_ADDRESS, &r_data, 1); 00073 00074 return r_data; 00075 } 00076 00077 uint16_t Adafruit_TCS34725::read16(uint8_t reg) 00078 { 00079 uint16_t x; uint16_t t; 00080 char data[2] = {TCS34725_COMMAND_BIT | reg, 0}; 00081 char r_data[2] = {}; 00082 _i2c->write(TCS34725_ADDRESS, data, 1); 00083 _i2c->read(TCS34725_ADDRESS, r_data, 2); 00084 t = r_data[0]; 00085 x = r_data[1]; 00086 x <<= 8; 00087 x |= t; 00088 return x; 00089 } 00090 00091 void Adafruit_TCS34725::enable(void) 00092 { 00093 write8(TCS34725_ENABLE, TCS34725_ENABLE_PON); 00094 wait_ms(3); 00095 write8(TCS34725_ENABLE, TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN); 00096 } 00097 00098 void Adafruit_TCS34725::disable(void) 00099 { 00100 /* Turn the device off to save power */ 00101 uint8_t reg = 0; 00102 reg = read8(TCS34725_ENABLE); 00103 write8(TCS34725_ENABLE, reg & ~(TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN)); 00104 } 00105 00106 Adafruit_TCS34725::Adafruit_TCS34725(I2C *i2c, tcs34725IntegrationTime_t it, tcs34725Gain_t gain) 00107 { 00108 _i2c = i2c; 00109 _tcs34725Initialised = false; 00110 _tcs34725IntegrationTime = it; 00111 _tcs34725Gain = gain; 00112 } 00113 00114 bool Adafruit_TCS34725::begin(void) 00115 { 00116 /* Make sure we're actually connected */ 00117 uint8_t x = read8(TCS34725_ID); 00118 //Serial.println(x, HEX); 00119 if (x != 0x44) 00120 { 00121 return false; 00122 } 00123 _tcs34725Initialised = true; 00124 00125 /* Set default integration time and gain */ 00126 setIntegrationTime(_tcs34725IntegrationTime); 00127 setGain(_tcs34725Gain); 00128 00129 /* Note: by default, the device is in power down mode on bootup */ 00130 enable(); 00131 00132 return true; 00133 } 00134 00135 void Adafruit_TCS34725::setIntegrationTime(tcs34725IntegrationTime_t it) 00136 { 00137 if (!_tcs34725Initialised) begin(); 00138 00139 /* Update the timing register */ 00140 write8(TCS34725_ATIME, it); 00141 00142 /* Update value placeholders */ 00143 _tcs34725IntegrationTime = it; 00144 } 00145 00146 void Adafruit_TCS34725::setGain(tcs34725Gain_t gain) 00147 { 00148 if (!_tcs34725Initialised) begin(); 00149 00150 /* Update the timing register */ 00151 write8(TCS34725_CONTROL, gain); 00152 00153 /* Update value placeholders */ 00154 _tcs34725Gain = gain; 00155 } 00156 00157 void Adafruit_TCS34725::getRawData (uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c) 00158 { 00159 if (!_tcs34725Initialised) begin(); 00160 00161 *c = read16(TCS34725_CDATAL); 00162 *r = read16(TCS34725_RDATAL); 00163 *g = read16(TCS34725_GDATAL); 00164 *b = read16(TCS34725_BDATAL); 00165 00166 /* Set a delay for the integration time */ 00167 switch (_tcs34725IntegrationTime) 00168 { 00169 case TCS34725_INTEGRATIONTIME_2_4MS: 00170 wait_ms(3); 00171 break; 00172 case TCS34725_INTEGRATIONTIME_24MS: 00173 wait_ms(24); 00174 break; 00175 case TCS34725_INTEGRATIONTIME_50MS: 00176 wait_ms(50); 00177 break; 00178 case TCS34725_INTEGRATIONTIME_101MS: 00179 wait_ms(101); 00180 break; 00181 case TCS34725_INTEGRATIONTIME_154MS: 00182 wait_ms(154); 00183 break; 00184 case TCS34725_INTEGRATIONTIME_700MS: 00185 wait_ms(700); 00186 break; 00187 } 00188 } 00189 00190 uint16_t Adafruit_TCS34725::calculateColorTemperature(uint16_t r, uint16_t g, uint16_t b) 00191 { 00192 float X, Y, Z; /* RGB to XYZ correlation */ 00193 float xc, yc; /* Chromaticity co-ordinates */ 00194 float n; /* McCamy's formula */ 00195 float cct; 00196 00197 /* 1. Map RGB values to their XYZ counterparts. */ 00198 /* Based on 6500K fluorescent, 3000K fluorescent */ 00199 /* and 60W incandescent values for a wide range. */ 00200 /* Note: Y = Illuminance or lux */ 00201 X = (-0.14282F * r) + (1.54924F * g) + (-0.95641F * b); 00202 Y = (-0.32466F * r) + (1.57837F * g) + (-0.73191F * b); 00203 Z = (-0.68202F * r) + (0.77073F * g) + ( 0.56332F * b); 00204 00205 /* 2. Calculate the chromaticity co-ordinates */ 00206 xc = (X) / (X + Y + Z); 00207 yc = (Y) / (X + Y + Z); 00208 00209 /* 3. Use McCamy's formula to determine the CCT */ 00210 n = (xc - 0.3320F) / (0.1858F - yc); 00211 00212 /* Calculate the final CCT */ 00213 cct = (449.0F * powf(n, 3)) + (3525.0F * powf(n, 2)) + (6823.3F * n) + 5520.33F; 00214 00215 /* Return the results in degrees Kelvin */ 00216 return (uint16_t)cct; 00217 } 00218 00219 uint16_t Adafruit_TCS34725::calculateLux(uint16_t r, uint16_t g, uint16_t b) 00220 { 00221 float illuminance; 00222 00223 /* This only uses RGB ... how can we integrate clear or calculate lux */ 00224 /* based exclusively on clear since this might be more reliable? */ 00225 illuminance = (-0.32466F * r) + (1.57837F * g) + (-0.73191F * b); 00226 00227 return (uint16_t)illuminance; 00228 } 00229 00230 00231 void Adafruit_TCS34725::setInterrupt(bool i) 00232 { 00233 uint8_t r = read8(TCS34725_ENABLE); 00234 if (i) 00235 { 00236 r |= TCS34725_ENABLE_AIEN; 00237 } 00238 else 00239 { 00240 r &= ~TCS34725_ENABLE_AIEN; 00241 } 00242 write8(TCS34725_ENABLE, r); 00243 } 00244 00245 void Adafruit_TCS34725::clearInterrupt(void) 00246 { 00247 char data[2] = {0x66, 0}; 00248 _i2c->write(TCS34725_ADDRESS, data, 1); 00249 } 00250 00251 00252 void Adafruit_TCS34725::setIntLimits(uint16_t low, uint16_t high) 00253 { 00254 write8(0x04, low & 0xFF); 00255 write8(0x05, low >> 8); 00256 write8(0x06, high & 0xFF); 00257 write8(0x07, high >> 8); 00258 }
Generated on Sun Jul 17 2022 20:00:18 by 1.7.2