mirror of
https://github.com/esphome/esphome.git
synced 2024-11-22 06:58:11 +01:00
Add support for ESP32 DAC (#1071)
* Add support for ESP32 onboard DAC * Newlines * Tests
This commit is contained in:
parent
39fbf9c56f
commit
4de44a99eb
5 changed files with 109 additions and 0 deletions
0
esphome/components/esp32_dac/__init__.py
Normal file
0
esphome/components/esp32_dac/__init__.py
Normal file
37
esphome/components/esp32_dac/esp32_dac.cpp
Normal file
37
esphome/components/esp32_dac/esp32_dac.cpp
Normal 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
|
32
esphome/components/esp32_dac/esp32_dac.h
Normal file
32
esphome/components/esp32_dac/esp32_dac.h
Normal 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
|
31
esphome/components/esp32_dac/output.py
Normal file
31
esphome/components/esp32_dac/output.py
Normal 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))
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue