Integrate Communication into Power Management

This commit is contained in:
Phillip Kühne 2025-02-12 17:58:32 +01:00
parent d64579eca4
commit 4bfae98f6b
Signed by: phillip
GPG Key ID: E4C1C4D2F90902AA
3 changed files with 76 additions and 63 deletions

View File

@ -1,4 +1,5 @@
#include "Communication.h" #include "Communication.h"
#include "../power/Power.h"
Scheduler userScheduler; // to control your personal task Scheduler userScheduler; // to control your personal task
painlessMesh mesh; painlessMesh mesh;
@ -7,24 +8,24 @@ uint32_t Communication::groupNumber = 0;
// User-defined callback function pointer // User-defined callback function pointer
void (*Communication::userCallback)(String &msg) = nullptr; void (*Communication::userCallback)(String &msg) = nullptr;
void Communication::sendMessage(String msg) void Communication::sendMessage(String msg) {
{
String data = String(groupNumber) + "#" + msg; String data = String(groupNumber) + "#" + msg;
mesh.sendBroadcast(data); mesh.sendBroadcast(data);
} }
// Needed for painless library // Needed for painless library
void Communication::receivedCallback(uint32_t from, String &msg) void Communication::receivedCallback(uint32_t from, String &msg) {
{
int separatorIndex = msg.indexOf('#'); int separatorIndex = msg.indexOf('#');
if (separatorIndex != -1) { if (separatorIndex != -1) {
String groupNumberStr = msg.substring(0, separatorIndex); String groupNumberStr = msg.substring(0, separatorIndex);
uint32_t num = groupNumberStr.toInt(); uint32_t num = groupNumberStr.toInt();
String restOfMsg = msg.substring(separatorIndex + 1); 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 // Execute user-defined callback if it is set
if (userCallback) { if (userCallback) {
@ -33,46 +34,50 @@ void Communication::receivedCallback(uint32_t from, String &msg)
} }
} }
void newConnectionCallback(uint32_t nodeId) void newConnectionCallback(uint32_t nodeId) {
{
Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId); Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId);
} }
void changedConnectionCallback() void changedConnectionCallback() { Serial.printf("Changed connections\n"); }
{
Serial.printf("Changed connections\n");
}
void nodeTimeAdjustedCallback(int32_t offset) void nodeTimeAdjustedCallback(int32_t offset) {
{
Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(), offset); Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(), offset);
} }
void vTaskUpdate(void *pvParameters) void vTaskUpdate(void *pvParameters) {
{ for (;;) {
for (;;) Power::waitForCurrentAllowance(
{ PowerParameters::PowerConsumers::WIFI,
PowerParameters::CurrentConsumptions::CURRENT_WIFI_PEAK +
PowerParameters::CurrentConsumptions::CURRENT_WIFI_BASE,
MESSH_MAX_EXECUTION_DELAY_MS, NULL);
mesh.update(); mesh.update();
Power::waitForCurrentAllowance(
PowerParameters::PowerConsumers::WIFI,
PowerParameters::CurrentConsumptions::CURRENT_WIFI_BASE,
MESSH_MAX_EXECUTION_DELAY_MS, NULL);
} }
} }
void Communication::setGroupNumber(uint32_t number) { void Communication::setGroupNumber(uint32_t number) { groupNumber = number; }
groupNumber = number;
}
// Method to set the user-defined callback function // Method to set the user-defined callback function
void Communication::onReceive(void (*callbackFunc)(String &msg)) void Communication::onReceive(void (*callbackFunc)(String &msg)) {
{
userCallback = callbackFunc; userCallback = callbackFunc;
} }
void Communication::begin(void) void Communication::begin(void) {
{
Serial.begin(115200); Serial.begin(115200);
// mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on // mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC |
mesh.setDebugMsgTypes(ERROR | STARTUP); // set before init() so that you can see startup messages // 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.init(MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT);
mesh.onReceive(&receivedCallback); mesh.onReceive(&receivedCallback);
mesh.onNewConnection(&newConnectionCallback); mesh.onNewConnection(&newConnectionCallback);
@ -82,6 +87,7 @@ void Communication::begin(void)
static uint8_t ucParameterToPass; static uint8_t ucParameterToPass;
TaskHandle_t xHandle = NULL; TaskHandle_t xHandle = NULL;
xTaskCreate(vTaskUpdate, "vTaskMeshUpdate", 4096, &ucParameterToPass, tskIDLE_PRIORITY, &xHandle); xTaskCreate(vTaskUpdate, "vTaskMeshUpdate", 4096, &ucParameterToPass,
tskIDLE_PRIORITY, &xHandle);
configASSERT(xHandle); configASSERT(xHandle);
}; };

View File

@ -8,6 +8,9 @@
#define MESH_PREFIX "DEZIBOT_MESH" #define MESH_PREFIX "DEZIBOT_MESH"
#define MESH_PASSWORD "somethingSneaky" #define MESH_PASSWORD "somethingSneaky"
#define MESH_PORT 5555 #define MESH_PORT 5555
#define MESSH_MAX_EXECUTION_DELAY_MS 10
class Communication{ class Communication{
@ -23,6 +26,7 @@ public:
void sendMessage(String msg); void sendMessage(String msg);
void onReceive(void (*callbackFunc)(String &msg)); void onReceive(void (*callbackFunc)(String &msg));
private: private:
static void (*userCallback)(String &msg); static void (*userCallback)(String &msg);
static void receivedCallback(uint32_t from, String &msg); static void receivedCallback(uint32_t from, String &msg);

View File

@ -59,6 +59,9 @@ namespace PowerParameters {
static constexpr float CURRENT_ESP_LOAD = 88.43; static constexpr float CURRENT_ESP_LOAD = 88.43;
static constexpr float CURRENT_ESP_AVG = static constexpr float CURRENT_ESP_AVG =
(CURRENT_ESP_BASE + CURRENT_ESP_LOAD) / 2; (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 // RGB LED quiescent current
static constexpr float CURRENT_LED_RGB_BASE = 0.7; static constexpr float CURRENT_LED_RGB_BASE = 0.7;
// RGB LED per channel current during PWM on-time. // RGB LED per channel current during PWM on-time.