ESPHome 2026.2.3
Loading...
Searching...
No Matches
pulse_counter_sensor.cpp
Go to the documentation of this file.
2#include "esphome/core/log.h"
3
4#ifdef HAS_PCNT
5#include <esp_private/esp_clk.h>
6#include <hal/pcnt_ll.h>
7#endif
8
9namespace esphome {
10namespace pulse_counter {
11
12static const char *const TAG = "pulse_counter";
13
14const char *const EDGE_MODE_TO_STRING[] = {"DISABLE", "INCREMENT", "DECREMENT"};
15
16#ifdef HAS_PCNT
21#else // HAS_PCNT
22PulseCounterStorageBase *get_storage(bool) { return new BasicPulseCounterStorage; }
23#endif // HAS_PCNT
24
26 const uint32_t now = micros();
27 const bool discard = now - arg->last_pulse < arg->filter_us;
28 arg->last_pulse = now;
29 if (discard)
30 return;
31
33 switch (mode) {
35 break;
37 auto x = arg->counter + 1;
38 arg->counter = x;
39 } break;
41 auto x = arg->counter - 1;
42 arg->counter = x;
43 } break;
44 }
45}
46
48 this->pin = pin;
49 this->pin->setup();
50 this->isr_pin = this->pin->to_isr();
52 return true;
53}
54
58 this->last_value = counter;
59 return ret;
60}
61
62#ifdef HAS_PCNT
64 this->pin = pin;
65 this->pin->setup();
66
67 pcnt_unit_config_t unit_config = {
68 .low_limit = INT16_MIN,
69 .high_limit = INT16_MAX,
70 .flags = {.accum_count = true},
71 };
72 esp_err_t error = pcnt_new_unit(&unit_config, &this->pcnt_unit);
73 if (error != ESP_OK) {
74 ESP_LOGE(TAG, "Creating PCNT unit failed: %s", esp_err_to_name(error));
75 return false;
76 }
77
78 pcnt_chan_config_t chan_config = {
79 .edge_gpio_num = this->pin->get_pin(),
80 .level_gpio_num = -1,
81 };
82 error = pcnt_new_channel(this->pcnt_unit, &chan_config, &this->pcnt_channel);
83 if (error != ESP_OK) {
84 ESP_LOGE(TAG, "Creating PCNT channel failed: %s", esp_err_to_name(error));
85 return false;
86 }
87
88 pcnt_channel_edge_action_t rising = PCNT_CHANNEL_EDGE_ACTION_HOLD;
89 pcnt_channel_edge_action_t falling = PCNT_CHANNEL_EDGE_ACTION_HOLD;
90 switch (this->rising_edge_mode) {
92 rising = PCNT_CHANNEL_EDGE_ACTION_HOLD;
93 break;
95 rising = PCNT_CHANNEL_EDGE_ACTION_INCREASE;
96 break;
98 rising = PCNT_CHANNEL_EDGE_ACTION_DECREASE;
99 break;
100 }
101 switch (this->falling_edge_mode) {
103 falling = PCNT_CHANNEL_EDGE_ACTION_HOLD;
104 break;
106 falling = PCNT_CHANNEL_EDGE_ACTION_INCREASE;
107 break;
109 falling = PCNT_CHANNEL_EDGE_ACTION_DECREASE;
110 break;
111 }
112
113 error = pcnt_channel_set_edge_action(this->pcnt_channel, rising, falling);
114 if (error != ESP_OK) {
115 ESP_LOGE(TAG, "Setting PCNT edge action failed: %s", esp_err_to_name(error));
116 return false;
117 }
118
119 if (this->filter_us != 0) {
120 uint32_t max_glitch_ns = PCNT_LL_MAX_GLITCH_WIDTH * 1000u / ((uint32_t) esp_clk_apb_freq() / 1000000u);
121 pcnt_glitch_filter_config_t filter_config = {
122 .max_glitch_ns = std::min(this->filter_us * 1000u, max_glitch_ns),
123 };
124 error = pcnt_unit_set_glitch_filter(this->pcnt_unit, &filter_config);
125 if (error != ESP_OK) {
126 ESP_LOGE(TAG, "Setting PCNT glitch filter failed: %s", esp_err_to_name(error));
127 return false;
128 }
129 }
130
131 error = pcnt_unit_add_watch_point(this->pcnt_unit, INT16_MIN);
132 if (error != ESP_OK) {
133 ESP_LOGE(TAG, "Adding PCNT low limit watch point failed: %s", esp_err_to_name(error));
134 return false;
135 }
136 error = pcnt_unit_add_watch_point(this->pcnt_unit, INT16_MAX);
137 if (error != ESP_OK) {
138 ESP_LOGE(TAG, "Adding PCNT high limit watch point failed: %s", esp_err_to_name(error));
139 return false;
140 }
141
142 error = pcnt_unit_enable(this->pcnt_unit);
143 if (error != ESP_OK) {
144 ESP_LOGE(TAG, "Enabling PCNT unit failed: %s", esp_err_to_name(error));
145 return false;
146 }
147 error = pcnt_unit_clear_count(this->pcnt_unit);
148 if (error != ESP_OK) {
149 ESP_LOGE(TAG, "Clearing PCNT unit failed: %s", esp_err_to_name(error));
150 return false;
151 }
152 error = pcnt_unit_start(this->pcnt_unit);
153 if (error != ESP_OK) {
154 ESP_LOGE(TAG, "Starting PCNT unit failed: %s", esp_err_to_name(error));
155 return false;
156 }
157 return true;
158}
159
161 int count;
162 pcnt_unit_get_count(this->pcnt_unit, &count);
163 pulse_counter_t ret = count - this->last_value;
164 this->last_value = count;
165 return ret;
166}
167#endif // HAS_PCNT
168
170 if (!this->storage_.pulse_counter_setup(this->pin_)) {
171 this->mark_failed();
172 return;
173 }
174}
175
177 this->current_total_ = pulses;
178 this->total_sensor_->publish_state(pulses);
179}
180
182 LOG_SENSOR("", "Pulse Counter", this);
183 LOG_PIN(" Pin: ", this->pin_);
184 ESP_LOGCONFIG(TAG,
185 " Rising Edge: %s\n"
186 " Falling Edge: %s\n"
187 " Filtering pulses shorter than %" PRIu32 " µs",
189 EDGE_MODE_TO_STRING[this->storage_.falling_edge_mode], this->storage_.filter_us);
190 LOG_UPDATE_INTERVAL(this);
191}
192
195 uint32_t now = millis();
196 if (this->last_time_ != 0) {
197 uint32_t interval = now - this->last_time_;
198 float value = (60000.0f * raw) / float(interval); // per minute
199 ESP_LOGD(TAG, "'%s': Retrieved counter: %0.2f pulses/min", this->get_name().c_str(), value);
200 this->publish_state(value);
201 }
202
203 if (this->total_sensor_ != nullptr) {
205 ESP_LOGD(TAG, "'%s': Total : %" PRIu32 " pulses", this->get_name().c_str(), current_total_);
207 }
208 this->last_time_ = now;
209}
210
211} // namespace pulse_counter
212} // namespace esphome
BedjetMode mode
BedJet operating mode.
uint8_t raw[35]
Definition bl0939.h:0
virtual void mark_failed()
Mark this component as failed.
const StringRef & get_name() const
virtual void setup()=0
virtual uint8_t get_pin() const =0
void attach_interrupt(void(*func)(T *), T *arg, gpio::InterruptType type) const
Definition gpio.h:107
virtual ISRInternalGPIOPin to_isr() const =0
void setup() override
Unit of measurement is "pulses/min".
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
@ INTERRUPT_ANY_EDGE
Definition gpio.h:52
const char *const EDGE_MODE_TO_STRING[]
PulseCounterStorageBase * get_storage(bool hw_pcnt)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint32_t IRAM_ATTR HOT micros()
Definition core.cpp:27
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:25
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