ESPHome 2025.9.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 {
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_LOGV(TAG, "Resetting device");
24
25 // Reset device
26 if (!this->write_byte_16(DAC7678_REG_SOFTWARE_RESET, 0x0000)) {
27 ESP_LOGE(TAG, "Reset failed");
28 this->mark_failed();
29 return;
30 } else {
31 ESP_LOGV(TAG, "Reset succeeded");
32 }
33
35
36 // Set internal reference
37 if (this->internal_reference_) {
38 if (!this->write_byte_16(DAC7678_REG_INTERNAL_REF_0, 1 << 4)) {
39 ESP_LOGE(TAG, "Set internal reference failed");
40 this->mark_failed();
41 return;
42 } else {
43 ESP_LOGV(TAG, "Internal reference enabled");
44 }
45 }
46}
47
49 if (this->is_failed()) {
50 ESP_LOGE(TAG, "Setting up DAC7678 failed!");
51 } else {
52 ESP_LOGCONFIG(TAG, "DAC7678 initialised");
53 }
54}
55
57 auto c = channel->channel_;
58 this->min_channel_ = std::min(this->min_channel_, c);
59 this->max_channel_ = std::max(this->max_channel_, c);
60 channel->set_parent(this);
61 ESP_LOGV(TAG, "Registered channel: %01u", channel->channel_);
62}
63
64void DAC7678Output::set_channel_value_(uint8_t channel, uint16_t value) {
65 if (this->dac_input_reg_[channel] != value) {
66 ESP_LOGV(TAG, "Channel %01u: input_reg=%04u ", channel, value);
67
68 if (!this->write_byte_16(DAC7678_REG_WRITE_N_UPDATE_N | channel, value << 4)) {
69 this->status_set_warning();
70 return;
71 }
72 }
73 this->dac_input_reg_[channel] = value;
75}
76
78 const float input_rounded = roundf(state * this->full_scale_);
79 auto input = static_cast<uint16_t>(input_rounded);
80 this->parent_->set_channel_value_(this->channel_, input);
81}
82
83} // namespace dac7678
84} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
void set_parent(T *parent)
Set the parent of this object.
Definition helpers.h:664
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:31