ESPHome 2025.9.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 {
5namespace mcp4725 {
6
7static const char *const TAG = "mcp4725";
8
10 auto err = this->write(nullptr, 0);
11 if (err != i2c::ERROR_OK) {
12 this->error_code_ = COMMUNICATION_FAILED;
13 this->mark_failed();
14 return;
15 }
16}
17
19 LOG_I2C_DEVICE(this);
20
21 if (this->error_code_ == COMMUNICATION_FAILED) {
22 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
23 }
24}
25
26// https://learn.sparkfun.com/tutorials/mcp4725-digital-to-analog-converter-hookup-guide?_ga=2.176055202.1402343014.1607953301-893095255.1606753886
28 const uint16_t value = (uint16_t) round(state * (pow(2, MCP4725_RES) - 1));
29
30 this->write_byte_16(64, value << 4);
31}
32
33} // namespace mcp4725
34} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
Definition i2c.h:190
bool write_byte_16(uint8_t a_register, uint16_t data)
Definition i2c.h:270
void setup() override
Definition mcp4725.cpp:9
void dump_config() override
Definition mcp4725.cpp:18
void write_state(float state) override
Definition mcp4725.cpp:27
bool state
Definition fan.h:0
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7