Skip to content

Latest commit

 

History

History
27 lines (18 loc) · 3.89 KB

encoder.md

File metadata and controls

27 lines (18 loc) · 3.89 KB

Rotary encoder

Pins → Rotary encoder has three blocks: initialize a selected encoder variable with CLK/DT, read left rotation, read right rotation. Initialization is a normal statement, assigning the object directly to the selected variable. Both readers select that variable directly; there is no separate variable assignment or getter block needed. Each direction reader returns Boolean and consumes one pending full-cycle step. SW is a separate GPIO button; connect + to a compatible supply and − to ground. Use 3.3 V signal levels on 3.3 V boards and verify the module's pull-up circuit.

The component generates a small standalone Python class with machine.Pin inputs and pull-ups and registers both-edge callbacks for both pins. It does not add an event scheduler or require asyncio. Initialize once, retain the object, and reserve both pins for the encoder. Multiple encoders on distinct pins have independent state. It uses the port's default Pin.irq callback behavior, not an assumed hard IRQ mode. Supported on compatible MicroPython firmware with both-edge Pin IRQs; not stock micro:bit or SPIKE. Pin numbering and pull-up support are board-specific. Pin API, IRQ rules.

Each valid Gray-code transition contributes +1 or −1. Reverse bounce cancels; simultaneous two-bit jumps reset the partial cycle, and repeated levels are ignored. Four net legal transitions produce one event. CLK-leading falling sequence 11→01→00→10→11 is called right; swap CLK/DT if the physical direction differs. Encoders with half-cycle detents may produce one event per two detents. This is not timed switch debounce or a high-speed encoder guarantee. Missed IRQ transitions, long blocking operations or overflow may lose steps.

Each direction has 1023 pending slots; excess steps in that direction are dropped until read. The separate producer and consumer cursors are bounded and written by different sides. Only one program consumer per direction is supported; cross-direction order is not retained. The callback does not invoke user handlers, sleep, print, allocate new collections or await. Import rotary-encoder.pyblocks.json for a complete ESP32 example: initialize encoder on GPIO32/33, start position at 0, decrement/increment it in left/right condition events, print the new position, and check all events every 5 ms in an ordinary main loop. SW is not required for this example. Adapt the GPIO numbers to the board. A minimal direct-reader alternative:

encoder = _RotaryEncoder(32, 33)
while True:
    if encoder.left():
        print("left")
    if encoder.right():
        print("right")
    time.sleep_ms(5)

In asyncio use the same readers and yield with await asyncio.sleep_ms(5). Consuming readers can also go directly into the shared event condition socket. Do not wrap them in edge/debounce detectors: repeated true results are distinct events, not one held state.

Automated tests simulate pin transitions, bounce, invalid jumps, repeated steps, independent instances, queue bounds and generation. Actual direction, firmware callback latency and physical detents require board testing.

The generated-code integration tests also exercise 100-step bursts between checks, simultaneous left/right backlogs, consecutive true readings, new IRQs during a handler, cursor wrap and the asyncio start-events shortcut. Each check consumes at most one pending step per direction; at a 20 ms interval this is at most 50 steps/s per direction before handler overhead. Bursts can drain later; a sustained higher rate can fill the 1023-slot backlog, and software simulation cannot establish actual hardware IRQ capture reliability.

Earlier expression-shaped encoder initialization and reader sockets are not migrated; replace them with the statement and variable selectors.