Skip to content

Instantly share code, notes, and snippets.

@amachronic
Created May 21, 2021 11:51
Show Gist options
  • Save amachronic/38daba97f3b129c8411bf176401ded39 to your computer and use it in GitHub Desktop.
Save amachronic/38daba97f3b129c8411bf176401ded39 to your computer and use it in GitHub Desktop.
rockbox input handling using a "regex"
specification
- press UP and DOWN = scroll up or down
- short press of PLAY = play
- hold PLAY and press UP or DOWN = skip tracks
implementation
STATE1
Press(UP) -> Release(UP) -> fire up, goto STATE1
Press(DOWN) -> Release(DOWN) -> fire down, goto STATE1
Press(PLAY) -> Release(PLAY) -> fire play, goto STATE1
-> Timeout(hold_time) -> goto STATE2
STATE2
Release(PLAY) -> goto STATE1
Press(UP) -> Release(UP) -> fire skip prev, goto STATE2
Press(DOWN) -> Release(DOWN) -> fire skip next, goto STATE2
as a "regular expression"
Press(UP) Release(UP) {fire ACTION_SCROLL_UP}
| Press(DOWN) Release(DOWN) {fire ACTION_SCROLL_DOWN}
| Press(PLAY) ( Timeout(hold_time) ( Press(UP) Release(UP) {fire ACTION_SKIP_NEXT}
| Press(DOWN) Release(DOWN) {fire ACTION_SKIP_PREV}
)*
Release(PLAY)
| Release(PLAY) {fire ACTION_PLAY}
)
explanation
Press(X) = matches a key press
Release(x) = matches a key release
Timeout(x) = x time elapses since the previous matched input event
{action} = do the specified action if we get to this state
this "matches" the empty string
also note that there has to be implicit ignoring of non-matching input,
and implicitly the input "regex" runs in a loop.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment