TCS34725(1671).c 20 KB

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