Skip to content

Commit

Permalink
Cosmetic change
Browse files Browse the repository at this point in the history
  • Loading branch information
GOB52 committed Aug 13, 2024
1 parent e5f99c0 commit 295139e
Show file tree
Hide file tree
Showing 16 changed files with 64 additions and 113 deletions.
8 changes: 3 additions & 5 deletions examples/UnitUnified/Ameter/Ameter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ void setup() {
i2c_cfg.pin_scl = m5::hal::gpio::getPin(pin_num_scl);
auto i2c_bus = m5::hal::bus::i2c::getBus(i2c_cfg);
M5_LOGI("Bus:%d", i2c_bus.has_value());
if (!Units.add(unit, i2c_bus ? i2c_bus.value() : nullptr) ||
!Units.begin()) {
if (!Units.add(unit, i2c_bus ? i2c_bus.value() : nullptr) || !Units.begin()) {
M5_LOGE("Failed to begin");
lcd.clear(TFT_RED);
while (true) {
Expand All @@ -74,9 +73,8 @@ void setup() {
M5_LOGI("M5UnitUnified has been begun");
M5_LOGI("%s", Units.debugInfo().c_str());

M5_LOGI(">RES:%f COEE:%f CF:%f CORR:%f periodic :%d", unit.resolution(),
unit.coefficient(), unit.calibrationFactor(), unit.correction(),
unit.inPeriodic());
M5_LOGI(">RES:%f COEE:%f CF:%f CORR:%f periodic :%d", unit.resolution(), unit.coefficient(),
unit.calibrationFactor(), unit.correction(), unit.inPeriodic());

lcd.clear(TFT_DARKGREEN);
}
Expand Down
8 changes: 3 additions & 5 deletions examples/UnitUnified/Vmeter/Vmeter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ void setup() {
i2c_cfg.pin_scl = m5::hal::gpio::getPin(pin_num_scl);
auto i2c_bus = m5::hal::bus::i2c::getBus(i2c_cfg);
M5_LOGI("Bus:%d", i2c_bus.has_value());
if (!Units.add(unit, i2c_bus ? i2c_bus.value() : nullptr) ||
!Units.begin()) {
if (!Units.add(unit, i2c_bus ? i2c_bus.value() : nullptr) || !Units.begin()) {
M5_LOGE("Failed to begin");
lcd.clear(TFT_RED);
while (true) {
Expand All @@ -75,9 +74,8 @@ void setup() {
M5_LOGI("M5UnitUnified has been begun");
M5_LOGI("%s", Units.debugInfo().c_str());

M5_LOGI(">RES:%f COEE:%f CF:%f CORR:%f periodic :%d", unit.resolution(),
unit.coefficient(), unit.calibrationFactor(), unit.correction(),
unit.inPeriodic());
M5_LOGI(">RES:%f COEE:%f CF:%f CORR:%f periodic :%d", unit.resolution(), unit.coefficient(),
unit.calibrationFactor(), unit.correction(), unit.inPeriodic());

lcd.clear(TFT_DARKGREEN);
}
Expand Down
1 change: 0 additions & 1 deletion src/M5UnitMETER.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,4 @@ namespace m5 {
*/
namespace unit {} // namespace unit
} // namespace m5

#endif
3 changes: 1 addition & 2 deletions src/unit/unit_ADS1113.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ class UnitADS1113 : public UnitADS111x {
M5_UNIT_COMPONENT_HPP_BUILDER(UnitADS1113, 0xFF);

public:
explicit UnitADS1113(const uint8_t addr = DEFAULT_ADDRESS)
: UnitADS111x(addr) {
explicit UnitADS1113(const uint8_t addr = DEFAULT_ADDRESS) : UnitADS111x(addr) {
}
virtual ~UnitADS1113() {
}
Expand Down
3 changes: 1 addition & 2 deletions src/unit/unit_ADS1114.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ const types::uid_t UnitADS1114::uid{"UnitADS1114"_mmh3};
const types::uid_t UnitADS1114::attr{0};
bool UnitADS1114::on_begin() {
M5_LIB_LOGV("mux is not support");
return setSamplingRate(_cfg.rate) && setGain(_cfg.gain) &&
setComparatorQueue(_cfg.comp_que);
return setSamplingRate(_cfg.rate) && setGain(_cfg.gain) && setComparatorQueue(_cfg.comp_que);
}
} // namespace unit
} // namespace m5
3 changes: 1 addition & 2 deletions src/unit/unit_ADS1114.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ class UnitADS1114 : public UnitADS111x {
M5_UNIT_COMPONENT_HPP_BUILDER(UnitADS1114, 0xFF);

public:
explicit UnitADS1114(const uint8_t addr = DEFAULT_ADDRESS)
: UnitADS111x(addr) {
explicit UnitADS1114(const uint8_t addr = DEFAULT_ADDRESS) : UnitADS111x(addr) {
}
virtual ~UnitADS1114() {
}
Expand Down
31 changes: 12 additions & 19 deletions src/unit/unit_ADS1115.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ using namespace m5::unit::ads111x::command;

namespace {
constexpr Gain gain_table[] = {
Gain::PGA_6144, Gain::PGA_4096, Gain::PGA_2048,
Gain::PGA_1024, Gain::PGA_512, Gain::PGA_256,
Gain::PGA_6144, Gain::PGA_4096, Gain::PGA_2048, Gain::PGA_1024, Gain::PGA_512, Gain::PGA_256,
};
}

Expand All @@ -46,8 +45,8 @@ const char UnitADS1115::name[] = "UnitADS1115";
const types::uid_t UnitADS1115::uid{"UnitADS1115"_mmh3};
const types::uid_t UnitADS1115::attr{0};
bool UnitADS1115::on_begin() {
return setSamplingRate(_cfg.rate) && setMultiplexer(_cfg.mux) &&
setGain(_cfg.gain) && setComparatorQueue(_cfg.comp_que);
return setSamplingRate(_cfg.rate) && setMultiplexer(_cfg.mux) && setGain(_cfg.gain) &&
setComparatorQueue(_cfg.comp_que);
}

// class UnitADS1115WithEEPROM
Expand Down Expand Up @@ -78,13 +77,11 @@ bool UnitADS1115WithEEPROM::assign(TwoWire& wire) {
bool UnitADS1115WithEEPROM::on_begin() {
int idx{};
for (auto&& e : gain_table) {
if (!read_calibration(e, _calibration[idx].hope,
_calibration[idx].actual)) {
if (!read_calibration(e, _calibration[idx].hope, _calibration[idx].actual)) {
M5_LIB_LOGE("Failed ti read calibration data");
return false;
}
M5_LIB_LOGV("Calibration[%u]: %d,%d", e, _calibration[idx].hope,
_calibration[idx].actual);
M5_LIB_LOGV("Calibration[%u]: %d,%d", e, _calibration[idx].hope, _calibration[idx].actual);
++idx;
}
apply_calibration(_adsCfg.pga());
Expand All @@ -99,18 +96,15 @@ bool UnitADS1115WithEEPROM::setGain(const ads111x::Gain gain) {
return false;
}

bool UnitADS1115WithEEPROM::read_calibration(const Gain gain, int16_t& hope,
int16_t& actual) {
bool UnitADS1115WithEEPROM::read_calibration(const Gain gain, int16_t& hope, int16_t& actual) {
uint8_t reg = 0xD0 + m5::stl::to_underlying(gain) * 8;
uint8_t buf[8]{};

if (_adapterEEPROM->writeWithTransaction(reg, nullptr, 0U) !=
m5::hal::error::error_t::OK) {
if (_adapterEEPROM->writeWithTransaction(reg, nullptr, 0U) != m5::hal::error::error_t::OK) {
M5_LIB_LOGE("Failed to write");
return false;
}
if (_adapterEEPROM->readWithTransaction(buf, sizeof(buf)) !=
m5::hal::error::error_t::OK) {
if (_adapterEEPROM->readWithTransaction(buf, sizeof(buf)) != m5::hal::error::error_t::OK) {
return false;
}

Expand All @@ -131,11 +125,10 @@ bool UnitADS1115WithEEPROM::read_calibration(const Gain gain, int16_t& hope,
}

void UnitADS1115WithEEPROM::apply_calibration(const Gain gain) {
auto idx = m5::stl::to_underlying(gain);
_calibrationFactor =
(idx < m5::stl::size(_calibration) && _calibration[idx].actual)
? (float)_calibration[idx].hope / _calibration[idx].actual
: 1.0f;
auto idx = m5::stl::to_underlying(gain);
_calibrationFactor = (idx < m5::stl::size(_calibration) && _calibration[idx].actual)
? (float)_calibration[idx].hope / _calibration[idx].actual
: 1.0f;
}

} // namespace unit
Expand Down
11 changes: 4 additions & 7 deletions src/unit/unit_ADS1115.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ class UnitADS1115 : public UnitADS111x {
M5_UNIT_COMPONENT_HPP_BUILDER(UnitADS1115, 0x00);

public:
explicit UnitADS1115(const uint8_t addr = DEFAULT_ADDRESS)
: UnitADS111x(addr) {
explicit UnitADS1115(const uint8_t addr = DEFAULT_ADDRESS) : UnitADS111x(addr) {
}
virtual ~UnitADS1115() {
}
Expand Down Expand Up @@ -74,9 +73,8 @@ class UnitADS1115WithEEPROM : public UnitADS1115 {
public:
constexpr static uint8_t DEFAULT_EEPROM_ADDRESS{0xFF};

explicit UnitADS1115WithEEPROM(
const uint8_t addr = DEFAULT_ADDRESS,
const uint8_t epromAddr = DEFAULT_EEPROM_ADDRESS)
explicit UnitADS1115WithEEPROM(const uint8_t addr = DEFAULT_ADDRESS,
const uint8_t epromAddr = DEFAULT_EEPROM_ADDRESS)
: UnitADS1115(addr), _eepromAddr(epromAddr) {
}
virtual ~UnitADS1115WithEEPROM() {
Expand All @@ -94,8 +92,7 @@ class UnitADS1115WithEEPROM : public UnitADS1115 {

protected:
virtual bool on_begin() override;
bool read_calibration(const ads111x::Gain gain, int16_t& hope,
int16_t& actual);
bool read_calibration(const ads111x::Gain gain, int16_t& hope, int16_t& actual);
void apply_calibration(const ads111x::Gain gain);

protected:
Expand Down
18 changes: 6 additions & 12 deletions src/unit/unit_ADS111x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ using namespace m5::unit::ads111x::command;

namespace {
constexpr elapsed_time_t interval_table[] = {
1000UL / 8, 1000UL / 16, 1000UL / 32, 1000UL / 64,
1000UL / 128, 1000UL / 250, 1000UL / 475, 1000UL / 860,
1000UL / 8, 1000UL / 16, 1000UL / 32, 1000UL / 64, 1000UL / 128, 1000UL / 250, 1000UL / 475, 1000UL / 860,
};

constexpr float coefficient_table[] = {
Expand Down Expand Up @@ -83,8 +82,7 @@ bool UnitADS111x::begin() {
apply_interval(_adsCfg.dr());
apply_coefficient(_adsCfg.pga());

return _cfg.start_periodic ? startPeriodicMeasurement()
: stopPeriodicMeasurement();
return _cfg.start_periodic ? startPeriodicMeasurement() : stopPeriodicMeasurement();
}

void UnitADS111x::update(const bool force) {
Expand Down Expand Up @@ -138,8 +136,7 @@ bool UnitADS111x::start_periodic_measurement() {
}

bool UnitADS111x::start_periodic_measurement(const ads111x::Sampling rate) {
return !inPeriodic() && setSamplingRate(rate) &&
start_periodic_measurement();
return !inPeriodic() && setSamplingRate(rate) && start_periodic_measurement();
}

bool UnitADS111x::stop_periodic_measurement() {
Expand All @@ -154,8 +151,7 @@ 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;
Expand Down Expand Up @@ -228,8 +224,7 @@ bool UnitADS111x::generalReset() {

bool UnitADS111x::readThreshould(int16_t& high, int16_t& low) {
uint16_t hh{}, ll{};
if (readRegister16(HIGH_THRESHOLD_REG, hh, 0) &&
readRegister16(LOW_THRESHOLD_REG, ll, 0)) {
if (readRegister16(HIGH_THRESHOLD_REG, hh, 0) && readRegister16(LOW_THRESHOLD_REG, ll, 0)) {
high = hh;
low = ll;
return true;
Expand All @@ -242,8 +237,7 @@ bool UnitADS111x::setThreshould(const int16_t high, const int16_t low) {
M5_LIB_LOGW("high must be greater than low");
return false;
}
return writeRegister16(HIGH_THRESHOLD_REG, (uint16_t)high) &&
writeRegister16(LOW_THRESHOLD_REG, (uint16_t)low);
return writeRegister16(HIGH_THRESHOLD_REG, (uint16_t)high) && writeRegister16(LOW_THRESHOLD_REG, (uint16_t)low);
}

//
Expand Down
22 changes: 7 additions & 15 deletions src/unit/unit_ADS111x.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,16 @@ struct Config {
value = (value & ~(1U << 15)) | ((b ? 1U : 0) << 15);
}
inline void mux(const Mux m) {
value = (value & ~(0x07 << 12)) |
((m5::stl::to_underlying(m) & 0x07) << 12);
value = (value & ~(0x07 << 12)) | ((m5::stl::to_underlying(m) & 0x07) << 12);
}
inline void pga(const Gain g) {
value =
(value & ~(0x07 << 9)) | ((m5::stl::to_underlying(g) & 0x07) << 9);
value = (value & ~(0x07 << 9)) | ((m5::stl::to_underlying(g) & 0x07) << 9);
}
inline void mode(const bool b) {
value = (value & ~(1U << 8)) | ((b ? 1U : 0) << 8);
}
inline void dr(const Sampling r) {
value =
(value & ~(0x07 << 5)) | ((m5::stl::to_underlying(r) & 0x07) << 5);
value = (value & ~(0x07 << 5)) | ((m5::stl::to_underlying(r) & 0x07) << 5);
}
inline void comp_mode(const bool b) {
value = (value & ~(1U << 4)) | ((b ? 1U : 0) << 4);
Expand Down Expand Up @@ -201,9 +198,7 @@ struct Data {
@class UnitADS111x
@brief Base class for ADS111x series
*/
class UnitADS111x
: public Component,
public PeriodicMeasurementAdapter<UnitADS111x, ads111x::Data> {
class UnitADS111x : public Component, public PeriodicMeasurementAdapter<UnitADS111x, ads111x::Data> {
M5_UNIT_COMPONENT_HPP_BUILDER(UnitADS111x, 0x00);

public:
Expand All @@ -221,8 +216,7 @@ class UnitADS111x
};

explicit UnitADS111x(const uint8_t addr = DEFAULT_ADDRESS)
: Component(addr),
_data{new m5::container::CircularBuffer<ads111x::Data>(1)} {
: Component(addr), _data{new m5::container::CircularBuffer<ads111x::Data>(1)} {
}
virtual ~UnitADS111x() {
}
Expand Down Expand Up @@ -336,8 +330,7 @@ class UnitADS111x
@warning Until it can be measured, it will be blocked until the timeout
time
*/
bool measureSingleshot(ads111x::Data& d,
const uint32_t timeoutMillis = 1000);
bool measureSingleshot(ads111x::Data& d, const uint32_t timeoutMillis = 1000);
///@}

///@name Threshold
Expand Down Expand Up @@ -409,8 +402,7 @@ class UnitADS111x
bool set_latching_comparator(const bool b);
bool set_comparator_queue(const ads111x::ComparatorQueue c);

M5_UNIT_COMPONENT_PERIODIC_MEASUREMENT_ADAPTER_HPP_BUILDER(UnitADS111x,
ads111x::Data);
M5_UNIT_COMPONENT_PERIODIC_MEASUREMENT_ADAPTER_HPP_BUILDER(UnitADS111x, ads111x::Data);

protected:
std::unique_ptr<m5::container::CircularBuffer<ads111x::Data>> _data{};
Expand Down
6 changes: 2 additions & 4 deletions src/unit/unit_Ameter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ class UnitAmeter : public UnitADS1115WithEEPROM {
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 epromAddr = DEFAULT_EEPROM_ADDRESS)
explicit UnitAmeter(const uint8_t addr = DEFAULT_ADDRESS, const uint8_t epromAddr = DEFAULT_EEPROM_ADDRESS)
: UnitADS1115WithEEPROM(addr, epromAddr) {
}
virtual ~UnitAmeter() {
Expand All @@ -45,8 +44,7 @@ class UnitAmeter : public UnitADS1115WithEEPROM {
}
//! @brief Oldest current
inline float current() const {
return !empty() ? correction() * std::abs(adc())
: std::numeric_limits<float>::quiet_NaN();
return !empty() ? correction() * std::abs(adc()) : std::numeric_limits<float>::quiet_NaN();
}

protected:
Expand Down
8 changes: 3 additions & 5 deletions src/unit/unit_Vmeter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ class UnitVmeter : public UnitADS1115WithEEPROM {
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 epromAddr = DEFAULT_EEPROM_ADDRESS)
explicit UnitVmeter(const uint8_t addr = DEFAULT_ADDRESS, const uint8_t epromAddr = DEFAULT_EEPROM_ADDRESS)
: UnitADS1115WithEEPROM(addr, epromAddr) {
}

Expand All @@ -47,10 +46,9 @@ class UnitVmeter : public UnitADS1115WithEEPROM {

//! @brief Oldest voltage
inline float voltage() const {
return !empty() ? correction() * std::abs(adc())
: std::numeric_limits<float>::quiet_NaN();
return !empty() ? correction() * std::abs(adc()) : std::numeric_limits<float>::quiet_NaN();
}

protected:
virtual void apply_coefficient(const ads111x::Gain gain) override;

Expand Down
Loading

0 comments on commit 295139e

Please sign in to comment.