10#ifndef M5_UNIT_ANADIG_UNIT_MCP4725_HPP
11#define M5_UNIT_ANADIG_UNIT_MCP4725_HPP
12#include <M5UnitComponent.hpp>
44 static constexpr uint16_t RESOLUTION{0x0FFF};
47 static inline float raw_to_voltage(
const uint16_t raw,
const float supply_voltage)
49 return static_cast<float>(raw) * supply_voltage / 4096.0f;
52 static inline uint16_t
voltage_to_raw(
const float mv,
const float supply_voltage)
54 float val = m5::stl::clamp(mv, 0.0f, supply_voltage);
55 uint32_t raw =
static_cast<uint32_t
>((val / supply_voltage) * 4096.0f);
56 return static_cast<uint16_t
>(raw > RESOLUTION ? RESOLUTION : raw);
73 explicit UnitMCP4725(
const uint8_t addr = DEFAULT_ADDRESS) : Component(addr)
75 auto ccfg = component_config();
76 ccfg.clock = 400 * 1000U;
77 component_config(ccfg);
79 virtual ~UnitMCP4725()
83 virtual bool begin()
override;
133 template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type =
nullptr>
137 return (mv >= 0.0f) &&
148 return write_voltage(Command::FastMode, raw);
151 template <typename T, typename std::enable_if<!std::is_same<uint16_t, T>::value && std::is_unsigned<T>::value,
152 std::nullptr_t>::type =
nullptr>
155 return writeVoltage(
static_cast<uint16_t
>(raw & RESOLUTION));
170 template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type =
nullptr>
174 return (mv >= 0.0f) &&
188 template <typename T, typename std::enable_if<!std::is_same<uint16_t, T>::value && std::is_unsigned<T>::value,
189 std::nullptr_t>::type =
nullptr>
223 enum class Command : uint8_t {
229 bool write_voltage(
const Command cmd,
const uint16_t raw);
230 bool is_eeprom_ready();
231 uint32_t make_buffer(uint8_t buf[3],
const uint16_t raw,
const Command cmd);
232 bool read_status(uint8_t rbuf[5]);
235 mcp4725::PowerDown _powerDown{};
236 uint16_t _lastValue{};
Digital-to-analog signal conversion unit.
Definition unit_MCP4725.hpp:40
static float raw_to_voltage(const uint16_t raw, const float supply_voltage)
Raw value to voltage(mV)
Definition unit_MCP4725.hpp:47
bool writeVoltage(const uint16_t raw)
Output the voltage.
Definition unit_MCP4725.hpp:146
bool writePowerDown(const mcp4725::PowerDown pd)
Write the power down mode.
Definition unit_MCP4725.cpp:87
void config(const config_t &cfg)
Set the configuration.
Definition unit_MCP4725.hpp:93
static uint16_t voltage_to_raw(const float mv, const float supply_voltage)
Voltage(mV) to raw value.
Definition unit_MCP4725.hpp:52
bool writeVoltageAndEEPROM(const T raw, const bool blocking=true)
Write to DAC register and EEPROM.
Definition unit_MCP4725.hpp:190
uint16_t lastValue() const
Gets the last output raw value.
Definition unit_MCP4725.hpp:107
mcp4725::PowerDown powerDown() const
Gets the inner power down mode.
Definition unit_MCP4725.hpp:102
bool readEEPROM(mcp4725::PowerDown &pd, uint16_t &raw)
Read the EEPROM settings.
Definition unit_MCP4725.cpp:104
bool readDACRegister(mcp4725::PowerDown &pd, uint16_t &raw)
Read the DAC register.
Definition unit_MCP4725.cpp:93
bool writeVoltage(const T raw)
Output the voltage.
Definition unit_MCP4725.hpp:153
bool writeVoltage(const T mv)
Output the voltage.
Definition unit_MCP4725.hpp:134
bool generalReset()
General reset.
Definition unit_MCP4725.cpp:78
config_t config()
Gets the configuration.
Definition unit_MCP4725.hpp:88
bool writeVoltageAndEEPROM(const T mv, const bool blocking=true)
Write to DAC register and EEPROM.
Definition unit_MCP4725.hpp:171
Top level namespace of M5Stack.
Settings for begin.
Definition unit_MCP4725.hpp:63
bool using_eeprom_settings
Using EEPROM settings on begin?
Definition unit_MCP4725.hpp:65
float saturation_voltage
Definition unit_MCP4725.hpp:70
float supply_voltage
VDD of MCP4725 chip (mV). Used for raw <-> voltage conversion.
Definition unit_MCP4725.hpp:67
PowerDown
Power-down mode.
Definition unit_MCP4725.hpp:27
@ OHM_500K
500k ohm resistor to ground
@ OHM_100K
100k ohm resistor to ground
@ OHM_1K
1k ohm resistor to ground