Hacker News new | ask | show | jobs
by CrLf 807 days ago
This project does use interrupts for the encoder. It uses the "Encoder" library mentioned in another comment.

https://www.pjrc.com/teensy/td_libs_Encoder.html

1 comments

I might be missing something but I think the author of this library missed a very simple implementation.

This library is hundreds of lines of assembly for implementing some jump table for all possible input combinaisons.

I propose instead to shift left the two inputs into a 8 bits accumulator. And then there are only 3 states to match. Inc, Dec, Invalid (happens if you jiggle the encoder in place, effectively doing halfinc/halfdec).

Something like that:

    sw = A << 1 | B
    if (state & 3 != sw)
        state = (state << 2) | sw
state == 0b00101101 for increment state == 0b01111000 for decrement Any other state is ignored.

If this code runs on interrupts, the conditional can be skipped if spurious interrupts are not possible.

Any switch bounce can be filtered either in hardware (capacitor + resistor), or in software by averaging over time.