Compare commits
2 Commits
8f5615a034
...
71c7ff8756
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
71c7ff8756 | ||
|
|
59d8c2c2d4 |
@@ -10,18 +10,18 @@ namespace drivers
|
|||||||
Buzzer::Buzzer()
|
Buzzer::Buzzer()
|
||||||
{
|
{
|
||||||
LOG_INFO("Initializing Beeper");
|
LOG_INFO("Initializing Beeper");
|
||||||
pinMode(buzzerPin, OUTPUT);
|
pinMode(c_buzzerPin, OUTPUT);
|
||||||
ledcAttach(buzzerPin, 1000, 8);
|
ledcAttach(c_buzzerPin, 1000, 8);
|
||||||
m_bp.pin = buzzerPin;
|
m_bp.pin = c_buzzerPin;
|
||||||
m_bp.beeperTask = NULL;
|
m_bp.beeperTask = NULL;
|
||||||
//beep(50, NOTE_G);
|
beep(50, NOTE_G);
|
||||||
}
|
}
|
||||||
|
|
||||||
Buzzer::~Buzzer()
|
Buzzer::~Buzzer()
|
||||||
{
|
{
|
||||||
beepStop();
|
beepStop();
|
||||||
ledcDetach(buzzerPin);
|
ledcDetach(c_buzzerPin);
|
||||||
pinMode(buzzerPin, INPUT);
|
pinMode(c_buzzerPin, INPUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Buzzer::beep(const uint16_t tBeep, const note_t note)
|
void Buzzer::beep(const uint16_t tBeep, const note_t note)
|
||||||
@@ -57,11 +57,11 @@ namespace drivers
|
|||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
ledcWriteNote(bPar->pin, bPar->note, OCTAVE); // on with selected note
|
ledcWriteNote(bPar->pin, bPar->note, OCTAVE); // on with selected note
|
||||||
vTaskDelay(pdMS_TO_TICKS(bPar->tOn));
|
delay(bPar->tOn);
|
||||||
ledcWriteTone(bPar->pin, 0); // off
|
ledcWriteTone(bPar->pin, 0); // off
|
||||||
if (bPar->tOff == 0)
|
if (bPar->tOff == 0)
|
||||||
break;
|
break;
|
||||||
vTaskDelay(pdMS_TO_TICKS(bPar->tOff));
|
delay(bPar->tOff);
|
||||||
}
|
}
|
||||||
LOG_DEBUG("Beeper Task Ended");
|
LOG_DEBUG("Beeper Task Ended");
|
||||||
bPar->beeperTask = NULL;
|
bPar->beeperTask = NULL;
|
||||||
|
|||||||
@@ -10,7 +10,8 @@ namespace drivers
|
|||||||
|
|
||||||
class Buzzer
|
class Buzzer
|
||||||
{
|
{
|
||||||
const uint8_t buzzerPin = 46; // hardware assigned
|
const uint8_t c_buzzerPin = 46; // hardware assigned
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
note_t note;
|
note_t note;
|
||||||
|
|||||||
@@ -9,21 +9,21 @@ namespace drivers
|
|||||||
Led::Led()
|
Led::Led()
|
||||||
{
|
{
|
||||||
LOG_INFO("Inizializing RGB Led");
|
LOG_INFO("Inizializing RGB Led");
|
||||||
pinMode(ledPin, OUTPUT);
|
pinMode(c_ledPin, OUTPUT);
|
||||||
m_lp.pin = ledPin;
|
m_lp.pin = c_ledPin;
|
||||||
m_lp.blinkTask = NULL;
|
m_lp.blinkTask = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
Led::~Led()
|
Led::~Led()
|
||||||
{
|
{
|
||||||
setColor({0, 0, 0});
|
setColor({0, 0, 0});
|
||||||
pinMode(ledPin, INPUT);
|
pinMode(c_ledPin, INPUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Led::setColor(const color_t color)
|
void Led::setColor(const color_t color)
|
||||||
{
|
{
|
||||||
blinkStop();
|
blinkStop();
|
||||||
rgbLedWrite(ledPin, color.r, color.g, color.b);
|
rgbLedWrite(c_ledPin, color.r, color.g, color.b);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Led::blinkColor(const uint16_t tOn, const uint16_t tOff, const color_t color)
|
void Led::blinkColor(const uint16_t tOn, const uint16_t tOff, const color_t color)
|
||||||
@@ -62,11 +62,11 @@ namespace drivers
|
|||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
rgbLedWrite(lPar->pin, lPar->color1.g, lPar->color1.r, lPar->color1.b);
|
rgbLedWrite(lPar->pin, lPar->color1.g, lPar->color1.r, lPar->color1.b);
|
||||||
vTaskDelay(pdMS_TO_TICKS(lPar->tOn));
|
delay(lPar->tOn);
|
||||||
rgbLedWrite(lPar->pin, lPar->color2.g, lPar->color2.r, lPar->color2.b); // off
|
rgbLedWrite(lPar->pin, lPar->color2.g, lPar->color2.r, lPar->color2.b); // off
|
||||||
if (lPar->tOff == 0)
|
if (lPar->tOff == 0)
|
||||||
break;
|
break;
|
||||||
vTaskDelay(pdMS_TO_TICKS(lPar->tOff));
|
delay(lPar->tOff);
|
||||||
}
|
}
|
||||||
LOG_DEBUG("Blinker Task Ended");
|
LOG_DEBUG("Blinker Task Ended");
|
||||||
lPar->blinkTask = NULL;
|
lPar->blinkTask = NULL;
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ namespace drivers
|
|||||||
|
|
||||||
class Led
|
class Led
|
||||||
{
|
{
|
||||||
const uint8_t ledPin = 38;
|
const uint8_t c_ledPin = 38;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
typedef struct
|
typedef struct
|
||||||
|
|||||||
@@ -65,12 +65,26 @@ namespace drivers
|
|||||||
readAll(garbage);
|
readAll(garbage);
|
||||||
LOG_INFO("Init MODBUS Master Mode");
|
LOG_INFO("Init MODBUS Master Mode");
|
||||||
m_crc.reset(CRC16_MODBUS_POLYNOME, CRC16_MODBUS_INITIAL, CRC16_MODBUS_XOR_OUT, CRC16_MODBUS_REV_IN, CRC16_MAXIM_REV_OUT);
|
m_crc.reset(CRC16_MODBUS_POLYNOME, CRC16_MODBUS_INITIAL, CRC16_MODBUS_XOR_OUT, CRC16_MODBUS_REV_IN, CRC16_MAXIM_REV_OUT);
|
||||||
|
m_lastAccess = millis();
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Func 0x01
|
// Func 0x01
|
||||||
const bool MODBUS::readCoils(const uint8_t device, const uint16_t reg, const uint16_t num, std::vector<bool> &coils)
|
const bool MODBUS::readCoils(const uint8_t device, const uint16_t reg, const uint16_t num, std::vector<bool> &coils)
|
||||||
{
|
{
|
||||||
constexpr uint8_t func = 0x01;
|
constexpr uint8_t func = 0x01;
|
||||||
|
delayAccess(device);
|
||||||
LOG_DEBUG("Read coils: dev[", device, "], reg[", reg, "], num[", num, "]");
|
LOG_DEBUG("Read coils: dev[", device, "], reg[", reg, "], num[", num, "]");
|
||||||
return readBinary(device, func, reg, num, coils);
|
return readBinary(device, func, reg, num, coils);
|
||||||
}
|
}
|
||||||
@@ -79,6 +93,7 @@ namespace drivers
|
|||||||
const bool MODBUS::readInputs(const uint8_t device, const uint16_t reg, const uint8_t num, std::vector<bool> &inputs)
|
const bool MODBUS::readInputs(const uint8_t device, const uint16_t reg, const uint8_t num, std::vector<bool> &inputs)
|
||||||
{
|
{
|
||||||
constexpr uint8_t func = 0x02;
|
constexpr uint8_t func = 0x02;
|
||||||
|
delayAccess(device);
|
||||||
LOG_DEBUG("Read multi inputs: dev[", device, "], reg[", reg, "], num[", num, "]");
|
LOG_DEBUG("Read multi inputs: dev[", device, "], reg[", reg, "], num[", num, "]");
|
||||||
return readBinary(device, func, reg, num, inputs);
|
return readBinary(device, func, reg, num, inputs);
|
||||||
}
|
}
|
||||||
@@ -87,6 +102,7 @@ namespace drivers
|
|||||||
const bool MODBUS::readHoldingRegisters(const uint8_t device, const uint16_t reg, const uint8_t num, std::vector<uint16_t> &values)
|
const bool MODBUS::readHoldingRegisters(const uint8_t device, const uint16_t reg, const uint8_t num, std::vector<uint16_t> &values)
|
||||||
{
|
{
|
||||||
constexpr uint8_t func = 0x03;
|
constexpr uint8_t func = 0x03;
|
||||||
|
delayAccess(device);
|
||||||
LOG_DEBUG("Read multi holding registers: dev[", device, "], reg[", reg, "], num[", num, "]");
|
LOG_DEBUG("Read multi holding registers: dev[", device, "], reg[", reg, "], num[", num, "]");
|
||||||
return readInteger(device, func, reg, num, values);
|
return readInteger(device, func, reg, num, values);
|
||||||
}
|
}
|
||||||
@@ -95,6 +111,7 @@ namespace drivers
|
|||||||
const bool MODBUS::readInputRegisters(const uint8_t device, const uint16_t reg, const uint8_t num, std::vector<uint16_t> &values)
|
const bool MODBUS::readInputRegisters(const uint8_t device, const uint16_t reg, const uint8_t num, std::vector<uint16_t> &values)
|
||||||
{
|
{
|
||||||
constexpr uint8_t func = 0x04;
|
constexpr uint8_t func = 0x04;
|
||||||
|
delayAccess(device);
|
||||||
LOG_DEBUG("Read multi input registers: dev[", device, "], reg[", reg, "], num[", num, "]");
|
LOG_DEBUG("Read multi input registers: dev[", device, "], reg[", reg, "], num[", num, "]");
|
||||||
return readInteger(device, func, reg, num, values);
|
return readInteger(device, func, reg, num, values);
|
||||||
}
|
}
|
||||||
@@ -103,6 +120,7 @@ namespace drivers
|
|||||||
const bool MODBUS::writeCoil(const uint8_t device, const uint16_t coil, const bool value)
|
const bool MODBUS::writeCoil(const uint8_t device, const uint16_t coil, const bool value)
|
||||||
{
|
{
|
||||||
constexpr uint8_t func = 0x05;
|
constexpr uint8_t func = 0x05;
|
||||||
|
delayAccess(device);
|
||||||
LOG_DEBUG("Write single coil: dev[", device, "], coil[", coil, "], value[", value ? "true" : "false", "]");
|
LOG_DEBUG("Write single coil: dev[", device, "], coil[", coil, "], value[", value ? "true" : "false", "]");
|
||||||
return writeBinary(device, func, coil, {value});
|
return writeBinary(device, func, coil, {value});
|
||||||
}
|
}
|
||||||
@@ -111,6 +129,7 @@ namespace drivers
|
|||||||
const bool MODBUS::writeRegister(const uint8_t device, const uint16_t reg, const uint16_t value)
|
const bool MODBUS::writeRegister(const uint8_t device, const uint16_t reg, const uint16_t value)
|
||||||
{
|
{
|
||||||
constexpr uint8_t func = 0x06;
|
constexpr uint8_t func = 0x06;
|
||||||
|
delayAccess(device);
|
||||||
LOG_DEBUG("Write single register: dev[", device, "], reg[", reg, "], value[", value, "]");
|
LOG_DEBUG("Write single register: dev[", device, "], reg[", reg, "], value[", value, "]");
|
||||||
return writeInteger(device, func, reg, {value}, false);
|
return writeInteger(device, func, reg, {value}, false);
|
||||||
}
|
}
|
||||||
@@ -119,6 +138,7 @@ namespace drivers
|
|||||||
const bool MODBUS::writeCoils(const uint8_t device, const uint16_t coils, const std::vector<bool> &values)
|
const bool MODBUS::writeCoils(const uint8_t device, const uint16_t coils, const std::vector<bool> &values)
|
||||||
{
|
{
|
||||||
constexpr uint8_t func = 0x0F;
|
constexpr uint8_t func = 0x0F;
|
||||||
|
delayAccess(device);
|
||||||
LOG_DEBUG("Write multi coils: dev[", device, "], start[", coils, "], num[", values.size(), "]");
|
LOG_DEBUG("Write multi coils: dev[", device, "], start[", coils, "], num[", values.size(), "]");
|
||||||
return writeBinary(device, func, coils, values);
|
return writeBinary(device, func, coils, values);
|
||||||
}
|
}
|
||||||
@@ -127,6 +147,7 @@ namespace drivers
|
|||||||
const bool MODBUS::writeRegisters(const uint8_t device, const uint16_t reg, const std::vector<uint16_t> &values)
|
const bool MODBUS::writeRegisters(const uint8_t device, const uint16_t reg, const std::vector<uint16_t> &values)
|
||||||
{
|
{
|
||||||
constexpr uint8_t func = 0x10;
|
constexpr uint8_t func = 0x10;
|
||||||
|
delayAccess(device);
|
||||||
LOG_DEBUG("Write multi registers: dev[", device, "], start[", reg, "], num[", values.size(), "]");
|
LOG_DEBUG("Write multi registers: dev[", device, "], start[", reg, "], num[", values.size(), "]");
|
||||||
return writeInteger(device, func, reg, values, true);
|
return writeInteger(device, func, reg, values, true);
|
||||||
}
|
}
|
||||||
@@ -143,7 +164,7 @@ namespace drivers
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const uint16_t nRespDataBytes = (uint16_t)ceil(bits / 8.0f); // 1 bit for every coil, if not 8 mutiple padded with zeroes
|
const uint16_t nRespDataBytes = (uint16_t)ceil(bits / 8.0f); // 1 bit for every coil, if not 8 mutiple padded with zeroes
|
||||||
const uint16_t expectedRespLen = (RESP_HEADER_SIZE + RESP_CRC_SIZE) + nRespDataBytes; // device + function + nbytes + data[] + crc(16b)
|
const uint16_t expectedRespLen = (c_respHeaderSize + c_respCrcSize) + nRespDataBytes; // device + function + nbytes + data[] + crc(16b)
|
||||||
std::vector<uint8_t> response;
|
std::vector<uint8_t> response;
|
||||||
if (!readN(expectedRespLen, response))
|
if (!readN(expectedRespLen, response))
|
||||||
{
|
{
|
||||||
@@ -172,7 +193,7 @@ namespace drivers
|
|||||||
uint16_t bitNum(0);
|
uint16_t bitNum(0);
|
||||||
|
|
||||||
// get response data bytes excluding header and crc
|
// get response data bytes excluding header and crc
|
||||||
const std::vector<uint8_t> respData(response.begin() + RESP_HEADER_SIZE, response.end() - sizeof(crc_t));
|
const std::vector<uint8_t> respData(response.begin() + c_respHeaderSize, response.end() - sizeof(crc_t));
|
||||||
for (auto it = respData.begin(); it < respData.end(); it++)
|
for (auto it = respData.begin(); it < respData.end(); it++)
|
||||||
{
|
{
|
||||||
for (uint8_t j(0); j < 8 && bitNum < bits; j++)
|
for (uint8_t j(0); j < 8 && bitNum < bits; j++)
|
||||||
@@ -193,7 +214,7 @@ namespace drivers
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const uint16_t nRespDataBytes = num * sizeof(uint16_t);
|
const uint16_t nRespDataBytes = num * sizeof(uint16_t);
|
||||||
const uint16_t expectedRespLen = (RESP_HEADER_SIZE + sizeof(crc_t)) + nRespDataBytes; // device + function + nbytes + data[] + crc(16b)
|
const uint16_t expectedRespLen = (c_respHeaderSize + sizeof(crc_t)) + nRespDataBytes; // device + function + nbytes + data[] + crc(16b)
|
||||||
std::vector<uint8_t> response;
|
std::vector<uint8_t> response;
|
||||||
if (!readN(expectedRespLen, response))
|
if (!readN(expectedRespLen, response))
|
||||||
{
|
{
|
||||||
@@ -220,7 +241,7 @@ namespace drivers
|
|||||||
out.clear();
|
out.clear();
|
||||||
out.reserve(nRespDataBytes / sizeof(uint16_t));
|
out.reserve(nRespDataBytes / sizeof(uint16_t));
|
||||||
// get response data bytes excluding header and crc
|
// get response data bytes excluding header and crc
|
||||||
const std::vector<uint8_t> respData(response.begin() + RESP_HEADER_SIZE, response.end() - RESP_CRC_SIZE);
|
const std::vector<uint8_t> respData(response.begin() + c_respHeaderSize, response.end() - c_respCrcSize);
|
||||||
for (auto it = respData.begin(); it < respData.end(); it++)
|
for (auto it = respData.begin(); it < respData.end(); it++)
|
||||||
{
|
{
|
||||||
const uint8_t lo(*it++);
|
const uint8_t lo(*it++);
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ namespace drivers
|
|||||||
{
|
{
|
||||||
class RS485
|
class RS485
|
||||||
{
|
{
|
||||||
static const uint8_t PORT = 1;
|
const uint8_t c_port = 1;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
RS485(const uint32_t baud, const SerialConfig conf);
|
RS485(const uint32_t baud, const SerialConfig conf);
|
||||||
@@ -32,8 +32,9 @@ namespace drivers
|
|||||||
class MODBUS : private RS485
|
class MODBUS : private RS485
|
||||||
{
|
{
|
||||||
|
|
||||||
static const uint8_t RESP_HEADER_SIZE = 3;
|
const uint8_t c_respHeaderSize = 3;
|
||||||
static const uint8_t RESP_CRC_SIZE = 2;
|
const uint8_t c_respCrcSize = 2;
|
||||||
|
const uint16_t c_minDelay = 50;
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
@@ -92,6 +93,9 @@ namespace drivers
|
|||||||
private:
|
private:
|
||||||
CRC16 m_crc;
|
CRC16 m_crc;
|
||||||
std::mutex m_mutex;
|
std::mutex m_mutex;
|
||||||
|
uint8_t m_lastDevice;
|
||||||
|
uint32_t m_lastAccess;
|
||||||
|
void delayAccess(const uint8_t device);
|
||||||
const std::vector<uint8_t> singleRequest(const uint8_t device, const uint8_t func, const uint16_t reg, const uint16_t data);
|
const std::vector<uint8_t> singleRequest(const uint8_t device, const uint8_t func, const uint16_t reg, const uint16_t data);
|
||||||
const std::vector<uint8_t> multiRequest(const uint8_t device, const uint8_t func, const uint16_t reg, const uint16_t qty, const std::vector<uint8_t> &data);
|
const std::vector<uint8_t> multiRequest(const uint8_t device, const uint8_t func, const uint16_t reg, const uint16_t qty, const std::vector<uint8_t> &data);
|
||||||
const bool readBinary(const uint8_t device, const uint8_t func, const uint16_t reg, const uint16_t bits, std::vector<bool> &out);
|
const bool readBinary(const uint8_t device, const uint8_t func, const uint16_t reg, const uint16_t bits, std::vector<bool> &out);
|
||||||
|
|||||||
@@ -63,38 +63,6 @@
|
|||||||
|
|
||||||
#define RTC_TIMER_FLAG (0x08)
|
#define RTC_TIMER_FLAG (0x08)
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
uint16_t year;
|
|
||||||
uint8_t month;
|
|
||||||
uint8_t day;
|
|
||||||
uint8_t dotw;
|
|
||||||
uint8_t hour;
|
|
||||||
uint8_t minute;
|
|
||||||
uint8_t second;
|
|
||||||
} datetime_t;
|
|
||||||
|
|
||||||
const unsigned char MonthStr[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
|
|
||||||
const unsigned char Week[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
|
|
||||||
|
|
||||||
extern datetime_t datetime;
|
|
||||||
void PCF85063_Init(void);
|
|
||||||
void PCF85063_Reset(void);
|
|
||||||
void PCF85063Task(void *parameter);
|
|
||||||
|
|
||||||
void PCF85063_Set_Time(datetime_t time);
|
|
||||||
void PCF85063_Set_Date(datetime_t date);
|
|
||||||
void PCF85063_Set_All(datetime_t time);
|
|
||||||
|
|
||||||
void PCF85063_Read_Time(datetime_t *time);
|
|
||||||
|
|
||||||
void PCF85063_Enable_Alarm(void);
|
|
||||||
uint8_t PCF85063_Get_Alarm_Flag(void);
|
|
||||||
void PCF85063_Set_Alarm(datetime_t time);
|
|
||||||
void PCF85063_Read_Alarm(datetime_t *time);
|
|
||||||
|
|
||||||
void datetime_to_str(char *datetime_str, datetime_t time);
|
|
||||||
|
|
||||||
namespace drivers
|
namespace drivers
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|||||||
@@ -27,47 +27,56 @@ namespace drivers
|
|||||||
|
|
||||||
const float_t S50140::getV()
|
const float_t S50140::getV()
|
||||||
{
|
{
|
||||||
|
auto lock = m_bus.getLock();
|
||||||
return readFloatReg(REG_V);
|
return readFloatReg(REG_V);
|
||||||
}
|
}
|
||||||
const float_t S50140::getA()
|
const float_t S50140::getA()
|
||||||
{
|
{
|
||||||
|
auto lock = m_bus.getLock();
|
||||||
return readFloatReg(REG_A);
|
return readFloatReg(REG_A);
|
||||||
}
|
}
|
||||||
const float_t S50140::getPact()
|
const float_t S50140::getPact()
|
||||||
{
|
{
|
||||||
|
auto lock = m_bus.getLock();
|
||||||
return readFloatReg(REG_Pact);
|
return readFloatReg(REG_Pact);
|
||||||
}
|
}
|
||||||
const float_t S50140::getPapp()
|
const float_t S50140::getPapp()
|
||||||
{
|
{
|
||||||
|
auto lock = m_bus.getLock();
|
||||||
return readFloatReg(REG_Papp);
|
return readFloatReg(REG_Papp);
|
||||||
}
|
}
|
||||||
const float_t S50140::getPrea()
|
const float_t S50140::getPrea()
|
||||||
{
|
{
|
||||||
|
auto lock = m_bus.getLock();
|
||||||
return readFloatReg(REG_Prea);
|
return readFloatReg(REG_Prea);
|
||||||
}
|
}
|
||||||
const float_t S50140::getPf()
|
const float_t S50140::getPf()
|
||||||
{
|
{
|
||||||
|
auto lock = m_bus.getLock();
|
||||||
return readFloatReg(REG_Pf);
|
return readFloatReg(REG_Pf);
|
||||||
}
|
}
|
||||||
const float_t S50140::getF()
|
const float_t S50140::getF()
|
||||||
{
|
{
|
||||||
|
auto lock = m_bus.getLock();
|
||||||
return readFloatReg(REG_Freq);
|
return readFloatReg(REG_Freq);
|
||||||
}
|
}
|
||||||
const float_t S50140::getWhTot()
|
const float_t S50140::getWhTot()
|
||||||
{
|
{
|
||||||
|
auto lock = m_bus.getLock();
|
||||||
return readFloatReg(REG_WhTot);
|
return readFloatReg(REG_WhTot);
|
||||||
}
|
}
|
||||||
const float_t S50140::getWhPar()
|
const float_t S50140::getWhPar()
|
||||||
{
|
{
|
||||||
|
auto lock = m_bus.getLock();
|
||||||
return readFloatReg(REG_WhPart);
|
return readFloatReg(REG_WhPart);
|
||||||
}
|
}
|
||||||
|
|
||||||
void S50140::delayRequest()
|
void S50140::delayRequest()
|
||||||
{
|
{
|
||||||
auto now = millis();
|
auto now = millis();
|
||||||
if ((now - m_lastRequest) < minDelay)
|
if ((now - m_lastRequest) < c_minDelay)
|
||||||
{ // minimum m_lastRequest between requests
|
{ // minimum m_lastRequest between requests
|
||||||
vTaskDelay(pdMS_TO_TICKS(now - m_lastRequest));
|
delay(now - m_lastRequest);
|
||||||
}
|
}
|
||||||
m_lastRequest = now;
|
m_lastRequest = now;
|
||||||
}
|
}
|
||||||
@@ -101,7 +110,7 @@ namespace drivers
|
|||||||
constexpr uint16_t resetAll = 0x0A03;
|
constexpr uint16_t resetAll = 0x0A03;
|
||||||
constexpr uint16_t stopAll = 0x0A02;
|
constexpr uint16_t stopAll = 0x0A02;
|
||||||
constexpr uint16_t startAll = 0x0A01;
|
constexpr uint16_t startAll = 0x0A01;
|
||||||
while (retries++ < maxRetries)
|
while (retries++ < c_maxRetries)
|
||||||
{
|
{
|
||||||
bool ok(true);
|
bool ok(true);
|
||||||
delayRequest();
|
delayRequest();
|
||||||
@@ -126,11 +135,10 @@ namespace drivers
|
|||||||
uint8_t retries(0);
|
uint8_t retries(0);
|
||||||
std::vector<uint16_t> values;
|
std::vector<uint16_t> values;
|
||||||
|
|
||||||
while (retries++ < maxRetries)
|
while (retries++ < c_maxRetries)
|
||||||
{
|
{
|
||||||
delayRequest();
|
delayRequest();
|
||||||
auto lock = m_bus.getLock();
|
if (m_bus.readHoldingRegisters(m_address, reg, c_dataWords, values) && values.size() == c_dataWords)
|
||||||
if (m_bus.readHoldingRegisters(m_address, reg, dataWords, values) && values.size() == dataWords)
|
|
||||||
{
|
{
|
||||||
floatval_t fv; // potrebbe essere il contrario, vedremo
|
floatval_t fv; // potrebbe essere il contrario, vedremo
|
||||||
fv.words.lo = values[0]; // magari va invertita ancora l'endianness
|
fv.words.lo = values[0]; // magari va invertita ancora l'endianness
|
||||||
|
|||||||
@@ -11,9 +11,9 @@ namespace drivers
|
|||||||
class S50140
|
class S50140
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
const uint8_t maxRetries = 5;
|
const uint8_t c_maxRetries = 5;
|
||||||
const uint8_t dataWords = 2;
|
const uint8_t c_dataWords = 2;
|
||||||
const uint16_t minDelay = 500;
|
const uint16_t c_minDelay = 200;
|
||||||
|
|
||||||
const uint16_t REG_V = 0x100C;
|
const uint16_t REG_V = 0x100C;
|
||||||
const uint16_t REG_A = 0x1016;
|
const uint16_t REG_A = 0x1016;
|
||||||
|
|||||||
@@ -20,9 +20,9 @@ namespace drivers
|
|||||||
LOG_ERROR("Invalid Temperature Channel number", ch);
|
LOG_ERROR("Invalid Temperature Channel number", ch);
|
||||||
return MAXFLOAT;
|
return MAXFLOAT;
|
||||||
}
|
}
|
||||||
|
auto lock = m_bus.getLock();
|
||||||
while (retries++ < maxRetries)
|
while (retries++ < maxRetries)
|
||||||
{
|
{
|
||||||
auto lock = m_bus.getLock();
|
|
||||||
if (m_bus.readHoldingRegisters(m_address, REG_TEMP + ch, 1, rawT) && !rawT.empty())
|
if (m_bus.readHoldingRegisters(m_address, REG_TEMP + ch, 1, rawT) && !rawT.empty())
|
||||||
{
|
{
|
||||||
return rawT.front() / 10.0f;
|
return rawT.front() / 10.0f;
|
||||||
@@ -39,9 +39,9 @@ namespace drivers
|
|||||||
uint8_t retries(0);
|
uint8_t retries(0);
|
||||||
std::vector<uint16_t> rawT;
|
std::vector<uint16_t> rawT;
|
||||||
std::vector<float> out;
|
std::vector<float> out;
|
||||||
|
auto lock = m_bus.getLock();
|
||||||
while (retries++ < maxRetries)
|
while (retries++ < maxRetries)
|
||||||
{
|
{
|
||||||
auto lock = m_bus.getLock();
|
|
||||||
if (m_bus.readHoldingRegisters(m_address, REG_TEMP, getNum(), rawT) && !rawT.empty())
|
if (m_bus.readHoldingRegisters(m_address, REG_TEMP, getNum(), rawT) && !rawT.empty())
|
||||||
{
|
{
|
||||||
out.reserve(rawT.size());
|
out.reserve(rawT.size());
|
||||||
@@ -65,12 +65,12 @@ namespace drivers
|
|||||||
uint8_t channel(0);
|
uint8_t channel(0);
|
||||||
corr.resize(getNum()); // max number of temperature correction values is equal to number of sensors
|
corr.resize(getNum()); // max number of temperature correction values is equal to number of sensors
|
||||||
|
|
||||||
|
auto lock = m_bus.getLock();
|
||||||
for (auto v : corr)
|
for (auto v : corr)
|
||||||
{ // convert to decimal degreees to register value
|
{
|
||||||
while (retries++ < maxRetries)
|
while (retries++ < maxRetries)
|
||||||
{
|
{
|
||||||
auto lock = m_bus.getLock();
|
if (m_bus.writeRegister(m_address, REG_TEMPCORR + channel, v * 10)) // convert to decimal degreees to register value
|
||||||
if (m_bus.writeRegister(m_address, REG_TEMPCORR + channel, v*10))
|
|
||||||
{
|
{
|
||||||
channel++;
|
channel++;
|
||||||
delay(50);
|
delay(50);
|
||||||
|
|||||||
15
src/main.cpp
15
src/main.cpp
@@ -1,10 +1,9 @@
|
|||||||
#define DEBUGLOG_DEFAULT_LOG_LEVEL_DEBUG
|
#define DEBUGLOG_DEFAULT_LOG_LEVEL_DEBUG
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
#include <DebugLog.h>
|
#include <DebugLog.h>
|
||||||
#include <DebugLogEnable.h>
|
#include <DebugLogEnable.h>
|
||||||
|
|
||||||
#include <Arduino.h>
|
|
||||||
|
|
||||||
#include <config.h>
|
#include <config.h>
|
||||||
#include <PCF85063_Driver.h>
|
#include <PCF85063_Driver.h>
|
||||||
#include <R4DCB08_Driver.h>
|
#include <R4DCB08_Driver.h>
|
||||||
@@ -14,8 +13,6 @@
|
|||||||
#include <ETH_Driver.h>
|
#include <ETH_Driver.h>
|
||||||
|
|
||||||
#include <digitalIO.h>
|
#include <digitalIO.h>
|
||||||
|
|
||||||
#define DEBUGLOG_DEFAULT_LOG_LEVEL_DEBUG
|
|
||||||
#include <mqtt.h>
|
#include <mqtt.h>
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
@@ -50,28 +47,26 @@ void loop()
|
|||||||
auto rtc = drivers::PCF85063(i2c, PCF85063_ADDRESS);
|
auto rtc = drivers::PCF85063(i2c, PCF85063_ADDRESS);
|
||||||
auto eth = drivers::Ethernet(conf.m_ethHostname);
|
auto eth = drivers::Ethernet(conf.m_ethHostname);
|
||||||
auto tmp = drivers::R4DCB08(bus, conf.m_modbusTemperatureAddr);
|
auto tmp = drivers::R4DCB08(bus, conf.m_modbusTemperatureAddr);
|
||||||
delay(100);
|
|
||||||
auto io = digitalIO(i2c, bus, {conf.m_modbusRelayAddr});
|
|
||||||
delay(100);
|
|
||||||
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();
|
||||||
//////////////// DEVICES ////////////////
|
auto io = digitalIO(i2c, bus, {conf.m_modbusRelayAddr});
|
||||||
// Initialize temperature sensors
|
// Initialize temperature sensors
|
||||||
sensors = tmp.getNum();
|
sensors = tmp.getNum();
|
||||||
LOG_INFO("Temperature sensors connected ->", sensors);
|
LOG_INFO("Temperature sensors connected ->", sensors);
|
||||||
|
//////////////// DEVICES ////////////////
|
||||||
|
|
||||||
//////////////// NETWORK ////////////////
|
//////////////// NETWORK ////////////////
|
||||||
auto mqtt = MQTTwrapper();
|
auto mqtt = MQTTwrapper();
|
||||||
//////////////// NETWORK ////////////////
|
//////////////// NETWORK ////////////////
|
||||||
|
|
||||||
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>());
|
io.digitalIOWrite(0, doc["stat"].as<bool>());
|
||||||
io.digitalIOWrite(15, doc["stat"].as<bool>());
|
io.digitalIOWrite(15, doc["stat"].as<bool>());
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
//////////////// NETWORK ////////////////
|
//////////////// NETWORK ////////////////
|
||||||
/////////////// CALLBACK ////////////////
|
/////////////// CALLBACK ////////////////
|
||||||
Network.onEvent(
|
Network.onEvent(
|
||||||
|
|||||||
14
src/mqtt.cpp
14
src/mqtt.cpp
@@ -1,7 +1,7 @@
|
|||||||
#include <mqtt.h>
|
#include <mqtt.h>
|
||||||
|
|
||||||
#define STACK_DEPTH 4096
|
#define STACK_DEPTH 4096
|
||||||
#define PRIOTITY 0
|
#define PRIOTITY 2
|
||||||
|
|
||||||
MQTTwrapper::MQTTwrapper() : m_config(Config::getInstance()), m_tcp(NetworkClient()), m_client(PubSubClient(m_tcp)), m_loopHandle(NULL)
|
MQTTwrapper::MQTTwrapper() : m_config(Config::getInstance()), m_tcp(NetworkClient()), m_client(PubSubClient(m_tcp)), m_loopHandle(NULL)
|
||||||
{
|
{
|
||||||
@@ -76,7 +76,8 @@ const bool MQTTwrapper::unsubscribe(topic_t topic)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const bool MQTTwrapper::connected() {
|
const bool MQTTwrapper::connected()
|
||||||
|
{
|
||||||
return m_loopHandle != NULL;
|
return m_loopHandle != NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,12 +147,12 @@ void MQTTwrapper::clientLoop(void *params)
|
|||||||
while (client.connected())
|
while (client.connected())
|
||||||
{
|
{
|
||||||
client.loop();
|
client.loop();
|
||||||
vTaskDelay(pdMS_TO_TICKS(loopTime));
|
delay(loopTime);
|
||||||
}
|
}
|
||||||
if (client.state() != MQTT_CONNECTED)
|
if (client.state() != MQTT_CONNECTED)
|
||||||
{
|
{
|
||||||
LOG_ERROR("MQTT disconnect reason ", stateMap.at(client.state()));
|
LOG_ERROR("MQTT disconnect reason ", stateMap.at(client.state()).c_str());
|
||||||
vTaskDelay(pdMS_TO_TICKS(loopTime * 50));
|
delay(loopTime * 50);
|
||||||
const bool ok = client.connect(clientName.c_str());
|
const bool ok = client.connect(clientName.c_str());
|
||||||
LOG_WARN("MQTT reconnected", ok ? "True" : "False");
|
LOG_WARN("MQTT reconnected", ok ? "True" : "False");
|
||||||
if (ok)
|
if (ok)
|
||||||
@@ -160,7 +161,8 @@ void MQTTwrapper::clientLoop(void *params)
|
|||||||
{
|
{
|
||||||
const std::string &topic(v.first);
|
const std::string &topic(v.first);
|
||||||
LOG_WARN("MQTT resubscribing to", topic.c_str());
|
LOG_WARN("MQTT resubscribing to", topic.c_str());
|
||||||
if(!wrapper->m_client.subscribe(topic.c_str())){
|
if (!wrapper->m_client.subscribe(topic.c_str()))
|
||||||
|
{
|
||||||
LOG_ERROR("Unable to resubscribe to", topic.c_str());
|
LOG_ERROR("Unable to resubscribe to", topic.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user