ESPHome 2025.5.0
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 ESP_LOGCONFIG(TAG, "Setting up MCP4725 (0x%02X)...", this->address_);
11 auto err = this->write(nullptr, 0);
12 if (err != i2c::ERROR_OK) {
13 this->error_code_ = COMMUNICATION_FAILED;
14 this->mark_failed();
15 return;
16 }
17}
18
20 LOG_I2C_DEVICE(this);
21
22 if (this->error_code_ == COMMUNICATION_FAILED) {
23 ESP_LOGE(TAG, "Communication with MCP4725 failed!");
24 }
25}
26
27// https://learn.sparkfun.com/tutorials/mcp4725-digital-to-analog-converter-hookup-guide?_ga=2.176055202.1402343014.1607953301-893095255.1606753886
29 const uint16_t value = (uint16_t) round(state * (pow(2, MCP4725_RES) - 1));
30
31 this->write_byte_16(64, value << 4);
32}
33
34} // namespace mcp4725
35} // 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
uint8_t address_
store the address of the device on the bus
Definition i2c.h:273
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:19
void write_state(float state) override
Definition mcp4725.cpp:28
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