M5Unit-ANADIG 0.3.0 git rev:6ce23e4
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:
51 static constexpr uint16_t RESOLUTION{0x7FFF}; // 15bits
52
57 struct config_t {
59 gp8413::Output range0{gp8413::Output::Range10V};
61 gp8413::Output range1{gp8413::Output::Range10V};
62 };
63
64 explicit UnitGP8413(const uint8_t addr = DEFAULT_ADDRESS) : Component(addr)
65 {
66 auto ccfg = component_config();
67 ccfg.clock = 400 * 1000U;
68 component_config(ccfg);
69 }
70 virtual ~UnitGP8413()
71 {
72 }
73
74 virtual bool begin() override;
75
78
80 {
81 return _cfg;
82 }
84 inline void config(const config_t& cfg)
85 {
86 _cfg = cfg;
87 }
89
94 {
95 return _range[m5::stl::to_underlying(channel)];
96 }
98 float maximumVoltage(const gp8413::Channel channel) const;
105 bool writeOutputRange(const gp8413::Output range0, const gp8413::Output range1);
107
110
117 template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr>
118 inline bool writeVoltage(const gp8413::Channel channel, const T mv)
119 {
120 return (mv >= 0.0f) && writeVoltage(channel, voltage_to_raw(channel, static_cast<float>(mv)));
121 }
123 template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr>
124 inline bool writeChannel0Voltage(const T mv)
125 {
126 return (mv >= 0.0f) &&
127 writeVoltage(gp8413::Channel::Zero, voltage_to_raw(gp8413::Channel::Zero, static_cast<float>(mv)));
128 }
130 template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr>
131 inline bool writeChannel1Voltage(const T mv)
132 {
133 return (mv >= 0.0f) &&
134 writeVoltage(gp8413::Channel::One, voltage_to_raw(gp8413::Channel::One, static_cast<float>(mv)));
135 }
142 template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr>
143 inline bool writeBothVoltage(const T mv0, const T mv1)
144 {
145 return writeBothVoltage(voltage_to_raw(gp8413::Channel::Zero, static_cast<float>(mv0)),
146 voltage_to_raw(gp8413::Channel::One, static_cast<float>(mv1)));
147 }
153 template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr>
154 inline bool writeBothVoltage(const T mv)
155 {
156 return (mv >= 0.0f) && writeBothVoltage(voltage_to_raw(gp8413::Channel::Zero, static_cast<float>(mv)),
157 voltage_to_raw(gp8413::Channel::One, static_cast<float>(mv)));
158 }
160
163
169 bool writeVoltage(const gp8413::Channel channel, const uint16_t raw);
171 inline bool writeChannel0Voltage(const uint16_t raw)
172 {
173 return writeVoltage(gp8413::Channel::Zero, static_cast<uint16_t>(raw & RESOLUTION));
174 }
176 inline bool writeChannel1Voltage(const uint16_t raw)
177 {
178 return writeVoltage(gp8413::Channel::One, static_cast<uint16_t>(raw & RESOLUTION));
179 }
186 bool writeBothVoltage(const uint16_t raw0, const uint16_t raw1);
192 inline bool writeBothVoltage(const uint16_t raw)
193 {
194 return writeBothVoltage(static_cast<uint16_t>(raw & RESOLUTION), static_cast<uint16_t>(raw & RESOLUTION));
195 }
197
202
208 bool storeBothVoltage();
210
211protected:
212 uint16_t voltage_to_raw(const gp8413::Channel channel, const float mv);
213 bool write_voltage(const uint8_t reg, const uint8_t* buf, const uint32_t len);
214
215private:
216 gp8413::Output _range[2]{};
217 config_t _cfg{};
218};
219
220namespace gp8413 {
222namespace command {
223constexpr uint8_t OUTPUT_RANGE_REG{0x01}; // W
224constexpr uint8_t OUTPUT_CHANNEL0_REG{0x02}; // W
225constexpr uint8_t OUTPUT_CHANNEL1_REG{0x04}; // W
226} // namespace command
228} // namespace gp8413
229
230} // namespace unit
231} // namespace m5
232#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:176
bool storeBothVoltage()
Store the current output voltage to the chip.
Definition unit_GP8413.cpp:99
bool writeVoltage(const gp8413::Channel channel, const T mv)
Output the voltage.
Definition unit_GP8413.hpp:118
bool writeBothVoltage(const uint16_t raw)
Output the raw value to both channel.
Definition unit_GP8413.hpp:192
bool writeBothVoltage(const T mv)
Output the voltage to both channel.
Definition unit_GP8413.hpp:154
bool writeChannel1Voltage(const T mv)
Output the voltage(mV) to channel 1.
Definition unit_GP8413.hpp:131
config_t config()
Gets the configuration.
Definition unit_GP8413.hpp:79
float maximumVoltage(const gp8413::Channel channel) const
Get the maximum voltage of the channel.
Definition unit_GP8413.cpp:51
bool writeChannel0Voltage(const uint16_t raw)
Output the raw value to channel 0.
Definition unit_GP8413.hpp:171
bool writeOutputRange(const gp8413::Output range0, const gp8413::Output range1)
Write the output range.
Definition unit_GP8413.cpp:61
gp8413::Output range(const gp8413::Channel channel) const
Gets the inner range.
Definition unit_GP8413.hpp:93
void config(const config_t &cfg)
Set the configuration.
Definition unit_GP8413.hpp:84
bool writeChannel0Voltage(const T mv)
Output the voltage(mV) to channel 0.
Definition unit_GP8413.hpp:124
bool writeBothVoltage(const T mv0, const T mv1)
Output the voltage to both channel.
Definition unit_GP8413.hpp:143
For GP8413.
Top level namespace of M5Stack.
Unit-related namespace.
Settings for begin.
Definition unit_GP8413.hpp:57
gp8413::Output range0
Output range for channel 0.
Definition unit_GP8413.hpp:59
gp8413::Output range1
Output range for channel 1.
Definition unit_GP8413.hpp:61
Output
Output voltage range.
Definition unit_GP8413.hpp:27
Channel
Output channel.
Definition unit_GP8413.hpp:36