ESPHome
2026.6.0-dev
Loading...
Searching...
No Matches
esphome
components
mcp4725
mcp4725.cpp
Go to the documentation of this file.
1
#include "
mcp4725.h
"
2
#include "
esphome/core/log.h
"
3
4
namespace
esphome::mcp4725
{
5
6
static
const
char
*
const
TAG =
"mcp4725"
;
7
8
void
MCP4725::setup
() {
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
17
void
MCP4725::dump_config
() {
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
26
void
MCP4725::write_state
(
float
state
) {
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
esphome::Component::mark_failed
void mark_failed()
Mark this component as failed.
Definition
component.cpp:274
esphome::i2c::I2CDevice::write
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
esphome::i2c::I2CDevice::write_byte_16
bool write_byte_16(uint8_t a_register, uint16_t data) const
Definition
i2c.h:267
esphome::mcp4725::MCP4725::setup
void setup() override
Definition
mcp4725.cpp:8
esphome::mcp4725::MCP4725::dump_config
void dump_config() override
Definition
mcp4725.cpp:17
esphome::mcp4725::MCP4725::COMMUNICATION_FAILED
@ COMMUNICATION_FAILED
Definition
mcp4725.h:18
esphome::mcp4725::MCP4725::write_state
void write_state(float state) override
Definition
mcp4725.cpp:26
state
bool state
Definition
fan.h:2
log.h
mcp4725.h
esphome::i2c::ERROR_OK
@ ERROR_OK
No error found during execution of method.
Definition
i2c_bus.h:14
esphome::mcp4725
Definition
mcp4725.cpp:4
Generated by
1.12.0