improved bus wait with raii class that updates last access
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user