dmadison / ServoInput

Interrupt-driven servo decoder library for Arduino
GNU Lesser General Public License v3.0
22 stars 10 forks source link

Remove automatic interrupt attachment #30

Closed dmadison closed 9 months ago

dmadison commented 9 months ago

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:

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.