🚚 Moves firmware to src and include
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 JsonVariant &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 JsonVariant &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(JsonVariant &root, const String &key, const IPAddress &ip) {
|
||||
if (IPUtils::isSet(ip)) {
|
||||
root[key] = ip.toString();
|
||||
}
|
||||
}
|
||||
};
|
||||
#endif // end JsonUtils
|
||||
@@ -0,0 +1,84 @@
|
||||
#ifndef MATHUTILS_H
|
||||
#define MATHUTILS_H
|
||||
|
||||
#include <dspm_mult.h>
|
||||
#include <cmath>
|
||||
|
||||
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
|
||||
|
||||
#define COPY_2D_ARRAY_4x4(dest, src) \
|
||||
do { \
|
||||
(dest)[0][0] = (src)[0][0]; \
|
||||
(dest)[0][1] = (src)[0][1]; \
|
||||
(dest)[0][2] = (src)[0][2]; \
|
||||
(dest)[0][3] = (src)[0][3]; \
|
||||
(dest)[1][0] = (src)[1][0]; \
|
||||
(dest)[1][1] = (src)[1][1]; \
|
||||
(dest)[1][2] = (src)[1][2]; \
|
||||
(dest)[1][3] = (src)[1][3]; \
|
||||
(dest)[2][0] = (src)[2][0]; \
|
||||
(dest)[2][1] = (src)[2][1]; \
|
||||
(dest)[2][2] = (src)[2][2]; \
|
||||
(dest)[2][3] = (src)[2][3]; \
|
||||
(dest)[3][0] = (src)[3][0]; \
|
||||
(dest)[3][1] = (src)[3][1]; \
|
||||
(dest)[3][2] = (src)[3][2]; \
|
||||
(dest)[3][3] = (src)[3][3]; \
|
||||
} while (0)
|
||||
|
||||
#define MAT_MULT(A, B, result, rows, cols, result_cols) \
|
||||
dspm_mult_f32_ae32((float *)(A), (float *)(B), (float *)(result), (rows), (cols), (result_cols))
|
||||
|
||||
#define INT_TO_STRING(state, output) \
|
||||
do { \
|
||||
itoa((int)(state), (output), 10); \
|
||||
} while (0)
|
||||
|
||||
#define PI_F 3.1415927f
|
||||
|
||||
#define DEG2RAD_F 0.0174532f
|
||||
|
||||
#define RAD2DEG_F 57.2957795f
|
||||
|
||||
#define RAD_TO_DEG_F(rad) ((rad) * RAD2DEG_F)
|
||||
|
||||
#define DEG_TO_RAD_F(deg) ((deg) * DEG2RAD_F)
|
||||
|
||||
#define COS_DEG_F(deg) (cosf(DEG_TO_RAD_F(deg)))
|
||||
|
||||
#define SIN_DEG_F(deg) (sinf(DEG_TO_RAD_F(deg)))
|
||||
|
||||
#define IS_EQUAL(a, b, epsilon) (std::fabs((a) - (b)) < (epsilon))
|
||||
|
||||
#define IS_ALMOST_EQUAL(a, b) IS_EQUAL((a), (b), 0.001f)
|
||||
|
||||
inline float lerp(float start, float end, float t) { return (1 - t) * start + t * end; }
|
||||
|
||||
inline bool isEqual(float a, float b, float epsilon) { return std::fabs(a - b) < epsilon; }
|
||||
|
||||
inline float round2(float value) { return (int)(value * 100 + 0.5) / 100.0; }
|
||||
|
||||
inline bool arrayEqual(const float arr1[4][4], const float arr2[4][4], float epsilon = 1e-3) {
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
for (int j = 0; j < 4; ++j) {
|
||||
if (std::fabs(arr1[i][j] - arr2[i][j]) > epsilon) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static constexpr float combinatorial_constexpr(const int n, int k) {
|
||||
if (k < 0 || k > n) return 0.0f;
|
||||
if (k == 0 || k == n) return 1.0f;
|
||||
k = (k < (n - k)) ? k : (n - k);
|
||||
float result = 1.0f;
|
||||
for (int i = 0; i < k; ++i) {
|
||||
result *= (n - i);
|
||||
result /= (i + 1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -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