Trackball: ISR edge capture + momentum model; extract difficulty tunables
- Trackball edges are counted by IRAM_ATTR ISRs (4 quadrant pins + BOOT click), so nothing is lost while loop() stalls in long TFT draws. The old ~1 ms polling hot path is gone. - Debounce is handled by the momentum model itself: edge counts are impulses on a heavy ball, friction bleeds velocity off each frame. Contact bounce decays into a sub-pixel nudge and never reverses motion, so no time-gate is needed. Slow rolls glide in 1-px steps via sub-pixel accumulators; hard flicks stay capped. - Per-platform sensitivity: device gets 4x impulse per edge (its real pulse rate is far lower than the emulator's synthetic streams) plus lower friction; selected via the shim's EMULATOR define. - Emulator shim: attachInterrupt/detachInterrupt emulation; handlers are dispatched from the host loop when pin levels change. - MissileCommand: extract arcade difficulty scaling into a tunables block (wave quota, bomb speed, spawn pacing, smart-bomb chance, craft speeds/drop rates, scoring, score multiplier, bonus-city interval, mercy rules). Formulas unchanged, only symboled.
This commit is contained in:
@@ -216,6 +216,9 @@ int main(int argc, char **argv)
|
||||
if (frame % 75 == 0) emu::keyboard.push_back('s');
|
||||
}
|
||||
|
||||
// fire any emulated interrupts triggered by the pin changes above
|
||||
emu::dispatchIsrs();
|
||||
|
||||
loop();
|
||||
|
||||
if (emuGfx::dirty) {
|
||||
|
||||
@@ -4,6 +4,11 @@
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
// Platform marker: the sketch picks device vs emulator trackball sensitivity
|
||||
// on this (the emulator feeds pulses from held keys / mouse wheel, which is
|
||||
// a much richer edge rate than a real roll on the device).
|
||||
#define EMULATOR 1
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <cstdio>
|
||||
@@ -98,6 +103,56 @@ inline void pinMode(int, int) {}
|
||||
inline int digitalRead(int pin) { return emu::pinLevels[pin & 63]; }
|
||||
inline void digitalWrite(int, int) {}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Interrupts (emulated: the host loop calls emu::dispatchIsrs() whenever pin
|
||||
// levels may have changed, which fires the matching handlers — edge counting
|
||||
// is one edge per dispatch, matching the emulator's one toggle per iteration)
|
||||
// ---------------------------------------------------------------------------
|
||||
#define FALLING 2
|
||||
#define RISING 3
|
||||
#define CHANGE 4
|
||||
#define IRAM_ATTR
|
||||
#define digitalPinToInterrupt(p) (p)
|
||||
|
||||
typedef void (*isr_handler_t)();
|
||||
|
||||
namespace emu {
|
||||
struct IsrEntry {
|
||||
isr_handler_t fn = nullptr;
|
||||
int mode = 0;
|
||||
int last = HIGH; // level seen at the previous dispatch
|
||||
};
|
||||
inline IsrEntry isrs[64];
|
||||
|
||||
inline void dispatchIsrs()
|
||||
{
|
||||
for (int p = 0; p < 64; p++) {
|
||||
IsrEntry &e = isrs[p];
|
||||
if (!e.fn) continue;
|
||||
int cur = pinLevels[p];
|
||||
bool edge =
|
||||
(e.mode == FALLING && e.last == HIGH && cur == LOW) ||
|
||||
(e.mode == RISING && e.last == LOW && cur == HIGH) ||
|
||||
(e.mode == CHANGE && e.last != cur);
|
||||
e.last = cur;
|
||||
if (edge) e.fn();
|
||||
}
|
||||
}
|
||||
} // namespace emu
|
||||
|
||||
inline void attachInterrupt(int pin, isr_handler_t fn, int mode)
|
||||
{
|
||||
emu::IsrEntry &e = emu::isrs[pin & 63];
|
||||
e.fn = fn;
|
||||
e.mode = mode;
|
||||
e.last = emu::pinLevels[pin & 63]; // no phantom edge at attach time
|
||||
}
|
||||
|
||||
inline void detachInterrupt(int pin)
|
||||
{
|
||||
emu::isrs[pin & 63].fn = nullptr;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// FreeRTOS-ish primitives (queues + detached threads)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@@ -48,7 +48,6 @@ 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 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;
|
||||
@@ -64,6 +63,52 @@ static const int MAX_CRAFTS = 2; // bombers + satellites
|
||||
static const float WIGGLE_AMP = 2.2f; // smart bomb wiggle amplitude (px)
|
||||
static const float WIGGLE_FREQ = 0.07f; // wave number (wavelength ~90 px)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Difficulty tunables (arcade-style wave scaling)
|
||||
// ---------------------------------------------------------------------------
|
||||
// Per-wave bomb quota: bombs aimed at the ground each wave.
|
||||
static const int QUOTA_BASE = 6;
|
||||
static const int QUOTA_PER_WAVE = 2;
|
||||
static const int QUOTA_MAX = 24;
|
||||
// Bomb fall speed (px/frame): starts slow, gains per wave, capped.
|
||||
static const float BOMB_SPEED_BASE = 0.3f;
|
||||
static const float BOMB_SPEED_PER_WAVE = 0.1f;
|
||||
static const float BOMB_SPEED_MAX = 2.0f;
|
||||
// Ground-spawn pacing (ms between spawns) tightens per wave.
|
||||
static const int SPAWN_MS_BASE = 1800;
|
||||
static const int SPAWN_MS_PER_WAVE = 70;
|
||||
static const int SPAWN_MS_MIN = 500;
|
||||
// Smart bombs: first appear on wave N, chance grows with the wave.
|
||||
static const int SMART_START_WAVE = 3;
|
||||
static const int SMART_CHANCE_BASE = 2; // % on the first smart wave
|
||||
static const int SMART_CHANCE_PER_WAVE = 1; // +% per wave
|
||||
static const int SMART_CHANCE_MAX = 28; // % cap
|
||||
static const float SMART_SPEED_MUL = 1.25f; // smart bombs fall faster
|
||||
// Crafts: bombers from wave N always, satellites later by chance.
|
||||
static const int BOMBER_START_WAVE = 2;
|
||||
static const int SAT_START_WAVE = 5;
|
||||
static const int SAT_CHANCE = 45; // % per wave
|
||||
static const int BOMBER_SPEED = 2; // px / frame
|
||||
static const int SAT_SPEED = 1;
|
||||
static const uint32_t BOMBER_DROP_MS = 1400; // between bomb drops
|
||||
static const uint32_t SAT_DROP_MS = 2200;
|
||||
static const uint32_t CRAFT_FIRST_DROP_MS = 600; // after entering the screen
|
||||
static const int BOMBER_SCORE = 100;
|
||||
static const int SAT_SCORE = 250;
|
||||
// Wave-clear scoring: points per item, times the score multiplier.
|
||||
static const int KILL_POINTS = 25;
|
||||
static const int CITY_POINTS = 100;
|
||||
static const int AMMO_POINTS = 5;
|
||||
// Score multiplier climbs every N waves, capped.
|
||||
static const int MULT_EVERY_WAVES = 2;
|
||||
static const int MULT_MAX = 6;
|
||||
// Bonus city in reserve every this many points.
|
||||
static const int CITY_BONUS_EVERY = 10000;
|
||||
// Classic mercy rules: every N waves one destroyed city is rebuilt, and at
|
||||
// most N city kills count per wave (further hits just scar the skyline).
|
||||
static const int CITY_REBUILD_EVERY = 3;
|
||||
static const int CITY_KILLS_MAX = 3;
|
||||
|
||||
static const uint16_t EXPL_COLORS[3] = { TFT_WHITE, TFT_ORANGE, TFT_YELLOW };
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -135,16 +180,16 @@ static int wave = 1;
|
||||
static int bombsSpawned = 0;
|
||||
static int waveQuota = 0;
|
||||
static float bombSpeed = 0.5f; // px per frame
|
||||
static uint32_t spawnIntervalMs = 1400;
|
||||
static uint32_t spawnIntervalMs = SPAWN_MS_BASE;
|
||||
static uint32_t lastSpawnMs = 0;
|
||||
static int waveKills = 0;
|
||||
static bool spawningStopped = false;
|
||||
static int citiesLostWave = 0; // classic rule: max 3 city kills per wave
|
||||
static int citiesLostWave = 0; // classic mercy rule: see CITY_KILLS_MAX
|
||||
|
||||
// Bonus cities: earned at preset score intervals (arcade: every 10,000),
|
||||
// kept in reserve and automatically deployed between waves as needed.
|
||||
static int reserveCities = 0;
|
||||
static int nextCityAt = 10000;
|
||||
static int nextCityAt = CITY_BONUS_EVERY;
|
||||
static uint32_t cityOSDUntil = 0;
|
||||
|
||||
static struct {
|
||||
@@ -354,8 +399,8 @@ static void redrawGroundArea(float cx, float cy, float r)
|
||||
// ---------------------------------------------------------------------------
|
||||
static int scoreMult()
|
||||
{
|
||||
int m = 1 + (wave - 1) / 2;
|
||||
return m > 6 ? 6 : m;
|
||||
int m = 1 + (wave - 1) / MULT_EVERY_WAVES;
|
||||
return m > MULT_MAX ? MULT_MAX : m;
|
||||
}
|
||||
|
||||
// All scoring goes through here: at preset score intervals the player
|
||||
@@ -365,7 +410,7 @@ static void addScore(int pts)
|
||||
score += pts;
|
||||
while (score >= nextCityAt) {
|
||||
reserveCities++;
|
||||
nextCityAt += 10000;
|
||||
nextCityAt += CITY_BONUS_EVERY;
|
||||
cityOSDUntil = millis() + 1400;
|
||||
drawHUD(); // show the new reserve count
|
||||
sfxTone(1500, 60, 9000);
|
||||
@@ -553,7 +598,7 @@ static void spawnBombAt(float sx, float sy, bool smart, bool fromCraft)
|
||||
b.diry = dy / len;
|
||||
b.len = len;
|
||||
b.d = 0;
|
||||
b.speed = bombSpeed * (b.smart ? 1.25f : 1.0f);
|
||||
b.speed = bombSpeed * (b.smart ? SMART_SPEED_MUL : 1.0f);
|
||||
b.x = b.sx;
|
||||
b.y = b.sy;
|
||||
b.hx = b.sx;
|
||||
@@ -568,7 +613,12 @@ static void spawnBombAt(float sx, float sy, bool smart, bool fromCraft)
|
||||
static void spawnBomb()
|
||||
{
|
||||
// spawn below the HUD band so trails never touch the score text
|
||||
bool smart = random(100) < (wave >= 3 ? (2 + wave > 28 ? 28 : 2 + wave) : 0);
|
||||
bool smart = false;
|
||||
if (wave >= SMART_START_WAVE) {
|
||||
int pct = SMART_CHANCE_BASE + wave * SMART_CHANCE_PER_WAVE;
|
||||
if (pct > SMART_CHANCE_MAX) pct = SMART_CHANCE_MAX;
|
||||
smart = random(100) < pct;
|
||||
}
|
||||
spawnBombAt(random(4, scrW - 4), 22, smart, false);
|
||||
}
|
||||
|
||||
@@ -807,7 +857,7 @@ static void spawnCraft(bool sat)
|
||||
cr.dir = random(0, 2) ? 1 : -1;
|
||||
cr.x = cr.dir > 0 ? -8 : scrW + 8;
|
||||
cr.y = sat ? 26 + random(0, 10) : 44 + random(0, 18);
|
||||
cr.lastDrop = millis() + 600;
|
||||
cr.lastDrop = millis() + CRAFT_FIRST_DROP_MS;
|
||||
if (sat) sfxSweep(900, 500, 350, 7000);
|
||||
else sfxSweep(480, 290, 420, 7000);
|
||||
return;
|
||||
@@ -829,7 +879,7 @@ static void updateCrafts()
|
||||
drawCrossAt(cross.dx, cross.dy, TFT_BLACK);
|
||||
cross.drawn = false;
|
||||
}
|
||||
cr.x += cr.dir * (cr.sat ? 1 : 2);
|
||||
cr.x += cr.dir * (cr.sat ? SAT_SPEED : BOMBER_SPEED);
|
||||
if (cr.x < -14 || cr.x > scrW + 14) {
|
||||
cr.alive = false;
|
||||
continue;
|
||||
@@ -840,7 +890,7 @@ static void updateCrafts()
|
||||
|
||||
// drop bombs on the defenses below
|
||||
uint32_t now = millis();
|
||||
uint32_t every = cr.sat ? 2200 : 1400;
|
||||
uint32_t every = cr.sat ? SAT_DROP_MS : BOMBER_DROP_MS;
|
||||
if (now - cr.lastDrop >= every) {
|
||||
cr.lastDrop = now;
|
||||
spawnBombAt((float)cr.x, (float)cr.y + 6, false, true);
|
||||
@@ -857,7 +907,7 @@ static void shootCraft(int k)
|
||||
bool sat = cr.sat;
|
||||
cr.alive = false;
|
||||
drawCraftAt(cr, TFT_BLACK);
|
||||
addScore((sat ? 250 : 100) * scoreMult());
|
||||
addScore((sat ? SAT_SCORE : BOMBER_SCORE) * scoreMult());
|
||||
spawnExpl(cx, cy, 16.0f, 1.5f);
|
||||
sfxKill();
|
||||
}
|
||||
@@ -894,7 +944,7 @@ static void killCity(int i)
|
||||
{
|
||||
// classic rule: enemy weapons are only able to destroy three cities
|
||||
// during one wave
|
||||
if (!cityAlive[i] || citiesLostWave >= 3) return;
|
||||
if (!cityAlive[i] || citiesLostWave >= CITY_KILLS_MAX) return;
|
||||
citiesLostWave++;
|
||||
cityAlive[i] = false;
|
||||
drawCity(i);
|
||||
@@ -963,7 +1013,7 @@ static void updateBombs()
|
||||
if (ddx * ddx + ddy * ddy < ex.r * ex.r) {
|
||||
b.alive = false;
|
||||
waveKills++;
|
||||
addScore(25 * scoreMult());
|
||||
addScore(KILL_POINTS * scoreMult());
|
||||
eraseBombTrail(b); // also refreshes the HUD with the new score
|
||||
spawnExpl(b.x, b.y, 14.0f, 1.4f);
|
||||
sfxKill();
|
||||
@@ -1075,7 +1125,7 @@ static void resetGame()
|
||||
wave = 0;
|
||||
citiesLostWave = 0;
|
||||
reserveCities = 0;
|
||||
nextCityAt = 10000;
|
||||
nextCityAt = CITY_BONUS_EVERY;
|
||||
cityOSDUntil = 0;
|
||||
cross.x = cross.dx = scrW / 2;
|
||||
cross.y = cross.dy = scrH / 2;
|
||||
@@ -1091,8 +1141,8 @@ static void startWave(int n)
|
||||
batAlive[i] = true;
|
||||
batAmmo[i] = AMMO_MAX;
|
||||
}
|
||||
// every third wave one destroyed city is rebuilt
|
||||
if (n > 1 && (n - 1) % 3 == 0) {
|
||||
// every CITY_REBUILD_EVERY waves one destroyed city is rebuilt
|
||||
if (n > 1 && (n - 1) % CITY_REBUILD_EVERY == 0) {
|
||||
for (int i = 0; i < CITY_COUNT; i++) {
|
||||
if (!cityAlive[i]) {
|
||||
cityAlive[i] = true;
|
||||
@@ -1100,12 +1150,12 @@ static void startWave(int n)
|
||||
}
|
||||
}
|
||||
}
|
||||
waveQuota = 8 + 2 * n;
|
||||
if (waveQuota > 24) waveQuota = 24;
|
||||
bombSpeed = 0.5f + 0.12f * (n - 1);
|
||||
if (bombSpeed > 2.4f) bombSpeed = 2.4f;
|
||||
spawnIntervalMs = 1400 - 70 * n;
|
||||
if (spawnIntervalMs < 500) spawnIntervalMs = 500;
|
||||
waveQuota = QUOTA_BASE + QUOTA_PER_WAVE * n;
|
||||
if (waveQuota > QUOTA_MAX) waveQuota = QUOTA_MAX;
|
||||
bombSpeed = BOMB_SPEED_BASE + BOMB_SPEED_PER_WAVE * (n - 1);
|
||||
if (bombSpeed > BOMB_SPEED_MAX) bombSpeed = BOMB_SPEED_MAX;
|
||||
spawnIntervalMs = SPAWN_MS_BASE - SPAWN_MS_PER_WAVE * n;
|
||||
if (spawnIntervalMs < SPAWN_MS_MIN) spawnIntervalMs = SPAWN_MS_MIN;
|
||||
bombsSpawned = 0;
|
||||
waveKills = 0;
|
||||
citiesLostWave = 0; // fresh city-kill budget each wave
|
||||
@@ -1116,8 +1166,8 @@ static void startWave(int n)
|
||||
|
||||
// bombers (and later satellites) cross the sky, launching bombs
|
||||
for (int i = 0; i < MAX_CRAFTS; i++) crafts[i].alive = false;
|
||||
if (n >= 2) spawnCraft(false);
|
||||
if (n >= 5 && random(100) < 45) spawnCraft(true);
|
||||
if (n >= BOMBER_START_WAVE) spawnCraft(false);
|
||||
if (n >= SAT_START_WAVE && random(100) < SAT_CHANCE) spawnCraft(true);
|
||||
|
||||
tft.setFreeFont(&FreeSansBold18pt7b);
|
||||
tft.setTextColor(TFT_YELLOW, TFT_BLACK);
|
||||
@@ -1162,8 +1212,8 @@ static void enterBonus()
|
||||
for (int i = 0; i < 3; i++) ammoLeft += batAmmo[i];
|
||||
|
||||
int mult = scoreMult();
|
||||
int cityPts = aliveCities * 100 * mult;
|
||||
int ammoPts = ammoLeft * 5 * mult;
|
||||
int cityPts = aliveCities * CITY_POINTS * mult;
|
||||
int ammoPts = ammoLeft * AMMO_POINTS * mult;
|
||||
addScore(cityPts + ammoPts);
|
||||
|
||||
int px = 60, pw = scrW - 120, py = 64, ph = 108;
|
||||
@@ -1175,9 +1225,9 @@ static void enterBonus()
|
||||
tft.setTextDatum(MC_DATUM);
|
||||
tft.drawString("WAVE " + String(wave) + " CLEARED", scrW / 2, py + 12);
|
||||
tft.setTextColor(TFT_WHITE, TFT_BLACK);
|
||||
tft.drawString("MISSILES " + String(waveKills) + " x " + String(25 * mult), scrW / 2, py + 30);
|
||||
tft.drawString("CITIES " + String(aliveCities) + " x " + String(100 * mult), scrW / 2, py + 46);
|
||||
tft.drawString("AMMO " + String(ammoLeft) + " x " + String(5 * mult), scrW / 2, py + 62);
|
||||
tft.drawString("MISSILES " + String(waveKills) + " x " + String(KILL_POINTS * mult), scrW / 2, py + 30);
|
||||
tft.drawString("CITIES " + String(aliveCities) + " x " + String(CITY_POINTS * mult), scrW / 2, py + 46);
|
||||
tft.drawString("AMMO " + String(ammoLeft) + " x " + String(AMMO_POINTS * mult), scrW / 2, py + 62);
|
||||
if (deployed > 0) {
|
||||
tft.setTextColor(TFT_CYAN, TFT_BLACK);
|
||||
tft.drawString("REBUILT " + String(deployed) + " (BONUS CITY)", scrW / 2, py + 78);
|
||||
@@ -1252,45 +1302,111 @@ 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()
|
||||
{
|
||||
// edge is one ball step. The ISRs below only count edges (so nothing is
|
||||
// lost while loop() stalls in long TFT draws); applyTrackball() turns the
|
||||
// counts into motion once per game frame.
|
||||
//
|
||||
// Quadrant pins + center click, per UnitTest's mouse mapping:
|
||||
// G02 -> right, G01 -> up, G04 -> left, G03 -> down, BOOT -> click
|
||||
static volatile uint32_t tbEdges[4];
|
||||
static volatile uint32_t tbClicks;
|
||||
|
||||
static void IRAM_ATTR tbIsrRight() { tbEdges[0]++; }
|
||||
static void IRAM_ATTR tbIsrUp() { tbEdges[1]++; }
|
||||
static void IRAM_ATTR tbIsrLeft() { tbEdges[2]++; }
|
||||
static void IRAM_ATTR tbIsrDown() { tbEdges[3]++; }
|
||||
static void IRAM_ATTR tbIsrClick() { tbClicks++; }
|
||||
|
||||
// Momentum model — this is also the debounce. The ball is heavy: edge counts
|
||||
// are impulses that build velocity, friction bleeds it off each frame. The
|
||||
// contacts bounce a stray edge or two after a pulse, but a heavy ball barely
|
||||
// notices: one bounce impulse decays into a sub-pixel nudge and never
|
||||
// reverses the motion, so no time-gate debounce is needed.
|
||||
static const int TB_FP = 8; // velocity fixed point
|
||||
// Sensitivity: impulse gained per ball edge. The real ball's pulse rate is
|
||||
// much lower than the emulator's synthetic streams, so the device gets 4x
|
||||
// per edge — a sustained 1-edge-per-frame roll paces at ~12 px/frame, close
|
||||
// to the old direct 14 px/pulse mapping.
|
||||
#ifdef EMULATOR
|
||||
static const int TB_IMPULSE = 6; // emulated: rich edge rate
|
||||
static const int TB_FRICTION = 12; // vel kept per frame (of 16)
|
||||
#else
|
||||
static const int TB_IMPULSE = 24; // device: fewer edges to move
|
||||
static const int TB_FRICTION = 15; // vel kept per frame (of 16)
|
||||
#endif
|
||||
static const int TB_VMAX = CROSS_MAX_STEP << TB_FP; // hard-flick cap
|
||||
|
||||
static int32_t tbVelX, tbVelY; // ball momentum, 1/8 px per frame
|
||||
static int32_t tbRemX, tbRemY; // sub-pixel movement accumulators
|
||||
|
||||
static void attachTrackball()
|
||||
{
|
||||
static const uint8_t tbPins[5] = {
|
||||
BOARD_TBOX_G02, BOARD_TBOX_G01, BOARD_TBOX_G04, BOARD_TBOX_G03, BOARD_BOOT_PIN
|
||||
};
|
||||
static bool last[5];
|
||||
bool cur[5];
|
||||
for (int i = 0; i < 5; i++) {
|
||||
cur[i] = digitalRead(tbPins[i]);
|
||||
pinMode(tbPins[0], INPUT_PULLUP);
|
||||
attachInterrupt(digitalPinToInterrupt(tbPins[0]), tbIsrRight, FALLING);
|
||||
pinMode(tbPins[1], INPUT_PULLUP);
|
||||
attachInterrupt(digitalPinToInterrupt(tbPins[1]), tbIsrUp, FALLING);
|
||||
pinMode(tbPins[2], INPUT_PULLUP);
|
||||
attachInterrupt(digitalPinToInterrupt(tbPins[2]), tbIsrLeft, FALLING);
|
||||
pinMode(tbPins[3], INPUT_PULLUP);
|
||||
attachInterrupt(digitalPinToInterrupt(tbPins[3]), tbIsrDown, FALLING);
|
||||
pinMode(tbPins[4], INPUT_PULLUP);
|
||||
attachInterrupt(digitalPinToInterrupt(tbPins[4]), tbIsrClick, FALLING);
|
||||
}
|
||||
|
||||
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;
|
||||
// Discard everything the ISRs caught (used while input is in grace).
|
||||
static void resetTrackball()
|
||||
{
|
||||
for (int i = 0; i < 4; i++) tbEdges[i] = 0;
|
||||
tbClicks = 0;
|
||||
tbVelX = tbVelY = 0;
|
||||
tbRemX = tbRemY = 0;
|
||||
}
|
||||
|
||||
// Consume the edges caught by the ISRs since the last frame. A slow roll
|
||||
// glides the cross in fine steps; a hard flick builds momentum that carries
|
||||
// it (capped, like the arcade trackball sending the cursor flying).
|
||||
static void applyTrackball()
|
||||
{
|
||||
uint32_t e[4], clicks;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
e[i] = tbEdges[i];
|
||||
tbEdges[i] = 0;
|
||||
}
|
||||
clicks = tbClicks;
|
||||
tbClicks = 0;
|
||||
|
||||
if (gameState == ST_PLAYING || gameState == ST_BANNER) {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
if (cur[i] != last[i]) tbSteps[i]++; // count every edge
|
||||
}
|
||||
// opposite edges fight the existing momentum, like rolling the
|
||||
// ball back against its spin
|
||||
tbVelX += ((int32_t)e[0] - (int32_t)e[2]) * TB_IMPULSE;
|
||||
tbVelY += ((int32_t)e[3] - (int32_t)e[1]) * TB_IMPULSE;
|
||||
if (tbVelX > TB_VMAX) tbVelX = TB_VMAX;
|
||||
if (tbVelX < -TB_VMAX) tbVelX = -TB_VMAX;
|
||||
if (tbVelY > TB_VMAX) tbVelY = TB_VMAX;
|
||||
if (tbVelY < -TB_VMAX) tbVelY = -TB_VMAX;
|
||||
|
||||
// friction, then integrate: only whole pixels move the cross, the
|
||||
// sub-pixel remainder waits in the accumulators
|
||||
tbVelX = tbVelX * TB_FRICTION / 16;
|
||||
tbVelY = tbVelY * TB_FRICTION / 16;
|
||||
tbRemX += tbVelX;
|
||||
tbRemY += tbVelY;
|
||||
int pxX = tbRemX / TB_FP;
|
||||
int pxY = tbRemY / TB_FP;
|
||||
tbRemX -= pxX * TB_FP;
|
||||
tbRemY -= pxY * TB_FP;
|
||||
if (pxX || pxY) nudgeCross(pxX, pxY);
|
||||
} else {
|
||||
// ball pinched: no momentum carries across menus
|
||||
tbVelX = tbVelY = 0;
|
||||
tbRemX = tbRemY = 0;
|
||||
}
|
||||
|
||||
bool pressed = (!cur[4] && last[4]); // falling edge = click
|
||||
for (int i = 0; i < 5; i++) last[i] = cur[i];
|
||||
|
||||
if (!pressed) return;
|
||||
if (!clicks) return;
|
||||
|
||||
switch (gameState) {
|
||||
case ST_TITLE:
|
||||
@@ -1310,26 +1426,6 @@ 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;
|
||||
@@ -1550,13 +1646,8 @@ static void hardwareInit()
|
||||
Serial.println("No GT911 touch found");
|
||||
}
|
||||
|
||||
// Trackball pins (quadrants + center click)
|
||||
const uint8_t tbPins[5] = {
|
||||
BOARD_TBOX_G02, BOARD_TBOX_G01, BOARD_TBOX_G04, BOARD_TBOX_G03, BOARD_BOOT_PIN
|
||||
};
|
||||
for (int i = 0; i < 5; i++) {
|
||||
pinMode(tbPins[i], INPUT_PULLUP);
|
||||
}
|
||||
// Trackball pins (quadrants + center click): edge-counted by ISRs
|
||||
attachTrackball();
|
||||
|
||||
randomSeed(esp_random());
|
||||
}
|
||||
@@ -1591,12 +1682,11 @@ void loop()
|
||||
// a game immediately after a restart
|
||||
uint32_t now = millis();
|
||||
if (now < INPUT_GRACE_MS) {
|
||||
resetTrackball(); // swallow stale edges from floating pins
|
||||
delay(1);
|
||||
return;
|
||||
}
|
||||
|
||||
pollTrackball(); // hot path: sample the ball every iteration (~1 ms)
|
||||
|
||||
static uint32_t nextFrame = 0;
|
||||
if ((int32_t)(now - nextFrame) < 0) {
|
||||
delay(1);
|
||||
|
||||
Reference in New Issue
Block a user