If you want to do it in pio, one of solutions would be some elaborate series of jump instructions to detect what changed and then in which direction. Every jump string would terminate with instruction sending direction to queue. Otherwise you could just output pins on every change into some array through dma and read that array on main processor and calculate current position in batched mode.