From a5921032d7f2194bee0145c16b7b3c886ec5629e Mon Sep 17 00:00:00 2001 From: Dejvino Date: Wed, 11 Nov 2026 06:25:06 +0100 Subject: [PATCH] Trackball: poll every iteration, count all pulses, accelerate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- emulator/src/main.cpp | 23 ++++++++- examples/MissileCommand/MissileCommand.ino | 59 ++++++++++++++++++---- 2 files changed, 70 insertions(+), 12 deletions(-) diff --git a/emulator/src/main.cpp b/emulator/src/main.cpp index 44967b3..0bdd344 100644 --- a/emulator/src/main.cpp +++ b/emulator/src/main.cpp @@ -3,6 +3,7 @@ * * Input mapping: * Arrow keys trackball roll (hold to keep rolling) + * Mouse wheel trackball roll (each notch = a ball pulse burst) * Enter / right-click trackball press (fire) * Left-click screen tap (aim + fire) * A S D fire from left / center / right battery @@ -93,6 +94,8 @@ int main(int argc, char **argv) const uint8_t dirPin[4] = { BOARD_TBOX_G02, BOARD_TBOX_G01, BOARD_TBOX_G04, BOARD_TBOX_G03 }; bool held[4] = { false, false, false, false }; + // mouse wheel -> simulated ball pulses: pending pin toggles per direction + int wheelPulse[4] = { 0, 0, 0, 0 }; uint32_t frame = 0; bool running = true; @@ -149,6 +152,17 @@ int main(int argc, char **argv) break; } + case SDL_MOUSEWHEEL: { + // Each wheel notch = a burst of ball edges on the matching + // direction pin (emitted over the next few iterations). + const int perNotch = 6; + if (e.wheel.y > 0) wheelPulse[1] += perNotch * e.wheel.y; // up + if (e.wheel.y < 0) wheelPulse[3] += perNotch * -e.wheel.y; // down + if (e.wheel.x > 0) wheelPulse[0] += perNotch * e.wheel.x; // right + if (e.wheel.x < 0) wheelPulse[2] += perNotch * -e.wheel.x; // left + break; + } + case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: { int ww, wh; @@ -175,7 +189,14 @@ int main(int argc, char **argv) } // Simulate a rolling trackball: toggle the held quadrant pin so the - // game keeps seeing edges while the arrow key is down. + // game keeps seeing edges while the arrow key is down, and drain the + // mouse-wheel pulse queues (one toggle per iteration = one ball edge). + for (int d = 0; d < 4; d++) { + if (wheelPulse[d] > 0) { + emu::pinLevels[dirPin[d]] ^= 1; + wheelPulse[d]--; + } + } if (++frame % 3 == 0) { for (int d = 0; d < 4; d++) { if (held[d]) emu::pinLevels[dirPin[d]] ^= 1; diff --git a/examples/MissileCommand/MissileCommand.ino b/examples/MissileCommand/MissileCommand.ino index e24805a..3edc36b 100644 --- a/examples/MissileCommand/MissileCommand.ino +++ b/examples/MissileCommand/MissileCommand.ino @@ -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();