/* * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2023-04-11 19813 the first version */ /*********************↓ define ↓************************/ #define DBG_TAG "iic" #define DBG_LVL DBG_LOG #include #define Errcode 0x7FFF /*********************↑ define ↑************************/ /** * * * * * */ /*********************↓ include ↓************************/ #include "proc_iic.h" /*********************↑ include ↑************************/ /** * * * * * */ /*********************↓ extern ↓************************/ /*********************↑ extern ↑************************/ /** * * * * * */ /********************↓ declaration ↓***********************/ /********************↑ declaration ↑***********************/ /** * * * * * */ /*********************↓ global variables ↓************************/ /*********************↑ global variables ↑************************/ /** * * * * * */ uint8_t string_compare(char array1[], char array2[], uint16_t lenght) { uint8_t comVAR = 0, i; for (i = 0; i < lenght; i++) { if (array1[i] == array2[i]) comVAR++; else comVAR = 0; } if (comVAR == lenght) return 1; else return 0; } // reverses a string 'str' of length 'len' void reverse(char *str, int len) { int i = 0, j = len - 1, temp; while (i < j) { temp = str[i]; str[i] = str[j]; str[j] = temp; i++; j--; } } // Converts a given integer x to string str[]. d is the number // of digits required in output. If d is more than the number // of digits in x, then 0s are added at the beginning. int intToStr(int x, char str[], int d) { int i = 0; while (x) { str[i++] = (x % 10) + '0'; x = x / 10; } // If number of digits required is more, then // add 0s at the beginning while (i < d) str[i++] = '0'; reverse(str, i); str[i] = '\0'; return i; } // Converts a floating point number to string. void ftos(float n, char *res, int afterpoint) { unsigned char minus_flag = 0; if (n < 0) { minus_flag = 1; n = -n; } // Extract integer part int ipart = (int)n; // Extract floating part float fpart = n - (float)ipart; // convert integer part to string int i = intToStr(ipart, res, 0); uint16_t pow10 = 10; // check for display option after point if (afterpoint != 0) { res[i] = '.'; // add dot for (uint8_t a = 1; a < afterpoint; a++) { pow10 *= 10; } // Get the value of fraction part upto given no. // of points after dot. The third parameter is needed // to handle cases like 233.007 fpart = fpart * pow10; intToStr((int)fpart, res + i + 1, afterpoint); } char string[30]; if (minus_flag == 1) { memset(string, 0, 30); string[0] = '-'; if (n < 1.0f) { string[1] = '0'; strcpy(&string[2], res); } else strcpy(&string[1], res); memset(res, 0, strlen(res)); strcpy(res, string); } else if (n < 1.0f) { string[0] = '0'; strcpy(&string[1], res); memset(res, 0, strlen(res)); strcpy(res, string); } } /** * @brief * */ void IICDevInit(void) { SHT30_Init(); } #include "ym310_protecl.h" extern sensor_t sensor; /** * @brief 读取sht30的数据 * */ void proc_sht30(void) { int temp = 0, humi = 0; static uint8_t printcnt = 0; if (SHT30_AddrCheck() == 0) { if (SHT30_Read(&temp, &humi) == 0) { sensor.valid_sht = VALID; sensor.temp_sht = (int16_t)temp; sensor.humi_sht = (int16_t)humi; if (printcnt < 1) { rt_kprintf("SHT30 temperature %d.%d C %d.%d %%\n", temp / 10, temp % 10, humi / 10, humi % 10); printcnt++; } } } else { //rt_kprintf("SHT 30 Read Failed\n"); sensor.valid_ntc = 0; } } /** * @brief IIC数据处理函数 * * @param param * * 轮询解析 * */ static void iic_entry(void *param) { IICDevInit(); while (1) { proc_sht30(); rt_thread_mdelay(600); } } #define THREAD_PRIORITY 12 #define THREAD_STACK_SIZE 512 #define THREAD_TIMESLICE 5 int iic_thread(void) { rt_thread_t tid1 = RT_NULL; tid1 = rt_thread_create("iic", iic_entry, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); /* 如果获得线程控制块,启动这个线程 */ if (tid1 != RT_NULL) rt_thread_startup(tid1); return 0; } INIT_COMPONENT_EXPORT(iic_thread);