M5Unit-ANADIG 0.4.1 git rev:a449179
Loading...
Searching...
No Matches
unit_GP8413.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_ANADIG_UNIT_GP8413_HPP
11#define M5_UNIT_ANADIG_UNIT_GP8413_HPP
12#include <M5UnitComponent.hpp>
13
14namespace m5 {
15namespace unit {
16
21namespace gp8413 {
22
27enum class Output : uint8_t {
28 Range5V,
29 Range10V,
30};
31
36enum class Channel : uint8_t {
37 Zero,
38 One,
39};
40
41} // namespace gp8413
42
47class UnitGP8413 : public Component {
48 M5_UNIT_COMPONENT_HPP_BUILDER(UnitGP8413, 0x59);
49
50public:
52 static constexpr uint16_t RESOLUTION{0x7FFF};
53
58 struct config_t {
60 gp8413::Output range0{gp8413::Output::Range10V};
62 gp8413::Output range1{gp8413::Output::Range10V};
63 };
64
69 explicit UnitGP8413(const uint8_t addr = DEFAULT_ADDRESS) : Component(addr)
70 {
71 auto ccfg = component_config();
72 ccfg.clock = 400 * 1000U;
73 component_config(ccfg);
74 }
75 virtual ~UnitGP8413()
76 {
77 }
78
83 virtual bool begin() override;
84
87
89 {
90 return _cfg;
91 }
93 inline void config(const config_t& cfg)
94 {
95 _cfg = cfg;
96 }
98
101
107 {
108 return _range[m5::stl::to_underlying(channel)];
109 }
115 float maximumVoltage(const gp8413::Channel channel) const;
122 bool writeOutputRange(const gp8413::Output range0, const gp8413::Output range1);
124
127
134 template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr>
135 inline bool writeVoltage(const gp8413::Channel channel, const T mv)
136 {
137 return (mv >= 0.0f) && writeVoltage(channel, voltage_to_raw(channel, static_cast<float>(mv)));
138 }
145 template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr>
146 inline bool writeChannel0Voltage(const T mv)
147 {
148 return (mv >= 0.0f) &&
149 writeVoltage(gp8413::Channel::Zero, voltage_to_raw(gp8413::Channel::Zero, static_cast<float>(mv)));
150 }
157 template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr>
158 inline bool writeChannel1Voltage(const T mv)
159 {
160 return (mv >= 0.0f) &&
161 writeVoltage(gp8413::Channel::One, voltage_to_raw(gp8413::Channel::One, static_cast<float>(mv)));
162 }
170 template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr>
171 inline bool writeBothVoltage(const T mv0, const T mv1)
172 {
173 return (mv0 >= 0.0f) && (mv1 >= 0.0f) &&
174 writeBothVoltage(voltage_to_raw(gp8413::Channel::Zero, static_cast<float>(mv0)),
175 voltage_to_raw(gp8413::Channel::One, static_cast<float>(mv1)));
176 }
183 template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr>
184 inline bool writeBothVoltage(const T mv)
185 {
186 return (mv >= 0.0f) && writeBothVoltage(voltage_to_raw(gp8413::Channel::Zero, static_cast<float>(mv)),
187 voltage_to_raw(gp8413::Channel::One, static_cast<float>(mv)));
188 }
190
193
199 bool writeVoltage(const gp8413::Channel channel, const uint16_t raw);
205 inline bool writeChannel0Voltage(const uint16_t raw)
206 {
207 return writeVoltage(gp8413::Channel::Zero, static_cast<uint16_t>(raw & RESOLUTION));
208 }
214 inline bool writeChannel1Voltage(const uint16_t raw)
215 {
216 return writeVoltage(gp8413::Channel::One, static_cast<uint16_t>(raw & RESOLUTION));
217 }
224 bool writeBothVoltage(const uint16_t raw0, const uint16_t raw1);
230 inline bool writeBothVoltage(const uint16_t raw)
231 {
232 return writeBothVoltage(static_cast<uint16_t>(raw & RESOLUTION), static_cast<uint16_t>(raw & RESOLUTION));
233 }
235
240
256 bool storeBothVoltage();
258
259protected:
260 uint16_t voltage_to_raw(const gp8413::Channel channel, const float mv) const;
261 bool write_voltage(const uint8_t reg, const uint8_t* buf, const uint32_t len);
262
263private:
264 gp8413::Output _range[2]{};
265 config_t _cfg{};
266};
267
268namespace gp8413 {
270namespace command {
271constexpr uint8_t OUTPUT_RANGE_REG{0x01}; // W
272constexpr uint8_t OUTPUT_CHANNEL0_REG{0x02}; // W
273constexpr uint8_t OUTPUT_CHANNEL1_REG{0x04}; // W
274} // namespace command
276} // namespace gp8413
277
278} // namespace unit
279} // namespace m5
280#endif
Digital-to-analog signal conversion unit.
Definition unit_GP8413.hpp:47
bool writeChannel1Voltage(const uint16_t raw)
Output the raw value to channel 1.
Definition unit_GP8413.hpp:214
bool storeBothVoltage()
Store the current output voltage to the chip.
Definition unit_GP8413.cpp:220
bool writeVoltage(const gp8413::Channel channel, const T mv)
Output the voltage.
Definition unit_GP8413.hpp:135
bool writeBothVoltage(const uint16_t raw)
Output the raw value to both channels.
Definition unit_GP8413.hpp:230
static constexpr uint16_t RESOLUTION
Maximum raw output value (15 bits)
Definition unit_GP8413.hpp:52
bool writeBothVoltage(const T mv)
Output the voltage to both channels.
Definition unit_GP8413.hpp:184
bool writeChannel1Voltage(const T mv)
Output the voltage to channel 1.
Definition unit_GP8413.hpp:158
config_t config()
Gets the configuration.
Definition unit_GP8413.hpp:88
virtual bool begin() override
Initialize the unit and apply config_t (output ranges)
Definition unit_GP8413.cpp:174
float maximumVoltage(const gp8413::Channel channel) const
Gets the maximum voltage of the channel.
Definition unit_GP8413.cpp:169
bool writeChannel0Voltage(const uint16_t raw)
Output the raw value to channel 0.
Definition unit_GP8413.hpp:205
bool writeOutputRange(const gp8413::Output range0, const gp8413::Output range1)
Write the output range.
Definition unit_GP8413.cpp:179
UnitGP8413(const uint8_t addr=DEFAULT_ADDRESS)
Constructor.
Definition unit_GP8413.hpp:69
gp8413::Output range(const gp8413::Channel channel) const
Gets the output range set for the channel.
Definition unit_GP8413.hpp:106
void config(const config_t &cfg)
Set the configuration.
Definition unit_GP8413.hpp:93
bool writeChannel0Voltage(const T mv)
Output the voltage to channel 0.
Definition unit_GP8413.hpp:146
bool writeBothVoltage(const T mv0, const T mv1)
Output the voltage to both channels.
Definition unit_GP8413.hpp:171
For GP8413.
Top level namespace of M5Stack.
Unit-related namespace.
Settings for begin.
Definition unit_GP8413.hpp:58
gp8413::Output range0
Output range for channel 0.
Definition unit_GP8413.hpp:60
gp8413::Output range1
Output range for channel 1.
Definition unit_GP8413.hpp:62
Output
Output voltage range.
Definition unit_GP8413.hpp:27
Channel
Output channel.
Definition unit_GP8413.hpp:36