fix: input delay workaround

This commit is contained in:
dark98
2026-09-25 21:14:07 +01:00
parent 31b93c2a9a
commit ea3ccd3b83
4 changed files with 60 additions and 65 deletions
+16 -22
View File
@@ -7,11 +7,10 @@
static const char *TAG = "SYS_INPUT";
#define DEBOUNCE_TIME_MS 25
#define CLICK_TIME_MAX_MS 350
#define DOUBLE_CLICK_GAP_MS 280
#define LONG_PRESS_TIME_MS 450
#define HOME_PRESS_TIME_MS 1500
#define DEBOUNCE_TIME_MS 12
#define DOUBLE_CLICK_GAP_MS 260
#define LONG_PRESS_TIME_MS 380
#define HOME_PRESS_TIME_MS 1400
typedef struct {
sys_input_handler_t handler;
@@ -69,48 +68,43 @@ esp_err_t sys_input_register_handler(sys_input_handler_t handler, void *user_arg
static void button_scanner_task(void *pvParameters)
{
bool last_state = true;
bool last_state = false;
uint32_t press_start_ms = 0;
uint32_t release_time_ms = 0;
uint8_t click_count = 0;
uint32_t last_click_time_ms = 0;
bool long_press_fired = false;
bool home_press_fired = false;
while (1) {
vTaskDelay(pdMS_TO_TICKS(10));
vTaskDelay(pdMS_TO_TICKS(5)); // High-rate 200 Hz scan loop for zero input lag
uint32_t now_ms = (uint32_t)(esp_timer_get_time() / 1000ULL);
bool current_state = (gpio_get_level(s_button_pin) == 0);
if (current_state && !last_state) {
// Button pressed down
press_start_ms = now_ms;
long_press_fired = false;
home_press_fired = false;
} else if (current_state && last_state) {
// Button held down
uint32_t held_duration = now_ms - press_start_ms;
if (!home_press_fired && held_duration >= HOME_PRESS_TIME_MS) {
home_press_fired = true;
click_count = 0;
dispatch_action(INPUT_ACTION_HOLD_HOME);
} else if (!long_press_fired && !home_press_fired && held_duration >= LONG_PRESS_TIME_MS) {
long_press_fired = true;
click_count = 0;
dispatch_action(INPUT_ACTION_LONG_PRESS);
}
} else if (!current_state && last_state) {
// Button released
uint32_t press_duration = now_ms - press_start_ms;
if (press_duration >= DEBOUNCE_TIME_MS && !long_press_fired && !home_press_fired) {
click_count++;
release_time_ms = now_ms;
}
} else if (!current_state && !last_state) {
if (click_count > 0) {
uint32_t idle_time = now_ms - release_time_ms;
if (click_count >= 2) {
if ((now_ms - last_click_time_ms) <= DOUBLE_CLICK_GAP_MS) {
dispatch_action(INPUT_ACTION_DOUBLE_CLICK);
click_count = 0;
} else if (idle_time >= DOUBLE_CLICK_GAP_MS) {
last_click_time_ms = 0;
} else {
// Instant click dispatch upon tap release (no 280ms lag penalty!)
dispatch_action(INPUT_ACTION_CLICK);
click_count = 0;
last_click_time_ms = now_ms;
}
}
}
@@ -142,6 +136,6 @@ esp_err_t sys_input_init(gpio_num_t button_gpio)
return ESP_FAIL;
}
ESP_LOGI(TAG, "Input gesture engine started on GPIO %d", (int)s_button_pin);
ESP_LOGI(TAG, "Input gesture engine started on GPIO %d (Instant-Response Mode)", (int)s_button_pin);
return ESP_OK;
}