From bcef0f49b43707eba92249b8749b0ac7e5d9b439 Mon Sep 17 00:00:00 2001 From: GOB Date: Wed, 25 Sep 2024 18:41:50 +0900 Subject: [PATCH] Change .clang-format to the latest M5 one --- .clang-format | 6 +- .../PlotToSerial/main/PlotToSerial.cpp | 6 +- .../PlotToSerial/main/PlotToSerial.cpp | 6 +- src/unit/unit_ADS1113.cpp | 3 +- src/unit/unit_ADS1113.hpp | 28 +++-- src/unit/unit_ADS1114.cpp | 3 +- src/unit/unit_ADS1114.hpp | 28 +++-- src/unit/unit_ADS1115.cpp | 19 +-- src/unit/unit_ADS1115.hpp | 45 +++++--- src/unit/unit_ADS111x.cpp | 69 +++++++---- src/unit/unit_ADS111x.hpp | 108 ++++++++++++------ src/unit/unit_Ameter.cpp | 3 +- src/unit/unit_Ameter.hpp | 21 ++-- src/unit/unit_EEPROM.cpp | 6 +- src/unit/unit_EEPROM.hpp | 21 ++-- src/unit/unit_Vmeter.cpp | 3 +- src/unit/unit_Vmeter.hpp | 21 ++-- test/common_main.cpp | 3 +- test/embedded/avmeter_template.hpp | 26 +++-- test/embedded/embedded_main.cpp | 9 +- test/embedded/test_ameter/ameter_test.cpp | 11 +- test/embedded/test_vmeter/vmeter_test.cpp | 11 +- 22 files changed, 294 insertions(+), 162 deletions(-) diff --git a/.clang-format b/.clang-format index 5b9aa0a..079c44e 100644 --- a/.clang-format +++ b/.clang-format @@ -1,7 +1,7 @@ --- Language: Cpp BasedOnStyle: Google -AccessModifierOffset: -1 +AccessModifierOffset: -4 AlignAfterOpenBracket: Align AlignConsecutiveMacros: true AlignConsecutiveAssignments: true @@ -29,7 +29,7 @@ BraceWrapping: AfterClass: false AfterControlStatement: false AfterEnum: false - AfterFunction: false + AfterFunction: true AfterNamespace: false AfterObjCDeclaration: false AfterStruct: false @@ -42,7 +42,7 @@ BraceWrapping: SplitEmptyRecord: true SplitEmptyNamespace: true BreakBeforeBinaryOperators: None -BreakBeforeBraces: Attach +BreakBeforeBraces: Custom BreakBeforeInheritanceComma: false BreakInheritanceList: BeforeColon BreakBeforeTernaryOperators: true diff --git a/examples/UnitUnified/UnitAmeter/PlotToSerial/main/PlotToSerial.cpp b/examples/UnitUnified/UnitAmeter/PlotToSerial/main/PlotToSerial.cpp index db7a43f..0ea8649 100644 --- a/examples/UnitUnified/UnitAmeter/PlotToSerial/main/PlotToSerial.cpp +++ b/examples/UnitUnified/UnitAmeter/PlotToSerial/main/PlotToSerial.cpp @@ -28,7 +28,8 @@ Sampling rate{Sampling::Rate32}; } // namespace -void setup() { +void setup() +{ M5.begin(); // Settings @@ -79,7 +80,8 @@ void setup() { lcd.clear(TFT_DARKGREEN); } -void loop() { +void loop() +{ M5.update(); Units.update(); if (unit.updated()) { diff --git a/examples/UnitUnified/UnitVmeter/PlotToSerial/main/PlotToSerial.cpp b/examples/UnitUnified/UnitVmeter/PlotToSerial/main/PlotToSerial.cpp index 3836e9f..7d4287d 100644 --- a/examples/UnitUnified/UnitVmeter/PlotToSerial/main/PlotToSerial.cpp +++ b/examples/UnitUnified/UnitVmeter/PlotToSerial/main/PlotToSerial.cpp @@ -28,7 +28,8 @@ Sampling rate{Sampling::Rate32}; } // namespace -void setup() { +void setup() +{ M5.begin(); // Settings @@ -79,7 +80,8 @@ void setup() { lcd.clear(TFT_DARKGREEN); } -void loop() { +void loop() +{ M5.update(); Units.update(); if (unit.updated()) { diff --git a/src/unit/unit_ADS1113.cpp b/src/unit/unit_ADS1113.cpp index 1e87bad..4e4a1b9 100644 --- a/src/unit/unit_ADS1113.cpp +++ b/src/unit/unit_ADS1113.cpp @@ -23,7 +23,8 @@ const types::uid_t UnitADS1113::uid{"UnitADS1113"_mmh3}; const types::uid_t UnitADS1113::attr{0}; bool UnitADS1113::start_periodic_measurement(const ads111x::Sampling rate, const ads111x::Mux, const ads111x::Gain, - const ads111x::ComparatorQueue) { + const ads111x::ComparatorQueue) +{ M5_LIB_LOGW("mux, gain, and comp_que not support"); return writeSamplingRate(rate) && UnitADS111x::start_periodic_measurement(); } diff --git a/src/unit/unit_ADS1113.hpp b/src/unit/unit_ADS1113.hpp index 70048c3..6c446cd 100644 --- a/src/unit/unit_ADS1113.hpp +++ b/src/unit/unit_ADS1113.hpp @@ -21,41 +21,49 @@ namespace unit { class UnitADS1113 : public UnitADS111x { M5_UNIT_COMPONENT_HPP_BUILDER(UnitADS1113, 0x00); - public: - explicit UnitADS1113(const uint8_t addr = DEFAULT_ADDRESS) : UnitADS111x(addr) { +public: + explicit UnitADS1113(const uint8_t addr = DEFAULT_ADDRESS) : UnitADS111x(addr) + { } - virtual ~UnitADS1113() { + virtual ~UnitADS1113() + { } ///@name Configration ///@{ /*! @brief Not support @warning Not support */ - virtual bool writeMultiplexer(const ads111x::Mux) override { + virtual bool writeMultiplexer(const ads111x::Mux) override + { return false; } //! @brief Not support @warning Not support - virtual bool writeGain(const ads111x::Gain) override { + virtual bool writeGain(const ads111x::Gain) override + { return false; } //! @brief Not support @warning Not support - virtual bool writeComparatorMode(const bool) override { + virtual bool writeComparatorMode(const bool) override + { return false; } //! @brief Not support @warning Not support - virtual bool writeComparatorPolarity(const bool) override { + virtual bool writeComparatorPolarity(const bool) override + { return false; } //! @brief Not support @warning Not support - virtual bool writeLatchingComparator(const bool) override { + virtual bool writeLatchingComparator(const bool) override + { return false; } //! @brief Not support @warning Not support - virtual bool writeComparatorQueue(const ads111x::ComparatorQueue) override { + virtual bool writeComparatorQueue(const ads111x::ComparatorQueue) override + { return false; } ///@} - protected: +protected: virtual bool start_periodic_measurement(const ads111x::Sampling rate, const ads111x::Mux mux, const ads111x::Gain gain, const ads111x::ComparatorQueue comp_que) override; }; diff --git a/src/unit/unit_ADS1114.cpp b/src/unit/unit_ADS1114.cpp index 4ff0610..74545b5 100644 --- a/src/unit/unit_ADS1114.cpp +++ b/src/unit/unit_ADS1114.cpp @@ -23,7 +23,8 @@ const types::uid_t UnitADS1114::uid{"UnitADS1114"_mmh3}; const types::uid_t UnitADS1114::attr{0}; bool UnitADS1114::start_periodic_measurement(const ads111x::Sampling rate, const ads111x::Mux, const ads111x::Gain gain, - const ads111x::ComparatorQueue comp_que) { + const ads111x::ComparatorQueue comp_que) +{ M5_LIB_LOGW("mux is not support"); return writeSamplingRate(_cfg.rate) && writeGain(_cfg.gain) && writeComparatorQueue(_cfg.comp_que) && UnitADS111x::start_periodic_measurement(); diff --git a/src/unit/unit_ADS1114.hpp b/src/unit/unit_ADS1114.hpp index 3d76aa5..a85abcc 100644 --- a/src/unit/unit_ADS1114.hpp +++ b/src/unit/unit_ADS1114.hpp @@ -21,41 +21,49 @@ namespace unit { class UnitADS1114 : public UnitADS111x { M5_UNIT_COMPONENT_HPP_BUILDER(UnitADS1114, 0x00); - public: - explicit UnitADS1114(const uint8_t addr = DEFAULT_ADDRESS) : UnitADS111x(addr) { +public: + explicit UnitADS1114(const uint8_t addr = DEFAULT_ADDRESS) : UnitADS111x(addr) + { } - virtual ~UnitADS1114() { + virtual ~UnitADS1114() + { } ///@name Configration ///@{ /*! @brief Not support @warning Not support */ - virtual bool writeMultiplexer(const ads111x::Mux) override { + virtual bool writeMultiplexer(const ads111x::Mux) override + { return false; } //! @brief Write the programmable gain amplifier - virtual bool writeGain(const ads111x::Gain gain) override { + virtual bool writeGain(const ads111x::Gain gain) override + { return write_gain(gain); } //! @brief Write the comparator mode - virtual bool writeComparatorMode(const bool b) override { + virtual bool writeComparatorMode(const bool b) override + { return write_comparator_mode(b); } //! @brief Write the comparator polarity - virtual bool writeComparatorPolarity(const bool b) override { + virtual bool writeComparatorPolarity(const bool b) override + { return write_comparator_polarity(b); } //! @brief Write the latching comparator - virtual bool writeLatchingComparator(const bool b) override { + virtual bool writeLatchingComparator(const bool b) override + { return write_latching_comparator(b); } //! @brief Write the comparator queue - virtual bool writeComparatorQueue(const ads111x::ComparatorQueue c) override { + virtual bool writeComparatorQueue(const ads111x::ComparatorQueue c) override + { return write_comparator_queue(c); } ///@} - protected: +protected: virtual bool start_periodic_measurement(const ads111x::Sampling rate, const ads111x::Mux mux, const ads111x::Gain gain, const ads111x::ComparatorQueue comp_que) override; }; diff --git a/src/unit/unit_ADS1115.cpp b/src/unit/unit_ADS1115.cpp index beaee21..bd762ef 100644 --- a/src/unit/unit_ADS1115.cpp +++ b/src/unit/unit_ADS1115.cpp @@ -28,7 +28,8 @@ const char UnitADS1115::name[] = "UnitADS1115"; const types::uid_t UnitADS1115::uid{"UnitADS1115"_mmh3}; const types::uid_t UnitADS1115::attr{0}; bool UnitADS1115::start_periodic_measurement(const ads111x::Sampling rate, const ads111x::Mux mux, - const ads111x::Gain gain, const ads111x::ComparatorQueue comp_que) { + const ads111x::Gain gain, const ads111x::ComparatorQueue comp_que) +{ return writeSamplingRate(_cfg.rate) && writeMultiplexer(_cfg.mux) && writeGain(_cfg.gain) && writeComparatorQueue(_cfg.comp_que) && UnitADS111x::start_periodic_measurement(); } @@ -38,8 +39,8 @@ const char UnitAVmeterBase::name[] = "UnitAVmeterBase"; const types::uid_t UnitAVmeterBase::uid{"UnitAVmeterBase"_mmh3}; const types::uid_t UnitAVmeterBase::attr{0}; -UnitAVmeterBase::UnitAVmeterBase(const uint8_t addr, const uint8_t eepromAddr) - : UnitADS1115(addr), _eeprom(eepromAddr) { +UnitAVmeterBase::UnitAVmeterBase(const uint8_t addr, const uint8_t eepromAddr) : UnitADS1115(addr), _eeprom(eepromAddr) +{ // Form a parent-child relationship auto cfg = component_config(); cfg.max_children = 1; @@ -47,7 +48,8 @@ UnitAVmeterBase::UnitAVmeterBase(const uint8_t addr, const uint8_t eepromAddr) _valid = add(_eeprom, 0) && m5::utility::isValidI2CAddress(_eeprom.address()); } -bool UnitAVmeterBase::begin() { +bool UnitAVmeterBase::begin() +{ if (!validChild()) { M5_LIB_LOGE("Child unit is invalid %x", _eeprom.address()); return false; @@ -60,7 +62,8 @@ bool UnitAVmeterBase::begin() { return UnitADS111x::begin(); } -bool UnitAVmeterBase::writeGain(const ads111x::Gain gain) { +bool UnitAVmeterBase::writeGain(const ads111x::Gain gain) +{ if (UnitADS1115::writeGain(gain)) { apply_calibration(gain); return true; @@ -68,7 +71,8 @@ bool UnitAVmeterBase::writeGain(const ads111x::Gain gain) { return false; } -Adapter* UnitAVmeterBase::duplicate_adapter(const uint8_t ch) { +Adapter* UnitAVmeterBase::duplicate_adapter(const uint8_t ch) +{ if (ch != 0) { M5_LIB_LOGE("Invalid channel %u", ch); return nullptr; @@ -77,7 +81,8 @@ Adapter* UnitAVmeterBase::duplicate_adapter(const uint8_t ch) { return myadapter ? myadapter->duplicate(_eeprom.address()) : nullptr; } -void UnitAVmeterBase::apply_calibration(const Gain gain) { +void UnitAVmeterBase::apply_calibration(const Gain gain) +{ _calibrationFactor = _eeprom.calibrationFactor(gain); } diff --git a/src/unit/unit_ADS1115.hpp b/src/unit/unit_ADS1115.hpp index 61f3dc0..3b227d8 100644 --- a/src/unit/unit_ADS1115.hpp +++ b/src/unit/unit_ADS1115.hpp @@ -22,41 +22,49 @@ namespace unit { class UnitADS1115 : public UnitADS111x { M5_UNIT_COMPONENT_HPP_BUILDER(UnitADS1115, 0x00); - public: - explicit UnitADS1115(const uint8_t addr = DEFAULT_ADDRESS) : UnitADS111x(addr) { +public: + explicit UnitADS1115(const uint8_t addr = DEFAULT_ADDRESS) : UnitADS111x(addr) + { } - virtual ~UnitADS1115() { + virtual ~UnitADS1115() + { } ///@name Configration ///@{ /*! @brief Write the input multiplexer */ - virtual bool writeMultiplexer(const ads111x::Mux mux) override { + virtual bool writeMultiplexer(const ads111x::Mux mux) override + { return write_multiplexer(mux); } //! @brief Write the programmable gain amplifier - virtual bool writeGain(const ads111x::Gain gain) override { + virtual bool writeGain(const ads111x::Gain gain) override + { return write_gain(gain); } //! @brief Write the comparator mode - virtual bool writeComparatorMode(const bool b) override { + virtual bool writeComparatorMode(const bool b) override + { return write_comparator_mode(b); } //! @brief Write the comparator polarity - virtual bool writeComparatorPolarity(const bool b) override { + virtual bool writeComparatorPolarity(const bool b) override + { return write_comparator_polarity(b); } //! @brief Write the latching comparator - virtual bool writeLatchingComparator(const bool b) override { + virtual bool writeLatchingComparator(const bool b) override + { return write_latching_comparator(b); } //! @brief Write the comparator queue - virtual bool writeComparatorQueue(const ads111x::ComparatorQueue c) override { + virtual bool writeComparatorQueue(const ads111x::ComparatorQueue c) override + { return write_comparator_queue(c); } ///@} - protected: +protected: virtual bool start_periodic_measurement(const ads111x::Sampling rate, const ads111x::Mux mux, const ads111x::Gain gain, const ads111x::ComparatorQueue comp_que) override; }; @@ -68,30 +76,33 @@ class UnitADS1115 : public UnitADS111x { class UnitAVmeterBase : public UnitADS1115 { M5_UNIT_COMPONENT_HPP_BUILDER(UnitAVmeterBase, 0x00); - public: +public: explicit UnitAVmeterBase(const uint8_t addr = DEFAULT_ADDRESS, const uint8_t eepromAddr = 0x00); - virtual ~UnitAVmeterBase() { + virtual ~UnitAVmeterBase() + { } virtual bool begin() override; - inline float calibrationFactor() const { + inline float calibrationFactor() const + { return _calibrationFactor; } virtual bool writeGain(const ads111x::Gain gain) override; - protected: +protected: virtual Adapter* duplicate_adapter(const uint8_t ch) override; void apply_calibration(const ads111x::Gain gain); - bool validChild() const { + bool validChild() const + { return _valid; } - protected: +protected: m5::unit::meter::UnitEEPROM _eeprom{}; - private: +private: float _calibrationFactor{1.0f}; bool _valid{}; // Did the constructor correctly add the child unit? }; diff --git a/src/unit/unit_ADS111x.cpp b/src/unit/unit_ADS111x.cpp index c19e089..8bb92a8 100644 --- a/src/unit/unit_ADS111x.cpp +++ b/src/unit/unit_ADS111x.cpp @@ -53,7 +53,8 @@ const char UnitADS111x::name[] = "UnitADS111x"; const types::uid_t UnitADS111x::uid{"UnitADS111x"_mmh3}; const types::uid_t UnitADS111x::attr{0}; -bool UnitADS111x::begin() { +bool UnitADS111x::begin() +{ auto ssize = stored_size(); assert(ssize && "stored_size must be greater than zero"); if (ssize != _data->capacity()) { @@ -81,7 +82,8 @@ bool UnitADS111x::begin() { : stopPeriodicMeasurement(); } -void UnitADS111x::update(const bool force) { +void UnitADS111x::update(const bool force) +{ _updated = false; if (inPeriodic()) { elapsed_time_t at{m5::utility::millis()}; @@ -99,11 +101,13 @@ void UnitADS111x::update(const bool force) { } } -Gain UnitADS111x::gain() const { +Gain UnitADS111x::gain() const +{ return gain_table[m5::stl::to_underlying(_ads_cfg.pga())]; } -bool UnitADS111x::writeSamplingRate(ads111x::Sampling rate) { +bool UnitADS111x::writeSamplingRate(ads111x::Sampling rate) +{ Config c{}; if (read_config(c)) { c.dr(rate); @@ -115,7 +119,8 @@ bool UnitADS111x::writeSamplingRate(ads111x::Sampling rate) { return false; } -bool UnitADS111x::start_periodic_measurement() { +bool UnitADS111x::start_periodic_measurement() +{ if (inPeriodic()) { return false; } @@ -131,7 +136,8 @@ bool UnitADS111x::start_periodic_measurement() { return false; } -bool UnitADS111x::stop_periodic_measurement() { +bool UnitADS111x::stop_periodic_measurement() +{ Config c{}; if (read_config(c)) { c.mode(true); @@ -143,7 +149,8 @@ bool UnitADS111x::stop_periodic_measurement() { return false; } -bool UnitADS111x::measureSingleshot(ads111x::Data& d, const uint32_t timeoutMillis) { +bool UnitADS111x::measureSingleshot(ads111x::Data& d, const uint32_t timeoutMillis) +{ if (inPeriodic()) { M5_LIB_LOGW("Periodic measurements are running"); return false; @@ -162,7 +169,8 @@ bool UnitADS111x::measureSingleshot(ads111x::Data& d, const uint32_t timeoutMill return false; } -bool UnitADS111x::start_single_measurement() { +bool UnitADS111x::start_single_measurement() +{ if (inPeriodic()) { M5_LIB_LOGW("Periodic measurements are running"); return false; @@ -179,19 +187,22 @@ bool UnitADS111x::start_single_measurement() { return false; } -bool UnitADS111x::in_conversion() { +bool UnitADS111x::in_conversion() +{ Config c{}; return read_config(c) && !c.os(); } -bool UnitADS111x::read_adc_raw(ads111x::Data& d) { +bool UnitADS111x::read_adc_raw(ads111x::Data& d) +{ if (readRegister16(CONVERSION_REG, d.raw, 0)) { return true; } return false; } -bool UnitADS111x::generalReset() { +bool UnitADS111x::generalReset() +{ uint8_t cmd{0x06}; // reset command generalCall(&cmd, 1); @@ -214,7 +225,8 @@ bool UnitADS111x::generalReset() { return done; } -bool UnitADS111x::readThreshold(int16_t& high, int16_t& low) { +bool UnitADS111x::readThreshold(int16_t& high, int16_t& low) +{ uint16_t hh{}, ll{}; if (readRegister16(HIGH_THRESHOLD_REG, hh, 0) && readRegister16(LOW_THRESHOLD_REG, ll, 0)) { high = hh; @@ -224,7 +236,8 @@ bool UnitADS111x::readThreshold(int16_t& high, int16_t& low) { return false; } -bool UnitADS111x::writeThreshold(const int16_t high, const int16_t low) { +bool UnitADS111x::writeThreshold(const int16_t high, const int16_t low) +{ if (high <= low) { M5_LIB_LOGW("high must be greater than low"); return false; @@ -233,11 +246,13 @@ bool UnitADS111x::writeThreshold(const int16_t high, const int16_t low) { } // -bool UnitADS111x::read_config(ads111x::Config& c) { +bool UnitADS111x::read_config(ads111x::Config& c) +{ return readRegister16(CONFIG_REG, c.value, 0); } -bool UnitADS111x::write_config(const ads111x::Config& c) { +bool UnitADS111x::write_config(const ads111x::Config& c) +{ if (writeRegister16(CONFIG_REG, c.value)) { _ads_cfg = c; return true; @@ -245,20 +260,23 @@ bool UnitADS111x::write_config(const ads111x::Config& c) { return false; } -void UnitADS111x::apply_interval(const ads111x::Sampling rate) { +void UnitADS111x::apply_interval(const ads111x::Sampling rate) +{ auto idx = m5::stl::to_underlying(rate); assert(idx < m5::stl::size(interval_table) && "Illegal value"); _interval = interval_table[idx]; M5_LIB_LOGV("interval %u", _interval); } -void UnitADS111x::apply_coefficient(const ads111x::Gain gain) { +void UnitADS111x::apply_coefficient(const ads111x::Gain gain) +{ auto idx = m5::stl::to_underlying(gain); assert(idx < m5::stl::size(coefficient_table) && "Illegal value"); _coefficient = coefficient_table[idx]; } -bool UnitADS111x::write_multiplexer(const ads111x::Mux mux) { +bool UnitADS111x::write_multiplexer(const ads111x::Mux mux) +{ Config c{}; if (read_config(c)) { c.mux(mux); @@ -267,7 +285,8 @@ bool UnitADS111x::write_multiplexer(const ads111x::Mux mux) { return false; } -bool UnitADS111x::write_gain(const ads111x::Gain gain) { +bool UnitADS111x::write_gain(const ads111x::Gain gain) +{ Config c{}; if (read_config(c)) { c.pga(gain); @@ -279,7 +298,8 @@ bool UnitADS111x::write_gain(const ads111x::Gain gain) { return false; } -bool UnitADS111x::write_comparator_mode(const bool b) { +bool UnitADS111x::write_comparator_mode(const bool b) +{ Config c{}; if (read_config(c)) { c.comp_mode(b); @@ -288,7 +308,8 @@ bool UnitADS111x::write_comparator_mode(const bool b) { return false; } -bool UnitADS111x::write_comparator_polarity(const bool b) { +bool UnitADS111x::write_comparator_polarity(const bool b) +{ Config c{}; if (read_config(c)) { c.comp_pol(b); @@ -297,7 +318,8 @@ bool UnitADS111x::write_comparator_polarity(const bool b) { return false; } -bool UnitADS111x::write_latching_comparator(const bool b) { +bool UnitADS111x::write_latching_comparator(const bool b) +{ Config c{}; if (read_config(c)) { c.comp_lat(b); @@ -306,7 +328,8 @@ bool UnitADS111x::write_latching_comparator(const bool b) { return false; } -bool UnitADS111x::write_comparator_queue(const ads111x::ComparatorQueue q) { +bool UnitADS111x::write_comparator_queue(const ads111x::ComparatorQueue q) +{ Config c{}; if (read_config(c)) { c.comp_que(q); diff --git a/src/unit/unit_ADS111x.hpp b/src/unit/unit_ADS111x.hpp index b57795b..559dac6 100644 --- a/src/unit/unit_ADS111x.hpp +++ b/src/unit/unit_ADS111x.hpp @@ -90,88 +90,106 @@ struct Config { ///@name Getter ///@{ /*! @brief Operational status */ - inline bool os() const { + inline bool os() const + { return value & (1U << 15); } /*! @brief Input multiplexer @warning This feature serve nofunction on the ADS1113 and ADS1114 */ - inline Mux mux() const { + inline Mux mux() const + { return static_cast((value >> 12) & 0x07); } /*! @brief Programmable gain amplifier @warning This feature serve nofunction on the ADS1113 */ - inline Gain pga() const { + inline Gain pga() const + { return static_cast((value >> 9) & 0x07); } //! @brief Device operating mode - inline bool mode() const { + inline bool mode() const + { return value & (1U << 8); } //! @brief Sampling rate - inline Sampling dr() const { + inline Sampling dr() const + { return static_cast((value >> 5) & 0x07); } /*! @brief Comparator mode @warning This feature serve nofunction on the ADS1113 */ - inline bool comp_mode() const { + inline bool comp_mode() const + { return value & (1U << 4); } /*! @brief Comparator polarity @warning This feature serve nofunction on the ADS1113 */ - inline bool comp_pol() const { + inline bool comp_pol() const + { return value & (1U << 3); } /*! @brief Latching comparator @warning This feature serve nofunction on the ADS1113 */ - inline bool comp_lat() const { + inline bool comp_lat() const + { return value & (1U << 2); } /*! @brief Comparator queue @warning This feature serve nofunction on the ADS1113 */ - inline ComparatorQueue comp_que() const { + inline ComparatorQueue comp_que() const + { return static_cast(value & 0x03); } ///@} ///@name Setter ///@{ - inline void os(const bool b) { + inline void os(const bool b) + { value = (value & ~(1U << 15)) | ((b ? 1U : 0) << 15); } - inline void mux(const Mux m) { + inline void mux(const Mux m) + { value = (value & ~(0x07 << 12)) | ((m5::stl::to_underlying(m) & 0x07) << 12); } - inline void pga(const Gain g) { + inline void pga(const Gain g) + { value = (value & ~(0x07 << 9)) | ((m5::stl::to_underlying(g) & 0x07) << 9); } - inline void mode(const bool b) { + inline void mode(const bool b) + { value = (value & ~(1U << 8)) | ((b ? 1U : 0) << 8); } - inline void dr(const Sampling r) { + inline void dr(const Sampling r) + { value = (value & ~(0x07 << 5)) | ((m5::stl::to_underlying(r) & 0x07) << 5); } - inline void comp_mode(const bool b) { + inline void comp_mode(const bool b) + { value = (value & ~(1U << 4)) | ((b ? 1U : 0) << 4); } - inline void comp_pol(const bool b) { + inline void comp_pol(const bool b) + { value = (value & ~(1U << 3)) | ((b ? 1U : 0) << 3); } - inline void comp_lat(const bool b) { + inline void comp_lat(const bool b) + { value = (value & ~(1U << 2)) | ((b ? 1U : 0) << 2); } - inline void comp_que(const ComparatorQueue c) { + inline void comp_que(const ComparatorQueue c) + { value = (value & ~0x03U) | (m5::stl::to_underlying(c) & 0x03); } ///@} @@ -186,7 +204,8 @@ struct Config { struct Data { uint16_t raw{}; //! @brief ADC - inline int16_t adc() const { + inline int16_t adc() const + { return static_cast(raw); } }; @@ -200,7 +219,7 @@ struct Data { class UnitADS111x : public Component, public PeriodicMeasurementAdapter { M5_UNIT_COMPONENT_HPP_BUILDER(UnitADS111x, 0x00); - public: +public: /*! @struct config_t @brief Settings for begin @@ -219,12 +238,14 @@ class UnitADS111x : public Component, public PeriodicMeasurementAdapter(1)} { + : Component(addr), _data{new m5::container::CircularBuffer(1)} + { auto ccfg = component_config(); ccfg.clock = 400 * 1000U; component_config(ccfg); } - virtual ~UnitADS111x() { + virtual ~UnitADS111x() + { } virtual bool begin() override; @@ -233,11 +254,13 @@ class UnitADS111x : public Component, public PeriodicMeasurementAdapter::min(); } ///@} @@ -267,7 +292,8 @@ class UnitADS111x : public Component, public PeriodicMeasurementAdapter::startPeriodicMeasurement(); } /*! @@ -279,7 +305,8 @@ class UnitADS111x : public Component, public PeriodicMeasurementAdapter::startPeriodicMeasurement(rate, mux, gain, comp_que); } @@ -287,7 +314,8 @@ class UnitADS111x : public Component, public PeriodicMeasurementAdapter::stopPeriodicMeasurement(); } ///@} @@ -296,13 +324,15 @@ class UnitADS111x : public Component, public PeriodicMeasurementAdapter> _data{}; float _coefficient{}; ads111x::Config _ads_cfg{}; diff --git a/src/unit/unit_Ameter.cpp b/src/unit/unit_Ameter.cpp index 13c18a3..dcd63e5 100644 --- a/src/unit/unit_Ameter.cpp +++ b/src/unit/unit_Ameter.cpp @@ -20,7 +20,8 @@ const char UnitAmeter::name[] = "UnitAmeter"; const types::uid_t UnitAmeter::uid{"UnitAmeter"_mmh3}; const types::uid_t UnitAmeter::attr{0}; -void UnitAmeter::apply_coefficient(const ads111x::Gain gain) { +void UnitAmeter::apply_coefficient(const ads111x::Gain gain) +{ UnitADS111x::apply_coefficient(gain); _correction = resolution() * calibrationFactor(); } diff --git a/src/unit/unit_Ameter.hpp b/src/unit/unit_Ameter.hpp index d595fbc..e9d98d2 100644 --- a/src/unit/unit_Ameter.hpp +++ b/src/unit/unit_Ameter.hpp @@ -22,35 +22,40 @@ namespace unit { class UnitAmeter : public UnitAVmeterBase { M5_UNIT_COMPONENT_HPP_BUILDER(UnitAmeter, 0x48); - public: +public: constexpr static uint8_t DEFAULT_EEPROM_ADDRESS{0x51}; constexpr static float PRESSURE_COEFFICIENT{0.05f}; explicit UnitAmeter(const uint8_t addr = DEFAULT_ADDRESS, const uint8_t eepromAddr = DEFAULT_EEPROM_ADDRESS) - : UnitAVmeterBase(addr, eepromAddr) { + : UnitAVmeterBase(addr, eepromAddr) + { // component_config().clock is set at ADS111x constructor } - virtual ~UnitAmeter() { + virtual ~UnitAmeter() + { } //! @brief Resolution of 1 LSB - inline float resolution() const { + inline float resolution() const + { return coefficient() / PRESSURE_COEFFICIENT; } //! @brief Gets the correction value - inline float correction() const { + inline float correction() const + { return _correction; } //! @brief Oldest current (mA) - inline float current() const { + inline float current() const + { return !empty() ? correction() * adc() : std::numeric_limits::quiet_NaN(); } - protected: +protected: virtual void apply_coefficient(const ads111x::Gain gain) override; - private: +private: float _correction{1.0f}; }; } // namespace unit diff --git a/src/unit/unit_EEPROM.cpp b/src/unit/unit_EEPROM.cpp index 5500c24..76c8735 100644 --- a/src/unit/unit_EEPROM.cpp +++ b/src/unit/unit_EEPROM.cpp @@ -28,7 +28,8 @@ const char UnitEEPROM::name[] = "UnitEEPROMforMeter"; const types::uid_t UnitEEPROM::uid{"UnitEEPROMforMeter"_mmh3}; const types::uid_t UnitEEPROM::attr{0}; -bool UnitEEPROM::readCalibration() { +bool UnitEEPROM::readCalibration() +{ int idx{}; _calibration.fill({}); @@ -45,7 +46,8 @@ bool UnitEEPROM::readCalibration() { return true; } -bool UnitEEPROM::read_calibration(const Gain gain, int16_t& hope, int16_t& actual) { +bool UnitEEPROM::read_calibration(const Gain gain, int16_t& hope, int16_t& actual) +{ uint8_t reg = DATA_ADDRESS + m5::stl::to_underlying(gain) * 8; std::array buf{}; hope = actual = 1; diff --git a/src/unit/unit_EEPROM.hpp b/src/unit/unit_EEPROM.hpp index e994d0b..df86e68 100644 --- a/src/unit/unit_EEPROM.hpp +++ b/src/unit/unit_EEPROM.hpp @@ -31,28 +31,33 @@ namespace meter { class UnitEEPROM : public Component { M5_UNIT_COMPONENT_HPP_BUILDER(UnitEEPROM, 0x00); - public: - explicit UnitEEPROM(const uint8_t addr = DEFAULT_ADDRESS) : Component(addr) { +public: + explicit UnitEEPROM(const uint8_t addr = DEFAULT_ADDRESS) : Component(addr) + { } - virtual ~UnitEEPROM() { + virtual ~UnitEEPROM() + { } - inline int16_t hope(m5::unit::ads111x::Gain gain) const { + inline int16_t hope(m5::unit::ads111x::Gain gain) const + { return _calibration[m5::stl::to_underlying(gain)].hope; } - inline int16_t actual(m5::unit::ads111x::Gain gain) const { + inline int16_t actual(m5::unit::ads111x::Gain gain) const + { return _calibration[m5::stl::to_underlying(gain)].actual; } - inline float calibrationFactor(m5::unit::ads111x::Gain gain) const { + inline float calibrationFactor(m5::unit::ads111x::Gain gain) const + { return actual(gain) ? (float)hope(gain) / actual(gain) : 1.0f; } bool readCalibration(); - protected: +protected: bool read_calibration(const m5::unit::ads111x::Gain gain, int16_t& hope, int16_t& actual); - private: +private: struct Calibration { int16_t hope{1}; int16_t actual{1}; diff --git a/src/unit/unit_Vmeter.cpp b/src/unit/unit_Vmeter.cpp index 8e33a43..ebc2301 100644 --- a/src/unit/unit_Vmeter.cpp +++ b/src/unit/unit_Vmeter.cpp @@ -20,7 +20,8 @@ const char UnitVmeter::name[] = "UnitVmeter"; const types::uid_t UnitVmeter::uid{"UnitVmeter"_mmh3}; const types::uid_t UnitVmeter::attr{0}; -void UnitVmeter::apply_coefficient(const ads111x::Gain gain) { +void UnitVmeter::apply_coefficient(const ads111x::Gain gain) +{ UnitADS111x::apply_coefficient(gain); _correction = resolution() * _eeprom.calibrationFactor(gain); } diff --git a/src/unit/unit_Vmeter.hpp b/src/unit/unit_Vmeter.hpp index 9cd3d12..88bf134 100644 --- a/src/unit/unit_Vmeter.hpp +++ b/src/unit/unit_Vmeter.hpp @@ -22,36 +22,41 @@ namespace unit { class UnitVmeter : public UnitAVmeterBase { M5_UNIT_COMPONENT_HPP_BUILDER(UnitVmeter, 0x49); - public: +public: constexpr static uint8_t DEFAULT_EEPROM_ADDRESS{0x53}; constexpr static float PRESSURE_COEFFICIENT{0.01591895f}; explicit UnitVmeter(const uint8_t addr = DEFAULT_ADDRESS, const uint8_t eepromAddr = DEFAULT_EEPROM_ADDRESS) - : UnitAVmeterBase(addr, eepromAddr) { + : UnitAVmeterBase(addr, eepromAddr) + { // component_config().clock is set at ADS111x constructor } - virtual ~UnitVmeter() { + virtual ~UnitVmeter() + { } //! @brief Resolution of 1 LSB - inline float resolution() const { + inline float resolution() const + { return coefficient() / PRESSURE_COEFFICIENT; } //! @brief Gets the correction value - inline float correction() const { + inline float correction() const + { return _correction; } //! @brief Oldest voltage (mV) - inline float voltage() const { + inline float voltage() const + { return !empty() ? correction() * adc() : std::numeric_limits::quiet_NaN(); } - protected: +protected: virtual void apply_coefficient(const ads111x::Gain gain) override; - private: +private: float _correction{1.0f}; }; } // namespace unit diff --git a/test/common_main.cpp b/test/common_main.cpp index 8a3e521..907b663 100644 --- a/test/common_main.cpp +++ b/test/common_main.cpp @@ -42,7 +42,8 @@ If the Arduino framework is used, the framework library main is used. */ #if !defined(ARDUINO) -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ testing::InitGoogleTest(&argc, argv); #ifdef GTEST_FILTER diff --git a/test/embedded/avmeter_template.hpp b/test/embedded/avmeter_template.hpp index 6b38d5e..ed37471 100644 --- a/test/embedded/avmeter_template.hpp +++ b/test/embedded/avmeter_template.hpp @@ -31,8 +31,9 @@ using namespace m5::unit; using namespace m5::unit::ads111x; class TestADS1115 : public ComponentTestBase { - protected: - virtual UnitAVmeterBase* get_instance() override { +protected: + virtual UnitAVmeterBase* get_instance() override + { TestParams tp = GetParam(); auto ptr = new m5::unit::UnitAVmeterBase(tp.reg, tp.reg_eeprom); if (ptr) { @@ -42,24 +43,28 @@ class TestADS1115 : public ComponentTestBase { } return ptr; } - virtual bool is_using_hal() const override { + virtual bool is_using_hal() const override + { return GetParam().hal; }; }; namespace { -inline void check_measurement_values(m5::unit::UnitAVmeterBase* u) { +inline void check_measurement_values(m5::unit::UnitAVmeterBase* u) +{ EXPECT_NE(u->adc(), std::numeric_limits::min()); } } // namespace -TEST_P(TestADS1115, Address) { +TEST_P(TestADS1115, Address) +{ SCOPED_TRACE(ustr); m5::unit::UnitAVmeterBase tmp(0x00, 0x00); EXPECT_FALSE(tmp.begin()); } -TEST_P(TestADS1115, GeneralReset) { +TEST_P(TestADS1115, GeneralReset) +{ SCOPED_TRACE(ustr); EXPECT_TRUE(unit->stopPeriodicMeasurement()); @@ -95,7 +100,8 @@ TEST_P(TestADS1115, GeneralReset) { EXPECT_EQ(low, default_low); } -TEST_P(TestADS1115, Configration) { +TEST_P(TestADS1115, Configration) +{ SCOPED_TRACE(ustr); uint16_t prev{}, now{}; @@ -264,7 +270,8 @@ TEST_P(TestADS1115, Configration) { } } -TEST_P(TestADS1115, Periodic) { +TEST_P(TestADS1115, Periodic) +{ SCOPED_TRACE(ustr); std::tuple table[] = { @@ -301,7 +308,8 @@ TEST_P(TestADS1115, Periodic) { } } -TEST_P(TestADS1115, SingleShot) { +TEST_P(TestADS1115, SingleShot) +{ SCOPED_TRACE(ustr); m5::unit::ads111x::Data d{}; diff --git a/test/embedded/embedded_main.cpp b/test/embedded/embedded_main.cpp index f8f695e..8a8695e 100644 --- a/test/embedded/embedded_main.cpp +++ b/test/embedded/embedded_main.cpp @@ -23,11 +23,13 @@ namespace { auto& lcd = M5.Display; } // namespace -void test() { +void test() +{ lcd.fillRect(0, 0, lcd.width() >> 1, lcd.height(), RUN_ALL_TESTS() ? TFT_RED : TFT_GREEN); } -void setup() { +void setup() +{ delay(1500); M5.begin(); @@ -46,7 +48,8 @@ void setup() { #endif } -void loop() { +void loop() +{ test(); #if 0 delay(1000); diff --git a/test/embedded/test_ameter/ameter_test.cpp b/test/embedded/test_ameter/ameter_test.cpp index e96b21d..38ea882 100644 --- a/test/embedded/test_ameter/ameter_test.cpp +++ b/test/embedded/test_ameter/ameter_test.cpp @@ -29,8 +29,9 @@ INSTANTIATE_TEST_SUITE_P(ParamValues, TestADS1115, // For UnitAmeter-specific testing class TestAmeter : public ComponentTestBase { - protected: - virtual UnitAmeter* get_instance() override { +protected: + virtual UnitAmeter* get_instance() override + { auto ptr = new m5::unit::UnitAmeter(); if (ptr) { auto cfg = ptr->config(); @@ -38,7 +39,8 @@ class TestAmeter : public ComponentTestBase { } return ptr; } - virtual bool is_using_hal() const override { + virtual bool is_using_hal() const override + { return GetParam(); }; }; @@ -48,7 +50,8 @@ class TestAmeter : public ComponentTestBase { // INSTANTIATE_TEST_SUITE_P(ParamValues, TestAmeter, ::testing::Values(true)); INSTANTIATE_TEST_SUITE_P(ParamValues, TestAmeter, ::testing::Values(false)); -TEST_P(TestAmeter, Correction) { +TEST_P(TestAmeter, Correction) +{ constexpr Gain gain_table[] = { Gain::PGA_6144, Gain::PGA_4096, Gain::PGA_2048, Gain::PGA_1024, Gain::PGA_512, Gain::PGA_256, }; diff --git a/test/embedded/test_vmeter/vmeter_test.cpp b/test/embedded/test_vmeter/vmeter_test.cpp index a427c1f..6b67570 100644 --- a/test/embedded/test_vmeter/vmeter_test.cpp +++ b/test/embedded/test_vmeter/vmeter_test.cpp @@ -25,8 +25,9 @@ INSTANTIATE_TEST_SUITE_P(ParamValues, TestADS1115, // For UnitVmeter-specific testing class TestVmeter : public ComponentTestBase { - protected: - virtual UnitVmeter* get_instance() override { +protected: + virtual UnitVmeter* get_instance() override + { auto ptr = new m5::unit::UnitVmeter(); if (ptr) { auto cfg = ptr->config(); @@ -34,7 +35,8 @@ class TestVmeter : public ComponentTestBase { } return ptr; } - virtual bool is_using_hal() const override { + virtual bool is_using_hal() const override + { return GetParam(); }; }; @@ -44,7 +46,8 @@ class TestVmeter : public ComponentTestBase { // INSTANTIATE_TEST_SUITE_P(ParamValues, TestVmeter, ::testing::Values(true)); INSTANTIATE_TEST_SUITE_P(ParamValues, TestVmeter, ::testing::Values(false)); -TEST_P(TestVmeter, Correction) { +TEST_P(TestVmeter, Correction) +{ SCOPED_TRACE(ustr); constexpr Gain gain_table[] = {