M5Unit-ANADIG 0.2.0 git rev:e984a40
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 }
143 template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr>
144 inline bool writeBothVoltage(const T mv)
145 {
146 return (mv >= 0.0f) && writeBothVoltage(voltage_to_raw(gp8413::Channel::Zero, (float)mv),
147 voltage_to_raw(gp8413::Channel::One, (float)mv));
148 }
150
153
159 bool writeVoltage(const gp8413::Channel channel, const uint16_t raw);
161 inline bool writeChannel0Voltage(const uint16_t raw)
162 {
163 return writeVoltage(gp8413::Channel::Zero, static_cast<uint16_t>(raw & RESOLUTION));
164 }
166 inline bool writeChannel1Voltage(const uint16_t raw)
167 {
168 return writeVoltage(gp8413::Channel::One, static_cast<uint16_t>(raw & RESOLUTION));
169 }
176 bool writeBothVoltage(const uint16_t raw0, const uint16_t raw1);
182 inline bool writeBothVoltage(const uint16_t raw)
183 {
184 return writeBothVoltage(static_cast<uint16_t>(raw & RESOLUTION), static_cast<uint16_t>(raw & RESOLUTION));
185 }
187
192
198 bool storeBothVoltage();
200
201protected:
202 uint16_t voltage_to_raw(const gp8413::Channel channel, const float mv);
203 bool write_voltage(const uint8_t reg, const uint8_t* buf, const uint32_t len);
204
205private:
206 gp8413::Output _range[2]{};
207 config_t _cfg{};
208};
209
210namespace gp8413 {
212namespace command {
213constexpr uint8_t OUTPUT_RANGE_REG{0x01}; // W
214constexpr uint8_t OUTPUT_CHANNEL0_REG{0x02}; // W
215constexpr uint8_t OUTPUT_CHANNEL1_REG{0x04}; // W
216} // namespace command
218} // namespace gp8413
219
220} // namespace unit
221} // namespace m5
222#endif
Digital-to-analog signal conversion unit.
Definition unit_GP8413.hpp:39
bool writeChannel1Voltage(const uint16_t raw)
Output the raw value to channel 1.
Definition unit_GP8413.hpp:166
bool storeBothVoltage()
Store the current output voltage to the chip.
Definition unit_GP8413.cpp:98
bool writeVoltage(const gp8413::Channel channel, const T mv)
Output the voltage.
Definition unit_GP8413.hpp:110
bool writeBothVoltage(const uint16_t raw)
Output the raw value to both channel.
Definition unit_GP8413.hpp:182
bool writeBothVoltage(const T mv)
Output the voltage to both channel.
Definition unit_GP8413.hpp:144
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:51
bool writeChannel0Voltage(const uint16_t raw)
Output the raw value to channel 0.
Definition unit_GP8413.hpp:161
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:85
void config(const config_t &cfg)
Set the configration.
Definition unit_GP8413.hpp:76
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