ESPHome 2026.5.0-dev
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 <driver/gpio.h>
6#include <esp_private/esp_clk.h>
7#include <hal/pcnt_ll.h>
8#endif
9
10namespace esphome {
11namespace pulse_counter {
12
13static const char *const TAG = "pulse_counter";
14
15const char *const EDGE_MODE_TO_STRING[] = {"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
34 switch (mode) {
36 break;
38 auto x = arg->counter + 1;
39 arg->counter = x;
40 } break;
42 auto x = arg->counter - 1;
43 arg->counter = x;
44 } break;
45 }
46}
47
49 this->pin = pin;
50 this->pin->setup();
51 this->isr_pin = this->pin->to_isr();
53 return true;
54}
55
59 this->last_value = counter;
60 return ret;
61}
62
63#ifdef HAS_PCNT
65 this->pin = pin;
66 this->pin->setup();
67
68 pcnt_unit_config_t unit_config = {
69 .low_limit = INT16_MIN,
70 .high_limit = INT16_MAX,
71 .flags = {.accum_count = true},
72 };
73 esp_err_t error = pcnt_new_unit(&unit_config, &this->pcnt_unit);
74 if (error != ESP_OK) {
75 ESP_LOGE(TAG, "Creating PCNT unit failed: %s", esp_err_to_name(error));
76 return false;
77 }
78
79 pcnt_chan_config_t chan_config = {
80 .edge_gpio_num = static_cast<gpio_num_t>(this->pin->get_pin()),
81 .level_gpio_num = GPIO_NUM_NC,
82 };
83 error = pcnt_new_channel(this->pcnt_unit, &chan_config, &this->pcnt_channel);
84 if (error != ESP_OK) {
85 ESP_LOGE(TAG, "Creating PCNT channel failed: %s", esp_err_to_name(error));
86 return false;
87 }
88
89 pcnt_channel_edge_action_t rising = PCNT_CHANNEL_EDGE_ACTION_HOLD;
90 pcnt_channel_edge_action_t falling = PCNT_CHANNEL_EDGE_ACTION_HOLD;
91 switch (this->rising_edge_mode) {
93 rising = PCNT_CHANNEL_EDGE_ACTION_HOLD;
94 break;
96 rising = PCNT_CHANNEL_EDGE_ACTION_INCREASE;
97 break;
99 rising = PCNT_CHANNEL_EDGE_ACTION_DECREASE;
100 break;
101 }
102 switch (this->falling_edge_mode) {
104 falling = PCNT_CHANNEL_EDGE_ACTION_HOLD;
105 break;
107 falling = PCNT_CHANNEL_EDGE_ACTION_INCREASE;
108 break;
110 falling = PCNT_CHANNEL_EDGE_ACTION_DECREASE;
111 break;
112 }
113
114 error = pcnt_channel_set_edge_action(this->pcnt_channel, rising, falling);
115 if (error != ESP_OK) {
116 ESP_LOGE(TAG, "Setting PCNT edge action failed: %s", esp_err_to_name(error));
117 return false;
118 }
119
120 if (this->filter_us != 0) {
121 uint32_t max_glitch_ns = PCNT_LL_MAX_GLITCH_WIDTH * 1000u / ((uint32_t) esp_clk_apb_freq() / 1000000u);
122 pcnt_glitch_filter_config_t filter_config = {
123 .max_glitch_ns = std::min(this->filter_us * 1000u, max_glitch_ns),
124 };
125 error = pcnt_unit_set_glitch_filter(this->pcnt_unit, &filter_config);
126 if (error != ESP_OK) {
127 ESP_LOGE(TAG, "Setting PCNT glitch filter failed: %s", esp_err_to_name(error));
128 return false;
129 }
130 }
131
132 error = pcnt_unit_add_watch_point(this->pcnt_unit, INT16_MIN);
133 if (error != ESP_OK) {
134 ESP_LOGE(TAG, "Adding PCNT low limit watch point failed: %s", esp_err_to_name(error));
135 return false;
136 }
137 error = pcnt_unit_add_watch_point(this->pcnt_unit, INT16_MAX);
138 if (error != ESP_OK) {
139 ESP_LOGE(TAG, "Adding PCNT high limit watch point failed: %s", esp_err_to_name(error));
140 return false;
141 }
142
143 error = pcnt_unit_enable(this->pcnt_unit);
144 if (error != ESP_OK) {
145 ESP_LOGE(TAG, "Enabling PCNT unit failed: %s", esp_err_to_name(error));
146 return false;
147 }
148 error = pcnt_unit_clear_count(this->pcnt_unit);
149 if (error != ESP_OK) {
150 ESP_LOGE(TAG, "Clearing PCNT unit failed: %s", esp_err_to_name(error));
151 return false;
152 }
153 error = pcnt_unit_start(this->pcnt_unit);
154 if (error != ESP_OK) {
155 ESP_LOGE(TAG, "Starting PCNT unit failed: %s", esp_err_to_name(error));
156 return false;
157 }
158 return true;
159}
160
162 int count;
163 pcnt_unit_get_count(this->pcnt_unit, &count);
164 pulse_counter_t ret = count - this->last_value;
165 this->last_value = count;
166 return ret;
167}
168#endif // HAS_PCNT
169
171 if (!this->storage_.pulse_counter_setup(this->pin_)) {
172 this->mark_failed();
173 return;
174 }
175}
176
178 this->current_total_ = pulses;
179 if (this->total_sensor_ != nullptr)
180 this->total_sensor_->publish_state(pulses);
181}
182
184 LOG_SENSOR("", "Pulse Counter", this);
185 LOG_PIN(" Pin: ", this->pin_);
186 ESP_LOGCONFIG(TAG,
187 " Rising Edge: %s\n"
188 " Falling Edge: %s\n"
189 " Filtering pulses shorter than %" PRIu32 " µs",
191 EDGE_MODE_TO_STRING[this->storage_.falling_edge_mode], this->storage_.filter_us);
192 LOG_UPDATE_INTERVAL(this);
193}
194
197 uint32_t now = millis();
198 if (this->last_time_ != 0) {
199 uint32_t interval = now - this->last_time_;
200 float value = (60000.0f * raw) / float(interval); // per minute
201 ESP_LOGD(TAG, "'%s': Retrieved counter: %0.2f pulses/min", this->get_name().c_str(), value);
202 this->publish_state(value);
203 }
204
205 if (this->total_sensor_ != nullptr) {
207 ESP_LOGD(TAG, "'%s': Total : %" PRIu32 " pulses", this->get_name().c_str(), current_total_);
209 }
210 this->last_time_ = now;
211}
212
213} // namespace pulse_counter
214} // namespace esphome
BedjetMode mode
BedJet operating mode.
uint8_t raw[35]
Definition bl0939.h:0
void mark_failed()
Mark this component as failed.
const StringRef & get_name() const
Definition entity_base.h:71
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:68
@ 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:29
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:26
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