As featured in the XtendedGreg YouTube Live Stream: https://youtube.com/live/jklc2Aq9-1E
This Gist has the code that we used to get the direction and speed of the quadrature output of a motor encoder. Quadrature is a simple two sensor mechanism that relies upon the order of offset to make that determination. This is a simplified example that counts the number of rotations and increments the counter or decrements the counter depending upon which direction it is rotating. Every second, it will print the number of cycles since the last time it was read to the console.
The majority of this code was taken from Peter Hinch's Micropython Samples: https://github.com/peterhinch/micropython-samples/blob/master/encoders/encoder_rp2.py