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

Remove automatic interrupt attachment #30

Merged
merged 8 commits into from
Jan 29, 2024
Merged

Remove automatic interrupt attachment #30

merged 8 commits into from
Jan 29, 2024

Conversation

dmadison
Copy link
Owner

In order to read servo signals, the library attaches an interrupt handler to an interrupt-capable pin that fires whenever the pin changes state. The library reads the rising/falling edges and calculates the time elapsed between the two to find the length of the servo pulses.

On some platforms, notably the Uno R4 (#28) and ESP32 (#19) the interrupt request (IRQ) table is not set up until the main() function is entered. The version 1 library idiom, following Arduino-y convention, is to declare ServoInputPin objects as globals. These objects call the interrupt handler during the constructor to apply the function hook. Because the request table is not setup until main(), these calls fail and the library will not work until the user tries to attach the interrupt themselves.

This PR removes this call to attach the interrupt in the constructor, and instead requires the user do it themselves before the library can begin parsing signals. This is a breaking change to the API, and necessitates a new major library version.

Because of that, I've taken the opportunity to make a few other breaking changes:

  • The library attachInterrupt() and detachInterrupt() functions have been refactored to simply attach() and detach(). This matches the syntax of the Servo library, and should fix issues with platforms that use macros instead of global functions for their interrupt handler (Compilation Issue on Arduino Nano ESP32 #27).
  • All boolean types have been refactored to simply bool, which is the proper C type

Since the user is now responsible for interrupt attachment/detachment, I've also added reference counting that will automatically detach the interrupt whenever no more class instances for a given pin exist.

Moving away from Arduino-ness conventions
Having the class functions share the name of the global function was good for intuitiveness, but causes issues on platforms that implement the global 'attachInterrupt' as something other than a function (see #27).

Removing 'Interrupt' from the name also makes these functions match their counterparts in the Servo library.
This was causing issues on certain platforms where the platform's interrupt attachment system was not set up or available when the object was constructed in global scope (see #28).

Instead, the user must attach() the object themselves before they can read signals. The library examples have been updated accordingly.
Uses a reference counter to detach the platform interrupt if no class instances remain.
The flag indicates whether the interrupt is currently attached, and will prevent the attach/detach functions from calling the platform functions unless it's to change state. This guards against two edge cases.

The first is if the user calls 'attach()' multiple times and the platform does something unexpected when calling the 'attachInterrupt()' function repeatedly. It shouldn't, but you never know and it's something outside of our control. Better to play it safe.

The second, and even more of an edge case, is if the user defines their own interrupt for the pin (not using the library 'attach()' function), and the last class instance is deallocated. This avoids the destructor automatically calling 'detach()' and removing the user's interrupt function. I can't imagine that occuring in practice, but it's a potential pitfall nonetheless.
This is now required for the library to work (c8d7925).
@dmadison dmadison merged commit 3cbc406 into master Jan 29, 2024
6 checks passed
@dmadison dmadison deleted the v2 branch January 29, 2024 16:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant