Remove automatic interrupt attachment #30
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 declareServoInputPin
objects as globals. These objects call the interrupt handler during the constructor to apply the function hook. Because the request table is not setup untilmain()
, 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:
attachInterrupt()
anddetachInterrupt()
functions have been refactored to simplyattach()
anddetach()
. This matches the syntax of theServo
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).boolean
types have been refactored to simplybool
, which is the proper C typeSince 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.