DigitalIO driver with dynamic channel count
This commit is contained in:
93
src/remoteIO.cpp
Normal file
93
src/remoteIO.cpp
Normal file
@@ -0,0 +1,93 @@
|
||||
#include <remoteIO.h>
|
||||
|
||||
remoteIO::remoteIO(const uint8_t address, drivers::MODBUS &bus) : m_address(address), m_initialized(false), m_bus(bus)
|
||||
{
|
||||
LOG_INFO("Initializing relay module");
|
||||
std::vector<uint16_t> response;
|
||||
if (!m_bus.readHoldingRegisters(m_address, REG_VERSION, 1, response))
|
||||
{
|
||||
LOG_ERROR("Unable to inizialize relay module");
|
||||
};
|
||||
LOG_INFO("Software version", std::to_string(response.at(0) / 100.0f).c_str());
|
||||
m_initialized = true;
|
||||
resetAll(false);
|
||||
}
|
||||
|
||||
remoteIO::~remoteIO()
|
||||
{
|
||||
m_initialized = false;
|
||||
resetAll(false);
|
||||
}
|
||||
|
||||
const bool remoteIO::setOut(const channel_t ch, const bool value)
|
||||
{
|
||||
if (!m_initialized)
|
||||
return false;
|
||||
LOG_DEBUG("Write Channel", ch, "->", value ? "True" : "False");
|
||||
return m_bus.writeCoil(m_address, REG_COILS + ch, value);
|
||||
}
|
||||
|
||||
const bool remoteIO::toggleOut(const channel_t ch)
|
||||
{
|
||||
if (!m_initialized)
|
||||
return false;
|
||||
std::vector<bool> value;
|
||||
if (!m_bus.readCoils(m_address, REG_COILS + ch, 1, value))
|
||||
return false;
|
||||
LOG_DEBUG("Toggle Channel", ch, "->", !value.front() ? "True" : "False");
|
||||
return m_bus.writeCoil(m_address, REG_COILS + ch, !value.front());
|
||||
}
|
||||
|
||||
const bool remoteIO::setOutPort(const std::vector<bool> values)
|
||||
{
|
||||
if (!m_initialized)
|
||||
return false;
|
||||
LOG_DEBUG("Write Port", CH_MAX);
|
||||
return m_bus.writeCoils(m_address, CH_MAX, values);
|
||||
}
|
||||
|
||||
const bool remoteIO::getOut(const channel_t ch, bool &value)
|
||||
{
|
||||
if (!m_initialized)
|
||||
return false;
|
||||
std::vector<bool> values;
|
||||
if (!m_bus.readCoils(m_address, REG_COILS + ch, 1, values))
|
||||
return false;
|
||||
value = values.front();
|
||||
LOG_DEBUG("Read Channel", ch, "->", value ? "True" : "False");
|
||||
return true;
|
||||
}
|
||||
|
||||
const bool remoteIO::getOutPort(std::vector<bool> &values)
|
||||
{
|
||||
if (!m_initialized)
|
||||
return false;
|
||||
LOG_DEBUG("Read Port", CH_MAX);
|
||||
return m_bus.readCoils(m_address, REG_COILS, 8, values);
|
||||
}
|
||||
|
||||
const bool remoteIO::getIn(const channel_t input, bool &value)
|
||||
{
|
||||
if (!m_initialized)
|
||||
return false;
|
||||
std::vector<bool> values;
|
||||
if (!m_bus.readInputs(m_address, REG_INPUT + input, 1, values))
|
||||
return false;
|
||||
value = values.front();
|
||||
LOG_DEBUG("Read Input", input, "->", values.front() ? "True" : "False");
|
||||
return true;
|
||||
}
|
||||
|
||||
const bool remoteIO::getInPort(std::vector<bool> &values)
|
||||
{
|
||||
if (!m_initialized)
|
||||
return false;
|
||||
LOG_DEBUG("Read Inputs", CH_MAX);
|
||||
return m_bus.readInputs(m_address, REG_INPUT, CH_MAX, values);
|
||||
}
|
||||
|
||||
void remoteIO::resetAll(const bool value)
|
||||
{
|
||||
LOG_DEBUG("Reset All ->", value ? "True" : "False");
|
||||
m_bus.writeCoil(m_address, REG_ALLCOILS, value);
|
||||
}
|
||||
Reference in New Issue
Block a user