new file: main/include/functions.h
modified: main/include/sht31.h renamed: main/include/lib.h -> main/src/functions.c modified: main/src/http_client.c modified: main/src/main.c modified: main/src/sht31.c
This commit is contained in:
1 parent
1341a1000a
commit
f4436308dc
6 files changed
+53
-60
No files matched your search
@@ -0,0 +1,119 @@
|
||||
#include "functions.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
|
||||
static const char * TAG = "functions.c";
|
||||
|
||||
void split_float(float num, int *int_part, float *dec_part) {
|
||||
// Extract the integer part
|
||||
*int_part = (float)((int)num);
|
||||
|
||||
// Calculate the fractional part by subtracting the integer part from the original number
|
||||
*dec_part = num - *int_part;
|
||||
}
|
||||
|
||||
// Helper function to reverse a string
|
||||
static void reverse(char* str, int len) {
|
||||
int i = 0, j = len - 1;
|
||||
while (i < j) {
|
||||
char temp = str[i];
|
||||
str[i] = str[j];
|
||||
str[j] = temp;
|
||||
i++;
|
||||
j--;
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to convert an integer to a string
|
||||
static int intToStr(int x, char str[], int d) {
|
||||
int i = 0;
|
||||
while (x) {
|
||||
str[i++] = (x % 10) + '0';
|
||||
x = x / 10;
|
||||
}
|
||||
|
||||
// If the 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;
|
||||
}
|
||||
|
||||
char* floatToString(float num) {
|
||||
// Allocate memory for the string
|
||||
char* str = (char*)malloc(50 * sizeof(char));
|
||||
if (str == NULL) {
|
||||
// If memory allocation fails, return NULL
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Handle integer part
|
||||
int intPart = (int)num;
|
||||
int i = intToStr(intPart, str, 0);
|
||||
|
||||
// Handle fractional part
|
||||
float fractionalPart = num - (float)intPart;
|
||||
if (fractionalPart < 0) {
|
||||
fractionalPart = -fractionalPart;
|
||||
}
|
||||
|
||||
if (fractionalPart != 0) {
|
||||
str[i++] = '.'; // Add decimal point
|
||||
|
||||
// Consider precision up to 6 digits after the decimal point
|
||||
for (int j = 0; j < 6; j++) {
|
||||
fractionalPart = fractionalPart * 10;
|
||||
int fracIntPart = (int)fractionalPart;
|
||||
str[i++] = fracIntPart + '0';
|
||||
fractionalPart = fractionalPart - fracIntPart;
|
||||
}
|
||||
}
|
||||
|
||||
str[i] = '\0'; // Null-terminate the string
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
// Function to convert an integer to a string
|
||||
char* intToString(int num)
|
||||
{
|
||||
// Allocate memory for the string
|
||||
char* str = (char*)malloc(50 * sizeof(char));
|
||||
|
||||
int i = 0;
|
||||
// Save the sign of the number
|
||||
int sign = num;
|
||||
|
||||
// If the number is negative, make it positive
|
||||
if (num < 0) {
|
||||
num = -num;
|
||||
}
|
||||
|
||||
// Extract digits from the number and add them to the
|
||||
// string
|
||||
do {
|
||||
// Convert integer digit to character
|
||||
str[i++] = num % 10 + '0';
|
||||
} while ((num /= 10) > 0);
|
||||
|
||||
// If the number was negative, add a minus sign to the
|
||||
// string
|
||||
if (sign < 0) {
|
||||
str[i++] = '-';
|
||||
}
|
||||
|
||||
// Null-terminate the string
|
||||
str[i] = '\0';
|
||||
|
||||
// Reverse the string to get the correct order
|
||||
for (int j = 0, k = i - 1; j < k; j++, k--) {
|
||||
char temp = str[j];
|
||||
str[j] = str[k];
|
||||
str[k] = temp;
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user