From 3e4e9997b391249fc0935e6b0a93876609e3d8ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Metrich?= Date: Fri, 15 Mar 2024 15:07:51 +0100 Subject: [PATCH] Some refactoring --- Mk2_3phase_RFdatalog_temp/dualtariff.h | 5 ++--- Mk2_3phase_RFdatalog_temp/movingAvg.h | 2 +- Mk2_3phase_RFdatalog_temp/platformio.ini | 4 ++-- Mk2_3phase_RFdatalog_temp/processing.cpp | 13 ++++++++----- Mk2_3phase_RFdatalog_temp/utils_dualtariff.h | 4 ++-- Mk2_3phase_RFdatalog_temp/utils_relay.h | 10 +++++----- 6 files changed, 20 insertions(+), 18 deletions(-) diff --git a/Mk2_3phase_RFdatalog_temp/dualtariff.h b/Mk2_3phase_RFdatalog_temp/dualtariff.h index f6fa53fc..baa8fce1 100644 --- a/Mk2_3phase_RFdatalog_temp/dualtariff.h +++ b/Mk2_3phase_RFdatalog_temp/dualtariff.h @@ -29,7 +29,6 @@ class _rg_OffsetForce { public: constexpr _rg_OffsetForce() - : _rg() { constexpr uint16_t uiPeakDurationInSec{ OffPeakDuration * 3600 }; // calculates offsets for force start and stop of each load @@ -51,13 +50,13 @@ class _rg_OffsetForce } } } - const uint32_t (&operator[](uint8_t i) const)[2] + const auto (&operator[](uint8_t i) const) { return _rg[i]; } private: - uint32_t _rg[N][2]; + uint32_t _rg[N][2]{}; }; inline uint32_t ul_TimeOffPeak; /**< 'timestamp' for start of off-peak period */ diff --git a/Mk2_3phase_RFdatalog_temp/movingAvg.h b/Mk2_3phase_RFdatalog_temp/movingAvg.h index a0ba6da8..d6db41b6 100644 --- a/Mk2_3phase_RFdatalog_temp/movingAvg.h +++ b/Mk2_3phase_RFdatalog_temp/movingAvg.h @@ -143,7 +143,7 @@ class movingAvg return _ar[idx]; } - constexpr uint8_t getSize() const + [[nodiscard]] constexpr uint8_t getSize() const { return DURATION_IN_MINUTES; } diff --git a/Mk2_3phase_RFdatalog_temp/platformio.ini b/Mk2_3phase_RFdatalog_temp/platformio.ini index 997aad1e..a4bfec8f 100644 --- a/Mk2_3phase_RFdatalog_temp/platformio.ini +++ b/Mk2_3phase_RFdatalog_temp/platformio.ini @@ -25,8 +25,8 @@ build_unflags = extra_scripts = pre:inject_sketch_name.py check_tool = cppcheck, clangtidy check_flags = - cppcheck: --enable=all - clangtidy: --fix --checks=*,-llvmlibc-callee-namespace,-llvmlibc-implementation-in-namespace,-clang-diagnostic-c++17-extensions + cppcheck: --enable=all --std=c++17 --suppress=missingIncludeSystem + clangtidy: --fix --extra-arg=-std=c++17 --checks=*,-llvmlibc-callee-namespace,-llvmlibc-implementation-in-namespace,-clang-diagnostic-c++17-extensions,-llvm-header-guard check_skip_packages = yes check_src_filters = +<*> monitor_filters = diff --git a/Mk2_3phase_RFdatalog_temp/processing.cpp b/Mk2_3phase_RFdatalog_temp/processing.cpp index e6791df8..5a7671d5 100644 --- a/Mk2_3phase_RFdatalog_temp/processing.cpp +++ b/Mk2_3phase_RFdatalog_temp/processing.cpp @@ -182,6 +182,9 @@ void initializeOptionalPins() } } +#if !defined(__DOXYGEN__) +void updatePortsStates() __attribute__((optimize("-O3"))); +#endif /** * @brief update the control ports for each of the physical loads * @@ -262,13 +265,13 @@ void updatePhysicalLoadStates() } const bool bDiversionOff{ b_diversionOff }; - uint8_t i{ NO_OF_DUMPLOADS }; + uint8_t idx{ NO_OF_DUMPLOADS }; do { - --i; - const auto iLoad{ loadPrioritiesAndState[i] & loadStateMask }; - physicalLoadState[iLoad] = !bDiversionOff && (b_overrideLoadOn[iLoad] || (loadPrioritiesAndState[i] & loadStateOnBit)) ? LoadStates::LOAD_ON : LoadStates::LOAD_OFF; - } while (i); + --idx; + const auto iLoad{ loadPrioritiesAndState[idx] & loadStateMask }; + physicalLoadState[iLoad] = !bDiversionOff && (b_overrideLoadOn[iLoad] || (loadPrioritiesAndState[idx] & loadStateOnBit)) ? LoadStates::LOAD_ON : LoadStates::LOAD_OFF; + } while (idx); } /** diff --git a/Mk2_3phase_RFdatalog_temp/utils_dualtariff.h b/Mk2_3phase_RFdatalog_temp/utils_dualtariff.h index 556e995b..5acef526 100644 --- a/Mk2_3phase_RFdatalog_temp/utils_dualtariff.h +++ b/Mk2_3phase_RFdatalog_temp/utils_dualtariff.h @@ -37,11 +37,11 @@ class pairForceLoad { } - constexpr int16_t getStartOffset() const + [[nodiscard]] constexpr int16_t getStartOffset() const { return iStartOffset; } - constexpr uint16_t getDuration() const + [[nodiscard]] constexpr uint16_t getDuration() const { return uiDuration; } diff --git a/Mk2_3phase_RFdatalog_temp/utils_relay.h b/Mk2_3phase_RFdatalog_temp/utils_relay.h index bc94321c..cc9e9955 100644 --- a/Mk2_3phase_RFdatalog_temp/utils_relay.h +++ b/Mk2_3phase_RFdatalog_temp/utils_relay.h @@ -36,7 +36,7 @@ class relayOutput * @param _relay_pin Control pin for the relay */ explicit constexpr relayOutput(const uint8_t _relay_pin) - : relay_pin(_relay_pin) + : relay_pin{_relay_pin} { } @@ -48,7 +48,7 @@ class relayOutput * @param _importThreshold Import threshold to turn relay OFF */ constexpr relayOutput(uint8_t _relay_pin, int16_t _surplusThreshold, int16_t _importThreshold) - : relay_pin(_relay_pin), surplusThreshold(-abs(_surplusThreshold)), importThreshold(abs(_importThreshold)) + : relay_pin{_relay_pin}, surplusThreshold{-abs(_surplusThreshold)}, importThreshold{abs(_importThreshold)} { } @@ -62,7 +62,7 @@ class relayOutput * @param _minOFF Minimum duration in minutes to leave relay OFF */ constexpr relayOutput(uint8_t _relay_pin, int16_t _surplusThreshold, int16_t _importThreshold, uint16_t _minON, uint16_t _minOFF) - : relay_pin(_relay_pin), surplusThreshold(-abs(_surplusThreshold)), importThreshold(abs(_importThreshold)), minON(_minON * 60), minOFF(_minOFF * 60) + : relay_pin{_relay_pin}, surplusThreshold{-abs(_surplusThreshold)}, importThreshold{abs(_importThreshold)}, minON{_minON * 60}, minOFF{_minOFF * 60} { } @@ -151,7 +151,7 @@ class relayOutput { return try_turnON(); } - else if (currentAvgPower > importThreshold) + if (currentAvgPower > importThreshold) { return try_turnOFF(); } @@ -255,7 +255,7 @@ class RelayEngine * @brief Construct a list of relays * */ - constexpr RelayEngine(const relayOutput (&ref)[N]) + explicit constexpr RelayEngine(const relayOutput (&ref)[N]) : relay(ref) { }