diff --git a/esphome/components/esp32_dac/__init__.py b/esphome/components/esp32_dac/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/esphome/components/esp32_dac/esp32_dac.cpp b/esphome/components/esp32_dac/esp32_dac.cpp new file mode 100644 index 0000000000..cd0696b1ad --- /dev/null +++ b/esphome/components/esp32_dac/esp32_dac.cpp @@ -0,0 +1,37 @@ +#include "esp32_dac.h" +#include "esphome/core/log.h" +#include "esphome/core/helpers.h" + +#ifdef ARDUINO_ARCH_ESP32 + +#include + +namespace esphome { +namespace esp32_dac { + +static const char *TAG = "esp32_dac"; + +void ESP32DAC::setup() { + ESP_LOGCONFIG(TAG, "Setting up ESP32 DAC Output..."); + this->pin_->setup(); + this->turn_off(); +} + +void ESP32DAC::dump_config() { + ESP_LOGCONFIG(TAG, "ESP32 DAC:"); + LOG_PIN(" Pin: ", this->pin_); + LOG_FLOAT_OUTPUT(this); +} + +void ESP32DAC::write_state(float state) { + if (this->pin_->is_inverted()) + state = 1.0f - state; + + state = state * 255; + dacWrite(this->pin_->get_pin(), state); +} + +} // namespace esp32_dac +} // namespace esphome + +#endif diff --git a/esphome/components/esp32_dac/esp32_dac.h b/esphome/components/esp32_dac/esp32_dac.h new file mode 100644 index 0000000000..648efcfe4f --- /dev/null +++ b/esphome/components/esp32_dac/esp32_dac.h @@ -0,0 +1,32 @@ +#pragma once + +#include "esphome/core/component.h" +#include "esphome/core/esphal.h" +#include "esphome/core/automation.h" +#include "esphome/components/output/float_output.h" + +#ifdef ARDUINO_ARCH_ESP32 + +namespace esphome { +namespace esp32_dac { + +class ESP32DAC : public output::FloatOutput, public Component { + public: + void set_pin(GPIOPin *pin) { pin_ = pin; } + + /// Initialize pin + void setup() override; + void dump_config() override; + /// HARDWARE setup_priority + float get_setup_priority() const override { return setup_priority::HARDWARE; } + + protected: + void write_state(float state) override; + + GPIOPin *pin_; +}; + +} // namespace esp32_dac +} // namespace esphome + +#endif diff --git a/esphome/components/esp32_dac/output.py b/esphome/components/esp32_dac/output.py new file mode 100644 index 0000000000..bb0a8b60e9 --- /dev/null +++ b/esphome/components/esp32_dac/output.py @@ -0,0 +1,31 @@ +from esphome import pins +from esphome.components import output +import esphome.config_validation as cv +import esphome.codegen as cg +from esphome.const import CONF_ID, CONF_NUMBER, CONF_PIN, ESP_PLATFORM_ESP32 + +ESP_PLATFORMS = [ESP_PLATFORM_ESP32] + + +def valid_dac_pin(value): + num = value[CONF_NUMBER] + cv.one_of(25, 26)(num) + return value + + +esp32_dac_ns = cg.esphome_ns.namespace('esp32_dac') +ESP32DAC = esp32_dac_ns.class_('ESP32DAC', output.FloatOutput, cg.Component) + +CONFIG_SCHEMA = output.FLOAT_OUTPUT_SCHEMA.extend({ + cv.Required(CONF_ID): cv.declare_id(ESP32DAC), + cv.Required(CONF_PIN): cv.All(pins.internal_gpio_output_pin_schema, valid_dac_pin), +}).extend(cv.COMPONENT_SCHEMA) + + +def to_code(config): + var = cg.new_Pvariable(config[CONF_ID]) + yield cg.register_component(var, config) + yield output.register_output(var, config) + + pin = yield cg.gpio_pin_expression(config[CONF_PIN]) + cg.add(var.set_pin(pin)) diff --git a/tests/test1.yaml b/tests/test1.yaml index 31da80ac36..6e63e8de31 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -1030,6 +1030,9 @@ output: id: dimmer1 gate_pin: GPIO5 zero_cross_pin: GPIO26 + - platform: esp32_dac + pin: GPIO25 + id: dac_output light: - platform: binary @@ -1358,6 +1361,12 @@ switch: - output.set_level: id: gpio_19 level: !lambda 'return 0.5;' + - output.set_level: + id: dac_output + level: 50% + - output.set_level: + id: dac_output + level: !lambda 'return 0.5;' turn_off_action: - switch.turn_on: living_room_lights_off restore_state: False