touch with abtraction working, SD is not working
This commit is contained in:
127
basic1.cpp
127
basic1.cpp
@@ -98,18 +98,19 @@ int main()
|
||||
printf("Touch initialized successfully\n");
|
||||
|
||||
// Run communication test if available
|
||||
// Note: Commented out as it may hang on some hardware configurations
|
||||
printf("\nRunning touch reliability test...\n");
|
||||
touch->test_communication();
|
||||
printf("\n");
|
||||
printf("...\n");
|
||||
} else {
|
||||
printf("Touch initialization failed or not configured\n");
|
||||
}
|
||||
|
||||
// Test SD card and FatFS
|
||||
if (sd_card_init_with_board_config()) {
|
||||
sd_card_test_fatfs();
|
||||
sd_card_test_fatfs();
|
||||
} else {
|
||||
printf("SD Card initialization failed or no card present\n");
|
||||
printf("SD Card initialization failed or no card present\n");
|
||||
}
|
||||
|
||||
// Main loop - handle touch events
|
||||
@@ -117,7 +118,7 @@ int main()
|
||||
|
||||
// Touch debouncing
|
||||
uint32_t last_touch_time = 0;
|
||||
const uint32_t debounce_ms = 20; // Minimum time between touch reads
|
||||
const uint32_t debounce_ms = 10; // Poll touch every 10ms (100 times per second)
|
||||
bool was_touched = false;
|
||||
int touch_fail_count = 0;
|
||||
int touch_success_count = 0;
|
||||
@@ -125,71 +126,65 @@ int main()
|
||||
printf("Entering main touch loop...\n");
|
||||
|
||||
while (1) {
|
||||
uint32_t now = to_ms_since_boot(get_absolute_time());
|
||||
// Always sleep to prevent tight loop and allow other operations
|
||||
sleep_us(100);
|
||||
|
||||
// Check if enough time has passed since last touch check
|
||||
if (now - last_touch_time < debounce_ms) {
|
||||
sleep_ms(1);
|
||||
continue;
|
||||
}
|
||||
// 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);
|
||||
|
||||
bool is_touched = touch && touch->is_touched();
|
||||
// Only process if INT pin indicates touch data available
|
||||
if (int_pin_low) {
|
||||
uint32_t now = to_ms_since_boot(get_absolute_time());
|
||||
|
||||
// Only process touch if state changed or still touching
|
||||
if (is_touched) {
|
||||
TouchData touch_data;
|
||||
|
||||
if (touch->read_touch(&touch_data)) {
|
||||
touch_success_count++;
|
||||
|
||||
if (touch_data.touch_count > 0) {
|
||||
int16_t x = touch_data.points[0].x;
|
||||
int16_t y = touch_data.points[0].y;
|
||||
|
||||
// Only print occasionally to avoid flooding serial
|
||||
//if (touch_success_count % 5 == 0) {
|
||||
printf("Touch: X=%d, Y=%d, Event=%d [Success: %d, Fail: %d]\n",
|
||||
x, y, touch_data.points[0].event,
|
||||
touch_success_count, touch_fail_count);
|
||||
//}
|
||||
|
||||
// Check if touch is in title area to clear screen
|
||||
if (y < 30) {
|
||||
if (!was_touched) { // Only on new touch
|
||||
// Clear drawing area in bit buffer
|
||||
renderer.draw_filled_rectangle(11, 130, V_WIDTH - 11 - 11, V_HEIGHT - 11 - 130, false, 1);
|
||||
refresh_screen(bit_buffer, display);
|
||||
printf("Drawing area cleared\n");
|
||||
}
|
||||
}
|
||||
// Draw in touch area (white line)
|
||||
else if (y > 100) {
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
last_x = x;
|
||||
last_y = y;
|
||||
}
|
||||
|
||||
was_touched = true;
|
||||
last_touch_time = now;
|
||||
}
|
||||
} else {
|
||||
// Touch detected but read failed
|
||||
touch_fail_count++;
|
||||
if (touch_fail_count % 10 == 0) {
|
||||
printf("Touch read failed (count: %d)\n", touch_fail_count);
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
last_x = x;
|
||||
last_y = y;
|
||||
|
||||
was_touched = true;
|
||||
last_touch_time = now;
|
||||
} else {
|
||||
// Reset last position when not touching
|
||||
if (was_touched) {
|
||||
@@ -198,8 +193,6 @@ int main()
|
||||
was_touched = false;
|
||||
}
|
||||
}
|
||||
|
||||
sleep_ms(5); // Faster polling for better responsiveness
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user