Joypad input
joy_read(), the button and switch
bit masks, per-frame edge detection, and how to debounce and auto-repeat the
controls.1. Reading the joypad
There is no loadable joystick driver: input is a static library function (see
design/LYNX_JOY_SER_DESIGN.md). One call to
joy_read() returns all nine
inputs at once — the four d-pad directions, the two fire buttons, the two
option buttons and the Pause switch. No installation is required and there is
nothing that can fail; there is exactly one, always-present joypad.
joy_read() returns an unsigned value. The low byte is
the raw hardware button register and the Pause switch (a Mikey-side button) is
reported in bit 8, so the result does not fit in a char. A set
bit means the control is currently held down.
2. Buttons and switches
Every input has a bit mask defined in <lynx/lynx.h>:
JOY_UP_MASK 0x80 JOY_OPT1_MASK 0x08
JOY_DOWN_MASK 0x40 JOY_OPT2_MASK 0x04
JOY_LEFT_MASK 0x20 JOY_BTN_1_MASK 0x01
JOY_RIGHT_MASK 0x10 JOY_BTN_2_MASK 0x02
JOY_PAUSE_MASK 0x100
JOY_BTN_A_MASK and JOY_BTN_B_MASK are aliases for
JOY_BTN_1_MASK and JOY_BTN_2_MASK. The convenience macros
JOY_BTN_A(v), JOY_BTN_B(v), JOY_OPT1(v),
JOY_OPT2(v) and JOY_PAUSE(v) test the matching bit in a
joy_read() result and evaluate to non-zero when that control is
held.
- Directional pad
JOY_UP_MASK,JOY_DOWN_MASK,JOY_LEFT_MASKandJOY_RIGHT_MASKreport the four-way pad. The Lynx can be turned 180° for left-handed play (seegfx_flip); when you flip the display you should also swap up/down, left/right and the A/B buttons in software so the controls still match the picture.- Fire buttons A and B
The two action buttons on the face of the unit are
JOY_BTN_1_MASKandJOY_BTN_2_MASK, also spelledJOY_BTN_A_MASKandJOY_BTN_B_MASK.- Option 1 and Option 2
JOY_OPT1_MASKandJOY_OPT2_MASKare the two smaller buttons beside the screen — conventionally used for start/select, a pause menu or a secondary action.- Pause
JOY_PAUSE_MASKis the Pause switch. It is wired to Mikey rather than the main button register, which is why it lands in bit 8 and whyjoy_read()must return anunsignedrather than achar.
A minimal read loop tests masks directly — this reacts every frame the control is held:
unsigned now = joy_read(); if (now & JOY_LEFT_MASK) player_x--; if (now & JOY_RIGHT_MASK) player_x++; if (JOY_BTN_A(now)) fire(); if (JOY_PAUSE(now)) open_menu();
3. Edge detection
Testing the raw word tells you a control is held, which is true on every frame until it is released. For actions that should happen once per press — jump, confirm, toggle — compare this frame against the previous one and act only on the transition:
static unsigned prev = 0; unsigned now = joy_read(); unsigned pressed = now & ~prev; /* bits that went 0 -> 1 this frame */ unsigned released = prev & ~now; /* bits that went 1 -> 0 this frame */ prev = now; if (pressed & JOY_BTN_A_MASK) jump(); /* exactly once per tap */ if (released & JOY_BTN_B_MASK) release_charge();
Because joy_read() hands back every input in one word, a single
pressed/released pair covers the whole pad; mask it with
whichever controls a given piece of code cares about.
4. Debouncing and auto-repeat
joy_read() samples the hardware directly, so like any mechanical
contact the switches can bounce: one physical press or release may read
as a short burst of 1/0 transitions while the contact settles. Polling once per
frame (~60–75 Hz) already hides most bounce, because a frame is long
compared with the settling time — but menu navigation and anything that
reacts to a single edge is more robust with an explicit debounce that accepts a
new state only after it has held steady for a few consecutive reads:
#define DEBOUNCE_FRAMES 3
/* Return a debounced joypad word: a bit only changes in the result after the
raw input has reported the new value on DEBOUNCE_FRAMES reads in a row. */
static unsigned joy_debounced(void)
{
static unsigned stable = 0; /* last accepted state */
static unsigned candidate = 0; /* value currently being timed */
static unsigned char count = 0;
unsigned raw = joy_read();
if (raw != candidate) { /* input moved: restart the timer */
candidate = raw;
count = 0;
} else if (count < DEBOUNCE_FRAMES && ++count == DEBOUNCE_FRAMES) {
stable = candidate; /* held long enough: accept it */
}
return stable;
}
Feed the debounced word into the edge-detection step from section 3 to get clean, single-press events. For a direction held down in a menu, auto-repeat adds an initial delay followed by a steady repeat rate:
#define REPEAT_DELAY 20 /* frames held before auto-repeat begins */
#define REPEAT_RATE 4 /* frames between repeats */
struct repeat { unsigned char timer; };
/* Nonzero on a frame the action should fire: immediately on press, then every
REPEAT_RATE frames after an initial REPEAT_DELAY while the mask stays held. */
unsigned char joy_repeat(struct repeat *r, unsigned now, unsigned mask)
{
if (!(now & mask)) { r->timer = 0; return 0; } /* not held */
if (r->timer == 0) { r->timer = REPEAT_DELAY; return 1; } /* first press */
if (--r->timer == 0) { r->timer = REPEAT_RATE; return 1; } /* repeat tick */
return 0;
}
Keep one struct repeat per control you want to repeat, and drive a
menu cursor that steps once when the pad is tapped and then scrolls smoothly while
it is held.