TCS34725.c 20 KB

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