Reduce touch overhead and cap frame rate at 24 FPS

This commit is contained in:
Adolfo Reyna
2026-02-18 15:53:48 -05:00
parent 73a78069e3
commit fe6e403a98
4 changed files with 26 additions and 9 deletions

View File

@@ -40,6 +40,8 @@ bool InputManager::has_buttons() const {
InputEvent InputManager::process_touch_input(uint32_t* last_time) {
InputEvent event = {INPUT_NONE, 0, 0, 0, 0, 0, false};
const uint32_t now = to_ms_since_boot(get_absolute_time());
static constexpr uint32_t ACTIVE_TOUCH_SAMPLE_INTERVAL_MS = 8;
// Process immediately on IRQ, and continue sampling while a touch is active.
// Some controllers only IRQ on edge transitions, so move events require polling.
@@ -47,16 +49,26 @@ InputEvent InputManager::process_touch_input(uint32_t* last_time) {
return event; // No touch event
}
// While a touch session is active, avoid reading touch hardware every loop
// iteration. IRQ edges (down/up) still bypass this throttle for responsiveness.
if (!touch_interrupt_flag && *last_time != 0) {
uint32_t elapsed = now - last_touch_sample_ms;
if (elapsed < ACTIVE_TOUCH_SAMPLE_INTERVAL_MS) {
return event;
}
}
// Always validate via controller state instead of relying on edge flag alone.
// Edge chatter can flip touch_event_down without a real touch transition.
TouchData touch_data;
if (!touch || !touch->read_touch(&touch_data)) {
last_touch_sample_ms = now;
touch_interrupt_flag = false;
return event;
}
last_touch_sample_ms = now;
touch_interrupt_flag = false;
uint32_t now = to_ms_since_boot(get_absolute_time());
// No active touch points: require consecutive empty reads before release to
// avoid false TOUCH_UP events from transient controller jitter.
@@ -65,6 +77,7 @@ InputEvent InputManager::process_touch_input(uint32_t* last_time) {
no_touch_samples++;
if (no_touch_samples >= 2) {
*last_time = 0;
last_touch_sample_ms = 0;
no_touch_samples = 0;
event.type = INPUT_TOUCH_UP;
event.valid = true;