ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
pulse_counter_sensor.cpp
Go to the documentation of this file.
2#include "esphome/core/log.h"
4
5#ifdef HAS_PCNT
6#include <driver/gpio.h>
7#include <esp_private/esp_clk.h>
8#include <hal/pcnt_ll.h>
9#endif
10
12
13ESPHOME_LOG_TAG(TAG, "pulse_counter");
14
15PROGMEM_STRING_TABLE(EdgeModeStrings, "DISABLE", "INCREMENT", "DECREMENT");
16
17#ifdef HAS_PCNT
22#else // HAS_PCNT
23PulseCounterStorageBase *get_storage(bool) { return new BasicPulseCounterStorage; }
24#endif // HAS_PCNT
25
27 const uint32_t now = micros();
28 const bool discard = now - arg->last_pulse < arg->filter_us;
29 arg->last_pulse = now;
30 if (discard)
31 return;
32
36 }
38 auto x = arg->counter + 1;
39 arg->counter = x;
40 } else if (mode == PULSE_COUNTER_DECREMENT) {
41 auto x = arg->counter - 1;
42 arg->counter = x;
43 }
44}
45
47 this->pin = pin;
48 this->pin->setup();
49 this->isr_pin = this->pin->to_isr();
50 const bool rising = this->rising_edge_mode != PULSE_COUNTER_DISABLE;
51 const bool falling = this->falling_edge_mode != PULSE_COUNTER_DISABLE;
52 if (!rising && !falling) {
53 return true;
54 }
56 if (this->filter_us == 0) {
57 if (!falling) {
59 this->fixed_mode = this->rising_edge_mode;
60 } else if (!rising) {
62 this->fixed_mode = this->falling_edge_mode;
63 } else if (this->rising_edge_mode == this->falling_edge_mode) {
64 this->fixed_mode = this->rising_edge_mode;
65 }
66 }
68 return true;
69}
70
77
78#ifdef HAS_PCNT
80 this->pin = pin;
81 this->pin->setup();
82
83 pcnt_unit_config_t unit_config = {
84 .low_limit = INT16_MIN,
85 .high_limit = INT16_MAX,
86 .flags = {.accum_count = true},
87 };
88 esp_err_t error = pcnt_new_unit(&unit_config, &this->pcnt_unit);
89 if (error != ESP_OK) {
90 ESP_LOGE(TAG, "Creating PCNT unit failed: %s", esp_err_to_name(error));
91 return false;
92 }
93
94 pcnt_chan_config_t chan_config = {
95 .edge_gpio_num = static_cast<gpio_num_t>(this->pin->get_pin()),
96 .level_gpio_num = GPIO_NUM_NC,
97 };
98 error = pcnt_new_channel(this->pcnt_unit, &chan_config, &this->pcnt_channel);
99 if (error != ESP_OK) {
100 ESP_LOGE(TAG, "Creating PCNT channel failed: %s", esp_err_to_name(error));
101 return false;
102 }
103
104 pcnt_channel_edge_action_t rising = PCNT_CHANNEL_EDGE_ACTION_HOLD;
105 pcnt_channel_edge_action_t falling = PCNT_CHANNEL_EDGE_ACTION_HOLD;
106 switch (this->rising_edge_mode) {
108 rising = PCNT_CHANNEL_EDGE_ACTION_HOLD;
109 break;
111 rising = PCNT_CHANNEL_EDGE_ACTION_INCREASE;
112 break;
114 rising = PCNT_CHANNEL_EDGE_ACTION_DECREASE;
115 break;
116 }
117 switch (this->falling_edge_mode) {
119 falling = PCNT_CHANNEL_EDGE_ACTION_HOLD;
120 break;
122 falling = PCNT_CHANNEL_EDGE_ACTION_INCREASE;
123 break;
125 falling = PCNT_CHANNEL_EDGE_ACTION_DECREASE;
126 break;
127 }
128
129 error = pcnt_channel_set_edge_action(this->pcnt_channel, rising, falling);
130 if (error != ESP_OK) {
131 ESP_LOGE(TAG, "Setting PCNT edge action failed: %s", esp_err_to_name(error));
132 return false;
133 }
134
135 if (this->filter_us != 0) {
136 uint32_t max_glitch_ns = PCNT_LL_MAX_GLITCH_WIDTH * 1000u / ((uint32_t) esp_clk_apb_freq() / 1000000u);
137 pcnt_glitch_filter_config_t filter_config = {
138 .max_glitch_ns = std::min(this->filter_us * 1000u, max_glitch_ns),
139 };
140 error = pcnt_unit_set_glitch_filter(this->pcnt_unit, &filter_config);
141 if (error != ESP_OK) {
142 ESP_LOGE(TAG, "Setting PCNT glitch filter failed: %s", esp_err_to_name(error));
143 return false;
144 }
145 }
146
147 error = pcnt_unit_add_watch_point(this->pcnt_unit, INT16_MIN);
148 if (error != ESP_OK) {
149 ESP_LOGE(TAG, "Adding PCNT low limit watch point failed: %s", esp_err_to_name(error));
150 return false;
151 }
152 error = pcnt_unit_add_watch_point(this->pcnt_unit, INT16_MAX);
153 if (error != ESP_OK) {
154 ESP_LOGE(TAG, "Adding PCNT high limit watch point failed: %s", esp_err_to_name(error));
155 return false;
156 }
157
158 error = pcnt_unit_enable(this->pcnt_unit);
159 if (error != ESP_OK) {
160 ESP_LOGE(TAG, "Enabling PCNT unit failed: %s", esp_err_to_name(error));
161 return false;
162 }
163 error = pcnt_unit_clear_count(this->pcnt_unit);
164 if (error != ESP_OK) {
165 ESP_LOGE(TAG, "Clearing PCNT unit failed: %s", esp_err_to_name(error));
166 return false;
167 }
168 error = pcnt_unit_start(this->pcnt_unit);
169 if (error != ESP_OK) {
170 ESP_LOGE(TAG, "Starting PCNT unit failed: %s", esp_err_to_name(error));
171 return false;
172 }
173 return true;
174}
175
177 int count;
178 pcnt_unit_get_count(this->pcnt_unit, &count);
180 this->last_value = count;
181 return ret;
182}
183#endif // HAS_PCNT
184
186 if (this->storage_.pin == nullptr) {
187 return true;
188 }
189 ESP_LOGE(TAG, "Edge modes and filter cannot change after setup");
190 return false;
191}
192
193void PulseCounterSensor::set_rising_edge_mode(PulseCounterCountMode mode) {
194 if (this->before_setup_()) {
196 }
197}
198
199void PulseCounterSensor::set_falling_edge_mode(PulseCounterCountMode mode) {
200 if (this->before_setup_()) {
202 }
203}
204
205void PulseCounterSensor::set_filter_us(uint32_t filter) {
206 if (this->before_setup_()) {
207 this->storage_.filter_us = filter;
208 }
209}
210
211void PulseCounterSensor::setup() {
212 if (!this->storage_.pulse_counter_setup(this->pin_)) {
213 this->mark_failed();
214 return;
215 }
216}
217
218void PulseCounterSensor::set_total_pulses(uint32_t pulses) {
219 this->current_total_ = pulses;
220 PUBLISH_STATE(this->total_sensor_, pulses);
221}
222
223void PulseCounterSensor::dump_config() {
224 LOG_SENSOR("", "Pulse Counter", this);
225 LOG_PIN(" Pin: ", this->pin_);
226 ESP_LOGCONFIG(TAG,
227 " Rising Edge: %s\n"
228 " Falling Edge: %s\n"
229 " Filtering pulses shorter than %" PRIu32 " µs",
230 LOG_STR_ARG(EdgeModeStrings::get_log_str(this->storage_.rising_edge_mode, 0)),
231 LOG_STR_ARG(EdgeModeStrings::get_log_str(this->storage_.falling_edge_mode, 0)),
232 this->storage_.filter_us);
233 LOG_UPDATE_INTERVAL(this);
234}
235
236void PulseCounterSensor::update() {
238 uint32_t now = millis();
239 if (this->last_time_ != 0) {
240 uint32_t interval = now - this->last_time_;
241 float value = (60000.0f * raw) / float(interval); // per minute
242 ESP_LOGD(TAG, "'%s': Retrieved counter: %0.2f pulses/min", LOG_STR_ARG(this->get_log_name()), value);
243 this->publish_state(value);
244 }
245
246 if (this->total_sensor_ != nullptr) {
248 ESP_LOGD(TAG, "'%s': Total : %" PRIu32 " pulses", LOG_STR_ARG(this->get_log_name()), current_total_);
249 this->total_sensor_->publish_state(current_total_);
250 }
251 this->last_time_ = now;
252}
253
254} // namespace esphome::pulse_counter
BedjetMode mode
BedJet operating mode.
uint8_t raw[35]
Definition bl0939.h:0
void mark_failed()
Mark this component as failed.
const LogString * get_log_name() const
Get the name for a "%s" log argument (LOG_STR_ARG).
void attach_interrupt(void(*func)(T *), T *arg, gpio::InterruptType type) const
Definition gpio.h:29
ISRInternalGPIOPin to_isr() const
Definition gpio.cpp:96
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
uint8_t type
unsigned count
Definition gif.cpp:51
int ret
@ INTERRUPT_FALLING_EDGE
Definition gpio_pin.h:53
@ INTERRUPT_RISING_EDGE
Definition gpio_pin.h:52
@ INTERRUPT_ANY_EDGE
Definition gpio_pin.h:54
ESPHOME_LOG_TAG(TAG, "pulse_counter")
PulseCounterStorageBase * get_storage(bool hw_pcnt)
PROGMEM_STRING_TABLE(EdgeModeStrings, "DISABLE", "INCREMENT", "DECREMENT")
uint32_t value
Definition hal.cpp:41
uint32_t IRAM_ATTR HOT micros()
Definition hal.cpp:43
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
static void uint32_t
bool pulse_counter_setup(InternalGPIOPin *pin) override
static void gpio_intr(BasicPulseCounterStorage *arg)
bool pulse_counter_setup(InternalGPIOPin *pin) override
virtual pulse_counter_t read_raw_value()=0
virtual bool pulse_counter_setup(InternalGPIOPin *pin)=0
uint16_t x
Definition tt21100.cpp:5