2026-05-07

FSM simulation — Rotary-dial pulse counter

This is the interactive simulator of the rotary-dial pulse-counting state machine that runs inside the USB HID Phone firmware. It animates the FSM in sync with a live timing diagram showing in_nsa, in_nsi, the active state and pulseCount, and is meant for teaching, documentation and screencasts.

The ISR body in the simulator is a direct port of InterruptHandlerHigh() from user/user_mouse.c — same four states, same transitions, same gating of the Leerlaufimpulse via in_nsa.

If the embedded view is too cramped: open the simulator in a new tab.

Scenarios

Value Description
dial1 Dial “1” — single pulse
dial2 Dial “2” — two pulses
dial3 Dial “3” — three pulses (default)
dial4 Dial “4” — four pulses
dial5 Dial “5” — five pulses
dial6 Dial “6” — six pulses
dial7 Dial “7” — seven pulses
dial8 Dial “8” — eight pulses
dial9 Dial “9” — nine pulses
dial0 Dial “0” — ten pulses (rotary-dial encoding for zero)
dial21 Dial “2” then “1” back-to-back, showing successive digits
manual Drive in_nsa / in_nsi yourself via the pills below the controls

Keyboard shortcuts

Key Action
Space Play / Pause
S or Step one tick
Step back
R Reset

URL parameters

The simulator can be deep-linked, which is handy for documentation or scripted recordings:

fsm_demo.html?scenario=dial3&speed=0.1&autoplay=1
Parameter Values Effect
scenario dial1dial9, dial0, dial21, manual Preload a scenario
speed 0.011.0 Real-time multiplier
autoplay 1 Start playing immediately
tick integer Seek to a specific tick on load (stills)

What you should see

  1. On ↑in_nsa the FSM jumps from STATE_INIT to STATE_DIAL and resets pulseCount to 0.
  2. Each ↓in_nsi while in STATE_DIAL flips us to STATE_PULSE_LOW and increments pulseCount.
  3. Each ↑in_nsi flips us back to STATE_DIAL.
  4. When ↓in_nsa arrives and pulseCount > 0, we go to STATE_DONE.
  5. The (simulated) processIO() then computes pulseCount + HID_KP_BASE, lights up the green digit N pill in the header, flashes the STATE_DONE → STATE_INIT edge, and we are armed for the next digit.
  6. Any extra in_nsi activity after ↓in_nsa (the Leerlaufimpulse) is ignored.

Back to the USB HID Phone project page.