3#if ESP_ARDUINO_VERSION_MAJOR < 3
4#warning "The Pulse Counter API is only available in ESP32 Arduino Core 3.0.0 and later"
6#include "driver/pulse_cnt.h"
10DigitalInputPcntCounter::DigitalInputPcntCounter(uint8_t pin,
int pin_mode,
12 unsigned int read_delay,
16 read_delay_(read_delay),
17 interrupt_type_(interrupt_type) {
20 ESP_ERROR_CHECK(configurePcnt(interrupt_type));
25 ESP_ERROR_CHECK(pcnt_unit_get_count(pcnt_unit_, &count));
26 ESP_ERROR_CHECK(pcnt_unit_clear_count(pcnt_unit_));
33DigitalInputPcntCounter::~DigitalInputPcntCounter() {
35 ESP_ERROR_CHECK(pcnt_del_channel(pcnt_channel_));
38 ESP_ERROR_CHECK(pcnt_unit_stop(pcnt_unit_));
39 ESP_ERROR_CHECK(pcnt_unit_disable(pcnt_unit_));
40 ESP_ERROR_CHECK(pcnt_del_unit(pcnt_unit_));
44esp_err_t DigitalInputPcntCounter::configurePcnt(
int interrupt_mode) {
45 pcnt_unit_config_t unit_config = {
47 .high_limit = INT16_MAX,
50 ESP_ERROR_CHECK(pcnt_new_unit(&unit_config, &pcnt_unit_));
52 pcnt_chan_config_t chan_config = {.edge_gpio_num = pin_,
54 .flags = {.invert_edge_input =
false,
55 .invert_level_input =
false,
56 .virt_edge_io_level = 0,
57 .virt_level_io_level = 0,
58 .io_loop_back =
false}};
60 ESP_ERROR_CHECK(pcnt_new_channel(pcnt_unit_, &chan_config, &pcnt_channel_));
62 pcnt_channel_edge_action_t edge_action_pos =
63 (interrupt_mode == RISING || interrupt_mode == CHANGE)
64 ? PCNT_CHANNEL_EDGE_ACTION_INCREASE
65 : PCNT_CHANNEL_EDGE_ACTION_HOLD;
66 pcnt_channel_edge_action_t edge_action_neg =
67 (interrupt_mode == FALLING || interrupt_mode == CHANGE)
68 ? PCNT_CHANNEL_EDGE_ACTION_INCREASE
69 : PCNT_CHANNEL_EDGE_ACTION_HOLD;
70 ESP_ERROR_CHECK(pcnt_channel_set_edge_action(pcnt_channel_, edge_action_pos,
73 ESP_ERROR_CHECK(pcnt_unit_enable(pcnt_unit_));
74 ESP_ERROR_CHECK(pcnt_unit_clear_count(pcnt_unit_));
76 return pcnt_unit_start(pcnt_unit_);
79bool DigitalInputPcntCounter::to_json(JsonObject& root) {
80 root[
"read_delay"] = read_delay_;
84bool DigitalInputPcntCounter::from_json(
const JsonObject& config) {
85 String
const expected[] = {
"read_delay"};
86 for (
auto str : expected) {
87 if (!config[str].is<JsonVariant>()) {
91 read_delay_ = config[
"read_delay"];
95const String
ConfigSchema(
const DigitalInputPcntCounter& obj) {
96 return R
"###({"type":"object","properties":{"read_delay":{"title":"Read delay","type":"number","description":"The time, in milliseconds, between each read of the input"}} })###";
Sensor template class for any sensor producing actual values.
const String ConfigSchema(const SmartSwitchController &obj)
std::shared_ptr< reactesp::EventLoop > event_loop()
bool ConfigRequiresRestart(const HTTPServer &obj)