expand and fix digitalIO class
This commit is contained in:
@@ -53,6 +53,12 @@ namespace drivers
|
||||
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)
|
||||
{
|
||||
if (writeRegister(TCA9554_OUTPUT_REG, state))
|
||||
@@ -61,7 +67,7 @@ namespace drivers
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool TCA9554PWR::readOut(const uint8_t ch)
|
||||
const bool TCA9554PWR::readOut(const uint8_t ch, bool &state)
|
||||
{
|
||||
uint8_t currState(0);
|
||||
if (ch < DO1 || ch > DO8)
|
||||
@@ -71,12 +77,13 @@ namespace drivers
|
||||
}
|
||||
if (!readPort(currState))
|
||||
return false;
|
||||
return (currState && (High >> ch));
|
||||
state = (currState && (High << ch));
|
||||
return true;
|
||||
}
|
||||
|
||||
const bool TCA9554PWR::readPort(uint8_t &state)
|
||||
{
|
||||
if (readRegister(TCA9554_INPUT_REG, state))
|
||||
if (readRegister(TCA9554_OUTPUT_REG, state))
|
||||
return true;
|
||||
LOG_ERROR("Unable to read IO port: state[%02x]", state);
|
||||
return false;
|
||||
|
||||
@@ -42,9 +42,10 @@ namespace drivers
|
||||
~TCA9554PWR();
|
||||
|
||||
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 readOut(const uint8_t channel);
|
||||
const bool readOut(const uint8_t channel, bool &state);
|
||||
const bool readPort(uint8_t &state);
|
||||
|
||||
private:
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -34,7 +34,7 @@ namespace drivers
|
||||
|
||||
const uint8_t c_respHeaderSize = 3;
|
||||
const uint8_t c_respCrcSize = 2;
|
||||
const uint16_t c_minDelay = 50;
|
||||
const uint16_t c_minDelay = 10;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
|
||||
@@ -78,7 +78,7 @@ namespace drivers
|
||||
{ // minimum m_lastRequest between requests
|
||||
delay(now - m_lastRequest);
|
||||
}
|
||||
m_lastRequest = now;
|
||||
m_lastRequest = millis();
|
||||
}
|
||||
|
||||
const uint8_t S50140::getRegset()
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace drivers
|
||||
private:
|
||||
const uint8_t c_maxRetries = 5;
|
||||
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_A = 0x1016;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "utils.h"
|
||||
|
||||
|
||||
void printBytes(const char title[], const std::vector<uint8_t> &b)
|
||||
{
|
||||
Serial0.flush();
|
||||
@@ -18,20 +19,32 @@ void printBytes(const char title[], const std::vector<uint16_t> &b)
|
||||
printf("%s: ", title);
|
||||
for (auto v : b)
|
||||
{
|
||||
printf("0x%04x ", v);
|
||||
}
|
||||
printf("\n");
|
||||
Serial0.flush();
|
||||
printf("0x%04x ", v);
|
||||
}
|
||||
printf("\n");
|
||||
Serial0.flush();
|
||||
}
|
||||
|
||||
void printBool(const char title[], const std::vector<bool> &vals)
|
||||
{
|
||||
Serial0.flush();
|
||||
printf("%s: ", title);
|
||||
for (auto j(0); j < vals.size(); j++)
|
||||
{
|
||||
printf("%s ", vals.at(j) ? "True" : "False");
|
||||
}
|
||||
printf("\n");
|
||||
Serial0.flush();
|
||||
Serial0.flush();
|
||||
printf("%s: ", title);
|
||||
for (auto j(0); j < vals.size(); j++)
|
||||
{
|
||||
printf("%s ", vals.at(j) ? "True" : "False");
|
||||
}
|
||||
printf("\n");
|
||||
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 <DebugLog.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
///////////// 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 printBool(const char title[], const std::vector<bool> &vals);
|
||||
|
||||
const std::string printBoolVec(const std::vector<bool> &vals);
|
||||
|
||||
Reference in New Issue
Block a user