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

Conversation

edwardtfn
Copy link
Contributor

@edwardtfn edwardtfn commented Dec 13, 2024

What does this implement/fix?

Fixes warnings about deprecated use of ++ and -- operators on volatile-qualified types in the pulse counter component. Replaces volatile variables with proper interrupt-safe access using ESPHome's InterruptLock.

The warnings being fixed are:

src/esphome/components/pulse_counter/pulse_counter_sensor.cpp:32:12: warning: '++' expression of 'volatile'-qualified type is deprecated [-Wvolatile]
src/esphome/components/pulse_counter/pulse_counter_sensor.cpp:35:12: warning: '--' expression of 'volatile'-qualified type is deprecated [-Wvolatile]

Changes:

  • Keep the volatile qualifier as it's important for interrupt context
  • Replace increment/decrement operations with load/store approach

This change maintains the same functionality.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Code quality improvements to existing code or addition of tests
  • Other

Related issue or feature (if applicable):

Pull request in esphome-docs with documentation (if applicable): N/A

Test Environment

  • ESP32
  • ESP32 IDF
  • ESP8266
  • RP2040
  • BK72xx
  • RTL87xx

Example entry for config.yaml: N/A

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

If user exposed functionality or configuration variables are added/changed:

Fixes warnings about deprecated use of ++ and -- operators on volatile-qualified types in the pulse counter component. Replaces volatile variables with proper interrupt-safe access using ESPHome's InterruptLock.

The warnings being fixed are:
```log
src/esphome/components/pulse_counter/pulse_counter_sensor.cpp:32:12: warning: '++' expression of 'volatile'-qualified type is deprecated [-Wvolatile]
src/esphome/components/pulse_counter/pulse_counter_sensor.cpp:35:12: warning: '--' expression of 'volatile'-qualified type is deprecated [-Wvolatile]
```
Changes:
- Removed volatile qualifier from counter and last_pulse variables
- Added InterruptLock protection around critical sections
- Changed increment/decrement operators to += 1 and -= 1
- Split critical sections to minimize interrupt disable time

This change maintains the same functionality while providing proper interrupt safety across all supported platforms (ESP8266, ESP32 variants, and RP2040).
@codecov-commenter
Copy link

codecov-commenter commented Dec 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 53.83%. Comparing base (4d8b5ed) to head (3b3a7f8).
Report is 1787 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #7954      +/-   ##
==========================================
+ Coverage   53.70%   53.83%   +0.12%     
==========================================
  Files          50       50              
  Lines        9408     9820     +412     
  Branches     1654     1353     -301     
==========================================
+ Hits         5053     5287     +234     
- Misses       4056     4207     +151     
- Partials      299      326      +27     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

edwardtfn added a commit to edwardtfn/esphome that referenced this pull request Dec 13, 2024
Similar to the [pulse counter fix](esphome#7954), this PR addresses warnings about deprecated use of volatile-qualified variables in the rotary encoder component. Replaces the volatile counter with proper interrupt-safe access using ESPHome's InterruptLock.

The main issue is the volatile counter in RotaryEncoderSensorStore that's modified in interrupt context. This change provides proper synchronization while maintaining the same functionality across all supported platforms.

Changes:
- Removed volatile qualifier from counter in RotaryEncoderSensorStore
- Added InterruptLock protection around counter modifications in ISR
- Counter is already properly protected with InterruptLock in the main loop
- Changed increment/decrement operations to += 1 and -= 1

This ensures proper interrupt safety while fixing the volatile deprecation warnings. The change is compatible with all ESPHome supported platforms (ESP8266, ESP32 variants, and RP2040).

This is a similar change to esphome#7954 (pulse counter fix) but for the rotary encoder component.
@edwardtfn edwardtfn changed the title [pulse_counter] Fix volatile deprecation warnings using InterruptLock [pulse_counter] Fix volatile increment/decrement warnings Dec 13, 2024
@edwardtfn edwardtfn changed the title [pulse_counter] Fix volatile increment/decrement warnings [pulse_counter] Fix volatile increment/decrement deprecation warnings Dec 13, 2024
@edwardtfn edwardtfn marked this pull request as draft December 13, 2024 20:45
@edwardtfn edwardtfn marked this pull request as ready for review December 13, 2024 20:51
@clydebarrow clydebarrow merged commit ce7ff15 into esphome:dev Dec 13, 2024
25 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Dec 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BETA Warning on deprecated expression on pulse_counter
3 participants