Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pulse_counter] Fix volatile increment/decrement deprecation warnings #7954

Merged
merged 5 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions esphome/components/pulse_counter/pulse_counter_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ PulseCounterStorageBase *get_storage(bool hw_pcnt) {
return (hw_pcnt ? (PulseCounterStorageBase *) (new HwPulseCounterStorage)
: (PulseCounterStorageBase *) (new BasicPulseCounterStorage));
}
#else
#else // HAS_PCNT
PulseCounterStorageBase *get_storage(bool) { return new BasicPulseCounterStorage; }
#endif
#endif // HAS_PCNT

void IRAM_ATTR BasicPulseCounterStorage::gpio_intr(BasicPulseCounterStorage *arg) {
const uint32_t now = micros();
Expand All @@ -28,21 +28,25 @@ void IRAM_ATTR BasicPulseCounterStorage::gpio_intr(BasicPulseCounterStorage *arg
switch (mode) {
case PULSE_COUNTER_DISABLE:
break;
case PULSE_COUNTER_INCREMENT:
arg->counter++;
break;
case PULSE_COUNTER_DECREMENT:
arg->counter--;
break;
case PULSE_COUNTER_INCREMENT: {
auto x = arg->counter + 1;
arg->counter = x;
} break;
case PULSE_COUNTER_DECREMENT: {
auto x = arg->counter - 1;
arg->counter = x;
} break;
}
}

bool BasicPulseCounterStorage::pulse_counter_setup(InternalGPIOPin *pin) {
this->pin = pin;
this->pin->setup();
this->isr_pin = this->pin->to_isr();
this->pin->attach_interrupt(BasicPulseCounterStorage::gpio_intr, this, gpio::INTERRUPT_ANY_EDGE);
return true;
}

pulse_counter_t BasicPulseCounterStorage::read_raw_value() {
pulse_counter_t counter = this->counter;
pulse_counter_t ret = counter - this->last_value;
Expand Down Expand Up @@ -141,14 +145,15 @@ bool HwPulseCounterStorage::pulse_counter_setup(InternalGPIOPin *pin) {
}
return true;
}

pulse_counter_t HwPulseCounterStorage::read_raw_value() {
pulse_counter_t counter;
pcnt_get_counter_value(this->pcnt_unit, &counter);
pulse_counter_t ret = counter - this->last_value;
this->last_value = counter;
return ret;
}
#endif
#endif // HAS_PCNT

void PulseCounterSensor::setup() {
ESP_LOGCONFIG(TAG, "Setting up pulse counter '%s'...", this->name_.c_str());
Expand Down
8 changes: 4 additions & 4 deletions esphome/components/pulse_counter/pulse_counter_sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#if defined(USE_ESP32) && !defined(USE_ESP32_VARIANT_ESP32C3)
#include <driver/pcnt.h>
#define HAS_PCNT
#endif
#endif // defined(USE_ESP32) && !defined(USE_ESP32_VARIANT_ESP32C3)

namespace esphome {
namespace pulse_counter {
Expand All @@ -22,9 +22,9 @@ enum PulseCounterCountMode {

#ifdef HAS_PCNT
using pulse_counter_t = int16_t;
#else
#else // HAS_PCNT
using pulse_counter_t = int32_t;
#endif
#endif // HAS_PCNT

struct PulseCounterStorageBase {
virtual bool pulse_counter_setup(InternalGPIOPin *pin) = 0;
Expand Down Expand Up @@ -57,7 +57,7 @@ struct HwPulseCounterStorage : public PulseCounterStorageBase {
pcnt_unit_t pcnt_unit;
pcnt_channel_t pcnt_channel;
};
#endif
#endif // HAS_PCNT

PulseCounterStorageBase *get_storage(bool hw_pcnt = false);

Expand Down
Loading