Skip to content

Commit 4694fb9

Browse files
napierajjValdron
authored andcommitted
Add support for ESP32 DAC (esphome#1071)
* Add support for ESP32 onboard DAC * Newlines * Tests
1 parent 2ef1267 commit 4694fb9

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed

esphome/components/esp32_dac/__init__.py

Whitespace-only changes.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "esp32_dac.h"
2+
#include "esphome/core/log.h"
3+
#include "esphome/core/helpers.h"
4+
5+
#ifdef ARDUINO_ARCH_ESP32
6+
7+
#include <esp32-hal-dac.h>
8+
9+
namespace esphome {
10+
namespace esp32_dac {
11+
12+
static const char *TAG = "esp32_dac";
13+
14+
void ESP32DAC::setup() {
15+
ESP_LOGCONFIG(TAG, "Setting up ESP32 DAC Output...");
16+
this->pin_->setup();
17+
this->turn_off();
18+
}
19+
20+
void ESP32DAC::dump_config() {
21+
ESP_LOGCONFIG(TAG, "ESP32 DAC:");
22+
LOG_PIN(" Pin: ", this->pin_);
23+
LOG_FLOAT_OUTPUT(this);
24+
}
25+
26+
void ESP32DAC::write_state(float state) {
27+
if (this->pin_->is_inverted())
28+
state = 1.0f - state;
29+
30+
state = state * 255;
31+
dacWrite(this->pin_->get_pin(), state);
32+
}
33+
34+
} // namespace esp32_dac
35+
} // namespace esphome
36+
37+
#endif
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#pragma once
2+
3+
#include "esphome/core/component.h"
4+
#include "esphome/core/esphal.h"
5+
#include "esphome/core/automation.h"
6+
#include "esphome/components/output/float_output.h"
7+
8+
#ifdef ARDUINO_ARCH_ESP32
9+
10+
namespace esphome {
11+
namespace esp32_dac {
12+
13+
class ESP32DAC : public output::FloatOutput, public Component {
14+
public:
15+
void set_pin(GPIOPin *pin) { pin_ = pin; }
16+
17+
/// Initialize pin
18+
void setup() override;
19+
void dump_config() override;
20+
/// HARDWARE setup_priority
21+
float get_setup_priority() const override { return setup_priority::HARDWARE; }
22+
23+
protected:
24+
void write_state(float state) override;
25+
26+
GPIOPin *pin_;
27+
};
28+
29+
} // namespace esp32_dac
30+
} // namespace esphome
31+
32+
#endif
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from esphome import pins
2+
from esphome.components import output
3+
import esphome.config_validation as cv
4+
import esphome.codegen as cg
5+
from esphome.const import CONF_ID, CONF_NUMBER, CONF_PIN, ESP_PLATFORM_ESP32
6+
7+
ESP_PLATFORMS = [ESP_PLATFORM_ESP32]
8+
9+
10+
def valid_dac_pin(value):
11+
num = value[CONF_NUMBER]
12+
cv.one_of(25, 26)(num)
13+
return value
14+
15+
16+
esp32_dac_ns = cg.esphome_ns.namespace('esp32_dac')
17+
ESP32DAC = esp32_dac_ns.class_('ESP32DAC', output.FloatOutput, cg.Component)
18+
19+
CONFIG_SCHEMA = output.FLOAT_OUTPUT_SCHEMA.extend({
20+
cv.Required(CONF_ID): cv.declare_id(ESP32DAC),
21+
cv.Required(CONF_PIN): cv.All(pins.internal_gpio_output_pin_schema, valid_dac_pin),
22+
}).extend(cv.COMPONENT_SCHEMA)
23+
24+
25+
def to_code(config):
26+
var = cg.new_Pvariable(config[CONF_ID])
27+
yield cg.register_component(var, config)
28+
yield output.register_output(var, config)
29+
30+
pin = yield cg.gpio_pin_expression(config[CONF_PIN])
31+
cg.add(var.set_pin(pin))

tests/test1.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,9 @@ output:
10301030
id: dimmer1
10311031
gate_pin: GPIO5
10321032
zero_cross_pin: GPIO26
1033+
- platform: esp32_dac
1034+
pin: GPIO25
1035+
id: dac_output
10331036

10341037
light:
10351038
- platform: binary
@@ -1358,6 +1361,12 @@ switch:
13581361
- output.set_level:
13591362
id: gpio_19
13601363
level: !lambda 'return 0.5;'
1364+
- output.set_level:
1365+
id: dac_output
1366+
level: 50%
1367+
- output.set_level:
1368+
id: dac_output
1369+
level: !lambda 'return 0.5;'
13611370
turn_off_action:
13621371
- switch.turn_on: living_room_lights_off
13631372
restore_state: False

0 commit comments

Comments
 (0)