Vhanged pin assignment to avoid 35,36,37 used in QSPI PSRAM

This commit is contained in:
2026-04-21 21:51:58 +02:00
parent fec59815a6
commit 6f372fcb49
9 changed files with 114 additions and 110 deletions

View File

@@ -27,14 +27,14 @@ monitor_port = /dev/ttyACM0
monitor_speed = 921600 monitor_speed = 921600
build_type = release build_type = release
build_flags = build_flags =
-DCORE_DEBUG_LEVEL=3 -DCORE_DEBUG_LEVEL=1
-DARDUINO_USB_CDC_ON_BOOT=0 -DARDUINO_USB_CDC_ON_BOOT=0
-DARDUINO_USB_MODE=0 -DARDUINO_USB_MODE=0
-DCONFIG_ASYNC_TCP_MAX_ACK_TIME=5000 -DCONFIG_ASYNC_TCP_MAX_ACK_TIME=5000
-DCONFIG_ASYNC_TCP_PRIORITY=21 -DCONFIG_ASYNC_TCP_PRIORITY=21
-DCONFIG_ASYNC_TCP_QUEUE_SIZE=128 -DCONFIG_ASYNC_TCP_QUEUE_SIZE=64
-DCONFIG_ASYNC_TCP_RUNNING_CORE=1 -DCONFIG_ASYNC_TCP_RUNNING_CORE=1
-DCONFIG_ASYNC_TCP_STACK_SIZE=8192 -DCONFIG_ASYNC_TCP_STACK_SIZE=4096
[env:esp32-s3-devkitc1-n16r8-debug] [env:esp32-s3-devkitc1-n16r8-debug]
board = ${env:esp32-s3-devkitc1-n16r8.board} board = ${env:esp32-s3-devkitc1-n16r8.board}
@@ -61,6 +61,6 @@ build_flags =
-DARDUINO_USB_MODE=0 -DARDUINO_USB_MODE=0
-DCONFIG_ASYNC_TCP_MAX_ACK_TIME=5000 -DCONFIG_ASYNC_TCP_MAX_ACK_TIME=5000
-DCONFIG_ASYNC_TCP_PRIORITY=21 -DCONFIG_ASYNC_TCP_PRIORITY=21
-DCONFIG_ASYNC_TCP_QUEUE_SIZE=128 -DCONFIG_ASYNC_TCP_QUEUE_SIZE=64
-DCONFIG_ASYNC_TCP_RUNNING_CORE=1 -DCONFIG_ASYNC_TCP_RUNNING_CORE=1
-DCONFIG_ASYNC_TCP_STACK_SIZE=8192 -DCONFIG_ASYNC_TCP_STACK_SIZE=4096

View File

@@ -14,8 +14,6 @@
#include "isr.h" #include "isr.h"
#include "psvector.h" #include "psvector.h"
const uint32_t max_history = 256;
class LITTLEFSGuard class LITTLEFSGuard
{ {
public: public:

View File

@@ -16,24 +16,23 @@
#include <ui.h> #include <ui.h>
#include <led.h> #include <led.h>
#define CH_A_ENABLE //#define CH_A_ENABLE
#define CH_B_ENABLE //#define CH_B_ENABLE
#define CH_A_RT_ENABLE #define CH_A_RT_ENABLE
#define CH_B_RT_ENABLE #define CH_B_RT_ENABLE
// #define I2C_ENABLE //#define I2C_ENABLE
// #define WEB_ENABLE #define WEB_ENABLE
// Debug Defines // Debug Defines
#define WIFI_SSID "AstroRotaxMonitor" #define WIFI_SSID "AstroRotaxMonitor"
#define WIFI_PASSWORD "maledettirotax" #define WIFI_PASSWORD "maledettirotax"
#define PSRAM_MAX 1024 #define PSRAM_MAX 4096
#define QUEUE_MAX 32 #define QUEUE_MAX 128
void setup() void setup()
{ {
Serial.begin(115200); Serial.begin(921600);
delay(250); delay(250);
Serial.setTimeout(5000);
// Setup Logger // Setup Logger
LOG_ATTACH_SERIAL(Serial); LOG_ATTACH_SERIAL(Serial);
@@ -106,7 +105,7 @@ void loop()
spiA_ok = SPI_A.begin(SPI_A_SCK, SPI_A_MISO, SPI_A_MOSI); spiA_ok = SPI_A.begin(SPI_A_SCK, SPI_A_MISO, SPI_A_MOSI);
SPI_A.setDataMode(SPI_MODE1); // ADS1256 requires SPI mode 1 SPI_A.setDataMode(SPI_MODE1); // ADS1256 requires SPI mode 1
LOG_DEBUG("Init SPI_A -> OK"); LOG_DEBUG("Init SPI_A -> OK");
delay(500); delay(100);
LOG_DEBUG("Begin Init ADC_A"); LOG_DEBUG("Begin Init ADC_A");
ADS1256 ADC_A(ADC_A_DRDY, ADS1256::PIN_UNUSED, ADS1256::PIN_UNUSED, ADC_A_CS, 2.5, &SPI_A); ADS1256 ADC_A(ADC_A_DRDY, ADS1256::PIN_UNUSED, ADS1256::PIN_UNUSED, ADC_A_CS, 2.5, &SPI_A);
ADC_A.InitializeADC(); ADC_A.InitializeADC();
@@ -115,7 +114,7 @@ void loop()
dev.m_adc_a = &ADC_A; dev.m_adc_a = &ADC_A;
dev.m_spi_a = &SPI_A; dev.m_spi_a = &SPI_A;
LOG_DEBUG("Init ADC_A -> OK"); LOG_DEBUG("Init ADC_A -> OK");
delay(1000); delay(100);
#endif #endif
#ifdef CH_B_ENABLE #ifdef CH_B_ENABLE
@@ -124,7 +123,7 @@ void loop()
spiB_ok = SPI_B.begin(SPI_B_SCK, SPI_B_MISO, SPI_B_MOSI); spiB_ok = SPI_B.begin(SPI_B_SCK, SPI_B_MISO, SPI_B_MOSI);
SPI_B.setDataMode(SPI_MODE1); // ADS1256 requires SPI mode 1 SPI_B.setDataMode(SPI_MODE1); // ADS1256 requires SPI mode 1
LOG_DEBUG("Init SPI_B -> OK"); LOG_DEBUG("Init SPI_B -> OK");
delay(500); delay(100);
LOG_DEBUG("Begin Init ADC_B"); LOG_DEBUG("Begin Init ADC_B");
ADS1256 ADC_B(ADC_B_DRDY, ADS1256::PIN_UNUSED, ADS1256::PIN_UNUSED, ADC_B_CS, 2.5, &SPI_B); ADS1256 ADC_B(ADC_B_DRDY, ADS1256::PIN_UNUSED, ADS1256::PIN_UNUSED, ADC_B_CS, 2.5, &SPI_B);
ADC_B.InitializeADC(); ADC_B.InitializeADC();
@@ -133,7 +132,7 @@ void loop()
dev.m_adc_b = &ADC_B; dev.m_adc_b = &ADC_B;
dev.m_spi_b = &SPI_B; dev.m_spi_b = &SPI_B;
LOG_DEBUG("Init ADC_B -> OK"); LOG_DEBUG("Init ADC_B -> OK");
delay(1000); delay(100);
#endif #endif
if (!spiA_ok || !spiB_ok) if (!spiA_ok || !spiB_ok)
@@ -159,10 +158,10 @@ void loop()
esp_restart(); esp_restart();
} }
LOG_DEBUG("Init I2c ok"); LOG_DEBUG("Init I2c ok");
Serial.readStringUntil('\n');
// Init IO Expanders // Init IO Expanders
dev->m_ext_io = std::make_unique<ExternalIO>(Wire, dev->m_i2c_mutex, EXPANDER_ALL_INTERRUPT); ExternalIO extIo(Wire, dev.m_i2c_mutex, EXPANDER_ALL_INTERRUPT);
dev.m_ext_io = &extIo;
#endif #endif
//////// INIT REALTIME TASKS PARAMETERS //////// //////// INIT REALTIME TASKS PARAMETERS ////////
@@ -238,17 +237,17 @@ void loop()
BaseType_t ignB_task_success = pdPASS; BaseType_t ignB_task_success = pdPASS;
#ifdef CH_A_RT_ENABLE #ifdef CH_A_RT_ENABLE
auto task_A = rtIgnitionTask(taskA_params, PSRAM_MAX, QUEUE_MAX, CORE_1, fs_mutex); auto task_A = rtIgnitionTask(taskA_params, PSRAM_MAX, QUEUE_MAX, CORE_0, fs_mutex);
ignA_task_success = task_A.getStatus() == rtIgnitionTask::OK ? pdPASS : pdFAIL; ignA_task_success = task_A.getStatus() == rtIgnitionTask::OK ? pdPASS : pdFAIL;
//tasK_A_rt = task_A.start(); tasK_A_rt = task_A.start();
delay(1000); delay(100);
#endif #endif
#ifdef CH_B_RT_ENABLE #ifdef CH_B_RT_ENABLE
auto task_B = rtIgnitionTask(taskB_params, PSRAM_MAX, QUEUE_MAX, CORE_1, fs_mutex); auto task_B = rtIgnitionTask(taskB_params, PSRAM_MAX, QUEUE_MAX, CORE_1, fs_mutex);
ignB_task_success = task_B.getStatus() == rtIgnitionTask::OK ? pdPASS : pdFAIL; ignB_task_success = task_B.getStatus() == rtIgnitionTask::OK ? pdPASS : pdFAIL;
//task_B_rt = task_B.start(); task_B_rt = task_B.start();
delay(1000); delay(100);
#endif #endif
// Ignition A on Core 0 // Ignition A on Core 0
@@ -275,7 +274,8 @@ void loop()
bool data_a = false, data_b = false; bool data_a = false, data_b = false;
#ifdef WEB_ENABLE #ifdef WEB_ENABLE
AstroWebServer webPage(80, LittleFS); AstroWebServer webPage(80, LittleFS);
delay(1000); delay(100);
task_A.onMessage([&webPage, &json_data, &data_a](ignitionBoxStatusFiltered sts) task_A.onMessage([&webPage, &json_data, &data_a](ignitionBoxStatusFiltered sts)
{ {
json_data["box_a"] = sts.toJson(); json_data["box_a"] = sts.toJson();
@@ -289,8 +289,8 @@ void loop()
#endif #endif
#endif #endif
// task_A.enableSave(true, "ignitionA_test.csv"); task_A.enableSave(true, "ignitionA_test.csv");
// task_B.enableSave(true, "ignitionB_test.csv"); task_B.enableSave(true, "ignitionB_test.csv");
uint32_t monitor_loop = millis(); uint32_t monitor_loop = millis();
uint32_t data_loop = monitor_loop; uint32_t data_loop = monitor_loop;

View File

@@ -33,16 +33,15 @@
// ===================== // =====================
// SPI BUS ADC2 (HSPI) // SPI BUS ADC2 (HSPI)
// ===================== // =====================
#define SPI_B_MOSI 36 #define SPI_B_MOSI 17
#define SPI_B_SCK 37 #define SPI_B_SCK 18
#define SPI_B_MISO 38 #define SPI_B_MISO 8
// ===================== // =====================
// I2C BUS (PCA9555) // I2C BUS (PCA9555)
// ===================== // =====================
#define SDA 8 #define SDA 21
#define SCL 9 #define SCL 38
#define I2C_INT 17
// ===================== // =====================
// ADC CONTROL // ADC CONTROL
@@ -50,8 +49,8 @@
#define ADC_A_CS 14 #define ADC_A_CS 14
#define ADC_A_DRDY 13 #define ADC_A_DRDY 13
#define ADC_B_CS 21 #define ADC_B_CS 3
#define ADC_B_DRDY 47 #define ADC_B_DRDY 9
// ===================== // =====================
// TRIGGER INPUT INTERRUPTS // TRIGGER INPUT INTERRUPTS
@@ -81,7 +80,7 @@
// ===================== // =====================
// PCA9555 I/O EXPANDER INTERRUPT (Common) // PCA9555 I/O EXPANDER INTERRUPT (Common)
// ===================== // =====================
#define EXPANDER_ALL_INTERRUPT 17 #define EXPANDER_ALL_INTERRUPT 45
// ===================== // =====================
// PCA9555 I/O EXPANDER BOX_A (OUT) // PCA9555 I/O EXPANDER BOX_A (OUT)

View File

@@ -6,7 +6,7 @@
//// GLOBAL STATIC FUNCTIONS //// GLOBAL STATIC FUNCTIONS
// Timeout callback for microsecond precision // Timeout callback for microsecond precision
void spark_timeout_callback(void *arg) void IRAM_ATTR spark_timeout_callback(void *arg)
{ {
TaskHandle_t handle = (TaskHandle_t)arg; TaskHandle_t handle = (TaskHandle_t)arg;
xTaskNotify(handle, SPARK_FLAG_TIMEOUT, eSetValueWithOverwrite); xTaskNotify(handle, SPARK_FLAG_TIMEOUT, eSetValueWithOverwrite);
@@ -21,10 +21,6 @@ void rtIgnitionTask::rtIgnitionTask_manager(void *pvParameters)
while (cls->m_running) while (cls->m_running)
{ {
cls->run(); cls->run();
// if (millis() - last_loop > 2000) {
// LOG_DEBUG("TASK [", cls->m_name.c_str(), "] Alive -", count++);
// last_loop = millis();
// }
vTaskDelay(pdMS_TO_TICKS(1)); vTaskDelay(pdMS_TO_TICKS(1));
} }
} }
@@ -47,10 +43,8 @@ void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters)
QueueHandle_t rt_queue = params->rt_queue; QueueHandle_t rt_queue = params->rt_queue;
Devices *dev = params->dev; Devices *dev = params->dev;
ExternalIO *io = dev->m_ext_io; ExternalIO *io = dev->m_ext_io;
// ADS1256 *adc = params->name == "rtIgnTask_A" ? dev->m_adc_a : dev->m_adc_b; ADS1256 *adc = params->name == "rtIgnTask_A" ? dev->m_adc_a : dev->m_adc_b;
ADS1256 *adc = NULL; std::mutex &spi_mutex = params->name == "rtIgnTask_A" ? dev->m_spi_a_mutex : dev->m_spi_b_mutex;
// std::mutex &spi_mutex = params->name == "rtIgnTask_A" ? dev->m_spi_a_mutex : dev->m_spi_b_mutex;
std::mutex spi_mutex;
TaskStatus_t rt_task_info; TaskStatus_t rt_task_info;
vTaskGetInfo(NULL, &rt_task_info, pdFALSE, eInvalid); vTaskGetInfo(NULL, &rt_task_info, pdFALSE, eInvalid);
@@ -321,15 +315,22 @@ rtIgnitionTask::rtIgnitionTask(const rtTaskParams params, const uint32_t history
else else
m_params.rt_queue = m_queue; m_params.rt_queue = m_queue;
try
{
// create PSram history vectors // create PSram history vectors
m_history_0 = PSHistory(history_size); m_history_0 = PSHistory(history_size);
m_history_1 = PSHistory(history_size); m_history_1 = PSHistory(history_size);
// assing active and writable history // assing active and writable history
m_active_history = std::unique_ptr<PSHistory>(&m_history_0); m_active_history = std::unique_ptr<PSHistory>(&m_history_0);
m_save_history = std::unique_ptr<PSHistory>(&m_history_1); m_save_history = std::unique_ptr<PSHistory>(&m_history_1);
}
catch (std::bad_alloc &e)
{
LOG_ERROR("Task [", params.name.c_str(), "] Unable to allocate history PSRAM: ", e.what());
return;
}
m_name = (std::string("man_") + m_params.name).c_str(); m_name = (std::string("man_") + m_params.name).c_str();
// auto task_success = pdPASS;
auto task_success = xTaskCreatePinnedToCore( auto task_success = xTaskCreatePinnedToCore(
rtIgnitionTask_manager, rtIgnitionTask_manager,
m_name.c_str(), m_name.c_str(),
@@ -379,7 +380,7 @@ void rtIgnitionTask::run()
m_partial_save = false; // reset partial save flag on new data cycle m_partial_save = false; // reset partial save flag on new data cycle
std::swap(m_active_history, m_save_history); std::swap(m_active_history, m_save_history);
if (m_enable_save) if (m_enable_save)
// saveHistory(m_save_history, m_history_path); // directly call the save task function to save without delay saveHistory(*m_save_history, m_history_path); // directly call the save task function to save without delay
LOG_INFO("Save History"); LOG_INFO("Save History");
} }
@@ -402,9 +403,9 @@ void rtIgnitionTask::run()
if (m_counter_status > 0 && !m_partial_save) if (m_counter_status > 0 && !m_partial_save)
{ {
LOG_DEBUG("Save Partial: ", m_counter_status); LOG_DEBUG("Save Partial: ", m_counter_status);
// m_active_history->resize(m_counter_status); m_active_history->resize(m_counter_status);
// saveHistory(m_active_history, m_history_path); saveHistory(*m_active_history, m_history_path);
// m_active_history->resize(m_max_history); m_active_history->resize(m_max_history);
m_counter_status = 0; m_counter_status = 0;
m_partial_save = true; m_partial_save = true;
} }

View File

@@ -41,7 +41,6 @@ static const std::map<const uint32_t, const char *> names = {
class rtIgnitionTask class rtIgnitionTask
{ {
using PSHistory = PSRAMVector<ignitionBoxStatus>; using PSHistory = PSRAMVector<ignitionBoxStatus>;
// using PSHistory = std::vector<ignitionBoxStatus>;
public: public:
// RT task Interrupt parameters // RT task Interrupt parameters

View File

@@ -62,7 +62,7 @@ void printBar(Print &printer, const char *label, size_t used, size_t total, cons
k += sprintf(&str[k], "-"); k += sprintf(&str[k], "-");
} }
sprintf(&str[k], "] %s%6.2f%%%s (%5.3f/%5.3f)MB\n", sprintf(&str[k], "] %s%6.2f%%%s (%5.3f/%5.3f)MB",
color, color,
perc * 100.0, perc * 100.0,
COLOR_RESET, COLOR_RESET,
@@ -104,14 +104,14 @@ void printRunningTasksMod(Print &printer, std::function<bool(const TaskStatus_t
// PRINT MEMORY INFO // PRINT MEMORY INFO
printer.printf("\033[H"); printer.printf("\033[H");
printer.printf(COLOR_LBLUE "=================== ESP32 SYSTEM MONITOR ===================\n" COLOR_RESET); printer.printf(COLOR_WHITE "====================== ESP32 SYSTEM MONITOR ======================\n" COLOR_RESET);
std::string buffer; std::string buffer;
time_t now = time(nullptr); time_t now = time(nullptr);
struct tm *t = localtime(&now); struct tm *t = localtime(&now);
buffer.resize(64); buffer.resize(64);
strftime(buffer.data(), sizeof(buffer), "%Y-%m-%d %H:%M:%S", t); strftime(buffer.data(), sizeof(buffer), "%Y-%m-%d %H:%M:%S", t);
printer.printf(COLOR_YELLOW "=============== Datetime: %s ===============\n\n" COLOR_RESET, buffer.c_str()); printer.printf(COLOR_WHITE "=================== Datetime: %s ==================\n\n" COLOR_RESET, buffer.c_str());
// ===== HEAP ===== // ===== HEAP =====
size_t freeHeap = esp_get_free_heap_size(); size_t freeHeap = esp_get_free_heap_size();
@@ -121,7 +121,7 @@ void printRunningTasksMod(Print &printer, std::function<bool(const TaskStatus_t
// ===== RAM INTERNA ===== // ===== RAM INTERNA =====
size_t freeInternal = heap_caps_get_free_size(MALLOC_CAP_INTERNAL); size_t freeInternal = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
size_t totalInternal = heap_caps_get_total_size(MALLOC_CAP_INTERNAL); size_t totalInternal = heap_caps_get_total_size(MALLOC_CAP_INTERNAL);
printBar(printer, "INTERNAL", totalInternal - freeInternal, totalInternal, COLOR_BLUE); printBar(printer, "INTERNAL", totalInternal - freeInternal, totalInternal, COLOR_CYAN);
// ===== PSRAM ===== // ===== PSRAM =====
size_t totalPsram = heap_caps_get_total_size(MALLOC_CAP_SPIRAM); size_t totalPsram = heap_caps_get_total_size(MALLOC_CAP_SPIRAM);
@@ -160,13 +160,13 @@ void printRunningTasksMod(Print &printer, std::function<bool(const TaskStatus_t
size_t minHeap = esp_get_minimum_free_heap_size(); size_t minHeap = esp_get_minimum_free_heap_size();
printer.printf("%s\nMin Heap Ever:%s %u KB\n", COLOR_RED, COLOR_RESET, minHeap / 1024); printer.printf("%s\nMin Heap Ever:%s %u KB\n", COLOR_RED, COLOR_RESET, minHeap / 1024);
size_t max_block = heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM); size_t max_block = heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM);
printer.printf("%s\nMax PSRAM Block:%s %u KB\n\n", COLOR_RED, COLOR_RESET, max_block / 1024); printer.printf("%sMax PSRAM Block:%s %u KB\n\n", COLOR_RED, COLOR_RESET, max_block / 1024);
// Print Runtime Information // Print Runtime Information
printer.printf("Tasks: %u, Runtime: %lus, Period: %luus\r\n", uxArraySize, ulTotalRunTime / 1000000, ulCurrentRunTime); printer.printf("Tasks: %u, Runtime: %lus, Period: %luus\n", uxArraySize, ulTotalRunTime / 1000000, ulCurrentRunTime);
// Print Task Headers // Print Task Headers
printer.printf("Num\t Name\tLoad\tPrio\t Free\tCore\tState\r\n"); printer.printf("Num\t Name\tLoad\tPrio\t Free\tCore\tState\n");
for (const auto &task : pxTaskStatusArray) for (const auto &task : pxTaskStatusArray)
{ {
@@ -179,7 +179,7 @@ void printRunningTasksMod(Print &printer, std::function<bool(const TaskStatus_t
"\t%3lu%%" "\t%3lu%%"
"\t%4u\t%5lu" "\t%4u\t%5lu"
"\t%4c" "\t%4c"
"\t%s\r\n", "\t%s\n",
task.xTaskNumber, task.pcTaskName, task.xTaskNumber, task.pcTaskName,
ulTaskRunTime, ulTaskRunTime,
task.uxCurrentPriority, task.usStackHighWaterMark, task.uxCurrentPriority, task.usStackHighWaterMark,

View File

@@ -1,9 +1,4 @@
#include <webserver.h> #include <webserver.h>
#include <ArduinoJson.h>
static std::map<const std::string, AstroWebServer::c_commandEnum> s_webserverCommands = {
{"setTime", AstroWebServer::SET_TIME},
};
void on_ping(TimerHandle_t xTimer) void on_ping(TimerHandle_t xTimer)
{ {
@@ -32,6 +27,10 @@ AstroWebServer::AstroWebServer(const uint8_t port, fs::FS &filesystem) : m_port(
m_websocket.enable(true); m_websocket.enable(true);
m_pingTimer = xTimerCreate("wsPingTimer", pdMS_TO_TICKS(2000), pdTRUE, (void *)&m_websocket, on_ping); m_pingTimer = xTimerCreate("wsPingTimer", pdMS_TO_TICKS(2000), pdTRUE, (void *)&m_websocket, on_ping);
registerWsCommand("setTime", [this](const ArduinoJson::JsonDocument &doc)
{ onSetTme(doc); });
LOG_DEBUG("Webserver Init OK"); LOG_DEBUG("Webserver Init OK");
} }
@@ -50,6 +49,21 @@ void AstroWebServer::sendWsData(const String &data)
} }
} }
void AstroWebServer::registerWsCommand(const std::string &cmd, const WScommand func)
{
if (cmd.empty() || m_webserverCommands.contains(cmd))
return;
if (!func)
return;
m_webserverCommands[cmd] = func;
}
void AstroWebServer::unRegisterWsCommand(const std::string &cmd)
{
if (m_webserverCommands.contains(cmd))
m_webserverCommands.erase(cmd);
}
void AstroWebServer::onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) void AstroWebServer::onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len)
{ {
switch (type) switch (type)
@@ -75,38 +89,13 @@ void AstroWebServer::onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *cli
LOG_ERROR("WS Client unable to deserialize Json"); LOG_ERROR("WS Client unable to deserialize Json");
return; return;
} }
if (!doc["cmd"].is<std::string>() || !s_webserverCommands.contains(doc["cmd"])) if (!doc["cmd"].is<std::string>() || !m_webserverCommands.contains(doc["cmd"]))
{ {
LOG_WARN("WS Client Invalid Json command [", doc["cmd"].as<std::string>().c_str(), "]"); LOG_WARN("WS Client Invalid Json command [", doc["cmd"].as<std::string>().c_str(), "]");
return; return;
} }
std::string buffer; // execute callback function
switch (s_webserverCommands.at(doc["cmd"])) m_webserverCommands[doc["cmd"]](doc);
{
case SET_TIME:
{
auto epoch = doc["time"].as<time_t>();
timeval te{
.tv_sec = epoch,
.tv_usec = 0,
};
timezone tz{
.tz_minuteswest = 0,
.tz_dsttime = DST_MET,
};
settimeofday(&te, &tz);
time_t now = time(nullptr);
struct tm *t = localtime(&now);
buffer.resize(64);
strftime(buffer.data(), sizeof(buffer), "%Y-%m-%d %H:%M:%S", t);
LOG_DEBUG("WS Client set Datetime to: ", buffer.c_str());
break;
}
default:
// call external command callback
break;
}
} }
} }
} }
@@ -164,3 +153,23 @@ void AstroWebServer::onUploadHandler(AsyncWebServerRequest *request, const Strin
LOG_INFO("Uploaded file to LittleFS:", filename.c_str()); LOG_INFO("Uploaded file to LittleFS:", filename.c_str());
} }
} }
void AstroWebServer::onSetTme(const ArduinoJson::JsonDocument &doc)
{
std::string buffer;
auto epoch = doc["time"].as<time_t>();
timeval te{
.tv_sec = epoch,
.tv_usec = 0,
};
timezone tz{
.tz_minuteswest = 0,
.tz_dsttime = DST_MET,
};
settimeofday(&te, &tz);
time_t now = time(nullptr);
struct tm *t = localtime(&now);
buffer.resize(64);
strftime(buffer.data(), sizeof(buffer), "%Y-%m-%d %H:%M:%S", t);
LOG_DEBUG("WS Client set Datetime to: ", buffer.c_str());
}

View File

@@ -8,11 +8,13 @@
#include <AsyncTCP.h> #include <AsyncTCP.h>
#include <filesystem> #include <filesystem>
#include <map> #include <map>
#include <FS.h> #include <FS.h>
#include <ArduinoJson.h>
class AstroWebServer class AstroWebServer
{ {
public:
using WScommand = std::function<void(const ArduinoJson::JsonDocument &)>;
public: public:
AstroWebServer(const uint8_t port, fs::FS &filesystem); AstroWebServer(const uint8_t port, fs::FS &filesystem);
@@ -20,6 +22,9 @@ public:
void sendWsData(const String &data); void sendWsData(const String &data);
void registerWsCommand(const std::string &cmd, const WScommand func);
void unRegisterWsCommand(const std::string &cmd);
private: private:
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client,
AwsEventType type, void *arg, uint8_t *data, size_t len); AwsEventType type, void *arg, uint8_t *data, size_t len);
@@ -27,9 +32,7 @@ private:
void onUploadRequest(AsyncWebServerRequest *request); void onUploadRequest(AsyncWebServerRequest *request);
void onUploadHandler(AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len, bool final); void onUploadHandler(AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len, bool final);
void onStart(AsyncWebServerRequest *request); void onSetTme(const ArduinoJson::JsonDocument &doc);
void onStop(AsyncWebServerRequest *request);
void onDownload(AsyncWebServerRequest *request);
private: private:
const uint8_t m_port = 80; const uint8_t m_port = 80;
@@ -39,10 +42,5 @@ private:
bool m_uploadFailed = false; bool m_uploadFailed = false;
fs::File m_uploadFile; fs::File m_uploadFile;
TimerHandle_t m_pingTimer = NULL; TimerHandle_t m_pingTimer = NULL;
std::map<const std::string, AstroWebServer::WScommand> m_webserverCommands;
public:
enum c_commandEnum
{
SET_TIME
};
}; };