M5Unit-ANADIG 0.1.0 git rev:3fbd370
Loading...
Searching...
No Matches
unit_ADS11xx.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
10#ifndef M5_UNIT_ANADIG_UNIT_ADS11XX_HPP
11#define M5_UNIT_ANADIG_UNIT_ADS11XX_HPP
12#include <M5UnitComponent.hpp>
13#include <m5_utility/stl/extension.hpp>
14#include <m5_utility/container/circular_buffer.hpp>
15#include <limits> // NaN
16
17namespace m5 {
18namespace unit {
19
24namespace ads11xx {
25
30enum class PGA : uint8_t {
31 Gain1,
32 Gain2,
33 Gain4,
34 Gain8,
35};
36
41struct Data {
42 std::array<uint8_t, 2> raw{};
43 uint8_t rate{};
44 PGA pga{};
45 float vdd{2048.f};
46 float factor{1.0f};
47
49 inline int16_t differentialValue() const
50 {
51 return (int16_t)m5::types::big_uint16_t(raw[0], raw[1]).get();
52 }
54 inline float differentialVoltage() const
55 {
56 return differentialValue() / (-min_code_table[rate & 0x03] / vdd * (1U << m5::stl::to_underlying(pga))) /
57 factor;
58 }
59 static const int32_t min_code_table[4];
60};
61
62} // namespace ads11xx
63
68class UnitADS11XX : public Component, public PeriodicMeasurementAdapter<UnitADS11XX, ads11xx::Data> {
69 M5_UNIT_COMPONENT_HPP_BUILDER(UnitADS11XX, 0x00);
70
71public:
72 explicit UnitADS11XX(const uint8_t addr = DEFAULT_ADDRESS)
73 : Component(addr), _data{new m5::container::CircularBuffer<ads11xx::Data>(1)}
74 {
75 auto ccfg = component_config();
76 ccfg.clock = 400 * 1000U;
77 component_config(ccfg);
78 }
79 virtual ~UnitADS11XX()
80 {
81 }
82
83 virtual bool begin() override;
84 virtual void update(const bool force = false) override;
85
89 inline int16_t differentialValue() const
90 {
91 return !empty() ? oldest().differentialValue() : 0;
92 }
94 inline float differentialVoltage() const
95 {
96 return !empty() ? oldest().differentialVoltage() : std::numeric_limits<float>::quiet_NaN();
97 }
99
102
107 bool readPGA(ads11xx::PGA& pga);
114 bool writePGA(const ads11xx::PGA pga);
116
123 virtual bool generalReset();
124
125protected:
126 bool start_periodic_measurement(const uint8_t cfg_value);
127 bool start_periodic_measurement();
128 bool stop_periodic_measurement();
129
130 bool measure_singleshot(ads11xx::Data& data, const uint8_t cfg_value);
131 bool measure_singleshot(ads11xx::Data& data);
132
133 bool read_config(uint8_t& v);
134 bool write_config(const uint8_t v);
135 bool read_measurement(uint8_t v[2]);
136 bool is_data_ready();
137
138 virtual bool read_if_ready_in_periodic(uint8_t v[2]);
139 virtual uint32_t get_interval(const uint8_t /* rate */)
140 {
141 return 0;
142 }
143
144 M5_UNIT_COMPONENT_PERIODIC_MEASUREMENT_ADAPTER_HPP_BUILDER(UnitADS11XX, ads11xx::Data);
145
146protected:
147 std::unique_ptr<m5::container::CircularBuffer<ads11xx::Data>> _data{};
148 ads11xx::PGA _pga{};
149 uint8_t _rate{};
150 float _vdd{2.048f};
151 float _factor{1.0f};
152
153 struct Config {
154 inline uint8_t rate() const
155 {
156 return (value >> 2) & 0x03;
157 }
158 inline ads11xx::PGA pga() const
159 {
160 return static_cast<ads11xx::PGA>(value & 0x03);
161 }
162 inline bool continuous() const
163 {
164 return value & (1U << 4);
165 }
166 inline bool single() const
167 {
168 return !continuous();
169 }
170 inline bool st() const
171 {
172 // ADS1100 ST/BSY Continuous: Always true, Single: False if data raedy
173 // ADS1110 ST/DRDY Continuous/Single: False if data ready
174 return (value & 0x80);
175 }
176
177 inline void rate(const uint8_t rate)
178 {
179 value = (value & ~(0x03 << 2)) | ((rate & 0x03) << 2);
180 }
181 inline void pga(const ads11xx::PGA pga)
182 {
183 value = (value & ~0x03) | m5::stl::to_underlying(pga);
184 }
185 inline void continuous(bool enable)
186 {
187 value = (value & ~(1U << 4)) | ((enable ? 0 : 1) << 4);
188 }
189 inline void single(bool enable)
190 {
191 continuous(false);
192 }
193 inline void st(const bool b)
194 {
195 value = (value & ~0x80) | (b ? 0x80 : 0x00);
196 }
197 uint8_t value{};
198 };
199};
200} // namespace unit
201} // namespace m5
202#endif
Base class of ADS1100,ADS1110.
Definition unit_ADS11xx.hpp:68
bool writePGA(const ads11xx::PGA pga)
Write the PGA.
Definition unit_ADS11xx.cpp:158
float differentialVoltage() const
Oldest measured differential voltage(mV)
Definition unit_ADS11xx.hpp:94
virtual bool generalReset()
General reset.
Definition unit_ADS11xx.cpp:173
int16_t differentialValue() const
Oldest measured differential value.
Definition unit_ADS11xx.hpp:89
bool readPGA(ads11xx::PGA &pga)
Read the PGA.
Definition unit_ADS11xx.cpp:148
For ADS1100,ADS1110.
Top level namespace of M5stack.
Unit-related namespace.
Definition unit_ADS11xx.hpp:153
Measurement data group.
Definition unit_ADS11xx.hpp:41
std::array< uint8_t, 2 > raw
Raw.
Definition unit_ADS11xx.hpp:42
float differentialVoltage() const
Gets the differential voltage(mV)
Definition unit_ADS11xx.hpp:54
PGA pga
PGA.
Definition unit_ADS11xx.hpp:44
uint8_t rate
SPS (Value and content depend on derived class)
Definition unit_ADS11xx.hpp:43
float vdd
VDD(mV)
Definition unit_ADS11xx.hpp:45
int16_t differentialValue() const
!
Definition unit_ADS11xx.hpp:49
float factor
Correction factor.
Definition unit_ADS11xx.hpp:46
PGA
Programmable Gain Amplifier.
Definition unit_ADS11xx.hpp:30