ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
mcp4725.cpp
Go to the documentation of this file.
1#include "mcp4725.h"
2#include "esphome/core/log.h"
3
4namespace esphome::mcp4725 {
5
6ESPHOME_LOG_TAG(TAG, "mcp4725");
7
9 auto err = this->write(nullptr, 0);
10 if (err != i2c::ERROR_OK) {
11 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
12 return;
13 }
14}
15
16void MCP4725::dump_config() { LOG_I2C_DEVICE(this); }
17
18// https://learn.sparkfun.com/tutorials/mcp4725-digital-to-analog-converter-hookup-guide?_ga=2.176055202.1402343014.1607953301-893095255.1606753886
20 constexpr uint16_t max_value = (1U << MCP4725_RES) - 1;
21 const uint16_t value = (uint16_t) roundf(state * max_value);
22
23 this->write_byte_16(64, value << 4);
24}
25
26} // namespace esphome::mcp4725
void mark_failed()
Mark this component as failed.
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:195
bool write_byte_16(uint8_t a_register, uint16_t data) const
Definition i2c.cpp:101
void setup() override
Definition mcp4725.cpp:8
void dump_config() override
Definition mcp4725.cpp:16
void write_state(float state) override
Definition mcp4725.cpp:19
bool state
Definition fan.h:2
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
ESPHOME_LOG_TAG(TAG, "mcp4725")