improve touch with pooling

This commit is contained in:
Adolfo Reyna
2026-01-29 14:16:19 -05:00
parent d19a2ca639
commit 3a08cb5119
3 changed files with 398 additions and 75 deletions

View File

@@ -8,6 +8,7 @@
#include "pico/stdlib.h"
#include "pico/binary_info.h"
#include "hardware/sync.h"
#include "board_config.h" // Board-specific pin configuration
#include "sd_card.h"
#include <stdlib.h>
@@ -24,6 +25,67 @@ bi_decl(bi_program_description("4.0\" TFT ST7796 with Touch and SD Card Demo"));
bi_decl(bi_program_version_string("0.1"));
bi_decl(bi_program_build_date_string(__DATE__));
// Touch interrupt handling
volatile bool touch_interrupt_flag = false;
volatile bool touch_event_down = false;
LowLevelTouch* touch = nullptr;
/**
* @brief Touch interrupt callback handler
*
* Called automatically by hardware when INT pin changes state:
* - Falling edge: Touch detected (INT goes LOW)
* - Rising edge: Touch released (INT goes HIGH)
*
* This runs in interrupt context, so keep it fast - just set a flag
*
* @param gpio GPIO pin number that triggered the interrupt
* @param events Event mask (GPIO_IRQ_EDGE_FALL and/or GPIO_IRQ_EDGE_RISE)
*/
void touch_interrupt_handler(uint gpio, uint32_t events) {
// Set flag to indicate touch event occurred
// Main loop will handle the actual touch reading
touch_interrupt_flag = true;
// Optional: track which edge triggered (for debugging)
if (events & GPIO_IRQ_EDGE_FALL) {
touch_event_down = true;
printf("Touch DOWN event detected\n");
}
if (events & GPIO_IRQ_EDGE_RISE) {
touch_event_down = false;
printf("Touch UP event detected\n");
}
TouchData touch_data;
touch->read_touch(&touch_data);
int16_t x = touch_data.points[0].x;
int16_t y = touch_data.points[0].y;
uint8_t event = touch_data.points[0].event;
uint8_t id = touch_data.points[0].id;
uint8_t weight = touch_data.points[0].pressure;
uint8_t gesture = touch_data.gesture;
// Display detailed touch information including weight and gesture
printf("Touch: X=%d Y=%d Event=%d ID=%d Weight=%d\n",
x, y, event, id, weight);
// Display gesture if detected (non-zero)
if (gesture != 0) {
const char* gesture_name = "Unknown";
switch(gesture) {
case 0x10: gesture_name = "Move Up"; break;
case 0x14: gesture_name = "Move Right"; break;
case 0x18: gesture_name = "Move Down"; break;
case 0x1C: gesture_name = "Move Left"; break;
case 0x48: gesture_name = "Zoom In"; break;
case 0x49: gesture_name = "Zoom Out"; break;
}
printf(" Gesture=0x%02X (%s)\n", gesture, gesture_name);
}
}
// Screen dimensions and configuration from board_config.h
const int V_WIDTH = DISPLAY_WIDTH;
const int V_HEIGHT = DISPLAY_HEIGHT;
@@ -91,12 +153,17 @@ int main()
refresh_screen(bit_buffer, display);
// Initialize touch screen using abstraction
LowLevelTouch* touch = LowLevelTouch::create((TouchType)TOUCH_TYPE_SELECTED, V_WIDTH, V_HEIGHT,
touch = LowLevelTouch::create((TouchType)TOUCH_TYPE_SELECTED, V_WIDTH, V_HEIGHT,
TOUCH_SWAP_XY, TOUCH_INVERT_X, TOUCH_INVERT_Y);
if (touch) {
printf("Touch initialized successfully\n");
// Set up interrupt-driven touch detection
printf("Setting up touch interrupt callback...\n");
touch->set_interrupt_callback(touch_interrupt_handler);
printf("Touch interrupt enabled on INT pin (falling and rising edges)\n");
// Run communication test if available
// Note: Commented out as it may hang on some hardware configurations
printf("\nRunning touch reliability test...\n");
@@ -107,11 +174,11 @@ int main()
}
// Test SD card and FatFS
if (sd_card_init_with_board_config()) {
sd_card_test_fatfs();
} else {
printf("SD Card initialization failed or no card present\n");
}
// if (sd_card_init_with_board_config()) {
// sd_card_test_fatfs();
// } else {
// printf("SD Card initialization failed or no card present\n");
// }
// Main loop - handle touch events
int last_x = -1, last_y = -1;
@@ -124,74 +191,108 @@ int main()
int touch_success_count = 0;
printf("Entering main touch loop...\n");
printf("Touch system uses interrupt-driven detection:\n");
printf(" - Hardware interrupt triggers on INT pin state changes\n");
printf(" - Falling edge (HIGH->LOW) = Touch detected\n");
printf(" - Rising edge (LOW->HIGH) = Touch released\n");
printf(" - No CPU polling needed - interrupt wakes us up!\n");
printf(" - Gesture support enabled in trigger mode\n");
while (1) {
// Always sleep to prevent tight loop and allow other operations
sleep_us(100);
// Sleep until interrupt wakes us up (very power efficient!)
// Te(); // Wait For Event - CPU sleeps until interrupt or evenurs
__wfi(); // Wait For Interrupt - CPU sleeps until any interrupt
// Check INT pin directly (LOW = touch detected, no I2C transaction needed!)
// Much faster than I2C read and doesn't interfere with other operations
bool int_pin_low = !gpio_get(TOUCH_INT_PIN);
// Check if our touch interrupt flag was set
if (!touch_interrupt_flag) {
continue; // Woken by different interrupt, go back to sleep
}
// Only process if INT pin indicates touch data available
if (int_pin_low) {
// Clear the flag
touch_interrupt_flag = false;
while(touch_event_down){
uint32_t now = to_ms_since_boot(get_absolute_time());
// Check if enough time has passed since last touch check (debounce)
if (now - last_touch_time < debounce_ms) {
//continue;
}
// Now read full touch data via I2C
TouchData touch_data;
if (!touch->read_touch(&touch_data) || touch_data.touch_count == 0) {
// Read failed or no actual touch data
touch_fail_count++;
was_touched = false;
last_x = -1;
last_y = -1;
last_touch_time = now;
continue;
}
//printf("Touch interrupt event detected (event_down=%d)\n", touch_event_down);
touch_success_count++;
int16_t x = touch_data.points[0].x;
int16_t y = touch_data.points[0].y;
uint8_t event = touch_data.points[0].event;
uint8_t id = touch_data.points[0].id;
uint8_t weight = touch_data.points[0].pressure;
uint8_t gesture = touch_data.gesture;
printf("Touch: X=%d Y=%d Event=%d ID=%d Weight=%d Gesture=0x%02X [S:%d F:%d]\n",
x, y, event, id, weight, gesture,
touch_success_count, touch_fail_count);
// Check if touch is in title area to clear screen
// Draw line from last position (for smooth drawing)
if (last_x >= 0 && last_y >= 0) {
int dx = abs(x - last_x);
int dy = abs(y - last_y);
// Only draw line if movement is reasonable (filter noise)
if (dx < 50 && dy < 50) {
renderer.draw_line(last_x, last_y, x, y, true);
refresh_screen(bit_buffer, display);
}
// Touch interrupt occurred - read the data
// is_touched() will check INT pin and confirm via I2C if needed
//if (touch && touch->is_touched()) {
// Now read full touch data via I2C (already confirmed by INT pin)
TouchData touch_data;
if (!touch->read_touch(&touch_data)) {
// Read failed or no actual touch data
touch_fail_count++;
//was_touched = false;
//last_x = -1;
//last_y = -1;
//last_touch_time = now;
continue;
}
last_x = x;
last_y = y;
was_touched = true;
last_touch_time = now;
} else {
// Reset last position when not touching
if (was_touched) {
last_x = -1;
last_y = -1;
was_touched = false;
}
touch_success_count++;
int16_t x = touch_data.points[0].x;
int16_t y = touch_data.points[0].y;
uint8_t event = touch_data.points[0].event;
uint8_t id = touch_data.points[0].id;
uint8_t weight = touch_data.points[0].pressure;
uint8_t gesture = touch_data.gesture;
// Display detailed touch information including weight and gesture
// printf("Touch: X=%d Y=%d Event=%d ID=%d Weight=%d",
// x, y, event, id, weight);
// Display gesture if detected (non-zero)
if (gesture != 0) {
const char* gesture_name = "Unknown";
switch(gesture) {
case 0x10: gesture_name = "Move Up"; break;
case 0x14: gesture_name = "Move Right"; break;
case 0x18: gesture_name = "Move Down"; break;
case 0x1C: gesture_name = "Move Left"; break;
case 0x48: gesture_name = "Zoom In"; break;
case 0x49: gesture_name = "Zoom Out"; break;
}
printf(" Gesture=0x%02X (%s)", gesture, gesture_name);
}
// printf(" [Success:%d Fail:%d]\n", touch_success_count, touch_fail_count);
// Check if touch is in title area to clear screen
// Draw line from last position (for smooth drawing)
if (last_x >= 0 && last_y >= 0) {
int dx = abs(x - last_x);
int dy = abs(y - last_y);
// Only draw line if movement is reasonable (filter noise)
//if (dx < 50 && dy < 50) {
renderer.draw_line(last_x, last_y, x, y, true);
//}
}
last_x = x;
last_y = y;
was_touched = true;
last_touch_time = now;
//} else {
// INT pin triggered but no touch data (likely release event)
//}
}
if (was_touched) {
last_x = -1;
last_y = -1;
was_touched = false;
refresh_screen(bit_buffer, display);
}
}