proc_iic.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-04-11 19813 the first version
  9. */
  10. /*********************↓ define ↓************************/
  11. #define DBG_TAG "iic"
  12. #define DBG_LVL DBG_LOG
  13. #include <rtdbg.h>
  14. #define Errcode 0x7FFF
  15. /*********************↑ define ↑************************/
  16. /**
  17. *
  18. *
  19. *
  20. *
  21. * */
  22. /*********************↓ include ↓************************/
  23. #include "proc_iic.h"
  24. /*********************↑ include ↑************************/
  25. /**
  26. *
  27. *
  28. *
  29. *
  30. * */
  31. /*********************↓ extern ↓************************/
  32. /*********************↑ extern ↑************************/
  33. /**
  34. *
  35. *
  36. *
  37. *
  38. * */
  39. /********************↓ declaration ↓***********************/
  40. /********************↑ declaration ↑***********************/
  41. /**
  42. *
  43. *
  44. *
  45. *
  46. * */
  47. /*********************↓ global variables ↓************************/
  48. /*********************↑ global variables ↑************************/
  49. /**
  50. *
  51. *
  52. *
  53. *
  54. * */
  55. uint8_t string_compare(char array1[], char array2[], uint16_t lenght)
  56. {
  57. uint8_t comVAR = 0, i;
  58. for (i = 0; i < lenght; i++)
  59. {
  60. if (array1[i] == array2[i])
  61. comVAR++;
  62. else
  63. comVAR = 0;
  64. }
  65. if (comVAR == lenght)
  66. return 1;
  67. else
  68. return 0;
  69. }
  70. // reverses a string 'str' of length 'len'
  71. void reverse(char *str, int len)
  72. {
  73. int i = 0, j = len - 1, temp;
  74. while (i < j)
  75. {
  76. temp = str[i];
  77. str[i] = str[j];
  78. str[j] = temp;
  79. i++;
  80. j--;
  81. }
  82. }
  83. // Converts a given integer x to string str[]. d is the number
  84. // of digits required in output. If d is more than the number
  85. // of digits in x, then 0s are added at the beginning.
  86. int intToStr(int x, char str[], int d)
  87. {
  88. int i = 0;
  89. while (x)
  90. {
  91. str[i++] = (x % 10) + '0';
  92. x = x / 10;
  93. }
  94. // If number of digits required is more, then
  95. // add 0s at the beginning
  96. while (i < d)
  97. str[i++] = '0';
  98. reverse(str, i);
  99. str[i] = '\0';
  100. return i;
  101. }
  102. // Converts a floating point number to string.
  103. void ftos(float n, char *res, int afterpoint)
  104. {
  105. unsigned char minus_flag = 0;
  106. if (n < 0)
  107. {
  108. minus_flag = 1;
  109. n = -n;
  110. }
  111. // Extract integer part
  112. int ipart = (int)n;
  113. // Extract floating part
  114. float fpart = n - (float)ipart;
  115. // convert integer part to string
  116. int i = intToStr(ipart, res, 0);
  117. uint16_t pow10 = 10;
  118. // check for display option after point
  119. if (afterpoint != 0)
  120. {
  121. res[i] = '.'; // add dot
  122. for (uint8_t a = 1; a < afterpoint; a++)
  123. {
  124. pow10 *= 10;
  125. }
  126. // Get the value of fraction part upto given no.
  127. // of points after dot. The third parameter is needed
  128. // to handle cases like 233.007
  129. fpart = fpart * pow10;
  130. intToStr((int)fpart, res + i + 1, afterpoint);
  131. }
  132. char string[30];
  133. if (minus_flag == 1)
  134. {
  135. memset(string, 0, 30);
  136. string[0] = '-';
  137. if (n < 1.0f)
  138. {
  139. string[1] = '0';
  140. strcpy(&string[2], res);
  141. }
  142. else
  143. strcpy(&string[1], res);
  144. memset(res, 0, strlen(res));
  145. strcpy(res, string);
  146. }
  147. else if (n < 1.0f)
  148. {
  149. string[0] = '0';
  150. strcpy(&string[1], res);
  151. memset(res, 0, strlen(res));
  152. strcpy(res, string);
  153. }
  154. }
  155. /**
  156. * @brief
  157. *
  158. */
  159. void IICDevInit(void)
  160. {
  161. SHT30_Init();
  162. }
  163. #include "ym310_protecl.h"
  164. extern sensor_t sensor;
  165. /**
  166. * @brief 读取sht30的数据
  167. *
  168. */
  169. void proc_sht30(void)
  170. {
  171. int temp = 0, humi = 0;
  172. static uint8_t printcnt = 0;
  173. if (SHT30_AddrCheck() == 0)
  174. {
  175. if (SHT30_Read(&temp, &humi) == 0)
  176. {
  177. sensor.valid_sht = VALID;
  178. sensor.temp_sht = (int16_t)temp;
  179. sensor.humi_sht = (int16_t)humi;
  180. if (printcnt < 1)
  181. {
  182. rt_kprintf("SHT30 temperature %d.%d C %d.%d %%\n", temp / 10, temp % 10, humi / 10, humi % 10);
  183. printcnt++;
  184. }
  185. }
  186. }
  187. else
  188. {
  189. //rt_kprintf("SHT 30 Read Failed\n");
  190. sensor.valid_ntc = 0;
  191. }
  192. }
  193. /**
  194. * @brief IIC数据处理函数
  195. *
  196. * @param param
  197. *
  198. * 轮询解析
  199. *
  200. */
  201. static void iic_entry(void *param)
  202. {
  203. IICDevInit();
  204. while (1)
  205. {
  206. proc_sht30();
  207. rt_thread_mdelay(600);
  208. }
  209. }
  210. #define THREAD_PRIORITY 12
  211. #define THREAD_STACK_SIZE 512
  212. #define THREAD_TIMESLICE 5
  213. int iic_thread(void)
  214. {
  215. rt_thread_t tid1 = RT_NULL;
  216. tid1 = rt_thread_create("iic",
  217. iic_entry, RT_NULL,
  218. THREAD_STACK_SIZE,
  219. THREAD_PRIORITY, THREAD_TIMESLICE);
  220. /* 如果获得线程控制块,启动这个线程 */
  221. if (tid1 != RT_NULL)
  222. rt_thread_startup(tid1);
  223. return 0;
  224. }
  225. INIT_COMPONENT_EXPORT(iic_thread);