expand and fix digitalIO class
This commit is contained in:
@@ -53,6 +53,12 @@ namespace drivers
|
|||||||
return setPort(newState);
|
return setPort(newState);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const bool TCA9554PWR::toggleOut(const uint8_t channel)
|
||||||
|
{
|
||||||
|
bool value;
|
||||||
|
return readOut(channel, value) && setOut(channel, value);
|
||||||
|
}
|
||||||
|
|
||||||
const bool TCA9554PWR::setPort(const uint8_t state)
|
const bool TCA9554PWR::setPort(const uint8_t state)
|
||||||
{
|
{
|
||||||
if (writeRegister(TCA9554_OUTPUT_REG, state))
|
if (writeRegister(TCA9554_OUTPUT_REG, state))
|
||||||
@@ -61,7 +67,7 @@ namespace drivers
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const bool TCA9554PWR::readOut(const uint8_t ch)
|
const bool TCA9554PWR::readOut(const uint8_t ch, bool &state)
|
||||||
{
|
{
|
||||||
uint8_t currState(0);
|
uint8_t currState(0);
|
||||||
if (ch < DO1 || ch > DO8)
|
if (ch < DO1 || ch > DO8)
|
||||||
@@ -71,12 +77,13 @@ namespace drivers
|
|||||||
}
|
}
|
||||||
if (!readPort(currState))
|
if (!readPort(currState))
|
||||||
return false;
|
return false;
|
||||||
return (currState && (High >> ch));
|
state = (currState && (High << ch));
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const bool TCA9554PWR::readPort(uint8_t &state)
|
const bool TCA9554PWR::readPort(uint8_t &state)
|
||||||
{
|
{
|
||||||
if (readRegister(TCA9554_INPUT_REG, state))
|
if (readRegister(TCA9554_OUTPUT_REG, state))
|
||||||
return true;
|
return true;
|
||||||
LOG_ERROR("Unable to read IO port: state[%02x]", state);
|
LOG_ERROR("Unable to read IO port: state[%02x]", state);
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -42,9 +42,10 @@ namespace drivers
|
|||||||
~TCA9554PWR();
|
~TCA9554PWR();
|
||||||
|
|
||||||
const bool setOut(const uint8_t channel, const bool state);
|
const bool setOut(const uint8_t channel, const bool state);
|
||||||
|
const bool toggleOut(const uint8_t channel);
|
||||||
const bool setPort(const uint8_t state);
|
const bool setPort(const uint8_t state);
|
||||||
|
|
||||||
const bool readOut(const uint8_t channel);
|
const bool readOut(const uint8_t channel, bool &state);
|
||||||
const bool readPort(uint8_t &state);
|
const bool readPort(uint8_t &state);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -70,13 +70,15 @@ namespace drivers
|
|||||||
|
|
||||||
void MODBUS::delayAccess(const uint8_t device)
|
void MODBUS::delayAccess(const uint8_t device)
|
||||||
{
|
{
|
||||||
if (device == m_lastDevice) return;
|
if (device == m_lastDevice)
|
||||||
|
return;
|
||||||
auto now = millis();
|
auto now = millis();
|
||||||
if ((now - m_lastAccess) < c_minDelay) // fixed 10 milliseconds delay between commands
|
if ((now - m_lastAccess) < c_minDelay) // fixed milliseconds delay between commands to different devices
|
||||||
{ // minimum m_lastRequest between requests
|
{ // minimum m_lastRequest between requests
|
||||||
|
LOG_WARN("MODBUS access delay", now - m_lastAccess, "device", device);
|
||||||
delay(now - m_lastAccess);
|
delay(now - m_lastAccess);
|
||||||
}
|
}
|
||||||
m_lastAccess = now;
|
m_lastAccess = millis();
|
||||||
m_lastDevice = device;
|
m_lastDevice = device;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ namespace drivers
|
|||||||
|
|
||||||
const uint8_t c_respHeaderSize = 3;
|
const uint8_t c_respHeaderSize = 3;
|
||||||
const uint8_t c_respCrcSize = 2;
|
const uint8_t c_respCrcSize = 2;
|
||||||
const uint16_t c_minDelay = 50;
|
const uint16_t c_minDelay = 10;
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ namespace drivers
|
|||||||
{ // minimum m_lastRequest between requests
|
{ // minimum m_lastRequest between requests
|
||||||
delay(now - m_lastRequest);
|
delay(now - m_lastRequest);
|
||||||
}
|
}
|
||||||
m_lastRequest = now;
|
m_lastRequest = millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
const uint8_t S50140::getRegset()
|
const uint8_t S50140::getRegset()
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ namespace drivers
|
|||||||
private:
|
private:
|
||||||
const uint8_t c_maxRetries = 5;
|
const uint8_t c_maxRetries = 5;
|
||||||
const uint8_t c_dataWords = 2;
|
const uint8_t c_dataWords = 2;
|
||||||
const uint16_t c_minDelay = 200;
|
const uint16_t c_minDelay = 500;
|
||||||
|
|
||||||
const uint16_t REG_V = 0x100C;
|
const uint16_t REG_V = 0x100C;
|
||||||
const uint16_t REG_A = 0x1016;
|
const uint16_t REG_A = 0x1016;
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
|
|
||||||
void printBytes(const char title[], const std::vector<uint8_t> &b)
|
void printBytes(const char title[], const std::vector<uint8_t> &b)
|
||||||
{
|
{
|
||||||
Serial0.flush();
|
Serial0.flush();
|
||||||
@@ -35,3 +36,15 @@ void printBool(const char title[], const std::vector<bool> &vals)
|
|||||||
printf("\n");
|
printf("\n");
|
||||||
Serial0.flush();
|
Serial0.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const std::string printBoolVec(const std::vector<bool> &vals)
|
||||||
|
{
|
||||||
|
std::string buf;
|
||||||
|
buf.reserve(vals.size()+1);
|
||||||
|
buf.append("b");
|
||||||
|
for (const auto v : vals)
|
||||||
|
{
|
||||||
|
buf.append(v ? "1" : "0");
|
||||||
|
}
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <DebugLog.h>
|
#include <DebugLog.h>
|
||||||
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
///////////// UTIL Functions /////////////////
|
///////////// UTIL Functions /////////////////
|
||||||
@@ -13,3 +14,5 @@ void printBytes(const char title[], const std::vector<uint8_t> &b);
|
|||||||
void printBytes(const char title[], const std::vector<uint16_t> &b);
|
void printBytes(const char title[], const std::vector<uint16_t> &b);
|
||||||
|
|
||||||
void printBool(const char title[], const std::vector<bool> &vals);
|
void printBool(const char title[], const std::vector<bool> &vals);
|
||||||
|
|
||||||
|
const std::string printBoolVec(const std::vector<bool> &vals);
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include <digitalIO.h>
|
#include <digitalIO.h>
|
||||||
|
#include <utils.h>
|
||||||
|
|
||||||
digitalIO::digitalIO(drivers::I2C &i2c, drivers::MODBUS &bus, std::vector<uint8_t> remotes) : m_localOuts(drivers::TCA9554PWR(i2c, TCA9554_ADDRESS)), m_remoteAddrs(remotes)
|
digitalIO::digitalIO(drivers::I2C &i2c, drivers::MODBUS &bus, std::vector<uint8_t> remotes) : m_localOuts(drivers::TCA9554PWR(i2c, TCA9554_ADDRESS)), m_remoteAddrs(remotes)
|
||||||
{
|
{
|
||||||
@@ -9,31 +10,76 @@ digitalIO::digitalIO(drivers::I2C &i2c, drivers::MODBUS &bus, std::vector<uint8_
|
|||||||
|
|
||||||
for (auto a : remotes)
|
for (auto a : remotes)
|
||||||
{
|
{
|
||||||
m_remotes.emplace_back(remoteIO(a, bus));
|
m_remotes.emplace_back(a, bus);
|
||||||
}
|
}
|
||||||
|
LOG_INFO("Initialized digitalIO -> inputs", getInNum(), "outputs", getOutNum());
|
||||||
}
|
}
|
||||||
|
|
||||||
digitalIO::~digitalIO()
|
digitalIO::~digitalIO()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void digitalIO::digitalIOWrite(const uint8_t ch, const bool value)
|
void digitalIO::digitalOutWrite(const uint8_t ch, const bool value)
|
||||||
{
|
{
|
||||||
if (ch < 0 || ch > getOutNum())
|
if (ch < 0 || ch > getOutNum())
|
||||||
{
|
{
|
||||||
LOG_ERROR("Invalid digitalIOWrite channel number", ch);
|
LOG_ERROR("Invalid digitalOutWrite channel number", ch);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ch < drivers::TCA9554PWR::DO_MAX) // write to i2c device for local outputs
|
if (ch < drivers::TCA9554PWR::DO_MAX) // write to i2c device for local outputs
|
||||||
{
|
{
|
||||||
digitalWriteLocal(ch, value);
|
writeLocal(ch, value);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
digitalWriteRemote(ch - drivers::TCA9554PWR::DO_MAX, value);
|
writeRemote(ch - drivers::TCA9554PWR::DO_MAX, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const bool digitalIO::digitalIORead(const uint8_t ch)
|
|
||||||
|
void digitalIO::digitalOutWritePort(const std::vector<bool> &values)
|
||||||
|
{
|
||||||
|
if (values.size() != getOutNum())
|
||||||
|
{
|
||||||
|
LOG_ERROR("Invalid digitalOutWrite channel number", values.size());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const std::vector<bool> locals(values.begin(), values.begin() + drivers::TCA9554PWR::DO_MAX);
|
||||||
|
const std::vector<bool> remotes(values.begin() + drivers::TCA9554PWR::DO_MAX, values.end());
|
||||||
|
writeLocalPort(locals);
|
||||||
|
writeRemotePort(remotes);
|
||||||
|
}
|
||||||
|
|
||||||
|
const bool digitalIO::digitalOutRead(const uint8_t ch)
|
||||||
|
{
|
||||||
|
if (ch < 0 || ch > getOutNum())
|
||||||
|
{
|
||||||
|
LOG_ERROR("Invalid digitalOutRead channel number", ch);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ch < drivers::TCA9554PWR::DO_MAX) // write to i2c device for local outputs
|
||||||
|
{
|
||||||
|
return readLocalIn(ch);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return readRemoteIn(ch - drivers::TCA9554PWR::DO_MAX);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::vector<bool> digitalIO::digitalOutReadPort()
|
||||||
|
{
|
||||||
|
const std::vector<bool> locals(readLocalOutPort());
|
||||||
|
const std::vector<bool> remotes(readRemoteOutPort());
|
||||||
|
std::vector<bool> rv;
|
||||||
|
rv.reserve(getOutNum());
|
||||||
|
rv.insert(rv.begin(), locals.begin(), locals.end());
|
||||||
|
rv.insert(rv.end(), remotes.begin(), remotes.end());
|
||||||
|
return std::move(rv);
|
||||||
|
}
|
||||||
|
|
||||||
|
const bool digitalIO::digitalInRead(const uint8_t ch)
|
||||||
{
|
{
|
||||||
if (ch < 0 || ch > getInNum())
|
if (ch < 0 || ch > getInNum())
|
||||||
{
|
{
|
||||||
@@ -42,14 +88,25 @@ const bool digitalIO::digitalIORead(const uint8_t ch)
|
|||||||
|
|
||||||
if (ch < (DI_MAX - DI1)) // read from local inputs not as gpio numbers
|
if (ch < (DI_MAX - DI1)) // read from local inputs not as gpio numbers
|
||||||
{
|
{
|
||||||
return digitalReadLocal(ch);
|
return readLocalIn(ch);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return digitalReadRemote(ch - (DI_MAX - DI1));
|
return readRemoteIn(ch - (DI_MAX - DI1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const std::vector<bool> digitalIO::digitalInReadPort()
|
||||||
|
{
|
||||||
|
const std::vector<bool> locals(readLocalInPort());
|
||||||
|
const std::vector<bool> remotes(readRemoteInPort());
|
||||||
|
std::vector<bool> rv;
|
||||||
|
rv.reserve(getInNum());
|
||||||
|
rv.insert(rv.begin(), locals.begin(), locals.end());
|
||||||
|
rv.insert(rv.end(), remotes.begin(), remotes.end());
|
||||||
|
return std::move(rv);
|
||||||
|
}
|
||||||
|
|
||||||
void digitalIO::reset()
|
void digitalIO::reset()
|
||||||
{
|
{
|
||||||
// set all local and remote outputs to 0
|
// set all local and remote outputs to 0
|
||||||
@@ -58,54 +115,165 @@ void digitalIO::reset()
|
|||||||
r.resetAll(false);
|
r.resetAll(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const uint8_t digitalIO::getLocalInNum()
|
||||||
|
{
|
||||||
|
return (DI_MAX - DI1);
|
||||||
|
}
|
||||||
|
const uint8_t digitalIO::getLocalOutNum()
|
||||||
|
{
|
||||||
|
return drivers::TCA9554PWR::DO_MAX;
|
||||||
|
}
|
||||||
|
const uint8_t digitalIO::getRemoteInNum()
|
||||||
|
{
|
||||||
|
return m_remotes.size() * remoteIO::CH_MAX;
|
||||||
|
}
|
||||||
|
const uint8_t digitalIO::getRemoteOutNum()
|
||||||
|
{
|
||||||
|
|
||||||
|
return m_remotes.size() * remoteIO::CH_MAX;
|
||||||
|
}
|
||||||
|
|
||||||
const uint8_t digitalIO::getOutNum()
|
const uint8_t digitalIO::getOutNum()
|
||||||
{
|
{
|
||||||
return drivers::TCA9554PWR::DO_MAX + m_remotes.size() * remoteIO::CH_MAX;
|
return getLocalOutNum() + getRemoteOutNum();
|
||||||
}
|
}
|
||||||
|
|
||||||
const uint8_t digitalIO::getInNum()
|
const uint8_t digitalIO::getInNum()
|
||||||
{
|
{
|
||||||
return DI_MAX + m_remotes.size() * remoteIO::CH_MAX;
|
return getLocalInNum() + getRemoteInNum();
|
||||||
}
|
}
|
||||||
|
|
||||||
void digitalIO::digitalWriteLocal(const uint8_t ch, const bool value)
|
void digitalIO::writeLocal(const uint8_t ch, const bool value)
|
||||||
{
|
{
|
||||||
uint8_t retries(0);
|
uint8_t retries(0);
|
||||||
while (retries++ < maxRetries)
|
while (retries++ < maxRetries)
|
||||||
{
|
{
|
||||||
if (m_localOuts.setOut(ch, value))
|
if (m_localOuts.setOut(ch, value))
|
||||||
{
|
{
|
||||||
LOG_DEBUG("digitalWriteLocal channel", ch, " status", value ? "True" : "False");
|
LOG_DEBUG("writeLocal channel", ch, " status", value ? "True" : "False");
|
||||||
break;
|
return;
|
||||||
}
|
}
|
||||||
LOG_ERROR("Failed digitalWriteLocal channel ", ch, " status", value ? "True" : "False");
|
LOG_ERROR("Failed writeLocal channel ", ch, " status", value ? "True" : "False");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void digitalIO::digitalWriteRemote(const uint8_t ch, const bool value)
|
void digitalIO::writeLocalPort(const std::vector<bool> &values)
|
||||||
{
|
{
|
||||||
uint8_t retries(0);
|
uint8_t retries(0);
|
||||||
const uint8_t selectedRemote(floor(ch / 8.0f));
|
uint8_t decValue(0);
|
||||||
|
for (uint8_t i(0); i < 8; i++) // convert from bits to byte value
|
||||||
|
{
|
||||||
|
if (values[i])
|
||||||
|
decValue |= High << i;
|
||||||
|
}
|
||||||
|
while (retries++ < maxRetries)
|
||||||
|
{
|
||||||
|
if (m_localOuts.setPort(decValue))
|
||||||
|
{
|
||||||
|
LOG_DEBUG("writeLocalPort value", printBoolVec(values).c_str());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
LOG_ERROR("Failed writeLocalPort value", printBoolVec(values).c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void digitalIO::writeRemote(const uint8_t ch, const bool value)
|
||||||
|
{
|
||||||
|
uint8_t retries(0);
|
||||||
|
const uint8_t selectedRemote(floor(ch / (float)remoteIO::CH_MAX));
|
||||||
const uint8_t selectedChannel(ch % remoteIO::CH_MAX);
|
const uint8_t selectedChannel(ch % remoteIO::CH_MAX);
|
||||||
while (retries++ < maxRetries)
|
while (retries++ < maxRetries)
|
||||||
{
|
{
|
||||||
if (m_remotes[selectedRemote].setOut((remoteIO::channel_t)selectedChannel, value))
|
if (m_remotes[selectedRemote].setOut((remoteIO::channel_t)selectedChannel, value))
|
||||||
{
|
{
|
||||||
LOG_DEBUG("digitalWriteRemote remote", selectedRemote, " channel ", selectedChannel, " status", value ? "True" : "False");
|
LOG_DEBUG("writeRemote remote", selectedRemote, " channel ", selectedChannel, " status", value ? "True" : "False");
|
||||||
break;
|
return;
|
||||||
}
|
}
|
||||||
LOG_ERROR("Failed digitalWriteRemote remote", selectedRemote, " channel ", selectedChannel, " status", value ? "True" : "False");
|
LOG_ERROR("Failed writeRemote remote", selectedRemote, " channel ", selectedChannel, " status", value ? "True" : "False");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const bool digitalIO::digitalReadLocal(const uint8_t ch)
|
void digitalIO::writeRemotePort(const std::vector<bool> &values)
|
||||||
|
{
|
||||||
|
uint8_t retries(0);
|
||||||
|
while (retries++ < maxRetries)
|
||||||
|
{
|
||||||
|
bool ok(true);
|
||||||
|
for (uint8_t i(0); i < values.size(); i += remoteIO::CH_MAX)
|
||||||
|
{
|
||||||
|
const uint8_t selectedRemote(floor(i / (float)remoteIO::CH_MAX));
|
||||||
|
const std::vector<bool> currValues(values.begin() + i, values.begin() + i + remoteIO::CH_MAX);
|
||||||
|
ok &= m_remotes[selectedRemote].setOutPort(currValues);
|
||||||
|
if (ok)
|
||||||
|
{
|
||||||
|
LOG_DEBUG("writeRemotePort remote", selectedRemote, "values", printBoolVec(values).c_str());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
LOG_ERROR("Failed writeRemotePort remote", selectedRemote, "values", printBoolVec(values).c_str());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (ok)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const bool digitalIO::readLocalIn(const uint8_t ch)
|
||||||
{
|
{
|
||||||
bool value = !digitalRead(ch + DI1); // base pin number in enum, inverted input
|
bool value = !digitalRead(ch + DI1); // base pin number in enum, inverted input
|
||||||
LOG_DEBUG("digitalReadLocal pin", (ch + DI1), " status", value ? "True" : "False");
|
LOG_DEBUG("readLocalIn pin", (ch + DI1), " status", value ? "True" : "False");
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
const bool digitalIO::digitalReadRemote(const uint8_t ch)
|
const bool digitalIO::readLocalOut(const uint8_t ch)
|
||||||
|
{
|
||||||
|
bool value(false);
|
||||||
|
uint8_t retries(0);
|
||||||
|
while (retries++ < maxRetries)
|
||||||
|
{
|
||||||
|
if (m_localOuts.readOut(ch, value))
|
||||||
|
{
|
||||||
|
LOG_DEBUG("readLocalOut pin", (ch), " status", value ? "True" : "False");
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
LOG_ERROR("Failed readLocalOut channel", ch);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::vector<bool> digitalIO::readLocalInPort()
|
||||||
|
{
|
||||||
|
std::vector<bool> values(getLocalInNum());
|
||||||
|
for (uint8_t i(0); i < values.size(); i++)
|
||||||
|
{
|
||||||
|
values[i] = readLocalIn(i);
|
||||||
|
}
|
||||||
|
LOG_DEBUG("readLocalInPort values", printBoolVec(values).c_str());
|
||||||
|
return values;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::vector<bool> digitalIO::readLocalOutPort()
|
||||||
|
{
|
||||||
|
uint8_t retries(0);
|
||||||
|
uint8_t state(0);
|
||||||
|
std::vector<bool> values(getLocalOutNum());
|
||||||
|
while (retries++ < maxRetries)
|
||||||
|
{
|
||||||
|
if (m_localOuts.readPort(state))
|
||||||
|
{
|
||||||
|
for (uint8_t i(0); i < values.size(); i++)
|
||||||
|
{
|
||||||
|
values[i] = (state >> i) & High;
|
||||||
|
}
|
||||||
|
LOG_DEBUG("readLocalOutPort values", printBoolVec(values).c_str());
|
||||||
|
return values;
|
||||||
|
}
|
||||||
|
LOG_ERROR("Failed readLocalOutPort");
|
||||||
|
}
|
||||||
|
values.clear();
|
||||||
|
return values;
|
||||||
|
}
|
||||||
|
|
||||||
|
const bool digitalIO::readRemoteIn(const uint8_t ch)
|
||||||
{
|
{
|
||||||
uint8_t retries(0);
|
uint8_t retries(0);
|
||||||
const uint8_t selectedRemote(floor(ch / 8.0f));
|
const uint8_t selectedRemote(floor(ch / 8.0f));
|
||||||
@@ -115,10 +283,86 @@ const bool digitalIO::digitalReadRemote(const uint8_t ch)
|
|||||||
{
|
{
|
||||||
if (m_remotes[selectedRemote].getIn((remoteIO::channel_t)selectedChannel, value))
|
if (m_remotes[selectedRemote].getIn((remoteIO::channel_t)selectedChannel, value))
|
||||||
{
|
{
|
||||||
LOG_DEBUG("digitalReadRemote remote", selectedRemote, " channel ", selectedChannel, " status", value ? "True" : "False");
|
LOG_DEBUG("readRemoteIn remote", selectedRemote, " channel ", selectedChannel, " status", value ? "True" : "False");
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
LOG_ERROR("Failed digitalReadRemote remote", selectedRemote, " channel ", selectedChannel, " status", value ? "True" : "False");
|
LOG_ERROR("Failed readRemoteIn remote", selectedRemote, " channel ", selectedChannel, " status", value ? "True" : "False");
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const bool digitalIO::readRemoteOut(const uint8_t ch)
|
||||||
|
{
|
||||||
|
uint8_t retries(0);
|
||||||
|
const uint8_t selectedRemote(floor(ch / (float)remoteIO::CH_MAX));
|
||||||
|
const uint8_t selectedChannel(ch % remoteIO::CH_MAX);
|
||||||
|
bool value;
|
||||||
|
while (retries++ < maxRetries)
|
||||||
|
{
|
||||||
|
if (m_remotes[selectedRemote].getOut((remoteIO::channel_t)selectedChannel, value))
|
||||||
|
{
|
||||||
|
LOG_DEBUG("readRemoteOut remote", selectedRemote, " channel ", selectedChannel, " status", value ? "True" : "False");
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
LOG_ERROR("Failed readRemoteOut remote", selectedRemote, " channel ", selectedChannel, " status", value ? "True" : "False");
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::vector<bool> digitalIO::readRemoteInPort()
|
||||||
|
{
|
||||||
|
uint8_t retries(0);
|
||||||
|
std::vector<bool> values;
|
||||||
|
values.reserve(getRemoteInNum());
|
||||||
|
while (retries++ < maxRetries)
|
||||||
|
{
|
||||||
|
bool ok(true);
|
||||||
|
for (uint8_t i(0); i < getRemoteInNum(); i += remoteIO::CH_MAX)
|
||||||
|
{
|
||||||
|
const uint8_t selectedRemote(floor(i / (float)remoteIO::CH_MAX));
|
||||||
|
std::vector<bool> remVals(remoteIO::CH_MAX);
|
||||||
|
ok &= m_remotes[selectedRemote].getInPort(remVals);
|
||||||
|
if (ok)
|
||||||
|
{
|
||||||
|
values.insert(values.begin() + values.size(), remVals.begin(), remVals.end());
|
||||||
|
LOG_DEBUG("readRemoteInPort remote", selectedRemote, "values", printBoolVec(remVals).c_str());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
LOG_ERROR("Failed readRemoteInPort remote", selectedRemote);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (ok)
|
||||||
|
return values;
|
||||||
|
}
|
||||||
|
values.clear();
|
||||||
|
return values;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::vector<bool> digitalIO::readRemoteOutPort()
|
||||||
|
{
|
||||||
|
uint8_t retries(0);
|
||||||
|
std::vector<bool> values;
|
||||||
|
values.reserve(getRemoteOutNum());
|
||||||
|
while (retries++ < maxRetries)
|
||||||
|
{
|
||||||
|
bool ok(true);
|
||||||
|
for (uint8_t i(0); i < getRemoteOutNum(); i += remoteIO::CH_MAX)
|
||||||
|
{
|
||||||
|
const uint8_t selectedRemote(floor(i / (float)remoteIO::CH_MAX));
|
||||||
|
std::vector<bool> remVals(remoteIO::CH_MAX);
|
||||||
|
ok &= m_remotes[selectedRemote].getOutPort(remVals);
|
||||||
|
if (ok)
|
||||||
|
{
|
||||||
|
values.insert(values.begin() + values.size(), remVals.begin(), remVals.end());
|
||||||
|
LOG_DEBUG("readRemoteOutPort remote", selectedRemote, "values", printBoolVec(remVals).c_str());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
LOG_ERROR("Failed readRemoteOutPort remote", selectedRemote);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (ok)
|
||||||
|
return values;
|
||||||
|
}
|
||||||
|
values.clear();
|
||||||
|
return values;
|
||||||
|
}
|
||||||
|
|||||||
@@ -34,19 +34,38 @@ public:
|
|||||||
digitalIO(drivers::I2C &i2c, drivers::MODBUS &bus, std::vector<uint8_t> remotes);
|
digitalIO(drivers::I2C &i2c, drivers::MODBUS &bus, std::vector<uint8_t> remotes);
|
||||||
~digitalIO();
|
~digitalIO();
|
||||||
|
|
||||||
void digitalIOWrite(const uint8_t ch, const bool value);
|
void digitalOutWrite(const uint8_t ch, const bool value);
|
||||||
const bool digitalIORead(const uint8_t ch);
|
void digitalOutWritePort(const std::vector<bool> &values);
|
||||||
|
const bool digitalOutRead(const uint8_t ch);
|
||||||
|
const std::vector<bool> digitalOutReadPort();
|
||||||
|
|
||||||
|
const bool digitalInRead(const uint8_t ch);
|
||||||
|
const std::vector<bool> digitalInReadPort();
|
||||||
|
|
||||||
void reset();
|
void reset();
|
||||||
|
|
||||||
const uint8_t getOutNum();
|
const uint8_t getOutNum();
|
||||||
const uint8_t getInNum();
|
const uint8_t getInNum();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void digitalWriteLocal(const uint8_t ch, const bool value);
|
const uint8_t getLocalInNum();
|
||||||
void digitalWriteRemote(const uint8_t ch, const bool value);
|
const uint8_t getLocalOutNum();
|
||||||
|
const uint8_t getRemoteInNum();
|
||||||
|
const uint8_t getRemoteOutNum();
|
||||||
|
|
||||||
const bool digitalReadLocal(const uint8_t ch);
|
void writeLocal(const uint8_t ch, const bool value);
|
||||||
const bool digitalReadRemote(const uint8_t ch);
|
void writeLocalPort(const std::vector<bool> &values);
|
||||||
|
void writeRemote(const uint8_t ch, const bool value);
|
||||||
|
void writeRemotePort(const std::vector<bool> &values);
|
||||||
|
|
||||||
|
const bool readLocalIn(const uint8_t ch);
|
||||||
|
const bool readLocalOut(const uint8_t ch);
|
||||||
|
const std::vector<bool> readLocalInPort();
|
||||||
|
const std::vector<bool> readLocalOutPort();
|
||||||
|
const bool readRemoteIn(const uint8_t ch);
|
||||||
|
const bool readRemoteOut(const uint8_t ch);
|
||||||
|
const std::vector<bool> readRemoteInPort();
|
||||||
|
const std::vector<bool> readRemoteOutPort();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<uint8_t> m_remoteAddrs;
|
std::vector<uint8_t> m_remoteAddrs;
|
||||||
|
|||||||
57
src/main.cpp
57
src/main.cpp
@@ -50,6 +50,7 @@ void loop()
|
|||||||
auto seneca = drivers::S50140(bus, conf.m_modbusSenecaAddr);
|
auto seneca = drivers::S50140(bus, conf.m_modbusSenecaAddr);
|
||||||
auto buzzer = drivers::Buzzer();
|
auto buzzer = drivers::Buzzer();
|
||||||
auto led = drivers::Led();
|
auto led = drivers::Led();
|
||||||
|
delay(500);
|
||||||
auto io = digitalIO(i2c, bus, {conf.m_modbusRelayAddr});
|
auto io = digitalIO(i2c, bus, {conf.m_modbusRelayAddr});
|
||||||
// Initialize temperature sensors
|
// Initialize temperature sensors
|
||||||
sensors = tmp.getNum();
|
sensors = tmp.getNum();
|
||||||
@@ -63,8 +64,24 @@ void loop()
|
|||||||
std::function<void(const ArduinoJson::JsonDocument &)> mycallback =
|
std::function<void(const ArduinoJson::JsonDocument &)> mycallback =
|
||||||
[&io](const ArduinoJson::JsonDocument &doc)
|
[&io](const ArduinoJson::JsonDocument &doc)
|
||||||
{
|
{
|
||||||
io.digitalIOWrite(0, doc["stat"].as<bool>());
|
std::vector<bool> v1 = {1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0};
|
||||||
io.digitalIOWrite(15, doc["stat"].as<bool>());
|
std::vector<bool> v2 = {0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1};
|
||||||
|
std::vector<bool> v0(io.getOutNum(), 0);
|
||||||
|
|
||||||
|
LOG_INFO("SET Digital Outputs V1: ", printBoolVec(v1).c_str());
|
||||||
|
io.digitalOutWritePort(v1);
|
||||||
|
delay(100);
|
||||||
|
LOG_INFO("GET Digital Outputs V1: ", printBoolVec(io.digitalOutReadPort()).c_str());
|
||||||
|
delay(2000);
|
||||||
|
|
||||||
|
LOG_INFO("SET Digital Outputs V2: ", printBoolVec(v2).c_str());
|
||||||
|
io.digitalOutWritePort(v2);
|
||||||
|
delay(100);
|
||||||
|
LOG_INFO("GET Digital Outputs V2: ", printBoolVec(io.digitalOutReadPort()).c_str());
|
||||||
|
delay(2000);
|
||||||
|
|
||||||
|
LOG_INFO("GET Digital Inputs: ", printBoolVec(io.digitalInReadPort()).c_str());
|
||||||
|
io.digitalOutWritePort(v0);
|
||||||
};
|
};
|
||||||
|
|
||||||
//////////////// NETWORK ////////////////
|
//////////////// NETWORK ////////////////
|
||||||
@@ -134,16 +151,32 @@ void loop()
|
|||||||
drivers::S50140::powerinfo_t pinfo = seneca.getAll();
|
drivers::S50140::powerinfo_t pinfo = seneca.getAll();
|
||||||
LOG_INFO("Power Info ==> V:", pinfo.v, "- A:", pinfo.a, "- W:", pinfo.pAct, "- F:", pinfo.f, "- Wh_t:", pinfo.whTot, "- Wh_p:", pinfo.whPar);
|
LOG_INFO("Power Info ==> V:", pinfo.v, "- A:", pinfo.a, "- W:", pinfo.pAct, "- F:", pinfo.f, "- Wh_t:", pinfo.whTot, "- Wh_p:", pinfo.whPar);
|
||||||
|
|
||||||
if (io.digitalIORead(0)) // rosso
|
if (io.digitalInRead(0)) // rosso
|
||||||
{
|
{
|
||||||
uint8_t regset(seneca.getRegset());
|
std::vector<bool> v1 = {1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0};
|
||||||
uint16_t countStat(seneca.getCounterStatus());
|
std::vector<bool> v2 = {0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1};
|
||||||
LOG_INFO("Register Set: ", regset);
|
std::vector<bool> v0(16, 0);
|
||||||
LOG_INFO("Counter Status: ", countStat);
|
|
||||||
seneca.resetPartialCounters();
|
LOG_INFO("SET Digital Outputs V1: ", printBoolVec(v1).c_str());
|
||||||
}
|
io.digitalOutWritePort(v1);
|
||||||
delay(100);
|
delay(100);
|
||||||
if (io.digitalIORead(8)) // blu
|
LOG_INFO("GET Digital Outputs V1: ", printBoolVec(io.digitalOutReadPort()).c_str());
|
||||||
|
delay(2000);
|
||||||
|
|
||||||
|
LOG_INFO("SET Digital Outputs V2: ", printBoolVec(v2).c_str());
|
||||||
|
io.digitalOutWritePort(v2);
|
||||||
|
delay(100);
|
||||||
|
LOG_INFO("GET Digital Outputs V2: ", printBoolVec(io.digitalOutReadPort()).c_str());
|
||||||
|
delay(2000);
|
||||||
|
|
||||||
|
LOG_INFO("GET Digital Inputs: ", printBoolVec(io.digitalInReadPort()).c_str());
|
||||||
|
delay(2000);
|
||||||
|
|
||||||
|
io.digitalOutWritePort(v0);
|
||||||
|
}
|
||||||
|
|
||||||
|
delay(100);
|
||||||
|
if (io.digitalInRead(8)) // blu
|
||||||
{
|
{
|
||||||
if (!buzzing)
|
if (!buzzing)
|
||||||
{
|
{
|
||||||
@@ -160,12 +193,12 @@ void loop()
|
|||||||
LOG_INFO("Buzzing -> ", buzzing ? "True" : "False");
|
LOG_INFO("Buzzing -> ", buzzing ? "True" : "False");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (io.digitalIORead(9))
|
if (io.digitalInRead(9))
|
||||||
{ // verde
|
{ // verde
|
||||||
conf.resetConfig();
|
conf.resetConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (io.digitalIORead(10))
|
if (io.digitalInRead(10))
|
||||||
{ // giallo
|
{ // giallo
|
||||||
esp_restart();
|
esp_restart();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ const bool remoteIO::setOutPort(const std::vector<bool> values)
|
|||||||
return false;
|
return false;
|
||||||
auto lock = m_bus.getLock();
|
auto lock = m_bus.getLock();
|
||||||
LOG_DEBUG("Write Port", CH_MAX);
|
LOG_DEBUG("Write Port", CH_MAX);
|
||||||
return m_bus.writeCoils(m_address, CH_MAX, values);
|
return m_bus.writeCoils(m_address, REG_COILS, values);
|
||||||
}
|
}
|
||||||
|
|
||||||
const bool remoteIO::getOut(const channel_t ch, bool &value)
|
const bool remoteIO::getOut(const channel_t ch, bool &value)
|
||||||
@@ -69,7 +69,7 @@ const bool remoteIO::getOutPort(std::vector<bool> &values)
|
|||||||
return false;
|
return false;
|
||||||
auto lock = m_bus.getLock();
|
auto lock = m_bus.getLock();
|
||||||
LOG_DEBUG("Read Port", CH_MAX);
|
LOG_DEBUG("Read Port", CH_MAX);
|
||||||
return m_bus.readCoils(m_address, REG_COILS, 8, values);
|
return m_bus.readCoils(m_address, REG_COILS, CH_MAX, values);
|
||||||
}
|
}
|
||||||
|
|
||||||
const bool remoteIO::getIn(const channel_t input, bool &value)
|
const bool remoteIO::getIn(const channel_t input, bool &value)
|
||||||
|
|||||||
Reference in New Issue
Block a user