improved bus wait with raii class that updates last access

This commit is contained in:
Emanuele Trabattoni
2025-07-25 10:57:17 +02:00
parent bb0832ad4f
commit 31c6cd9606
14 changed files with 173 additions and 127 deletions

View File

@@ -32,11 +32,12 @@ namespace commands
ArduinoJson::JsonDocument response;
const auto pinfo = dev.seneca.getAll();
response["cmd"] = "getHPpower";
auto values = response["params"].to<JsonObject>();
auto values = response["values"].to<JsonObject>();
values["power"] = pinfo.pAct;
values["current"] = pinfo.a;
values["voltage"] = pinfo.v;
values["energy"] = pinfo.whPar;
LOG_INFO("getHPpower -> power", pinfo.pAct, "current", pinfo.a, "energy", pinfo.whPar);
LOG_INFO("getHPpower -> power", pinfo.pAct, "current", pinfo.a, "voltage", pinfo.v, "energy", pinfo.whPar);
return response;
}

View File

@@ -1,5 +1,7 @@
#pragma once
#define DEBUGLOG_DEFAULT_LOG_LEVEL_INFO
#include <DebugLog.h>
#include <Arduino.h>
#include <ArduinoJson.h>

View File

@@ -1,3 +1,5 @@
#define DEBUGLOG_DEFAULT_LOG_LEVEL_INFO
#include <Arduino.h>
#include <DebugLog.h>
#include <DebugLogEnable.h>
@@ -57,7 +59,7 @@ void loop()
std::function<void(const ArduinoJson::JsonDocument &)> commandsCallback =
[&mqtt, &devices](const ArduinoJson::JsonDocument &doc)
{
if (!doc["cmd"].is<std::string>() || !doc["params"].is<ArduinoJson::JsonObject>())
if (!doc["cmd"].is<std::string>())
{
LOG_ERROR("Invalid Json Command");
return;
@@ -123,19 +125,19 @@ void loop()
while (true)
{
const uint32_t start(millis());
const std::string timeStr(rtc.getTimeStr());
LOG_INFO("[", k++, "] Loop - Current Datetime", timeStr.c_str());
{
ArduinoJson::JsonDocument poll;
poll["cmd"] = "POLL";
auto params = poll["params"].to<ArduinoJson::JsonObject>();
auto params = poll["values"].to<ArduinoJson::JsonObject>();
params["time"] = timeStr;
params["number"] = k;
mqtt.publish(conf.m_mqttPublish["answers"], poll);
};
{
ArduinoJson::JsonDocument ti;
auto tempinfo = tmp.getTempAll();
@@ -144,17 +146,7 @@ void loop()
ti["heating"] = tempinfo.at(0);
mqtt.publish(conf.m_mqttPublish["temperatures"], ti);
};
{
ArduinoJson::JsonDocument pi;
auto powerinfo = seneca.getAll();
pi["power"] = powerinfo.pAct;
pi["current"] = powerinfo.a;
pi["energy"] = powerinfo.whPar;
pi["voltage"] = powerinfo.v;
mqtt.publish(conf.m_mqttPublish["heatpump"], pi);
}
if (io.digitalInRead(0)) // ROSSO - Config Reset
{
LOG_WARN("Config RESET!");
@@ -162,7 +154,7 @@ void loop()
delay(500);
conf.resetConfig();
}
if (io.digitalInRead(1)) // GIALLO - Restart
{
LOG_WARN("RESTART!");
@@ -171,9 +163,9 @@ void loop()
esp_restart();
}
delay(conf.m_globalLoopDelay); // to avoid too fast loop
delay(conf.m_globalLoopDelay - (start - millis())); // to avoid too fast loop, keep precise timing computing loop time
}
////////////////////////////////////////
///////// MAIN LOOP INSIDE LOOP ////////
////////////////////////////////////////

View File

@@ -1,4 +1,7 @@
#include <remoteIO.h>
#include <busdelay.h>
#define BUS_DELAY drivers::BusDelay(m_lastRequest, c_minDelay, "remoteIO")
remoteIO::remoteIO(const uint8_t address, drivers::MODBUS &bus) : m_address(address), m_initialized(false), m_bus(bus)
{
@@ -21,23 +24,12 @@ remoteIO::~remoteIO()
resetAll(false);
}
void remoteIO::delayRequest()
{
auto now = millis();
if ((now - m_lastRequest) < c_minDelay)
{ // minimum m_lastRequest between requests
LOG_DEBUG("remoteIO delay request", (now - m_lastRequest));
delay(now - m_lastRequest);
}
m_lastRequest = millis();
}
const bool remoteIO::setOut(const channel_t ch, const bool value)
{
if (!m_initialized)
return false;
std::lock_guard<std::mutex> lock(m_bus.getMutex());
delayRequest();
BUS_DELAY;
LOG_DEBUG("Write Channel", ch, "->", value ? "True" : "False");
return m_bus.writeCoil(m_address, REG_COILS + ch, value);
}
@@ -47,7 +39,7 @@ const bool remoteIO::toggleOut(const channel_t ch)
if (!m_initialized)
return false;
std::lock_guard<std::mutex> lock(m_bus.getMutex());
delayRequest();
BUS_DELAY;
std::vector<bool> value;
if (!m_bus.readCoils(m_address, REG_COILS + ch, 1, value))
return false;
@@ -60,7 +52,7 @@ const bool remoteIO::setOutPort(const std::vector<bool> values)
if (!m_initialized)
return false;
std::lock_guard<std::mutex> lock(m_bus.getMutex());
delayRequest();
BUS_DELAY;
LOG_DEBUG("Write Port", CH_MAX);
return m_bus.writeCoils(m_address, REG_COILS, values);
}
@@ -70,7 +62,7 @@ const bool remoteIO::getOut(const channel_t ch, bool &value)
if (!m_initialized)
return false;
std::lock_guard<std::mutex> lock(m_bus.getMutex());
delayRequest();
BUS_DELAY;
std::vector<bool> values;
if (!m_bus.readCoils(m_address, REG_COILS + ch, 1, values))
return false;
@@ -84,7 +76,7 @@ const bool remoteIO::getOutPort(std::vector<bool> &values)
if (!m_initialized)
return false;
std::lock_guard<std::mutex> lock(m_bus.getMutex());
delayRequest();
BUS_DELAY;
LOG_DEBUG("Read Port", CH_MAX);
return m_bus.readCoils(m_address, REG_COILS, CH_MAX, values);
}
@@ -94,7 +86,7 @@ const bool remoteIO::getIn(const channel_t input, bool &value)
if (!m_initialized)
return false;
std::lock_guard<std::mutex> lock(m_bus.getMutex());
delayRequest();
BUS_DELAY;
std::vector<bool> values;
if (!m_bus.readInputs(m_address, REG_INPUT + input, 1, values))
return false;
@@ -108,7 +100,7 @@ const bool remoteIO::getInPort(std::vector<bool> &values)
if (!m_initialized)
return false;
std::lock_guard<std::mutex> lock(m_bus.getMutex());
delayRequest();
BUS_DELAY;
LOG_DEBUG("Read Inputs", CH_MAX);
return m_bus.readInputs(m_address, REG_INPUT, CH_MAX, values);
}
@@ -117,4 +109,6 @@ void remoteIO::resetAll(const bool value)
{
LOG_DEBUG("Reset All ->", value ? "True" : "False");
m_bus.writeCoil(m_address, REG_ALLCOILS, value);
}
}
#undef BUS_DELAY

View File

@@ -4,6 +4,7 @@
#include <DebugLog.h>
#include <RS485_Driver.h>
#include <utils.h>
class remoteIO
{
@@ -44,9 +45,6 @@ public:
void resetAll(const bool value);
private:
void delayRequest();
private:
bool m_initialized;
drivers::MODBUS &m_bus;