From 349dc506cebe2617bf93393e821838218d7d6e17 Mon Sep 17 00:00:00 2001 From: brisk Date: Sat, 23 Mar 2024 20:54:30 +1030 Subject: [PATCH] pulse_counter: Add custom CMakeLists when using use_ulp This is necessary to compile the ULP program in ESP-IDF --- esphome/components/pulse_counter/.gitignore | 1 + .../components/pulse_counter/CMakeLists.txt | 3 +++ esphome/components/pulse_counter/sensor.py | 19 +++++++++++++++++-- 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 esphome/components/pulse_counter/.gitignore create mode 100644 esphome/components/pulse_counter/CMakeLists.txt diff --git a/esphome/components/pulse_counter/.gitignore b/esphome/components/pulse_counter/.gitignore new file mode 100644 index 0000000000..f23dc1dffb --- /dev/null +++ b/esphome/components/pulse_counter/.gitignore @@ -0,0 +1 @@ +!CMakeLists.txt diff --git a/esphome/components/pulse_counter/CMakeLists.txt b/esphome/components/pulse_counter/CMakeLists.txt new file mode 100644 index 0000000000..28e2b97a1e --- /dev/null +++ b/esphome/components/pulse_counter/CMakeLists.txt @@ -0,0 +1,3 @@ +FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/src/*.*) + +idf_component_register(SRCS ${app_sources}) diff --git a/esphome/components/pulse_counter/sensor.py b/esphome/components/pulse_counter/sensor.py index 27364a34b3..7772f57cdc 100644 --- a/esphome/components/pulse_counter/sensor.py +++ b/esphome/components/pulse_counter/sensor.py @@ -1,7 +1,9 @@ +import os import esphome.codegen as cg import esphome.config_validation as cv from esphome import automation, pins from esphome.components import sensor +from esphome.components import esp32 from esphome.const import ( CONF_COUNT_MODE, CONF_FALLING_EDGE, @@ -21,6 +23,7 @@ from esphome.const import ( from esphome.core import CORE CONF_USE_PCNT = "use_pcnt" +CONF_USE_ULP = "use_ulp" pulse_counter_ns = cg.esphome_ns.namespace("pulse_counter") PulseCounterCountMode = pulse_counter_ns.enum("PulseCounterCountMode") @@ -43,9 +46,11 @@ SetTotalPulsesAction = pulse_counter_ns.class_( def validate_internal_filter(value): use_pcnt = value.get(CONF_USE_PCNT) - if CORE.is_esp8266 and use_pcnt: + use_ulp = value.get(CONF_USE_ULP) + + if CORE.is_esp8266 and (use_pcnt or use_ulp): raise cv.Invalid( - "Using hardware PCNT is only available on ESP32", + "Using hardware pulse counters is only available on ESP32", [CONF_USE_PCNT], ) @@ -106,6 +111,7 @@ CONFIG_SCHEMA = cv.All( validate_count_mode, ), cv.SplitDefault(CONF_USE_PCNT, esp32=True): cv.boolean, + cv.Optional(CONF_USE_ULP): cv.boolean, cv.Optional( CONF_INTERNAL_FILTER, default="13us" ): cv.positive_time_period_microseconds, @@ -123,6 +129,15 @@ CONFIG_SCHEMA = cv.All( async def to_code(config): + if config.get(CONF_USE_ULP): + var = await sensor.new_sensor(config, 2) + else: + var = await sensor.new_sensor(config, config.get(CONF_USE_PCNT)) + if config.get(CONF_USE_ULP): + esp32.add_extra_build_file( + "src/CMakeLists.txt", + os.path.join(os.path.dirname(__file__), "CMakeLists.txt"), + ) var = await sensor.new_sensor(config, config.get(CONF_USE_PCNT)) await cg.register_component(var, config)