From 4bfae98f6b203f7490536af62a2d2394d5804e7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Phillip=20K=C3=BChne?= Date: Wed, 12 Feb 2025 17:58:32 +0100 Subject: [PATCH] Integrate Communication into Power Management --- src/communication/Communication.cpp | 116 +++++++++++++++------------- src/communication/Communication.h | 20 +++-- src/power/PowerParameters.h | 3 + 3 files changed, 76 insertions(+), 63 deletions(-) diff --git a/src/communication/Communication.cpp b/src/communication/Communication.cpp index 6e443f9..d2ef5d8 100644 --- a/src/communication/Communication.cpp +++ b/src/communication/Communication.cpp @@ -1,4 +1,5 @@ #include "Communication.h" +#include "../power/Power.h" Scheduler userScheduler; // to control your personal task painlessMesh mesh; @@ -7,81 +8,86 @@ uint32_t Communication::groupNumber = 0; // User-defined callback function pointer void (*Communication::userCallback)(String &msg) = nullptr; -void Communication::sendMessage(String msg) -{ - String data = String(groupNumber) + "#" + msg; - mesh.sendBroadcast(data); +void Communication::sendMessage(String msg) { + String data = String(groupNumber) + "#" + msg; + mesh.sendBroadcast(data); } // Needed for painless library -void Communication::receivedCallback(uint32_t from, String &msg) -{ - int separatorIndex = msg.indexOf('#'); - if (separatorIndex != -1) { - String groupNumberStr = msg.substring(0, separatorIndex); - uint32_t num = groupNumberStr.toInt(); - String restOfMsg = msg.substring(separatorIndex + 1); +void Communication::receivedCallback(uint32_t from, String &msg) { + int separatorIndex = msg.indexOf('#'); + if (separatorIndex != -1) { + String groupNumberStr = msg.substring(0, separatorIndex); + uint32_t num = groupNumberStr.toInt(); + String restOfMsg = msg.substring(separatorIndex + 1); - Serial.printf("startHere: Received from %u groupNumber=%u msg=%s\n", from, num, restOfMsg.c_str()); + Serial.printf("startHere: Received from %u groupNumber=%u msg=%s\n", from, + num, restOfMsg.c_str()); - if (groupNumber != num) return; + if (groupNumber != num) + return; - // Execute user-defined callback if it is set - if (userCallback) { - userCallback(restOfMsg); - } + // Execute user-defined callback if it is set + if (userCallback) { + userCallback(restOfMsg); } + } } -void newConnectionCallback(uint32_t nodeId) -{ - Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId); +void newConnectionCallback(uint32_t nodeId) { + Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId); } -void changedConnectionCallback() -{ - Serial.printf("Changed connections\n"); +void changedConnectionCallback() { Serial.printf("Changed connections\n"); } + +void nodeTimeAdjustedCallback(int32_t offset) { + Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(), offset); } -void nodeTimeAdjustedCallback(int32_t offset) -{ - Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(), offset); +void vTaskUpdate(void *pvParameters) { + for (;;) { + Power::waitForCurrentAllowance( + PowerParameters::PowerConsumers::WIFI, + PowerParameters::CurrentConsumptions::CURRENT_WIFI_PEAK + + PowerParameters::CurrentConsumptions::CURRENT_WIFI_BASE, + MESSH_MAX_EXECUTION_DELAY_MS, NULL); + mesh.update(); + Power::waitForCurrentAllowance( + PowerParameters::PowerConsumers::WIFI, + PowerParameters::CurrentConsumptions::CURRENT_WIFI_BASE, + MESSH_MAX_EXECUTION_DELAY_MS, NULL); + } } -void vTaskUpdate(void *pvParameters) -{ - for (;;) - { - mesh.update(); - } -} - -void Communication::setGroupNumber(uint32_t number) { - groupNumber = number; -} +void Communication::setGroupNumber(uint32_t number) { groupNumber = number; } // Method to set the user-defined callback function -void Communication::onReceive(void (*callbackFunc)(String &msg)) -{ - userCallback = callbackFunc; +void Communication::onReceive(void (*callbackFunc)(String &msg)) { + userCallback = callbackFunc; } -void Communication::begin(void) -{ - Serial.begin(115200); +void Communication::begin(void) { + Serial.begin(115200); - // mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on - mesh.setDebugMsgTypes(ERROR | STARTUP); // set before init() so that you can see startup messages + // mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | + // COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on + mesh.setDebugMsgTypes( + ERROR | + STARTUP); // set before init() so that you can see startup messages + Power::waitForCurrentAllowance( + PowerParameters::PowerConsumers::WIFI, + PowerParameters::CurrentConsumptions::CURRENT_WIFI_BASE, + MESSH_MAX_EXECUTION_DELAY_MS, NULL); + mesh.init(MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT); + mesh.onReceive(&receivedCallback); + mesh.onNewConnection(&newConnectionCallback); + mesh.onChangedConnections(&changedConnectionCallback); + mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback); - mesh.init(MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT); - mesh.onReceive(&receivedCallback); - mesh.onNewConnection(&newConnectionCallback); - mesh.onChangedConnections(&changedConnectionCallback); - mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback); + static uint8_t ucParameterToPass; + TaskHandle_t xHandle = NULL; - static uint8_t ucParameterToPass; - TaskHandle_t xHandle = NULL; - - xTaskCreate(vTaskUpdate, "vTaskMeshUpdate", 4096, &ucParameterToPass, tskIDLE_PRIORITY, &xHandle); - configASSERT(xHandle); + xTaskCreate(vTaskUpdate, "vTaskMeshUpdate", 4096, &ucParameterToPass, + tskIDLE_PRIORITY, &xHandle); + configASSERT(xHandle); }; \ No newline at end of file diff --git a/src/communication/Communication.h b/src/communication/Communication.h index e6644f7..215fc9f 100644 --- a/src/communication/Communication.h +++ b/src/communication/Communication.h @@ -8,22 +8,26 @@ #define MESH_PREFIX "DEZIBOT_MESH" #define MESH_PASSWORD "somethingSneaky" #define MESH_PORT 5555 +#define MESSH_MAX_EXECUTION_DELAY_MS 10 + + class Communication{ -public: + public: /** - * @brief initialize the Mesh Compnent, must be called before the other methods are used. - * - */ + * @brief initialize the Mesh Compnent, must be called before the other methods are used. + * + */ static void begin(void); - + void setGroupNumber(uint32_t number); - + void sendMessage(String msg); - + void onReceive(void (*callbackFunc)(String &msg)); -private: + + private: static void (*userCallback)(String &msg); static void receivedCallback(uint32_t from, String &msg); static uint32_t groupNumber; diff --git a/src/power/PowerParameters.h b/src/power/PowerParameters.h index 6b21529..1487e1c 100644 --- a/src/power/PowerParameters.h +++ b/src/power/PowerParameters.h @@ -59,6 +59,9 @@ namespace PowerParameters { static constexpr float CURRENT_ESP_LOAD = 88.43; static constexpr float CURRENT_ESP_AVG = (CURRENT_ESP_BASE + CURRENT_ESP_LOAD) / 2; + // WiFi current consumptions + static constexpr float CURRENT_WIFI_BASE = 64.58; + static constexpr float CURRENT_WIFI_PEAK = 128; // RGB LED quiescent current static constexpr float CURRENT_LED_RGB_BASE = 0.7; // RGB LED per channel current during PWM on-time.