ESPHome 2026.6.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
6static const char *const TAG = "mcp4725";
7
9 auto err = this->write(nullptr, 0);
10 if (err != i2c::ERROR_OK) {
11 this->error_code_ = COMMUNICATION_FAILED;
12 this->mark_failed();
13 return;
14 }
15}
16
18 LOG_I2C_DEVICE(this);
19
20 if (this->error_code_ == COMMUNICATION_FAILED) {
21 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
22 }
23}
24
25// https://learn.sparkfun.com/tutorials/mcp4725-digital-to-analog-converter-hookup-guide?_ga=2.176055202.1402343014.1607953301-893095255.1606753886
27 const uint16_t value = (uint16_t) round(state * (pow(2, MCP4725_RES) - 1));
28
29 this->write_byte_16(64, value << 4);
30}
31
32} // 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:183
bool write_byte_16(uint8_t a_register, uint16_t data) const
Definition i2c.h:267
void setup() override
Definition mcp4725.cpp:8
void dump_config() override
Definition mcp4725.cpp:17
void write_state(float state) override
Definition mcp4725.cpp:26
bool state
Definition fan.h:2
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14