📦 Moves utilities to own folder
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
#ifndef IPUtils_h
|
||||
#define IPUtils_h
|
||||
|
||||
/**
|
||||
* ESP32 SvelteKit
|
||||
*
|
||||
* A simple, secure and extensible framework for IoT projects for ESP32 platforms
|
||||
* with responsive Sveltekit front-end built with TailwindCSS and DaisyUI.
|
||||
* https://github.com/theelims/ESP32-sveltekit
|
||||
*
|
||||
* Copyright (C) 2018 - 2023 rjwats
|
||||
* Copyright (C) 2023 theelims
|
||||
*
|
||||
* All Rights Reserved. This software may be modified and distributed under
|
||||
* the terms of the LGPL v3 license. See the LICENSE file for details.
|
||||
**/
|
||||
|
||||
#include <IPAddress.h>
|
||||
|
||||
const IPAddress IP_NOT_SET = IPAddress(INADDR_NONE);
|
||||
|
||||
class IPUtils {
|
||||
public:
|
||||
static bool isSet(const IPAddress &ip) { return ip != IP_NOT_SET; }
|
||||
static bool isNotSet(const IPAddress &ip) { return ip == IP_NOT_SET; }
|
||||
};
|
||||
|
||||
#endif // end IPUtils_h
|
||||
@@ -0,0 +1,31 @@
|
||||
#ifndef JsonUtils_h
|
||||
#define JsonUtils_h
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <utils/ip_utils.h>
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
class JsonUtils {
|
||||
public:
|
||||
static void readIP(const JsonObject &root, const String &key, IPAddress &ip, const String &def) {
|
||||
IPAddress defaultIp = {};
|
||||
if (!defaultIp.fromString(def)) {
|
||||
defaultIp = INADDR_NONE;
|
||||
}
|
||||
readIP(root, key, ip, defaultIp);
|
||||
}
|
||||
|
||||
static void readIP(const JsonObject &root, const String &key, IPAddress &ip,
|
||||
const IPAddress &defaultIp = INADDR_NONE) {
|
||||
if (!root[key].is<String>() || !ip.fromString(root[key].as<String>())) {
|
||||
ip = defaultIp;
|
||||
}
|
||||
}
|
||||
|
||||
static void writeIP(JsonObject &root, const String &key, const IPAddress &ip) {
|
||||
if (IPUtils::isSet(ip)) {
|
||||
root[key] = ip.toString();
|
||||
}
|
||||
}
|
||||
};
|
||||
#endif // end JsonUtils
|
||||
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
constexpr static const char* PLATFORM = "esp32";
|
||||
|
||||
static String getRandom() { return String(random(2147483647), HEX); }
|
||||
|
||||
static String getUniqueId() {
|
||||
uint8_t mac[6];
|
||||
esp_read_mac(mac, ESP_MAC_WIFI_STA);
|
||||
char macStr[13] = {0};
|
||||
sprintf(macStr, "%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
return String(macStr);
|
||||
}
|
||||
|
||||
static String replaceEach(String value, String pattern, String (*generateReplacement)()) {
|
||||
while (true) {
|
||||
int index = value.indexOf(pattern);
|
||||
if (index == -1) {
|
||||
break;
|
||||
}
|
||||
value = value.substring(0, index) + generateReplacement() + value.substring(index + pattern.length());
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
static String format(String value) {
|
||||
value = replaceEach(value, "#{random}", getRandom);
|
||||
value.replace("#{unique_id}", getUniqueId());
|
||||
value.replace("#{platform}", PLATFORM);
|
||||
return value;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
#ifndef TIMING_H
|
||||
#define TIMING_H
|
||||
|
||||
#define CONCAT(a, b) a##b
|
||||
|
||||
#define UNIQUE_VAR(base) CONCAT(base, __LINE__)
|
||||
|
||||
#define EXECUTE_EVERY_N_MS(n, code) \
|
||||
do { \
|
||||
static volatile unsigned long UNIQUE_VAR(lastExecution_) = 0; \
|
||||
unsigned long currentMillis = millis(); \
|
||||
if (UNIQUE_VAR(lastExecution_) == 0 || currentMillis - UNIQUE_VAR(lastExecution_) >= n) { \
|
||||
code; \
|
||||
UNIQUE_VAR(lastExecution_) = currentMillis; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define TIME_IT(code) \
|
||||
{ \
|
||||
uint32_t time_it_start = micros(); \
|
||||
code; \
|
||||
uint32_t time_it_elapsed = micros() - time_it_start; \
|
||||
if (time_it_elapsed < 1000) { \
|
||||
ESP_LOGI("Time It", "Time elapsed: %lu microseconds", time_it_elapsed); \
|
||||
} else if (time_it_elapsed < 1000000) { \
|
||||
ESP_LOGI("Time It", "Time elapsed: %lu milliseconds", time_it_elapsed / 1000); \
|
||||
} else { \
|
||||
ESP_LOGI("Time It", "Time elapsed: %.2f seconds", time_it_elapsed / 1000000.0); \
|
||||
} \
|
||||
}
|
||||
|
||||
#define CALLS_PER_SECOND(name) \
|
||||
static unsigned long name##_count = 0; \
|
||||
static unsigned long last_time = 0; \
|
||||
name##_count++; \
|
||||
if (millis() - last_time >= 1000) { \
|
||||
Serial.printf("%s: %lu calls per second\n", #name, name##_count); \
|
||||
name##_count = 0; \
|
||||
last_time = millis(); \
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user