expand and fix digitalIO class

This commit is contained in:
Emanuele Trabattoni
2025-07-24 13:51:21 +02:00
parent 71c7ff8756
commit 07dd200de8
12 changed files with 403 additions and 81 deletions

View File

@@ -69,16 +69,18 @@ namespace drivers
}
void MODBUS::delayAccess(const uint8_t device)
{
if (device == m_lastDevice) return;
auto now = millis();
if ((now - m_lastAccess) < c_minDelay) // fixed 10 milliseconds delay between commands
{ // minimum m_lastRequest between requests
delay(now - m_lastAccess);
}
m_lastAccess = now;
m_lastDevice = device;
{
if (device == m_lastDevice)
return;
auto now = millis();
if ((now - m_lastAccess) < c_minDelay) // fixed milliseconds delay between commands to different devices
{ // minimum m_lastRequest between requests
LOG_WARN("MODBUS access delay", now - m_lastAccess, "device", device);
delay(now - m_lastAccess);
}
m_lastAccess = millis();
m_lastDevice = device;
}
// Func 0x01
const bool MODBUS::readCoils(const uint8_t device, const uint16_t reg, const uint16_t num, std::vector<bool> &coils)
@@ -88,7 +90,7 @@ namespace drivers
LOG_DEBUG("Read coils: dev[", device, "], reg[", reg, "], num[", num, "]");
return readBinary(device, func, reg, num, coils);
}
// Func 0x02
const bool MODBUS::readInputs(const uint8_t device, const uint16_t reg, const uint8_t num, std::vector<bool> &inputs)
{
@@ -97,7 +99,7 @@ namespace drivers
LOG_DEBUG("Read multi inputs: dev[", device, "], reg[", reg, "], num[", num, "]");
return readBinary(device, func, reg, num, inputs);
}
// Func 0x03
const bool MODBUS::readHoldingRegisters(const uint8_t device, const uint16_t reg, const uint8_t num, std::vector<uint16_t> &values)
{
@@ -106,7 +108,7 @@ namespace drivers
LOG_DEBUG("Read multi holding registers: dev[", device, "], reg[", reg, "], num[", num, "]");
return readInteger(device, func, reg, num, values);
}
// Func 0x04
const bool MODBUS::readInputRegisters(const uint8_t device, const uint16_t reg, const uint8_t num, std::vector<uint16_t> &values)
{
@@ -115,7 +117,7 @@ namespace drivers
LOG_DEBUG("Read multi input registers: dev[", device, "], reg[", reg, "], num[", num, "]");
return readInteger(device, func, reg, num, values);
}
// Func 0x05
const bool MODBUS::writeCoil(const uint8_t device, const uint16_t coil, const bool value)
{
@@ -124,7 +126,7 @@ namespace drivers
LOG_DEBUG("Write single coil: dev[", device, "], coil[", coil, "], value[", value ? "true" : "false", "]");
return writeBinary(device, func, coil, {value});
}
// Func 0x06
const bool MODBUS::writeRegister(const uint8_t device, const uint16_t reg, const uint16_t value)
{
@@ -133,7 +135,7 @@ namespace drivers
LOG_DEBUG("Write single register: dev[", device, "], reg[", reg, "], value[", value, "]");
return writeInteger(device, func, reg, {value}, false);
}
// Func 0x0F
const bool MODBUS::writeCoils(const uint8_t device, const uint16_t coils, const std::vector<bool> &values)
{
@@ -142,7 +144,7 @@ namespace drivers
LOG_DEBUG("Write multi coils: dev[", device, "], start[", coils, "], num[", values.size(), "]");
return writeBinary(device, func, coils, values);
}
// Func 0x10
const bool MODBUS::writeRegisters(const uint8_t device, const uint16_t reg, const std::vector<uint16_t> &values)
{