ESPHome  2024.3.2
mcp4725.cpp
Go to the documentation of this file.
1 #include "mcp4725.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace mcp4725 {
6 
7 static const char *const TAG = "mcp4725";
8 
9 void MCP4725::setup() {
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
void setup() override
Definition: mcp4725.cpp:9
void write_state(float state) override
Definition: mcp4725.cpp:28
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:186
No error found during execution of method.
Definition: i2c_bus.h:13
uint8_t address_
store the address of the device on the bus
Definition: i2c.h:269
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:113
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
bool write_byte_16(uint8_t a_register, uint16_t data)
Definition: i2c.h:266
void dump_config() override
Definition: mcp4725.cpp:19
bool state
Definition: fan.h:34