M5Unit-ANADIG 0.3.0 git rev:6ce23e4
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 static_cast<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:
74 explicit UnitADS11XX(const uint8_t addr = DEFAULT_ADDRESS)
75 : Component(addr), _data{new m5::container::CircularBuffer<ads11xx::Data>(1)}
76 {
77 auto ccfg = component_config();
78 ccfg.clock = 400 * 1000U;
79 component_config(ccfg);
80 }
81 virtual ~UnitADS11XX()
82 {
83 }
84
86 virtual bool begin() override;
88 virtual void update(const bool force = false) override;
89
93 inline int16_t differentialValue() const
94 {
95 return !empty() ? oldest().differentialValue() : 0;
96 }
98 inline float differentialVoltage() const
99 {
100 return !empty() ? oldest().differentialVoltage() : std::numeric_limits<float>::quiet_NaN();
101 }
103
106
111 bool readPGA(ads11xx::PGA& pga);
118 bool writePGA(const ads11xx::PGA pga);
120
129 virtual bool generalReset();
130
131protected:
132 bool start_periodic_measurement(const uint8_t cfg_value);
133 bool start_periodic_measurement();
134 bool stop_periodic_measurement();
135
136 bool measure_singleshot(ads11xx::Data& data, const uint8_t cfg_value);
137 bool measure_singleshot(ads11xx::Data& data);
138
139 bool read_config(uint8_t& v);
140 bool write_config(const uint8_t v);
141 bool read_measurement(uint8_t v[2]);
142 bool is_data_ready();
143
144 virtual bool read_if_ready_in_periodic(uint8_t v[2]);
145 virtual uint32_t get_interval(const uint8_t /* rate */)
146 {
147 return 0;
148 }
149
150 M5_UNIT_COMPONENT_PERIODIC_MEASUREMENT_ADAPTER_HPP_BUILDER(UnitADS11XX, ads11xx::Data);
151
152protected:
153 std::unique_ptr<m5::container::CircularBuffer<ads11xx::Data>> _data{};
154 ads11xx::PGA _pga{};
155 uint8_t _rate{};
156 float _vdd{2048.f};
157 float _factor{1.0f};
158
159 struct Config {
160 inline uint8_t rate() const
161 {
162 return (value >> 2) & 0x03;
163 }
164 inline ads11xx::PGA pga() const
165 {
166 return static_cast<ads11xx::PGA>(value & 0x03);
167 }
168 inline bool continuous() const
169 {
170 return !(value & (1U << 4));
171 }
172 inline bool single() const
173 {
174 return !continuous();
175 }
176 inline bool st() const
177 {
178 // ADS1100 ST/BSY Continuous: Always true, Single: False if data ready
179 // ADS1110 ST/DRDY Continuous/Single: False if data ready
180 return (value & 0x80);
181 }
182
183 inline void rate(const uint8_t rate)
184 {
185 value = (value & ~(0x03 << 2)) | ((rate & 0x03) << 2);
186 }
187 inline void pga(const ads11xx::PGA pga)
188 {
189 value = (value & ~0x03) | m5::stl::to_underlying(pga);
190 }
191 inline void continuous(bool enable)
192 {
193 value = (value & ~(1U << 4)) | ((enable ? 0 : 1) << 4);
194 }
195 inline void single(bool enable)
196 {
197 continuous(!enable);
198 }
199 inline void st(const bool b)
200 {
201 value = (value & ~0x80) | (b ? 0x80 : 0x00);
202 }
203 uint8_t value{};
204 };
205};
206} // namespace unit
207} // namespace m5
208#endif
Base class of ADS1100,ADS1110.
Definition unit_ADS11xx.hpp:68
bool writePGA(const ads11xx::PGA pga)
Write the PGA.
Definition unit_ADS11xx.cpp:174
float differentialVoltage() const
Oldest measured differential voltage(mV)
Definition unit_ADS11xx.hpp:98
virtual bool generalReset()
General reset.
Definition unit_ADS11xx.cpp:189
UnitADS11XX(const uint8_t addr=DEFAULT_ADDRESS)
Constructor.
Definition unit_ADS11xx.hpp:74
virtual bool begin() override
Begin the unit.
Definition unit_ADS11xx.cpp:33
virtual void update(const bool force=false) override
Update the unit.
Definition unit_ADS11xx.cpp:72
int16_t differentialValue() const
Oldest measured differential value.
Definition unit_ADS11xx.hpp:93
bool readPGA(ads11xx::PGA &pga)
Read the PGA.
Definition unit_ADS11xx.cpp:164
For ADS1100,ADS1110.
Top level namespace of M5Stack.
Unit-related namespace.
Definition unit_ADS11xx.hpp:159
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
Gets the differential value.
Definition unit_ADS11xx.hpp:49
float factor
Correction factor.
Definition unit_ADS11xx.hpp:46
PGA
Programmable Gain Amplifier.
Definition unit_ADS11xx.hpp:30