M5Unit-ANADIG 0.1.0 git rev:3fbd370
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
23enum class Output : uint8_t {
24 Range5V,
25 Range10V,
26};
27
28enum class Channel : uint8_t {
29 Zero,
30 One,
31};
32
33} // namespace gp8413
34
39class UnitGP8413 : public Component {
40 M5_UNIT_COMPONENT_HPP_BUILDER(UnitGP8413, 0x59);
41
42public:
43 static constexpr uint16_t RESOLUTION{0x7FFF}; // 15bits
44
49 struct config_t {
51 gp8413::Output range0{gp8413::Output::Range10V};
53 gp8413::Output range1{gp8413::Output::Range10V};
54 };
55
56 explicit UnitGP8413(const uint8_t addr = DEFAULT_ADDRESS) : Component(addr)
57 {
58 auto ccfg = component_config();
59 ccfg.clock = 400 * 1000U;
60 component_config(ccfg);
61 }
62 virtual ~UnitGP8413()
63 {
64 }
65
66 virtual bool begin() override;
67
70
72 {
73 return _cfg;
74 }
76 inline void config(const config_t& cfg)
77 {
78 _cfg = cfg;
79 }
81
86 {
87 return _range[m5::stl::to_underlying(channel)];
88 }
90 float maximumVoltage(const gp8413::Channel channel) const;
97 bool writeOutputRange(const gp8413::Output range0, const gp8413::Output range1);
99
102
109 template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr>
110 inline bool writeVoltage(const gp8413::Channel channel, const T mv)
111 {
112 return (mv >= 0.0f) && writeVoltage(channel, voltage_to_raw(channel, (float)mv));
113 }
115 template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr>
116 inline bool writeChannel0Voltage(const T mv)
117 {
118 return (mv >= 0.0f) && writeVoltage(gp8413::Channel::Zero, voltage_to_raw(gp8413::Channel::Zero, (float)mv));
119 }
121 template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr>
122 inline bool writeChannel1Voltage(const T mv)
123 {
124 return (mv >= 0.0f) && writeVoltage(gp8413::Channel::One, voltage_to_raw(gp8413::Channel::One, (float)mv));
125 }
132 template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr>
133 inline bool writeBothVoltage(const T mv0, const T mv1)
134 {
135 return writeBothVoltage(voltage_to_raw(gp8413::Channel::Zero, (float)mv0),
136 voltage_to_raw(gp8413::Channel::One, (float)mv1));
137 }
144 template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr>
145 inline bool writeBothVoltage(const T mv)
146 {
147 return (mv >= 0.0f) && writeBothVoltage(voltage_to_raw(gp8413::Channel::Zero, (float)mv),
148 voltage_to_raw(gp8413::Channel::One, (float)mv));
149 }
151
154
160 bool writeVoltage(const gp8413::Channel channel, const uint16_t raw);
161
163 template <typename T, typename std::enable_if<!std::is_same<uint16_t, T>::value && std::is_unsigned<T>::value,
164 std::nullptr_t>::type = nullptr>
165 inline bool writeVoltage(const gp8413::Channel channel, const T raw)
166 {
167 return writeVoltage(channel, static_cast<uint16_t>(raw & RESOLUTION));
168 }
170 template <typename T, typename std::enable_if<!std::is_same<uint16_t, T>::value && std::is_unsigned<T>::value,
171 std::nullptr_t>::type = nullptr>
172 inline bool writeChannel0Voltage(const T raw)
173 {
174 return writeVoltage(gp8413::Channel::Zero, static_cast<uint16_t>(raw & RESOLUTION));
175 }
177 template <typename T, typename std::enable_if<!std::is_same<uint16_t, T>::value && std::is_unsigned<T>::value,
178 std::nullptr_t>::type = nullptr>
179 inline bool writeChannel1Voltage(const T raw)
180 {
181 return writeVoltage(gp8413::Channel::One, static_cast<uint16_t>(raw & RESOLUTION));
182 }
189 bool writeBothVoltage(const uint16_t raw0, const uint16_t raw1);
191 template <typename T, typename std::enable_if<!std::is_same<uint16_t, T>::value && std::is_unsigned<T>::value,
192 std::nullptr_t>::type = nullptr>
193 inline bool writeBothVoltage(const T raw0, const T raw1)
194 {
195 return writeBothVoltage(static_cast<uint16_t>(raw0 & RESOLUTION), static_cast<uint16_t>(raw1 & RESOLUTION));
196 }
203 template <typename T, typename std::enable_if<!std::is_same<uint16_t, T>::value && std::is_unsigned<T>::value,
204 std::nullptr_t>::type = nullptr>
205 inline bool writeBothVoltage(const T raw)
206 {
207 return writeBothVoltage(static_cast<uint16_t>(raw & RESOLUTION), static_cast<uint16_t>(raw & RESOLUTION));
208 }
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
221namespace gp8413 {
222namespace command {
223
224constexpr uint8_t OUTPUT_RANGE{0x01};
225constexpr uint8_t OUTPUT_CHANNEL_0{0x02};
226constexpr uint8_t OUTPUT_CHANNEL_1{0x04};
227
228} // namespace command
229} // namespace gp8413
231
232} // namespace unit
233} // namespace m5
234
235#endif
Digital-to-analog signal conversion unit.
Definition unit_GP8413.hpp:39
bool writeVoltage(const gp8413::Channel channel, const T mv)
Output the voltage.
Definition unit_GP8413.hpp:110
bool writeBothVoltage(const T mv)
Output the voltage to both channel.
Definition unit_GP8413.hpp:145
bool writeChannel1Voltage(const T mv)
Output the voltage(mV) to channel 1.
Definition unit_GP8413.hpp:122
config_t config()
Gets the configration.
Definition unit_GP8413.hpp:71
float maximumVoltage(const gp8413::Channel channel) const
Get the maximum voltage of the channel.
Definition unit_GP8413.cpp:36
bool writeVoltage(const gp8413::Channel channel, const T raw)
Output the raw value.
Definition unit_GP8413.hpp:165
bool writeBothVoltage(const T raw0, const T raw1)
Output the raw value to both channel.
Definition unit_GP8413.hpp:193
bool writeOutputRange(const gp8413::Output range0, const gp8413::Output range1)
Write the output range.
Definition unit_GP8413.cpp:46
bool writeBothVoltage(const T raw)
Output the raw value to both channel.
Definition unit_GP8413.hpp:205
bool writeChannel0Voltage(const T raw)
Output the raw value to channel 0.
Definition unit_GP8413.hpp:172
gp8413::Output range(const gp8413::Channel channel) const
Gets the inner range.
Definition unit_GP8413.hpp:85
void config(const config_t &cfg)
Set the configration.
Definition unit_GP8413.hpp:76
bool writeChannel1Voltage(const T raw)
Output the raw value to channel 1.
Definition unit_GP8413.hpp:179
bool writeChannel0Voltage(const T mv)
Output the voltage(mV) to channel 0.
Definition unit_GP8413.hpp:116
bool writeBothVoltage(const T mv0, const T mv1)
Output the voltage to both channel.
Definition unit_GP8413.hpp:133
For GP8413.
Top level namespace of M5stack.
Unit-related namespace.
Settings for begin.
Definition unit_GP8413.hpp:49
gp8413::Output range0
Output range for channel 0.
Definition unit_GP8413.hpp:51
gp8413::Output range1
Output range for channel 1.
Definition unit_GP8413.hpp:53
Output
Definition unit_GP8413.hpp:23
Channel
Definition unit_GP8413.hpp:28