Please add incremental encoder support at a low level (interrupts)

Issue #521 open
Russell Owen created an issue

Please add low-level interrupt-driven code to read one or more incremental encoders. My understanding, based on some research John did this morning, is that the RPi pico and MicroBit are probably best handled using a single interrupt function tied to all the relevant pins (we know those two boards can assign a given interrupt to many pins, but not N interrupts to N pins if N gets large).

Given use of a single interrupt routine, the algorithm will be the same one used in the Encoded DC Motors library in the current pilot release: instead of knowing which input pin changed state, the interrupt service routine must read every A and B pin and compare their state to the recorded most-recently-seen state in order to increment the appropriate counters.

I suggest the API be something like the following:

  • configure encoders num_encoders a_pins b_pins: configure encoders, where a_pins and b_pins are lists of length num_encoders.
  • get encoder index: get the current count of the specified encoder.
  • reset encoder index: reset the specified encoder count to 0.

Note: an alternative is to call the config function once for each encoder. That would allow more than one library to have encoders (though we don’t plan to support for than about 8), but that is an unlikely use case and it greatly complicates use of encoders, because the user has to keep track of which encoder has which index. The suggested design also means the configure method knows all pins that need interrupts, which might be helpful, depending on the interrupt API.

What happens to the encoders when a user pressed the Stop button? The microBlocks code that uses encoders must configure them again (I know of no way to persist such information), but my question is: are there implications for the existing interrupts that were already set for encoders? Does this encoder API have to explicitly clear existing interrupt configuration?

I am willing to do this work (though may need some advice on configuring interrupts).

Comments (3)

  1. Log in to comment