M5Unit-METER 0.0.3 git rev:c518ea3
Loading...
Searching...
No Matches
unit_ADS111x.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
10#ifndef M5_UNIT_METER_UNIT_ADS111X_HPP
11#define M5_UNIT_METER_UNIT_ADS111X_HPP
12
13#include <M5UnitComponent.hpp>
14#include <m5_utility/stl/extension.hpp>
15#include <m5_utility/container/circular_buffer.hpp>
16#include <limits>
17
18namespace m5 {
19namespace unit {
20
25namespace ads111x {
31enum class Mux : uint8_t {
32 AIN_01,
33 AIN_03,
34 AIN_13,
35 AIN_23,
36 GND_0,
37 GND_1,
38 GND_2,
39 GND_3,
40};
41
47enum class Gain : uint8_t {
48 PGA_6144,
49 PGA_4096,
50 PGA_2048,
51 PGA_1024,
52 PGA_512,
53 PGA_256,
54 // [6,7] PGA_256 (duplicate)
55};
56
61enum class Sampling : uint8_t {
62 Rate8,
63 Rate16,
64 Rate32,
65 Rate64,
66 Rate128,
67 Rate250,
68 Rate475,
69 Rate860,
70};
71
77enum class ComparatorQueue : uint8_t {
78 One,
79 Two,
80 Four,
81 Disable,
83};
84
89struct Config {
92
93 inline bool os() const
94 {
95 return value & (1U << 15);
96 }
101 inline Mux mux() const
102 {
103 return static_cast<Mux>((value >> 12) & 0x07);
104 }
109 inline Gain pga() const
110 {
111 return static_cast<Gain>((value >> 9) & 0x07);
112 }
114 inline bool mode() const
115 {
116 return value & (1U << 8);
117 }
119 inline Sampling dr() const
120 {
121 return static_cast<Sampling>((value >> 5) & 0x07);
122 }
127 inline bool comp_mode() const
128 {
129 return value & (1U << 4);
130 }
135 inline bool comp_pol() const
136 {
137 return value & (1U << 3);
138 }
143 inline bool comp_lat() const
144 {
145 return value & (1U << 2);
146 }
152 {
153 return static_cast<ComparatorQueue>(value & 0x03);
154 }
156
159 inline void os(const bool b)
160 {
161 value = (value & ~(1U << 15)) | ((b ? 1U : 0) << 15);
162 }
163 inline void mux(const Mux m)
164 {
165 value = (value & ~(0x07 << 12)) | ((m5::stl::to_underlying(m) & 0x07) << 12);
166 }
167 inline void pga(const Gain g)
168 {
169 value = (value & ~(0x07 << 9)) | ((m5::stl::to_underlying(g) & 0x07) << 9);
170 }
171 inline void mode(const bool b)
172 {
173 value = (value & ~(1U << 8)) | ((b ? 1U : 0) << 8);
174 }
175 inline void dr(const Sampling r)
176 {
177 value = (value & ~(0x07 << 5)) | ((m5::stl::to_underlying(r) & 0x07) << 5);
178 }
179 inline void comp_mode(const bool b)
180 {
181 value = (value & ~(1U << 4)) | ((b ? 1U : 0) << 4);
182 }
183 inline void comp_pol(const bool b)
184 {
185 value = (value & ~(1U << 3)) | ((b ? 1U : 0) << 3);
186 }
187 inline void comp_lat(const bool b)
188 {
189 value = (value & ~(1U << 2)) | ((b ? 1U : 0) << 2);
190 }
191 inline void comp_que(const ComparatorQueue c)
192 {
193 value = (value & ~0x03U) | (m5::stl::to_underlying(c) & 0x03);
194 }
196
197 uint16_t value{};
198};
199
204struct Data {
205 uint16_t raw{};
207 inline int16_t adc() const
208 {
209 return static_cast<int16_t>(raw);
210 }
211};
212
213} // namespace ads111x
214
219class UnitADS111x : public Component, public PeriodicMeasurementAdapter<UnitADS111x, ads111x::Data> {
220 M5_UNIT_COMPONENT_HPP_BUILDER(UnitADS111x, 0x00);
221
222public:
227 struct config_t {
229 bool start_periodic{true};
231 ads111x::Sampling rate{ads111x::Sampling::Rate128};
233 ads111x::Mux mux{ads111x::Mux::AIN_01};
235 ads111x::Gain gain{ads111x::Gain::PGA_2048};
237 ads111x::ComparatorQueue comp_que{ads111x::ComparatorQueue::Disable};
238 };
239
240 explicit UnitADS111x(const uint8_t addr = DEFAULT_ADDRESS)
241 : Component(addr), _data{new m5::container::CircularBuffer<ads111x::Data>(1)}
242 {
243 auto ccfg = component_config();
244 ccfg.clock = 400 * 1000U;
245 component_config(ccfg);
246 }
247 virtual ~UnitADS111x()
248 {
249 }
250
251 virtual bool begin() override;
252 virtual void update(const bool force = false) override;
253
256
258 {
259 return _cfg;
260 }
262 inline void config(const config_t& cfg)
263 {
264 _cfg = cfg;
265 }
267
270
274 inline float coefficient() const
275 {
276 return _coefficient;
277 }
279
283 inline int16_t adc() const
284 {
285 return !empty() ? oldest().adc() : std::numeric_limits<int16_t>::min();
286 }
288
291
296 {
297 return PeriodicMeasurementAdapter<UnitADS111x, ads111x::Data>::startPeriodicMeasurement();
298 }
308 const ads111x::ComparatorQueue comp_que)
309 {
310 return PeriodicMeasurementAdapter<UnitADS111x, ads111x::Data>::startPeriodicMeasurement(rate, mux, gain,
311 comp_que);
312 }
318 {
319 return PeriodicMeasurementAdapter<UnitADS111x, ads111x::Data>::stopPeriodicMeasurement();
320 }
322
326
328 {
329 return _ads_cfg.mux();
330 }
332 ads111x::Gain gain() const;
335 {
336 return _ads_cfg.dr();
337 }
343 inline bool comparatorMode() const
344 {
345 return _ads_cfg.comp_mode();
346 }
352 inline bool comparatorPolarity() const
353 {
354 return _ads_cfg.comp_pol();
355 }
361 inline bool latchingComparator() const
362 {
363 return _ads_cfg.comp_lat();
364 }
367 {
368 return _ads_cfg.comp_que();
369 }
370
372 virtual bool writeMultiplexer(const ads111x::Mux mux) = 0;
378 virtual bool writeGain(const ads111x::Gain gain) = 0;
380 bool writeSamplingRate(const ads111x::Sampling rate);
382 virtual bool writeComparatorMode(const bool b) = 0;
384 virtual bool writeComparatorPolarity(const bool b) = 0;
386 virtual bool writeLatchingComparator(const bool b) = 0;
390
393
403 bool measureSingleshot(ads111x::Data& d, const uint32_t timeoutMillis = 1000U);
405
408
414 bool readThreshold(int16_t& high, int16_t& low);
422 bool writeThreshold(const int16_t high, const int16_t low);
424
430 bool generalReset();
431
432protected:
433 bool start_periodic_measurement();
434 virtual bool start_periodic_measurement(const ads111x::Sampling rate, const ads111x::Mux mux,
435 const ads111x::Gain gain, const ads111x::ComparatorQueue comp_que) = 0;
436 bool stop_periodic_measurement();
437
438 bool read_adc_raw(ads111x::Data& d);
439 bool start_single_measurement();
440 bool in_conversion();
441
442 bool read_config(ads111x::Config& c);
443 bool write_config(const ads111x::Config& c);
444 void apply_interval(const ads111x::Sampling rate);
445 virtual void apply_coefficient(const ads111x::Gain gain);
446
447 bool write_multiplexer(const ads111x::Mux mux);
448 bool write_gain(const ads111x::Gain gain);
449 bool write_comparator_mode(const bool b);
450 bool write_comparator_polarity(const bool b);
451 bool write_latching_comparator(const bool b);
452 bool write_comparator_queue(const ads111x::ComparatorQueue c);
453
454 M5_UNIT_COMPONENT_PERIODIC_MEASUREMENT_ADAPTER_HPP_BUILDER(UnitADS111x, ads111x::Data);
455
456protected:
457 std::unique_ptr<m5::container::CircularBuffer<ads111x::Data>> _data{};
458 float _coefficient{};
459 ads111x::Config _ads_cfg{};
460
461 config_t _cfg{};
462};
463
465namespace ads111x {
466namespace command {
467
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};
472
473} // namespace command
474} // namespace ads111x
476
477} // namespace unit
478} // namespace m5
479
480#endif
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
For ADS111x families.
Top level namespace of M5stack.
Unit-related namespace.
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