ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
dac7678_output.cpp
Go to the documentation of this file.
1#include "dac7678_output.h"
2#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
5
6namespace esphome::dac7678 {
7
8ESPHOME_LOG_TAG(TAG, "dac7678");
9
10static const uint8_t DAC7678_REG_INPUT_N = 0x00;
11static const uint8_t DAC7678_REG_SELECT_UPDATE_N = 0x10;
12static const uint8_t DAC7678_REG_WRITE_N_UPDATE_ALL = 0x20;
13static const uint8_t DAC7678_REG_WRITE_N_UPDATE_N = 0x30;
14static const uint8_t DAC7678_REG_POWER = 0x40;
15static const uint8_t DAC7678_REG_CLEAR_CODE = 0x50;
16static const uint8_t DAC7678_REG_LDAC = 0x60;
17static const uint8_t DAC7678_REG_SOFTWARE_RESET = 0x70;
18static const uint8_t DAC7678_REG_INTERNAL_REF_0 = 0x80;
19static const uint8_t DAC7678_REG_INTERNAL_REF_1 = 0x90;
20
22 ESP_LOGV(TAG, "Resetting device");
23
24 // Reset device
25 if (!this->write_byte_16(DAC7678_REG_SOFTWARE_RESET, 0x0000)) {
26 this->mark_failed(LOG_STR("Reset failed"));
27 return;
28 } else {
29 ESP_LOGV(TAG, "Reset succeeded");
30 }
31
33
34 // Set internal reference
35 if (this->internal_reference_) {
36 if (!this->write_byte_16(DAC7678_REG_INTERNAL_REF_0, 1 << 4)) {
37 this->mark_failed(LOG_STR("Set internal reference failed"));
38 return;
39 } else {
40 ESP_LOGV(TAG, "Internal reference enabled");
41 }
42 }
43}
44
46 if (!this->is_failed()) {
47 ESP_LOGCONFIG(TAG, "DAC7678 initialised");
48 }
49}
50
52 auto c = channel->channel_;
53 this->min_channel_ = std::min(this->min_channel_, c);
54 this->max_channel_ = std::max(this->max_channel_, c);
55 channel->set_parent(this);
56 ESP_LOGV(TAG, "Registered channel: %01u", channel->channel_);
57}
58
59void DAC7678Output::set_channel_value_(uint8_t channel, uint16_t value) {
60 if (channel >= std::size(this->dac_input_reg_))
61 return;
62 if (this->dac_input_reg_[channel] != value) {
63 ESP_LOGV(TAG, "Channel %01u: input_reg=%04u ", channel, value);
64
65 if (!this->write_byte_16(DAC7678_REG_WRITE_N_UPDATE_N | channel, value << 4)) {
66 this->status_set_warning();
67 return;
68 }
69 }
70 this->dac_input_reg_[channel] = value;
72}
73
75 const float input_rounded = roundf(state * this->full_scale_);
76 auto input = static_cast<uint16_t>(input_rounded);
77 this->parent_->set_channel_value_(this->channel_, input);
78}
79
80} // namespace esphome::dac7678
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:274
void status_clear_warning()
Definition component.h:296
void set_parent(T *parent)
Set the parent of this object.
Definition helpers.h:2081
void write_state(float state) override
void register_channel(DAC7678Channel *channel)
void set_channel_value_(uint8_t channel, uint16_t value)
bool write_byte_16(uint8_t a_register, uint16_t data) const
Definition i2c.cpp:101
bool state
Definition fan.h:2
ESPHOME_LOG_TAG(TAG, "dac7678")
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition hal.cpp:48