Add support for ESP32 DAC (#1071)

* Add support for ESP32 onboard DAC

* Newlines

* Tests
This commit is contained in:
Oskar Napieraj 2020-05-24 05:06:55 +02:00 committed by GitHub
parent 39fbf9c56f
commit 4de44a99eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 109 additions and 0 deletions

View File

View File

@ -0,0 +1,37 @@
#include "esp32_dac.h"
#include "esphome/core/log.h"
#include "esphome/core/helpers.h"
#ifdef ARDUINO_ARCH_ESP32
#include <esp32-hal-dac.h>
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

View File

@ -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

View File

@ -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))

View File

@ -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