pulse_counter_ulp: rename classes and remove filter

This commit is contained in:
brisk 2024-07-05 22:04:03 +09:30
parent 5fb117e0ea
commit 4517698322
4 changed files with 26 additions and 45 deletions

View file

@ -6,19 +6,19 @@
namespace esphome {
namespace pulse_counter {
namespace pulse_counter_ulp {
template<typename... Ts> class SetTotalPulsesAction : public Action<Ts...> {
public:
SetTotalPulsesAction(PulseCounterSensor *pulse_counter) : pulse_counter_(pulse_counter) {}
SetTotalPulsesAction(PulseCounterUlpSensor *pulse_counter) : pulse_counter_(pulse_counter) {}
TEMPLATABLE_VALUE(uint32_t, total_pulses)
void play(Ts... x) override { this->pulse_counter_->set_total_pulses(this->total_pulses_.value(x...)); }
protected:
PulseCounterSensor *pulse_counter_;
PulseCounterUlpSensor *pulse_counter_;
};
} // namespace pulse_counter
} // namespace pulse_counter_ulp
} // namespace esphome

View file

@ -7,7 +7,7 @@
#include <esp_sleep.h>
namespace esphome {
namespace pulse_counter {
namespace pulse_counter_ulp {
static const char *const TAG = "pulse_counter_ulp";
@ -98,8 +98,6 @@ bool UlpPulseCounterStorage::pulse_counter_setup(InternalGPIOPin *pin) {
return false;
}
// TODO Support Filter
return true;
}
@ -112,7 +110,7 @@ pulse_counter_t UlpPulseCounterStorage::read_raw_value() {
/* === END ULP ===*/
void PulseCounterSensor::setup() {
void PulseCounterUlpSensor::setup() {
ESP_LOGCONFIG(TAG, "Setting up pulse counter '%s'...", this->name_.c_str());
if (!this->storage_.pulse_counter_setup(this->pin_)) {
this->mark_failed();
@ -128,21 +126,20 @@ void PulseCounterSensor::setup() {
#endif
}
void PulseCounterSensor::set_total_pulses(uint32_t pulses) {
void PulseCounterUlpSensor::set_total_pulses(uint32_t pulses) {
this->current_total_ = pulses;
this->total_sensor_->publish_state(pulses);
}
void PulseCounterSensor::dump_config() {
void PulseCounterUlpSensor::dump_config() {
LOG_SENSOR("", "Pulse Counter", this);
LOG_PIN(" Pin: ", this->pin_);
ESP_LOGCONFIG(TAG, " Rising Edge: %s", EDGE_MODE_TO_STRING[this->storage_.rising_edge_mode]);
ESP_LOGCONFIG(TAG, " Falling Edge: %s", EDGE_MODE_TO_STRING[this->storage_.falling_edge_mode]);
ESP_LOGCONFIG(TAG, " Filtering pulses shorter than %" PRIu32 " µs", this->storage_.filter_us);
LOG_UPDATE_INTERVAL(this);
}
void PulseCounterSensor::update() {
void PulseCounterUlpSensor::update() {
#ifdef CONF_USE_TIME
// Can't clear the pulse count until we can report the rate, so there's
// nothing to do until the time is synchronized
@ -178,5 +175,5 @@ void PulseCounterSensor::update() {
#endif
}
} // namespace pulse_counter
} // namespace pulse_counter_ulp
} // namespace esphome

View file

@ -11,7 +11,7 @@
#include <cinttypes>
namespace esphome {
namespace pulse_counter {
namespace pulse_counter_ulp {
enum PulseCounterCountMode {
PULSE_COUNTER_DISABLE = 0,
@ -22,30 +22,23 @@ enum PulseCounterCountMode {
using pulse_counter_t = int32_t;
using timestamp_t = int64_t;
struct PulseCounterStorageBase {
virtual bool pulse_counter_setup(InternalGPIOPin *pin) = 0;
virtual pulse_counter_t read_raw_value() = 0;
struct UlpPulseCounterStorage {
bool pulse_counter_setup(InternalGPIOPin *pin);
pulse_counter_t read_raw_value();
InternalGPIOPin *pin;
PulseCounterCountMode rising_edge_mode{PULSE_COUNTER_INCREMENT};
PulseCounterCountMode falling_edge_mode{PULSE_COUNTER_DISABLE};
uint32_t filter_us{0};
pulse_counter_t last_value{0};
};
struct UlpPulseCounterStorage : public PulseCounterStorageBase {
bool pulse_counter_setup(InternalGPIOPin *pin) override;
pulse_counter_t read_raw_value() override;
};
class PulseCounterSensor : public sensor::Sensor, public PollingComponent {
class PulseCounterUlpSensor : public sensor::Sensor, public PollingComponent {
public:
explicit PulseCounterSensor(PulseCounterStorageBase *storage) : storage_(*storage) {}
explicit PulseCounterUlpSensor() {}
void set_pin(InternalGPIOPin *pin) { pin_ = pin; }
void set_rising_edge_mode(PulseCounterCountMode mode) { storage_.rising_edge_mode = mode; }
void set_falling_edge_mode(PulseCounterCountMode mode) { storage_.falling_edge_mode = mode; }
void set_filter_us(uint32_t filter) { storage_.filter_us = filter; }
void set_total_sensor(sensor::Sensor *total_sensor) { total_sensor_ = total_sensor; }
#ifdef USE_TIME
void set_time_id(time::RealTimeClock *time_id) { time_id_ = time_id; }
@ -61,7 +54,7 @@ class PulseCounterSensor : public sensor::Sensor, public PollingComponent {
protected:
InternalGPIOPin *pin_;
PulseCounterStorageBase &storage_;
UlpPulseCounterStorage storage_;
timestamp_t last_time_{0};
uint32_t current_total_{0};
sensor::Sensor *total_sensor_{nullptr};
@ -73,5 +66,5 @@ class PulseCounterSensor : public sensor::Sensor, public PollingComponent {
#endif
};
} // namespace pulse_counter
} // namespace pulse_counter_ulp
} // namespace esphome

View file

@ -23,10 +23,8 @@ from esphome.const import (
)
from esphome.core import CORE
CONF_STORAGE_ID = "storage"
pulse_counter_ns = cg.esphome_ns.namespace("pulse_counter")
PulseCounterCountMode = pulse_counter_ns.enum("PulseCounterCountMode")
pulse_counter_ulp_ns = cg.esphome_ns.namespace("pulse_counter_ulp")
PulseCounterCountMode = pulse_counter_ulp_ns.enum("PulseCounterCountMode")
COUNT_MODES = {
"DISABLE": PulseCounterCountMode.PULSE_COUNTER_DISABLE,
"INCREMENT": PulseCounterCountMode.PULSE_COUNTER_INCREMENT,
@ -35,11 +33,11 @@ COUNT_MODES = {
COUNT_MODE_SCHEMA = cv.enum(COUNT_MODES, upper=True)
PulseCounterSensor = pulse_counter_ns.class_(
"PulseCounterSensor", sensor.Sensor, cg.PollingComponent
PulseCounterUlpSensor = pulse_counter_ulp_ns.class_(
"PulseCounterUlpSensor", sensor.Sensor, cg.PollingComponent
)
SetTotalPulsesAction = pulse_counter_ns.class_(
SetTotalPulsesAction = pulse_counter_ulp_ns.class_(
"SetTotalPulsesAction", automation.Action
)
@ -66,7 +64,7 @@ def validate_count_mode(value):
CONFIG_SCHEMA = cv.All(
sensor.sensor_schema(
PulseCounterSensor,
PulseCounterUlpSensor,
unit_of_measurement=UNIT_PULSES_PER_MINUTE,
icon=ICON_PULSE,
accuracy_decimals=2,
@ -96,9 +94,6 @@ CONFIG_SCHEMA = cv.All(
accuracy_decimals=0,
state_class=STATE_CLASS_TOTAL_INCREASING,
),
cv.GenerateID(CONF_STORAGE_ID): cv.declare_id(
"pulse_counter::PulseCounterStorageBase"
),
cv.Optional(CONF_TIME_ID): cv.use_id(time.RealTimeClock),
},
)
@ -107,10 +102,6 @@ CONFIG_SCHEMA = cv.All(
async def to_code(config):
storage = cg.Pvariable(
config[CONF_STORAGE_ID],
cg.RawExpression("new pulse_counter::UlpPulseCounterStorage()"),
)
esp32.add_extra_build_file(
"src/CMakeLists.txt",
os.path.join(os.path.dirname(__file__), "CMakeLists.txt"),
@ -123,7 +114,7 @@ async def to_code(config):
esp32.add_idf_sdkconfig_option("CONFIG_ULP_COPROC_ENABLED", True)
esp32.add_idf_sdkconfig_option("CONFIG_ULP_COPROC_TYPE_FSM", True)
esp32.add_idf_sdkconfig_option("CONFIG_ULP_COPROC_RESERVE_MEM", 1024)
var = await sensor.new_sensor(config, storage)
var = await sensor.new_sensor(config)
await cg.register_component(var, config)
pin = await cg.gpio_pin_expression(config[CONF_PIN])
@ -147,7 +138,7 @@ async def to_code(config):
SetTotalPulsesAction,
cv.Schema(
{
cv.Required(CONF_ID): cv.use_id(PulseCounterSensor),
cv.Required(CONF_ID): cv.use_id(PulseCounterUlpSensor),
cv.Required(CONF_VALUE): cv.templatable(cv.uint32_t),
}
),