TCS34725(1125).c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. #include "TCS34725.h"
  2. tcs34725IntegrationTime_t _tcs34725IntegrationTime;
  3. tcs34725Gain_t _tcs34725Gain;
  4. uint8_t TCS34725_I2C_Read(uint8_t addr, uint8_t reg)
  5. {
  6. uint8_t TCS34725_I2C[3]={0,};
  7. uint16_t value = 0;
  8. uint8_t data = 0;
  9. data=HAL_I2C_Mem_Read(&hi2c1, addr, reg, 1, TCS34725_I2C, 1, 10);
  10. // i2c_status(data);
  11. value = TCS34725_I2C[0];
  12. return value;
  13. }
  14. void TCS34725_I2C_Write(uint8_t addr, uint8_t reg, uint8_t data)
  15. {
  16. uint8_t tmp_afe;
  17. tmp_afe = data;
  18. HAL_I2C_Mem_Write(&hi2c1, addr, reg, 1, &tmp_afe, 1, 10);
  19. }
  20. /**************************************************************************/
  21. /*!
  22. Adjusts the gain on the TCS34725 (adjusts the sensitivity to light)
  23. */
  24. /**************************************************************************/
  25. void RGB_data_arrage(uint16_t Clear,uint16_t Red,uint16_t Green,uint16_t Blue){
  26. static uint16_t Clear_Calc_data = 0;
  27. static uint16_t Red_Calc_data = 0;
  28. static uint16_t Green_Calc_data = 0;
  29. static uint16_t Blue_Calc_data = 0;
  30. static uint8_t Cnt = 0;
  31. uint8_t temp[8] = {0,};
  32. #if 0 // PYJ.2019.03.15_BEGIN --
  33. switch(Cnt){
  34. case 0:
  35. Clear_Calc_data = Clear;
  36. Red_Calc_data = Red;
  37. Green_Calc_data = Green;
  38. Blue_Calc_data = Blue;
  39. Cnt = 1;
  40. break;
  41. case 1:
  42. Clear_Calc_data += Clear;
  43. Red_Calc_data += Red;
  44. Green_Calc_data += Green;
  45. Blue_Calc_data += Blue;
  46. Cnt = 2;
  47. break;
  48. case 2:
  49. Clear_Calc_data += Clear;
  50. Red_Calc_data += Red;
  51. Green_Calc_data += Green;
  52. Blue_Calc_data += Blue;
  53. Cnt = 3;
  54. break;
  55. case 3:
  56. Clear_Calc_data += Clear;
  57. Red_Calc_data += Red;
  58. Green_Calc_data += Green;
  59. Blue_Calc_data += Blue;
  60. temp[0] = ((Clear_Calc_data & 0xFF00) >> 8);
  61. temp[1] = (Clear_Calc_data & 0x00FF);
  62. temp[2] = ((Red_Calc_data & 0xFF00) >> 8);
  63. temp[3] = (Red_Calc_data & 0x00FF);
  64. temp[4] = ((Green_Calc_data & 0xFF00) >> 8);
  65. temp[5] = (Green_Calc_data & 0x00FF);
  66. temp[6] = ((Blue_Calc_data & 0xFF00) >> 8);
  67. temp[7] = (Blue_Calc_data & 0x00FF);
  68. Uart2_Data_Send(temp,8);
  69. Cnt = 0;
  70. break;
  71. }
  72. #else
  73. temp[0] = ((Clear_Calc_data & 0xFF00) >> 8);
  74. temp[1] = (Clear_Calc_data & 0x00FF);
  75. temp[2] = ((Red_Calc_data & 0xFF00) >> 8);
  76. temp[3] = (Red_Calc_data & 0x00FF);
  77. temp[4] = ((Green_Calc_data & 0xFF00) >> 8);
  78. temp[5] = (Green_Calc_data & 0x00FF);
  79. temp[6] = ((Blue_Calc_data & 0xFF00) >> 8);
  80. temp[7] = (Blue_Calc_data & 0x00FF);
  81. Uart2_Data_Send(temp,8);
  82. #endif // PYJ.2019.03.15_END --
  83. }
  84. void TCS34725_getrawdata(void)
  85. {
  86. uint8_t data_ret_R =0;
  87. uint8_t data_ret_G =0;
  88. uint8_t data_ret_B =0;
  89. uint8_t data_ret_C =0;
  90. uint8_t DEV_DATA = TCS34725_I2C_Read(TCS34725_ADDRESS, TCS34725_COMMAND_BIT | TCS34725_ID);
  91. uint8_t C_DATA_L = TCS34725_I2C_Read(TCS34725_ADDRESS, TCS34725_COMMAND_BIT | TCS34725_CDATAL);
  92. uint8_t C_DATA_H = TCS34725_I2C_Read(TCS34725_ADDRESS, TCS34725_COMMAND_BIT | TCS34725_CDATAH);
  93. uint8_t R_DATA_L = TCS34725_I2C_Read(TCS34725_ADDRESS, TCS34725_COMMAND_BIT | TCS34725_RDATAL);
  94. uint8_t R_DATA_H = TCS34725_I2C_Read(TCS34725_ADDRESS, TCS34725_COMMAND_BIT | TCS34725_RDATAH);
  95. uint8_t G_DATA_L = TCS34725_I2C_Read(TCS34725_ADDRESS, TCS34725_COMMAND_BIT | TCS34725_GDATAL);
  96. uint8_t G_DATA_H = TCS34725_I2C_Read(TCS34725_ADDRESS, TCS34725_COMMAND_BIT | TCS34725_GDATAH);
  97. uint8_t B_DATA_L = TCS34725_I2C_Read(TCS34725_ADDRESS, TCS34725_COMMAND_BIT | TCS34725_BDATAL);
  98. uint8_t B_DATA_H = TCS34725_I2C_Read(TCS34725_ADDRESS, TCS34725_COMMAND_BIT | TCS34725_BDATAH);
  99. #if 0 // PYJ.2019.03.15_BEGIN --
  100. printf("C_DATA_L : %02x C_DATA_H %02x \r\n",C_DATA_L,C_DATA_H);
  101. printf("R_DATA_L : %02x R_DATA_H %02x \r\n",R_DATA_L,R_DATA_H);
  102. printf("G_DATA_L : %02x G_DATA_H %02x \r\n",G_DATA_L,G_DATA_H);
  103. printf("B_DATA_L : %02x B_DATA_H %02x \r\n",B_DATA_L,B_DATA_H);
  104. #endif // PYJ.2019.03.15_END --
  105. #if 0
  106. double CLEAR = 0;
  107. double RED = 0;
  108. double GREEN = 0;
  109. double BLUE = 0;
  110. printf("************************************\r\n");
  111. printf("1. DEV ID\t:\t%x\r\n", DEV_DATA);
  112. CLEAR = (C_DATA_H << 8) | C_DATA_L;
  113. data_ret_C = (CLEAR / 65535)*255;
  114. printf("CLEAR : %d\r\n",data_ret_C);
  115. RED = (R_DATA_H << 8) | R_DATA_L;
  116. data_ret_R = (RED / 65535)*255;
  117. printf("RED : %d\r\n",data_ret_R);
  118. GREEN = (G_DATA_H << 8) | G_DATA_L;
  119. data_ret_G = (GREEN / 65535)*255;
  120. printf("GREEN : %d\r\n",data_ret_G);
  121. BLUE = (B_DATA_H << 8) | B_DATA_L;
  122. data_ret_B = (BLUE / 65535)*255;
  123. printf("BLUE : %d\r\n",data_ret_B);
  124. #else
  125. uint16_t CLEAR = 0;
  126. uint16_t RED = 0;
  127. uint16_t GREEN = 0;
  128. uint16_t BLUE = 0;
  129. #if 0 // PYJ.2019.03.15_BEGIN --
  130. CLEAR = (C_DATA_H << 8) | C_DATA_L;
  131. // data_ret_C = (CLEAR / 65535)*255;
  132. RED = (R_DATA_H << 8) | R_DATA_L;
  133. // data_ret_R = (RED / 65535)*255;
  134. GREEN = (G_DATA_H << 8) | G_DATA_L;
  135. // data_ret_G = (GREEN / 65535)*255;
  136. BLUE = (B_DATA_H << 8) | B_DATA_L;
  137. // data_ret_B = (BLUE / 65535)*255;
  138. #else
  139. CLEAR = (C_DATA_H << 8) | C_DATA_L;
  140. RED = (R_DATA_H << 8) | R_DATA_L;
  141. GREEN = (G_DATA_H << 8) | G_DATA_L;
  142. BLUE = (B_DATA_H << 8) | B_DATA_L;
  143. RGB_data_arrage(CLEAR,RED,GREEN,BLUE);
  144. #endif // PYJ.2019.03.15_END --
  145. #if 0
  146. printf("02%04x%05d%04x%05d%04x%05d%04x%05d03\r\n",data_ret_C,data_ret_C,data_ret_R,data_ret_R,data_ret_G,data_ret_G,data_ret_B,data_ret_B);
  147. #else
  148. // printf("%04x,%05d,%04x,%05d,%04x,%05d,%04x,%05d,\r\n",data_ret_C,data_ret_C,data_ret_R,data_ret_R,data_ret_G,data_ret_G,data_ret_B,data_ret_B);
  149. // printf("%x %d %d %d %d %d %d %d %d\r\n",DEV_DATA,(uint16_t)CLEAR,data_ret_C,(uint16_t)RED,data_ret_R,(uint16_t)GREEN,data_ret_G,(uint16_t)BLUE,data_ret_B);
  150. printf("Dev ID : (%x) %d %d %d %d \r\n",DEV_DATA,(uint16_t)CLEAR,(uint16_t)RED,(uint16_t)GREEN,(uint16_t)BLUE);
  151. #endif
  152. #endif
  153. #if 0
  154. uint16_t CLEAR = 0;
  155. uint16_t RED = 0;
  156. uint16_t GREEN = 0;
  157. uint16_t BLUE = 0;
  158. CLEAR = (C_DATA_H << 8) | C_DATA_L;
  159. RED = (R_DATA_H << 8) | R_DATA_L;
  160. GREEN = (G_DATA_H << 8) | G_DATA_L;
  161. BLUE = (B_DATA_H << 8) | B_DATA_L;
  162. printf("1. DEV ID\t:\t%x\r\n", DEV_DATA);
  163. printf("3. C\t:\tHEX : [%04x]\tDEC : [%05d]\r\n", CLEAR,CLEAR);
  164. printf("4. R\t:\tHEX : [%04x]\tDEC : [%05d]\r\n", RED,RED);
  165. printf("5. G\t:\tHEX : [%04x]\tDEC : [%05d]\r\n", GREEN,GREEN);
  166. printf("6. B\t:\tHEX : [%04x]\tDEC : [%05d]\r\n", BLUE,BLUE);
  167. #else
  168. #endif
  169. }
  170. void TCS34725_disable(void){
  171. /* Turn the device off to save power */
  172. uint8_t reg = 0;
  173. reg = TCS34725_I2C_Read(TCS34725_ADDRESS,TCS34725_ENABLE);
  174. TCS34725_I2C_Write(TCS34725_ADDRESS,TCS34725_ENABLE, reg & ~(TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN));
  175. }
  176. void TCS34725_enable(void)
  177. {
  178. TCS34725_I2C_Write(TCS34725_ADDRESS, TCS34725_ENABLE, TCS34725_ENABLE_PON);
  179. HAL_Delay(3);
  180. TCS34725_I2C_Write(TCS34725_ADDRESS, TCS34725_ENABLE, TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN);
  181. switch (_tcs34725IntegrationTime)
  182. {
  183. case TCS34725_INTEGRATIONTIME_2_4MS:
  184. HAL_Delay(3);
  185. break;
  186. case TCS34725_INTEGRATIONTIME_24MS:
  187. HAL_Delay(24);
  188. break;
  189. case TCS34725_INTEGRATIONTIME_50MS:
  190. HAL_Delay(50);
  191. break;
  192. case TCS34725_INTEGRATIONTIME_101MS:
  193. HAL_Delay(101);
  194. break;
  195. case TCS34725_INTEGRATIONTIME_154MS:
  196. HAL_Delay(154);
  197. break;
  198. case TCS34725_INTEGRATIONTIME_700MS:
  199. HAL_Delay(700);
  200. break;
  201. }
  202. }
  203. void tcs34725SetIntegrationTime(tcs34725IntegrationTime_t it)
  204. {
  205. TCS34725_I2C_Write(TCS34725_ADDRESS,TCS34725_ATIME, it);
  206. _tcs34725IntegrationTime = it;
  207. }
  208. void getRawDataOneShot (void)
  209. {
  210. // TCS34725_enable();
  211. TCS34725_getrawdata();
  212. // TCS34725_disable();
  213. }
  214. /**************************************************************************/
  215. /*!
  216. @brief Converts the raw R/G/B values to color temperature in degrees
  217. Kelvin
  218. */
  219. /**************************************************************************/
  220. uint16_t calculateColorTemperature(uint16_t r, uint16_t g, uint16_t b)
  221. {
  222. float X, Y, Z; /* RGB to XYZ correlation */
  223. float xc, yc; /* Chromaticity co-ordinates */
  224. float n; /* McCamy's formula */
  225. float cct;
  226. /* 1. Map RGB values to their XYZ counterparts. */
  227. /* Based on 6500K fluorescent, 3000K fluorescent */
  228. /* and 60W incandescent values for a wide range. */
  229. /* Note: Y = Illuminance or lux */
  230. X = (-0.14282F * r) + (1.54924F * g) + (-0.95641F * b);
  231. Y = (-0.32466F * r) + (1.57837F * g) + (-0.73191F * b);
  232. Z = (-0.68202F * r) + (0.77073F * g) + ( 0.56332F * b);
  233. /* 2. Calculate the chromaticity co-ordinates */
  234. xc = (X) / (X + Y + Z);
  235. yc = (Y) / (X + Y + Z);
  236. /* 3. Use McCamy's formula to determine the CCT */
  237. n = (xc - 0.3320F) / (0.1858F - yc);
  238. /* Calculate the final CCT */
  239. cct = (449.0F * powf(n, 3)) + (3525.0F * powf(n, 2)) + (6823.3F * n) + 5520.33F;
  240. /* Return the results in degrees Kelvin */
  241. return (uint16_t)cct;
  242. }
  243. /**************************************************************************/
  244. /*!
  245. @brief Converts the raw R/G/B values to lux
  246. */
  247. /**************************************************************************/
  248. uint16_t calculateLux(uint16_t r, uint16_t g, uint16_t b)
  249. {
  250. float illuminance;
  251. /* This only uses RGB ... how can we integrate clear or calculate lux */
  252. /* based exclusively on clear since this might be more reliable? */
  253. illuminance = (-0.32466F * r) + (1.57837F * g) + (-0.73191F * b);
  254. return (uint16_t)illuminance;
  255. }
  256. /**************************************************************************/
  257. /*!
  258. @brief Sets gain to the specified value
  259. */
  260. /**************************************************************************/
  261. void tcs34725SetGain(tcs34725Gain_t gain)
  262. {
  263. TCS34725_I2C_Write(TCS34725_ADDRESS,TCS34725_CONTROL, gain);
  264. _tcs34725Gain = gain;
  265. }
  266. /**************************************************************************/
  267. /*!
  268. @brief Converts the raw R/G/B values to color temperature in degrees
  269. Kelvin using the algorithm described in DN40 from Taos (now AMS).
  270. */
  271. /**************************************************************************/
  272. uint16_t calculateColorTemperature_dn40(uint16_t r, uint16_t g, uint16_t b, uint16_t c)
  273. {
  274. int rc; /* Error return code */
  275. uint16_t r2, g2, b2; /* RGB values minus IR component */
  276. int gl; /* Results of the initial lux conversion */
  277. uint8_t gain_int; /* Gain multiplier as a normal integer */
  278. uint16_t sat; /* Digital saturation level */
  279. uint16_t ir; /* Inferred IR content */
  280. /* Analog/Digital saturation:
  281. *
  282. * (a) As light becomes brighter, the clear channel will tend to
  283. * saturate first since R+G+B is approximately equal to C.
  284. * (b) The TCS34725 accumulates 1024 counts per 2.4ms of integration
  285. * time, up to a maximum values of 65535. This means analog
  286. * saturation can occur up to an integration time of 153.6ms
  287. * (64*2.4ms=153.6ms).
  288. * (c) If the integration time is > 153.6ms, digital saturation will
  289. * occur before analog saturation. Digital saturation occurs when
  290. * the count reaches 65535.
  291. */
  292. if ((256 - _tcs34725IntegrationTime) > 63) {
  293. /* Track digital saturation */
  294. sat = 65535;
  295. } else {
  296. /* Track analog saturation */
  297. sat = 1024 * (256 - _tcs34725IntegrationTime);
  298. }
  299. /* Ripple rejection:
  300. *
  301. * (a) An integration time of 50ms or multiples of 50ms are required to
  302. * reject both 50Hz and 60Hz ripple.
  303. * (b) If an integration time faster than 50ms is required, you may need
  304. * to average a number of samples over a 50ms period to reject ripple
  305. * from fluorescent and incandescent light sources.
  306. *
  307. * Ripple saturation notes:
  308. *
  309. * (a) If there is ripple in the received signal, the value read from C
  310. * will be less than the max, but still have some effects of being
  311. * saturated. This means that you can be below the 'sat' value, but
  312. * still be saturating. At integration times >150ms this can be
  313. * ignored, but <= 150ms you should calculate the 75% saturation
  314. * level to avoid this problem.
  315. */
  316. if ((256 - _tcs34725IntegrationTime) <= 63) {
  317. /* Adjust sat to 75% to avoid analog saturation if atime < 153.6ms */
  318. sat -= sat/4;
  319. }
  320. /* Check for saturation and mark the sample as invalid if true */
  321. if (c >= sat) {
  322. return 0;
  323. }
  324. /* AMS RGB sensors have no IR channel, so the IR content must be */
  325. /* calculated indirectly. */
  326. ir = (r + g + b > c) ? (r + g + b - c) / 2 : 0;
  327. /* Remove the IR component from the raw RGB values */
  328. r2 = r - ir;
  329. g2 = g - ir;
  330. b2 = b - ir;
  331. /* Convert gain to a usable integer value */
  332. switch(_tcs34725Gain) {
  333. case TCS34725_GAIN_4X: /* GAIN 4X */
  334. gain_int = 4;
  335. break;
  336. case TCS34725_GAIN_16X: /* GAIN 16X */
  337. gain_int = 16;
  338. break;
  339. case TCS34725_GAIN_60X: /* GAIN 60X */
  340. gain_int = 60;
  341. break;
  342. case TCS34725_GAIN_1X: /* GAIN 1X */
  343. default:
  344. gain_int = 1;
  345. break;
  346. }
  347. /* Calculate the counts per lux (CPL), taking into account the optional
  348. * arguments for Glass Attenuation (GA) and Device Factor (DF).
  349. *
  350. * GA = 1/T where T is glass transmissivity, meaning if glass is 50%
  351. * transmissive, the GA is 2 (1/0.5=2), and if the glass attenuates light
  352. * 95% the GA is 20 (1/0.05). A GA of 1.0 assumes perfect transmission.
  353. *
  354. * NOTE: It is recommended to have a CPL > 5 to have a lux accuracy
  355. * < +/- 0.5 lux, where the digitization error can be calculated via:
  356. * 'DER = (+/-2) / CPL'.
  357. */
  358. float cpl = (((256-_tcs34725IntegrationTime)*2.4f) * gain_int) /
  359. (1.0f * 310.0f);
  360. /* Determine lux accuracy (+/- lux) */
  361. float der = 2.0f / cpl;
  362. /* Determine the maximum lux value */
  363. float max_lux = 65535.0 / (cpl * 3);
  364. /* Lux is a function of the IR-compensated RGB channels and the associated
  365. * color coefficients, with G having a particularly heavy influence to
  366. * match the nature of the human eye.
  367. *
  368. * NOTE: The green value should be > 10 to ensure the accuracy of the lux
  369. * conversions. If it is below 10, the gain should be increased, but
  370. * the clear<100 check earlier should cover this edge case.
  371. */
  372. gl = 0.136f * (float)r2 + /** Red coefficient. */
  373. 1.000f * (float)g2 + /** Green coefficient. */
  374. -0.444f * (float)b2; /** Blue coefficient. */
  375. float lux = gl / cpl;
  376. /* A simple method of measuring color temp is to use the ratio of blue */
  377. /* to red light, taking IR cancellation into account. */
  378. uint16_t cct = (3810 * (uint32_t)b2) / /** Color temp coefficient. */
  379. (uint32_t)r2 + 1391; /** Color temp offset. */
  380. return cct;
  381. }
  382. void i2c_status(HAL_StatusTypeDef data){
  383. switch(data){
  384. case HAL_OK :printf("HAL_OK \r\n");break;
  385. case HAL_ERROR :printf("HAL_ERROR \r\n");break;
  386. case HAL_BUSY :printf("HAL_BUSY \r\n");break;
  387. case HAL_TIMEOUT :printf("HAL_TIMEOUT\r\n");break;
  388. }
  389. }
  390. void TCS34725_init(void){
  391. uint8_t DEV_DATA = TCS34725_I2C_Read(TCS34725_ADDRESS, TCS34725_COMMAND_BIT | TCS34725_ID);
  392. // tcs34725SetIntegrationTime(TCS34725_INTEGRATIONTIME_700MS);
  393. // tcs34725SetGain(TCS34725_GAIN_60X);
  394. TCS34725_enable();
  395. if(DEV_DATA == 0x44 || DEV_DATA == 0x4D){
  396. printf("TCS34725_Success\r\n");
  397. }else{
  398. printf("TCS34725_Failed : %02x\r\n",DEV_DATA);
  399. }
  400. }