From 3923aa3c05506695606bd4ad9f80c1b504a27cb8 Mon Sep 17 00:00:00 2001 From: Emanuele Trabattoni Date: Wed, 16 Jul 2025 20:41:38 +0200 Subject: [PATCH] Added power factor register --- lib/SENECA/S50140_Driver.cpp | 22 ++++++++++++++-------- lib/SENECA/S50140_Driver.h | 3 +++ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/SENECA/S50140_Driver.cpp b/lib/SENECA/S50140_Driver.cpp index 1ced720..0531275 100644 --- a/lib/SENECA/S50140_Driver.cpp +++ b/lib/SENECA/S50140_Driver.cpp @@ -18,6 +18,7 @@ namespace drivers info.pAct = getPact(); info.pApp = getPapp(); info.pRea = getPrea(); + info.pf = getPf(); info.f = getF(); info.whTot = getWhTot(); info.whPar = getWhPar(); @@ -44,6 +45,10 @@ namespace drivers { return readFloatReg(REG_Prea); } + const float_t S50140::getPf() + { + return readFloatReg(REG_Pf); + } const float_t S50140::getF() { return readFloatReg(REG_Freq); @@ -61,8 +66,8 @@ namespace drivers { auto now = millis(); if ((now - m_lastRequest) < minDelay) - { // minimum 500ms between requests - delay(now - m_lastRequest); + { // minimum m_lastRequest between requests + vTaskDelay(pdMS_TO_TICKS(now - m_lastRequest)); } m_lastRequest = now; } @@ -90,21 +95,22 @@ namespace drivers void S50140::resetPartialCounters() { uint8_t retries(0); - const uint16_t resetAll = 0x0A03; - const uint16_t stopAll = 0x0A02; - const uint16_t startAll = 0x0A01; + constexpr uint16_t nullVal = 0x0000; + constexpr uint16_t resetAll = 0x0A03; + constexpr uint16_t stopAll = 0x0A02; + constexpr uint16_t startAll = 0x0A01; while (retries++ < maxRetries) { bool ok(true); delayRequest(); LOG_WARN("Powermeter Counter STOP"); - ok &= m_bus.writeRegisters(m_address, REG_PartCount, {0x0000, stopAll}); + ok &= m_bus.writeRegisters(m_address, REG_PartCount, {nullVal, stopAll}); delayRequest(); LOG_WARN("Powermeter Counter RESET"); - ok &= m_bus.writeRegisters(m_address, REG_PartCount, {0x0000, resetAll}); + ok &= m_bus.writeRegisters(m_address, REG_PartCount, {nullVal, resetAll}); delayRequest(); LOG_WARN("Powermeter Counter START"); - ok &= m_bus.writeRegisters(m_address, REG_PartCount, {0x0000, startAll}); + ok &= m_bus.writeRegisters(m_address, REG_PartCount, {nullVal, startAll}); if (ok) return; LOG_ERROR("Unable to Reset Powermeter Partial Counters, device", m_address); diff --git a/lib/SENECA/S50140_Driver.h b/lib/SENECA/S50140_Driver.h index 1ff0289..810b37e 100644 --- a/lib/SENECA/S50140_Driver.h +++ b/lib/SENECA/S50140_Driver.h @@ -21,6 +21,7 @@ namespace drivers const uint16_t REG_Papp = 0x102E; const uint16_t REG_Prea = 0x1036; const uint16_t REG_Freq = 0x1038; + const uint16_t REG_Pf = 0x101E; const uint16_t REG_WhTot = 0x1106; const uint16_t REG_WhPart = 0x1400; const uint16_t REG_Serial = 0x0500; @@ -45,6 +46,7 @@ namespace drivers float_t pAct; float_t pApp; float_t pRea; + float_t pf; float_t f; float_t whTot; float_t whPar; @@ -61,6 +63,7 @@ namespace drivers const float_t getPact(); const float_t getPapp(); const float_t getPrea(); + const float_t getPf(); const float_t getF(); const float_t getWhTot(); const float_t getWhPar();