ESPHome 2025.5.0
Loading...
Searching...
No Matches
esp32_dac.cpp
Go to the documentation of this file.
1#include "esp32_dac.h"
2#include "esphome/core/log.h"
4
5#ifdef USE_ESP32
6
7#ifdef USE_ARDUINO
8#include <esp32-hal-dac.h>
9#endif
10
11namespace esphome {
12namespace esp32_dac {
13
14#ifdef USE_ESP32_VARIANT_ESP32S2
15static constexpr uint8_t DAC0_PIN = 17;
16#else
17static constexpr uint8_t DAC0_PIN = 25;
18#endif
19
20static const char *const TAG = "esp32_dac";
21
23 ESP_LOGCONFIG(TAG, "Setting up ESP32 DAC Output...");
24 this->pin_->setup();
25 this->turn_off();
26
27#ifdef USE_ESP_IDF
28 const dac_channel_t channel = this->pin_->get_pin() == DAC0_PIN ? DAC_CHAN_0 : DAC_CHAN_1;
29 const dac_oneshot_config_t oneshot_cfg{channel};
30 dac_oneshot_new_channel(&oneshot_cfg, &this->dac_handle_);
31#endif
32}
33
35#ifdef USE_ESP_IDF
36 dac_oneshot_del_channel(this->dac_handle_);
37#endif
38}
39
41 ESP_LOGCONFIG(TAG, "ESP32 DAC:");
42 LOG_PIN(" Pin: ", this->pin_);
43 LOG_FLOAT_OUTPUT(this);
44}
45
47 if (this->pin_->is_inverted())
48 state = 1.0f - state;
49
50 state = state * 255;
51
52#ifdef USE_ESP_IDF
53 dac_oneshot_output_voltage(this->dac_handle_, state);
54#endif
55#ifdef USE_ARDUINO
56 dacWrite(this->pin_->get_pin(), state);
57#endif
58}
59
60} // namespace esp32_dac
61} // namespace esphome
62
63#endif
virtual void setup()=0
virtual uint8_t get_pin() const =0
virtual bool is_inverted() const =0
void on_safe_shutdown() override
Definition esp32_dac.cpp:34
dac_oneshot_handle_t dac_handle_
Definition esp32_dac.h:33
InternalGPIOPin * pin_
Definition esp32_dac.h:31
void write_state(float state) override
Definition esp32_dac.cpp:46
void setup() override
Initialize pin.
Definition esp32_dac.cpp:22
void dump_config() override
Definition esp32_dac.cpp:40
virtual void turn_off()
Disable this binary output.
bool state
Definition fan.h:0
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7