ESPHome 2025.5.0
Loading...
Searching...
No Matches
dac7678_output.cpp
Go to the documentation of this file.
1#include "dac7678_output.h"
2#include "esphome/core/log.h"
4#include "esphome/core/hal.h"
5
6namespace esphome {
7namespace dac7678 {
8
9static const char *const TAG = "dac7678";
10
11static const uint8_t DAC7678_REG_INPUT_N = 0x00;
12static const uint8_t DAC7678_REG_SELECT_UPDATE_N = 0x10;
13static const uint8_t DAC7678_REG_WRITE_N_UPDATE_ALL = 0x20;
14static const uint8_t DAC7678_REG_WRITE_N_UPDATE_N = 0x30;
15static const uint8_t DAC7678_REG_POWER = 0x40;
16static const uint8_t DAC7678_REG_CLEAR_CODE = 0x50;
17static const uint8_t DAC7678_REG_LDAC = 0x60;
18static const uint8_t DAC7678_REG_SOFTWARE_RESET = 0x70;
19static const uint8_t DAC7678_REG_INTERNAL_REF_0 = 0x80;
20static const uint8_t DAC7678_REG_INTERNAL_REF_1 = 0x90;
21
23 ESP_LOGCONFIG(TAG, "Setting up DAC7678OutputComponent...");
24
25 ESP_LOGV(TAG, "Resetting device...");
26
27 // Reset device
28 if (!this->write_byte_16(DAC7678_REG_SOFTWARE_RESET, 0x0000)) {
29 ESP_LOGE(TAG, "Reset failed");
30 this->mark_failed();
31 return;
32 } else {
33 ESP_LOGV(TAG, "Reset succeeded");
34 }
35
37
38 // Set internal reference
39 if (this->internal_reference_) {
40 if (!this->write_byte_16(DAC7678_REG_INTERNAL_REF_0, 1 << 4)) {
41 ESP_LOGE(TAG, "Set internal reference failed");
42 this->mark_failed();
43 return;
44 } else {
45 ESP_LOGV(TAG, "Internal reference enabled");
46 }
47 }
48}
49
51 if (this->is_failed()) {
52 ESP_LOGE(TAG, "Setting up DAC7678 failed!");
53 } else {
54 ESP_LOGCONFIG(TAG, "DAC7678 initialised");
55 }
56}
57
59 auto c = channel->channel_;
60 this->min_channel_ = std::min(this->min_channel_, c);
61 this->max_channel_ = std::max(this->max_channel_, c);
62 channel->set_parent(this);
63 ESP_LOGV(TAG, "Registered channel: %01u", channel->channel_);
64}
65
66void DAC7678Output::set_channel_value_(uint8_t channel, uint16_t value) {
67 if (this->dac_input_reg_[channel] != value) {
68 ESP_LOGV(TAG, "Channel %01u: input_reg=%04u ", channel, value);
69
70 if (!this->write_byte_16(DAC7678_REG_WRITE_N_UPDATE_N | channel, value << 4)) {
71 this->status_set_warning();
72 return;
73 }
74 }
75 this->dac_input_reg_[channel] = value;
77}
78
80 const float input_rounded = roundf(state * this->full_scale_);
81 auto input = static_cast<uint16_t>(input_rounded);
82 this->parent_->set_channel_value_(this->channel_, input);
83}
84
85} // namespace dac7678
86} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
void status_set_warning(const char *message="unspecified")
void status_clear_warning()
void set_parent(T *parent)
Set the parent of this object.
Definition helpers.h:546
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)
Definition i2c.h:270
bool state
Definition fan.h:0
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition core.cpp:30