| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- /*
- * 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 <rtdbg.h>
- #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);
|