10#ifndef M5_UNIT_METER_UNIT_ADS111X_HPP
11#define M5_UNIT_METER_UNIT_ADS111X_HPP
13#include <M5UnitComponent.hpp>
14#include <m5_utility/stl/extension.hpp>
15#include <m5_utility/container/circular_buffer.hpp>
31enum class Mux : uint8_t {
93 inline bool os()
const
95 return value & (1U << 15);
103 return static_cast<Mux>((value >> 12) & 0x07);
111 return static_cast<Gain>((value >> 9) & 0x07);
116 return value & (1U << 8);
121 return static_cast<Sampling>((value >> 5) & 0x07);
129 return value & (1U << 4);
137 return value & (1U << 3);
145 return value & (1U << 2);
159 inline void os(
const bool b)
161 value = (value & ~(1U << 15)) | ((b ? 1U : 0) << 15);
163 inline void mux(
const Mux m)
165 value = (value & ~(0x07 << 12)) | ((m5::stl::to_underlying(m) & 0x07) << 12);
167 inline void pga(
const Gain g)
169 value = (value & ~(0x07 << 9)) | ((m5::stl::to_underlying(g) & 0x07) << 9);
171 inline void mode(
const bool b)
173 value = (value & ~(1U << 8)) | ((b ? 1U : 0) << 8);
175 inline void dr(
const Sampling r)
177 value = (value & ~(0x07 << 5)) | ((m5::stl::to_underlying(r) & 0x07) << 5);
181 value = (value & ~(1U << 4)) | ((b ? 1U : 0) << 4);
185 value = (value & ~(1U << 3)) | ((b ? 1U : 0) << 3);
189 value = (value & ~(1U << 2)) | ((b ? 1U : 0) << 2);
191 inline void comp_que(
const ComparatorQueue c)
193 value = (value & ~0x03U) | (m5::stl::to_underlying(c) & 0x03);
207 inline int16_t
adc()
const
209 return static_cast<int16_t
>(raw);
219class UnitADS111x :
public Component,
public PeriodicMeasurementAdapter<UnitADS111x, ads111x::Data> {
240 explicit UnitADS111x(
const uint8_t addr = DEFAULT_ADDRESS)
241 : Component(addr), _data{new
m5::container::CircularBuffer<
ads111x::
Data>(1)}
243 auto ccfg = component_config();
244 ccfg.clock = 400 * 1000U;
245 component_config(ccfg);
247 virtual ~UnitADS111x()
251 virtual bool begin()
override;
252 virtual void update(
const bool force =
false)
override;
283 inline int16_t
adc()
const
285 return !empty() ? oldest().adc() : std::numeric_limits<int16_t>::min();
297 return PeriodicMeasurementAdapter<UnitADS111x, ads111x::Data>::startPeriodicMeasurement();
310 return PeriodicMeasurementAdapter<UnitADS111x, ads111x::Data>::startPeriodicMeasurement(rate, mux,
gain,
319 return PeriodicMeasurementAdapter<UnitADS111x, ads111x::Data>::stopPeriodicMeasurement();
329 return _ads_cfg.
mux();
336 return _ads_cfg.
dr();
433 bool start_periodic_measurement();
436 bool stop_periodic_measurement();
439 bool start_single_measurement();
440 bool in_conversion();
449 bool write_comparator_mode(
const bool b);
450 bool write_comparator_polarity(
const bool b);
451 bool write_latching_comparator(
const bool b);
457 std::unique_ptr<m5::container::CircularBuffer<ads111x::Data>> _data{};
458 float _coefficient{};
459 ads111x::Config _ads_cfg{};
468constexpr uint8_t CONVERSION_REG{0x00};
469constexpr uint8_t CONFIG_REG{0x01};
470constexpr uint8_t LOW_THRESHOLD_REG{0x02};
471constexpr uint8_t HIGH_THRESHOLD_REG{0x03};
Base class for ADS111x series.
Definition unit_ADS111x.hpp:219
bool comparatorMode() const
Gets the comparator mode.
Definition unit_ADS111x.hpp:343
bool latchingComparator() const
Gets the Latching comparator.
Definition unit_ADS111x.hpp:361
bool readThreshold(int16_t &high, int16_t &low)
Reads the threshold values.
Definition unit_ADS111x.cpp:228
virtual bool writeComparatorMode(const bool b)=0
Write the comparator mode.
ads111x::ComparatorQueue comparatorQueue() const
Gets the comparator queue.
Definition unit_ADS111x.hpp:366
bool comparatorPolarity() const
Gets the comparator polarity.
Definition unit_ADS111x.hpp:352
bool writeSamplingRate(const ads111x::Sampling rate)
Write the data rate
Definition unit_ADS111x.cpp:109
virtual bool writeComparatorQueue(const ads111x::ComparatorQueue c)=0
Write the comparator queue.
bool startPeriodicMeasurement()
Start periodic measurement in the current settings.
Definition unit_ADS111x.hpp:295
virtual bool writeMultiplexer(const ads111x::Mux mux)=0
Write the input multiplexer.
bool measureSingleshot(ads111x::Data &d, const uint32_t timeoutMillis=1000U)
Measurement single shot.
Definition unit_ADS111x.cpp:152
ads111x::Sampling samplingRate() const
Gets the sampling rate.
Definition unit_ADS111x.hpp:334
bool stopPeriodicMeasurement()
Stop periodic measurement.
Definition unit_ADS111x.hpp:317
virtual bool writeComparatorPolarity(const bool b)=0
Write the comparator polarity.
float coefficient() const
Coefficient value.
Definition unit_ADS111x.hpp:274
virtual bool writeLatchingComparator(const bool b)=0
Write the latching comparator.
virtual bool writeGain(const ads111x::Gain gain)=0
Write the programmable gain amplifier.
config_t config()
Gets the configration.
Definition unit_ADS111x.hpp:257
bool writeThreshold(const int16_t high, const int16_t low)
Write the threshold values.
Definition unit_ADS111x.cpp:239
ads111x::Mux multiplexer() const
Gets the input multiplexer.
Definition unit_ADS111x.hpp:327
void config(const config_t &cfg)
Set the configration.
Definition unit_ADS111x.hpp:262
int16_t adc() const
Oldest measured ADC.
Definition unit_ADS111x.hpp:283
bool startPeriodicMeasurement(const ads111x::Sampling rate, const ads111x::Mux mux, const ads111x::Gain gain, const ads111x::ComparatorQueue comp_que)
Start periodic measurement.
Definition unit_ADS111x.hpp:307
bool generalReset()
General reset.
Definition unit_ADS111x.cpp:204
ads111x::Gain gain() const
Gets the programmable gain amplifier.
Definition unit_ADS111x.cpp:104
Top level namespace of M5stack.
Settings for begin.
Definition unit_ADS111x.hpp:227
bool start_periodic
Start periodic measurement on begin?
Definition unit_ADS111x.hpp:229
ads111x::Gain gain
Gain if start on begin (Not supported in some classes)
Definition unit_ADS111x.hpp:235
ads111x::Mux mux
Mux if start on begin (Not supported in some classes)
Definition unit_ADS111x.hpp:233
ads111x::ComparatorQueue comp_que
ComparatorQueue if start on begin (Not supported in some classes)
Definition unit_ADS111x.hpp:237
ads111x::Sampling rate
sampling rate if start on begin
Definition unit_ADS111x.hpp:231
Accessor for configration.
Definition unit_ADS111x.hpp:89
bool os() const
Operational status.
Definition unit_ADS111x.hpp:93
Sampling dr() const
Sampling rate.
Definition unit_ADS111x.hpp:119
Gain pga() const
Programmable gain amplifier.
Definition unit_ADS111x.hpp:109
Mux mux() const
Input multiplexer.
Definition unit_ADS111x.hpp:101
bool comp_pol() const
Comparator polarity.
Definition unit_ADS111x.hpp:135
bool comp_lat() const
Latching comparator.
Definition unit_ADS111x.hpp:143
bool mode() const
Device operating mode.
Definition unit_ADS111x.hpp:114
bool comp_mode() const
Comparator mode.
Definition unit_ADS111x.hpp:127
ComparatorQueue comp_que() const
Comparator queue.
Definition unit_ADS111x.hpp:151
Measurement data group.
Definition unit_ADS111x.hpp:204
int16_t adc() const
ADC.
Definition unit_ADS111x.hpp:207
Sampling
Data rate setting (samples per second)
Definition unit_ADS111x.hpp:61
@ Rate128
128 sps as default
Mux
Input multiplexer.
Definition unit_ADS111x.hpp:31
@ GND_2
Positive:AIN2 Negative:GND.
@ AIN_01
Positive:AIN0 Negative:AIN1 as default.
@ AIN_23
Positive:AIN2 Negative:AIN3.
@ AIN_13
Positive:AIN1 Negative:AIN3.
@ GND_1
Positive:AIN1 Negative:GND.
@ GND_0
Positive:AIN0 Negative:GND.
@ AIN_03
Positive:AIN0 Negative:AIN3.
@ GND_3
Positive:AIN3 Negative:GND.
ComparatorQueue
the value determines the number of successive conversions exceeding the upper orlower threshold requi...
Definition unit_ADS111x.hpp:77
@ One
Assert after one conversion.
@ Four
Assert after four conversion.
@ Two
Assert after two conversion.
Gain
Programmable gain amplifier.
Definition unit_ADS111x.hpp:47
@ PGA_2048
+/- 2.048 V as default