Add CAL and new counter code

This implements the calibration routine and a new algorithm for decoding
the quadrature signals.
It also combines the main loop flags into a single variable.

TODO: Document calibration procedure.