Lib
Dependents: ProjetLong_Serre_V3 ProjetLong_Serre_V3_1 AgroTech
Adafruit_TCS34725.cpp@0:69073a593c65, 2018-10-07 (annotated)
- Committer:
- leandre
- Date:
- Sun Oct 07 16:01:58 2018 +0000
- Revision:
- 0:69073a593c65
OK
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
leandre | 0:69073a593c65 | 1 | #include "Adafruit_TCS34725.h" |
leandre | 0:69073a593c65 | 2 | |
leandre | 0:69073a593c65 | 3 | /*example***************************************************************** |
leandre | 0:69073a593c65 | 4 | |
leandre | 0:69073a593c65 | 5 | #include "mbed.h" |
leandre | 0:69073a593c65 | 6 | #include "Adafruit_TCS34725.h" |
leandre | 0:69073a593c65 | 7 | |
leandre | 0:69073a593c65 | 8 | #define commonAnode true |
leandre | 0:69073a593c65 | 9 | |
leandre | 0:69073a593c65 | 10 | I2C i2c(p28, p27); |
leandre | 0:69073a593c65 | 11 | Adafruit_TCS34725 tcs = Adafruit_TCS34725(&i2c, TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X); |
leandre | 0:69073a593c65 | 12 | |
leandre | 0:69073a593c65 | 13 | int main() |
leandre | 0:69073a593c65 | 14 | { |
leandre | 0:69073a593c65 | 15 | char gammatable[256]; |
leandre | 0:69073a593c65 | 16 | if (tcs.begin()) |
leandre | 0:69073a593c65 | 17 | { |
leandre | 0:69073a593c65 | 18 | printf("Found sensor"); |
leandre | 0:69073a593c65 | 19 | } |
leandre | 0:69073a593c65 | 20 | else |
leandre | 0:69073a593c65 | 21 | { |
leandre | 0:69073a593c65 | 22 | printf("No TCS34725 found ... check your connections"); |
leandre | 0:69073a593c65 | 23 | while (1); // halt! |
leandre | 0:69073a593c65 | 24 | } |
leandre | 0:69073a593c65 | 25 | for (int i=0; i<256; i++) |
leandre | 0:69073a593c65 | 26 | { |
leandre | 0:69073a593c65 | 27 | float x = i; |
leandre | 0:69073a593c65 | 28 | x /= 255; |
leandre | 0:69073a593c65 | 29 | x = pow((double)x, 2.5); |
leandre | 0:69073a593c65 | 30 | x *= 255; |
leandre | 0:69073a593c65 | 31 | if (commonAnode) |
leandre | 0:69073a593c65 | 32 | { |
leandre | 0:69073a593c65 | 33 | gammatable[i] = 255 - x; |
leandre | 0:69073a593c65 | 34 | } |
leandre | 0:69073a593c65 | 35 | else |
leandre | 0:69073a593c65 | 36 | { |
leandre | 0:69073a593c65 | 37 | gammatable[i] = x; |
leandre | 0:69073a593c65 | 38 | } |
leandre | 0:69073a593c65 | 39 | printf("%d\r\n", gammatable[i]); |
leandre | 0:69073a593c65 | 40 | } |
leandre | 0:69073a593c65 | 41 | while(1) |
leandre | 0:69073a593c65 | 42 | { |
leandre | 0:69073a593c65 | 43 | uint16_t clear, red, green, blue; |
leandre | 0:69073a593c65 | 44 | tcs.setInterrupt(false); // turn on LED |
leandre | 0:69073a593c65 | 45 | tcs.getRawData(&red, &green, &blue, &clear); |
leandre | 0:69073a593c65 | 46 | tcs.setInterrupt(true); // turn off LED |
leandre | 0:69073a593c65 | 47 | printf("%d, %d, %d, %d\r\n", clear, red, green, blue); |
leandre | 0:69073a593c65 | 48 | // Figure out some basic hex code for visualization |
leandre | 0:69073a593c65 | 49 | uint32_t sum = clear; |
leandre | 0:69073a593c65 | 50 | float r, g, b; |
leandre | 0:69073a593c65 | 51 | r = red; r /= sum; |
leandre | 0:69073a593c65 | 52 | g = green; g /= sum; |
leandre | 0:69073a593c65 | 53 | b = blue; b /= sum; |
leandre | 0:69073a593c65 | 54 | r *= 256; g *= 256; b *= 256; |
leandre | 0:69073a593c65 | 55 | } |
leandre | 0:69073a593c65 | 56 | } |
leandre | 0:69073a593c65 | 57 | |
leandre | 0:69073a593c65 | 58 | |
leandre | 0:69073a593c65 | 59 | *************************************************************************/ |
leandre | 0:69073a593c65 | 60 | |
leandre | 0:69073a593c65 | 61 | void Adafruit_TCS34725::write8 (uint8_t reg, uint32_t value) |
leandre | 0:69073a593c65 | 62 | { |
leandre | 0:69073a593c65 | 63 | char data[2] = {TCS34725_COMMAND_BIT | reg, value & 0xFF}; |
leandre | 0:69073a593c65 | 64 | _i2c->write(TCS34725_ADDRESS, data, 2); |
leandre | 0:69073a593c65 | 65 | } |
leandre | 0:69073a593c65 | 66 | |
leandre | 0:69073a593c65 | 67 | uint8_t Adafruit_TCS34725::read8(uint8_t reg) |
leandre | 0:69073a593c65 | 68 | { |
leandre | 0:69073a593c65 | 69 | char data[2] = {TCS34725_COMMAND_BIT | reg, 0}; |
leandre | 0:69073a593c65 | 70 | char r_data = 0; |
leandre | 0:69073a593c65 | 71 | _i2c->write(TCS34725_ADDRESS, data, 1); |
leandre | 0:69073a593c65 | 72 | _i2c->read(TCS34725_ADDRESS, &r_data, 1); |
leandre | 0:69073a593c65 | 73 | |
leandre | 0:69073a593c65 | 74 | return r_data; |
leandre | 0:69073a593c65 | 75 | } |
leandre | 0:69073a593c65 | 76 | |
leandre | 0:69073a593c65 | 77 | uint16_t Adafruit_TCS34725::read16(uint8_t reg) |
leandre | 0:69073a593c65 | 78 | { |
leandre | 0:69073a593c65 | 79 | uint16_t x; uint16_t t; |
leandre | 0:69073a593c65 | 80 | char data[2] = {TCS34725_COMMAND_BIT | reg, 0}; |
leandre | 0:69073a593c65 | 81 | char r_data[2] = {}; |
leandre | 0:69073a593c65 | 82 | _i2c->write(TCS34725_ADDRESS, data, 1); |
leandre | 0:69073a593c65 | 83 | _i2c->read(TCS34725_ADDRESS, r_data, 2); |
leandre | 0:69073a593c65 | 84 | t = r_data[0]; |
leandre | 0:69073a593c65 | 85 | x = r_data[1]; |
leandre | 0:69073a593c65 | 86 | x <<= 8; |
leandre | 0:69073a593c65 | 87 | x |= t; |
leandre | 0:69073a593c65 | 88 | return x; |
leandre | 0:69073a593c65 | 89 | } |
leandre | 0:69073a593c65 | 90 | |
leandre | 0:69073a593c65 | 91 | void Adafruit_TCS34725::enable(void) |
leandre | 0:69073a593c65 | 92 | { |
leandre | 0:69073a593c65 | 93 | write8(TCS34725_ENABLE, TCS34725_ENABLE_PON); |
leandre | 0:69073a593c65 | 94 | wait_ms(3); |
leandre | 0:69073a593c65 | 95 | write8(TCS34725_ENABLE, TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN); |
leandre | 0:69073a593c65 | 96 | } |
leandre | 0:69073a593c65 | 97 | |
leandre | 0:69073a593c65 | 98 | void Adafruit_TCS34725::disable(void) |
leandre | 0:69073a593c65 | 99 | { |
leandre | 0:69073a593c65 | 100 | /* Turn the device off to save power */ |
leandre | 0:69073a593c65 | 101 | uint8_t reg = 0; |
leandre | 0:69073a593c65 | 102 | reg = read8(TCS34725_ENABLE); |
leandre | 0:69073a593c65 | 103 | write8(TCS34725_ENABLE, reg & ~(TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN)); |
leandre | 0:69073a593c65 | 104 | } |
leandre | 0:69073a593c65 | 105 | |
leandre | 0:69073a593c65 | 106 | Adafruit_TCS34725::Adafruit_TCS34725(I2C *i2c, tcs34725IntegrationTime_t it, tcs34725Gain_t gain) |
leandre | 0:69073a593c65 | 107 | { |
leandre | 0:69073a593c65 | 108 | _i2c = i2c; |
leandre | 0:69073a593c65 | 109 | _tcs34725Initialised = false; |
leandre | 0:69073a593c65 | 110 | _tcs34725IntegrationTime = it; |
leandre | 0:69073a593c65 | 111 | _tcs34725Gain = gain; |
leandre | 0:69073a593c65 | 112 | } |
leandre | 0:69073a593c65 | 113 | |
leandre | 0:69073a593c65 | 114 | bool Adafruit_TCS34725::begin(void) |
leandre | 0:69073a593c65 | 115 | { |
leandre | 0:69073a593c65 | 116 | /* Make sure we're actually connected */ |
leandre | 0:69073a593c65 | 117 | uint8_t x = read8(TCS34725_ID); |
leandre | 0:69073a593c65 | 118 | //Serial.println(x, HEX); |
leandre | 0:69073a593c65 | 119 | if (x != 0x44) |
leandre | 0:69073a593c65 | 120 | { |
leandre | 0:69073a593c65 | 121 | return false; |
leandre | 0:69073a593c65 | 122 | } |
leandre | 0:69073a593c65 | 123 | _tcs34725Initialised = true; |
leandre | 0:69073a593c65 | 124 | |
leandre | 0:69073a593c65 | 125 | /* Set default integration time and gain */ |
leandre | 0:69073a593c65 | 126 | setIntegrationTime(_tcs34725IntegrationTime); |
leandre | 0:69073a593c65 | 127 | setGain(_tcs34725Gain); |
leandre | 0:69073a593c65 | 128 | |
leandre | 0:69073a593c65 | 129 | /* Note: by default, the device is in power down mode on bootup */ |
leandre | 0:69073a593c65 | 130 | enable(); |
leandre | 0:69073a593c65 | 131 | |
leandre | 0:69073a593c65 | 132 | return true; |
leandre | 0:69073a593c65 | 133 | } |
leandre | 0:69073a593c65 | 134 | |
leandre | 0:69073a593c65 | 135 | void Adafruit_TCS34725::setIntegrationTime(tcs34725IntegrationTime_t it) |
leandre | 0:69073a593c65 | 136 | { |
leandre | 0:69073a593c65 | 137 | if (!_tcs34725Initialised) begin(); |
leandre | 0:69073a593c65 | 138 | |
leandre | 0:69073a593c65 | 139 | /* Update the timing register */ |
leandre | 0:69073a593c65 | 140 | write8(TCS34725_ATIME, it); |
leandre | 0:69073a593c65 | 141 | |
leandre | 0:69073a593c65 | 142 | /* Update value placeholders */ |
leandre | 0:69073a593c65 | 143 | _tcs34725IntegrationTime = it; |
leandre | 0:69073a593c65 | 144 | } |
leandre | 0:69073a593c65 | 145 | |
leandre | 0:69073a593c65 | 146 | void Adafruit_TCS34725::setGain(tcs34725Gain_t gain) |
leandre | 0:69073a593c65 | 147 | { |
leandre | 0:69073a593c65 | 148 | if (!_tcs34725Initialised) begin(); |
leandre | 0:69073a593c65 | 149 | |
leandre | 0:69073a593c65 | 150 | /* Update the timing register */ |
leandre | 0:69073a593c65 | 151 | write8(TCS34725_CONTROL, gain); |
leandre | 0:69073a593c65 | 152 | |
leandre | 0:69073a593c65 | 153 | /* Update value placeholders */ |
leandre | 0:69073a593c65 | 154 | _tcs34725Gain = gain; |
leandre | 0:69073a593c65 | 155 | } |
leandre | 0:69073a593c65 | 156 | |
leandre | 0:69073a593c65 | 157 | void Adafruit_TCS34725::getRawData (uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c) |
leandre | 0:69073a593c65 | 158 | { |
leandre | 0:69073a593c65 | 159 | if (!_tcs34725Initialised) begin(); |
leandre | 0:69073a593c65 | 160 | |
leandre | 0:69073a593c65 | 161 | *c = read16(TCS34725_CDATAL); |
leandre | 0:69073a593c65 | 162 | *r = read16(TCS34725_RDATAL); |
leandre | 0:69073a593c65 | 163 | *g = read16(TCS34725_GDATAL); |
leandre | 0:69073a593c65 | 164 | *b = read16(TCS34725_BDATAL); |
leandre | 0:69073a593c65 | 165 | |
leandre | 0:69073a593c65 | 166 | /* Set a delay for the integration time */ |
leandre | 0:69073a593c65 | 167 | switch (_tcs34725IntegrationTime) |
leandre | 0:69073a593c65 | 168 | { |
leandre | 0:69073a593c65 | 169 | case TCS34725_INTEGRATIONTIME_2_4MS: |
leandre | 0:69073a593c65 | 170 | wait_ms(3); |
leandre | 0:69073a593c65 | 171 | break; |
leandre | 0:69073a593c65 | 172 | case TCS34725_INTEGRATIONTIME_24MS: |
leandre | 0:69073a593c65 | 173 | wait_ms(24); |
leandre | 0:69073a593c65 | 174 | break; |
leandre | 0:69073a593c65 | 175 | case TCS34725_INTEGRATIONTIME_50MS: |
leandre | 0:69073a593c65 | 176 | wait_ms(50); |
leandre | 0:69073a593c65 | 177 | break; |
leandre | 0:69073a593c65 | 178 | case TCS34725_INTEGRATIONTIME_101MS: |
leandre | 0:69073a593c65 | 179 | wait_ms(101); |
leandre | 0:69073a593c65 | 180 | break; |
leandre | 0:69073a593c65 | 181 | case TCS34725_INTEGRATIONTIME_154MS: |
leandre | 0:69073a593c65 | 182 | wait_ms(154); |
leandre | 0:69073a593c65 | 183 | break; |
leandre | 0:69073a593c65 | 184 | case TCS34725_INTEGRATIONTIME_700MS: |
leandre | 0:69073a593c65 | 185 | wait_ms(700); |
leandre | 0:69073a593c65 | 186 | break; |
leandre | 0:69073a593c65 | 187 | } |
leandre | 0:69073a593c65 | 188 | } |
leandre | 0:69073a593c65 | 189 | |
leandre | 0:69073a593c65 | 190 | uint16_t Adafruit_TCS34725::calculateColorTemperature(uint16_t r, uint16_t g, uint16_t b) |
leandre | 0:69073a593c65 | 191 | { |
leandre | 0:69073a593c65 | 192 | float X, Y, Z; /* RGB to XYZ correlation */ |
leandre | 0:69073a593c65 | 193 | float xc, yc; /* Chromaticity co-ordinates */ |
leandre | 0:69073a593c65 | 194 | float n; /* McCamy's formula */ |
leandre | 0:69073a593c65 | 195 | float cct; |
leandre | 0:69073a593c65 | 196 | |
leandre | 0:69073a593c65 | 197 | /* 1. Map RGB values to their XYZ counterparts. */ |
leandre | 0:69073a593c65 | 198 | /* Based on 6500K fluorescent, 3000K fluorescent */ |
leandre | 0:69073a593c65 | 199 | /* and 60W incandescent values for a wide range. */ |
leandre | 0:69073a593c65 | 200 | /* Note: Y = Illuminance or lux */ |
leandre | 0:69073a593c65 | 201 | X = (-0.14282F * r) + (1.54924F * g) + (-0.95641F * b); |
leandre | 0:69073a593c65 | 202 | Y = (-0.32466F * r) + (1.57837F * g) + (-0.73191F * b); |
leandre | 0:69073a593c65 | 203 | Z = (-0.68202F * r) + (0.77073F * g) + ( 0.56332F * b); |
leandre | 0:69073a593c65 | 204 | |
leandre | 0:69073a593c65 | 205 | /* 2. Calculate the chromaticity co-ordinates */ |
leandre | 0:69073a593c65 | 206 | xc = (X) / (X + Y + Z); |
leandre | 0:69073a593c65 | 207 | yc = (Y) / (X + Y + Z); |
leandre | 0:69073a593c65 | 208 | |
leandre | 0:69073a593c65 | 209 | /* 3. Use McCamy's formula to determine the CCT */ |
leandre | 0:69073a593c65 | 210 | n = (xc - 0.3320F) / (0.1858F - yc); |
leandre | 0:69073a593c65 | 211 | |
leandre | 0:69073a593c65 | 212 | /* Calculate the final CCT */ |
leandre | 0:69073a593c65 | 213 | cct = (449.0F * powf(n, 3)) + (3525.0F * powf(n, 2)) + (6823.3F * n) + 5520.33F; |
leandre | 0:69073a593c65 | 214 | |
leandre | 0:69073a593c65 | 215 | /* Return the results in degrees Kelvin */ |
leandre | 0:69073a593c65 | 216 | return (uint16_t)cct; |
leandre | 0:69073a593c65 | 217 | } |
leandre | 0:69073a593c65 | 218 | |
leandre | 0:69073a593c65 | 219 | uint16_t Adafruit_TCS34725::calculateLux(uint16_t r, uint16_t g, uint16_t b) |
leandre | 0:69073a593c65 | 220 | { |
leandre | 0:69073a593c65 | 221 | float illuminance; |
leandre | 0:69073a593c65 | 222 | |
leandre | 0:69073a593c65 | 223 | /* This only uses RGB ... how can we integrate clear or calculate lux */ |
leandre | 0:69073a593c65 | 224 | /* based exclusively on clear since this might be more reliable? */ |
leandre | 0:69073a593c65 | 225 | illuminance = (-0.32466F * r) + (1.57837F * g) + (-0.73191F * b); |
leandre | 0:69073a593c65 | 226 | |
leandre | 0:69073a593c65 | 227 | return (uint16_t)illuminance; |
leandre | 0:69073a593c65 | 228 | } |
leandre | 0:69073a593c65 | 229 | |
leandre | 0:69073a593c65 | 230 | |
leandre | 0:69073a593c65 | 231 | void Adafruit_TCS34725::setInterrupt(bool i) |
leandre | 0:69073a593c65 | 232 | { |
leandre | 0:69073a593c65 | 233 | uint8_t r = read8(TCS34725_ENABLE); |
leandre | 0:69073a593c65 | 234 | if (i) |
leandre | 0:69073a593c65 | 235 | { |
leandre | 0:69073a593c65 | 236 | r |= TCS34725_ENABLE_AIEN; |
leandre | 0:69073a593c65 | 237 | } |
leandre | 0:69073a593c65 | 238 | else |
leandre | 0:69073a593c65 | 239 | { |
leandre | 0:69073a593c65 | 240 | r &= ~TCS34725_ENABLE_AIEN; |
leandre | 0:69073a593c65 | 241 | } |
leandre | 0:69073a593c65 | 242 | write8(TCS34725_ENABLE, r); |
leandre | 0:69073a593c65 | 243 | } |
leandre | 0:69073a593c65 | 244 | |
leandre | 0:69073a593c65 | 245 | void Adafruit_TCS34725::clearInterrupt(void) |
leandre | 0:69073a593c65 | 246 | { |
leandre | 0:69073a593c65 | 247 | char data[2] = {0x66, 0}; |
leandre | 0:69073a593c65 | 248 | _i2c->write(TCS34725_ADDRESS, data, 1); |
leandre | 0:69073a593c65 | 249 | } |
leandre | 0:69073a593c65 | 250 | |
leandre | 0:69073a593c65 | 251 | |
leandre | 0:69073a593c65 | 252 | void Adafruit_TCS34725::setIntLimits(uint16_t low, uint16_t high) |
leandre | 0:69073a593c65 | 253 | { |
leandre | 0:69073a593c65 | 254 | write8(0x04, low & 0xFF); |
leandre | 0:69073a593c65 | 255 | write8(0x05, low >> 8); |
leandre | 0:69073a593c65 | 256 | write8(0x06, high & 0xFF); |
leandre | 0:69073a593c65 | 257 | write8(0x07, high >> 8); |
leandre | 0:69073a593c65 | 258 | } |