Trackball: poll every iteration, count all pulses, accelerate

The crosshair crawled one 14px segment per tick because the ball pins
were sampled only once per game frame (33 ms) and just compared to the
previous level: a fast roll completed whole pulse cycles between reads,
so most edges were dropped and at most one step per frame was applied.

Now pollTrackball() runs in the loop() hot path (~1 ms apart) and counts
every edge into per-direction accumulators; applyTrackball() consumes
them once per frame, moving the cross steps * CROSS_STEP and capping at
CROSS_MAX_STEP (48 px/frame) — slow roll = fine steps, hard flick =
the cursor flies across the screen, arcade-style. Accumulation is
disabled outside PLAYING/BANNER and the first read is primed so the
power-up pin levels don't count as steps.

Emulator: mouse wheel now simulates ball pulses (6 edges per notch,
drained one toggle per iteration) for desktop testing.
This commit is contained in:
Dejvino
2026-11-11 06:25:06 +01:00
parent 0f85c75331
commit a5921032d7
2 changed files with 70 additions and 12 deletions
+48 -11
View File
@@ -47,7 +47,8 @@ static const int AMMO_MAX = 10;
static const int MAX_BOMBS = 20;
static const int MAX_ROCKETS = 8;
static const int MAX_EXPL = 12;
static const int CROSS_STEP = 14; // px per trackball step
static const int CROSS_STEP = 14; // px per trackball pulse
static const int CROSS_MAX_STEP = 48; // px/frame cap: fast rolls accelerate
static const float ROCKET_SPEED = 6.0f; // px / frame
static const float ROCKET_RADIUS = 24.0f;
static const uint32_t BANNER_MS = 1500;
@@ -451,15 +452,11 @@ static void clampCross()
if (cross.y > groundY - 3) cross.y = groundY - 3;
}
static void moveCross(int dir)
static void nudgeCross(int dx, int dy)
{
if (gameState != ST_PLAYING && gameState != ST_BANNER) return;
switch (dir) {
case 0: cross.x += CROSS_STEP; break; // right
case 1: cross.y -= CROSS_STEP; break; // up
case 2: cross.x -= CROSS_STEP; break; // left
case 3: cross.y += CROSS_STEP; break; // down
}
cross.x += dx;
cross.y += dy;
clampCross();
cross.moved = true;
}
@@ -1236,6 +1233,15 @@ static void drawTitle()
// ---------------------------------------------------------------------------
// Input
// ---------------------------------------------------------------------------
// Trackball step accumulators: filled by pollTrackball() (called every loop
// iteration) and consumed by applyTrackball() once per game frame.
static int tbSteps[4];
// The ball emits pulse trains on the direction pins while rolling; every
// edge is one ball step. This runs in the loop() hot path (~1 ms apart) —
// at the old once-per-frame (33 ms) rate a fast roll completed whole pulse
// cycles between two reads, so most edges were dropped and the cross
// crawled across the screen one segment per tick.
static void pollTrackball()
{
// Quadrant pins + center click, per UnitTest's mouse mapping:
@@ -1249,8 +1255,17 @@ static void pollTrackball()
cur[i] = digitalRead(tbPins[i]);
}
for (int i = 0; i < 4; i++) {
if (cur[i] != last[i]) moveCross(i);
static bool primed = false;
if (!primed) { // don't count the power-up pin levels as edges
primed = true;
for (int i = 0; i < 5; i++) last[i] = cur[i];
return;
}
if (gameState == ST_PLAYING || gameState == ST_BANNER) {
for (int i = 0; i < 4; i++) {
if (cur[i] != last[i]) tbSteps[i]++; // count every edge
}
}
bool pressed = (!cur[4] && last[4]); // falling edge = click
@@ -1276,6 +1291,26 @@ static void pollTrackball()
}
}
// Consume the pulses accumulated since the last frame: a slow roll moves
// the cross in fine steps, a fast roll accelerates it (capped per frame,
// like the arcade trackball where a hard flick sends the cursor flying).
static void applyTrackball()
{
for (int i = 0; i < 4; i++) {
int n = tbSteps[i];
if (!n) continue;
tbSteps[i] = 0;
int px = n * CROSS_STEP;
if (px > CROSS_MAX_STEP) px = CROSS_MAX_STEP;
switch (i) {
case 0: nudgeCross(+px, 0); break; // right
case 1: nudgeCross(0, -px); break; // up
case 2: nudgeCross(-px, 0); break; // left
case 3: nudgeCross(0, +px); break; // down
}
}
}
static void pollTouch()
{
if (!touchAvail) return;
@@ -1523,6 +1558,8 @@ void setup()
void loop()
{
pollTrackball(); // hot path: sample the ball every iteration (~1 ms)
static uint32_t nextFrame = 0;
uint32_t now = millis();
if ((int32_t)(now - nextFrame) < 0) {
@@ -1532,7 +1569,7 @@ void loop()
nextFrame = now + FRAME_MS;
frameCount++;
pollTrackball();
applyTrackball();
pollTouch();
pollKeyboard();