71 lines
1.1 KiB
C++
71 lines
1.1 KiB
C++
/// © MiroZ 2024
|
|
|
|
#include <stdint.h>
|
|
#include <Arduino.h>
|
|
|
|
static const char *TAG = "utils";
|
|
|
|
void dump_bytes(const void *data, uint32_t len)
|
|
{
|
|
char c[128] = {0};
|
|
char b[32];
|
|
|
|
uint8_t *pt = (uint8_t*)data;
|
|
bool hasMore = false;
|
|
for (int n = 0; n < len; n++)
|
|
{
|
|
sprintf(b, "%02x ", *pt++);
|
|
strcat(c, b);
|
|
hasMore = true;
|
|
if ((n + 1) % 8 == 0)
|
|
{
|
|
ESP_LOGI(TAG, "%s", c);
|
|
c[0] = 0;
|
|
hasMore = false;
|
|
}
|
|
}
|
|
if (hasMore)
|
|
ESP_LOGI(TAG, "%s", c);
|
|
}
|
|
|
|
bool strToInt(const char *str, int & val, int base)
|
|
{
|
|
if(str == nullptr || str[0] == 0)
|
|
{
|
|
ESP_LOGE(TAG, "input str invalid");
|
|
return false;
|
|
}
|
|
|
|
char *endptr;
|
|
|
|
val = strtol(str, &endptr, base);
|
|
|
|
if(endptr == str || *endptr != 0)
|
|
{
|
|
ESP_LOGE(TAG, "input str invalid (2)");
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool strToFloat(const char *str, float & val)
|
|
{
|
|
if(str == nullptr || str[0] == 0)
|
|
{
|
|
ESP_LOGE(TAG, "input str invalid");
|
|
return false;
|
|
}
|
|
|
|
char *endptr;
|
|
|
|
val = strtof(str, &endptr);
|
|
|
|
if(endptr == str || *endptr != 0)
|
|
{
|
|
ESP_LOGE(TAG, "input str invalid (2)");
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
} |