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)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user