Reduce touch overhead and cap frame rate at 24 FPS
This commit is contained in:
@@ -27,8 +27,9 @@ static bool ft6336u_read_reg(uint8_t reg, uint8_t *value) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Add delay after write, before read (like Arduino library does)
|
||||
sleep_us(10000); // 10ms delay
|
||||
// Short settle time between register address write and read.
|
||||
// 10ms here heavily throttles touch sampling during hold/move.
|
||||
sleep_us(100); // 0.1ms
|
||||
|
||||
result = i2c_read_blocking(g_config->i2c, FT6336U_ADDR, value, 1, false);
|
||||
if (result == 1) {
|
||||
@@ -50,8 +51,9 @@ static bool ft6336u_read_regs(uint8_t reg, uint8_t *buf, size_t len) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Add delay after write, before read (like Arduino library does)
|
||||
sleep_us(10000); // 10ms delay
|
||||
// Short settle time between register address write and read.
|
||||
// 10ms here heavily throttles touch sampling during hold/move.
|
||||
sleep_us(100); // 0.1ms
|
||||
|
||||
result = i2c_read_blocking(g_config->i2c, FT6336U_ADDR, buf, len, false);
|
||||
if (result == (int)len) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -97,6 +97,8 @@ public:
|
||||
private:
|
||||
LowLevelTouch* touch;
|
||||
const GameConfig* config;
|
||||
// Last time we sampled touch over I2C while a touch session is active.
|
||||
uint32_t last_touch_sample_ms = 0;
|
||||
// Consecutive touch_count==0 samples while a touch session is active.
|
||||
// Used to suppress false TOUCH_UP events from transient touch controller jitter.
|
||||
uint8_t no_touch_samples = 0;
|
||||
|
||||
Reference in New Issue
Block a user