ESPHome  2024.5.2
dac7678_output.cpp
Go to the documentation of this file.
1 #include "dac7678_output.h"
2 #include "esphome/core/log.h"
3 #include "esphome/core/helpers.h"
4 #include "esphome/core/hal.h"
5 
6 namespace esphome {
7 namespace dac7678 {
8 
9 static const char *const TAG = "dac7678";
10 
11 static const uint8_t DAC7678_REG_INPUT_N = 0x00;
12 static const uint8_t DAC7678_REG_SELECT_UPDATE_N = 0x10;
13 static const uint8_t DAC7678_REG_WRITE_N_UPDATE_ALL = 0x20;
14 static const uint8_t DAC7678_REG_WRITE_N_UPDATE_N = 0x30;
15 static const uint8_t DAC7678_REG_POWER = 0x40;
16 static const uint8_t DAC7678_REG_CLEAR_CODE = 0x50;
17 static const uint8_t DAC7678_REG_LDAC = 0x60;
18 static const uint8_t DAC7678_REG_SOFTWARE_RESET = 0x70;
19 static const uint8_t DAC7678_REG_INTERNAL_REF_0 = 0x80;
20 static 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 
36  delayMicroseconds(1000);
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 
66 void 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;
76  this->status_clear_warning();
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
void write_state(float state) override
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
void set_parent(T *parent)
Set the parent of this object.
Definition: helpers.h:533
void status_clear_warning()
Definition: component.cpp:166
void set_channel_value_(uint8_t channel, uint16_t value)
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void register_channel(DAC7678Channel *channel)
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition: core.cpp:28
bool write_byte_16(uint8_t a_register, uint16_t data)
Definition: i2c.h:266
bool state
Definition: fan.h:34