ESPHome  2024.4.0
esp32_dac.cpp
Go to the documentation of this file.
1 #include "esp32_dac.h"
2 #include "esphome/core/log.h"
3 #include "esphome/core/helpers.h"
4 
5 #ifdef USE_ESP32
6 
7 #ifdef USE_ARDUINO
8 #include <esp32-hal-dac.h>
9 #endif
10 #ifdef USE_ESP_IDF
11 #include <driver/dac.h>
12 #endif
13 
14 namespace esphome {
15 namespace esp32_dac {
16 
17 static const char *const TAG = "esp32_dac";
18 
20  ESP_LOGCONFIG(TAG, "Setting up ESP32 DAC Output...");
21  this->pin_->setup();
22  this->turn_off();
23 
24 #ifdef USE_ESP_IDF
25  auto channel = pin_->get_pin() == 25 ? DAC_CHANNEL_1 : DAC_CHANNEL_2;
26  dac_output_enable(channel);
27 #endif
28 }
29 
31  ESP_LOGCONFIG(TAG, "ESP32 DAC:");
32  LOG_PIN(" Pin: ", this->pin_);
33  LOG_FLOAT_OUTPUT(this);
34 }
35 
37  if (this->pin_->is_inverted())
38  state = 1.0f - state;
39 
40  state = state * 255;
41 
42 #ifdef USE_ESP_IDF
43  auto channel = pin_->get_pin() == 25 ? DAC_CHANNEL_1 : DAC_CHANNEL_2;
44  dac_output_voltage(channel, (uint8_t) state);
45 #endif
46 #ifdef USE_ARDUINO
47  dacWrite(this->pin_->get_pin(), state);
48 #endif
49 }
50 
51 } // namespace esp32_dac
52 } // namespace esphome
53 
54 #endif
void setup() override
Initialize pin.
Definition: esp32_dac.cpp:19
InternalGPIOPin * pin_
Definition: esp32_dac.h:26
virtual void turn_off()
Disable this binary output.
Definition: binary_output.h:51
virtual void setup()=0
virtual uint8_t get_pin() const =0
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 write_state(float state) override
Definition: esp32_dac.cpp:36
void dump_config() override
Definition: esp32_dac.cpp:30
virtual bool is_inverted() const =0
bool state
Definition: fan.h:34