/* * Copyright (c) 2021 Arm Limited and Contributors. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * 4.0" TFT ST7796 with Touch Screen and SD Card Demo */ #include "pico/stdlib.h" #include "pico/binary_info.h" #include "board_config.h" // Board-specific pin configuration #include "sd_card.h" #include #include #include #include "display/low_level_render.h" #include "display/low_level_gui.h" #include "display/low_level_display.h" #include "display/low_level_touch.h" // Binary info for RP2350 - ensures proper boot image structure 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__)); // Screen dimensions and configuration from board_config.h const int V_WIDTH = DISPLAY_WIDTH; const int V_HEIGHT = DISPLAY_HEIGHT; // Touch indicator settings #define TOUCH_RADIUS 10 uint8_t bit_buffer[V_WIDTH * V_HEIGHT / 8]; /** * @brief Refresh the screen with the 1-bit buffer * * Displays work directly with 1-bit monochrome buffers. * The display driver internally converts to its native format (RGB565, etc.) * * @param buffer Pointer to 1-bit framebuffer (width*height/8 bytes) * @param display Pointer to display abstraction layer */ void refresh_screen(const uint8_t *buffer, LowLevelDisplay* display) { display->draw_buffer(buffer); display->refresh(); } int main() { // Initialize standard I/O for debugging with timeout // This prevents hanging when USB is not connected stdio_init_all(); sleep_ms(5000); // Wait for USB connection (if present) printf("\n=== %s Demo ===\n", BOARD_NAME); // Create display abstraction using factory method // The factory handles all board-specific configuration internally LowLevelDisplay* display = LowLevelDisplay::create((DisplayType)DISPLAY_TYPE_SELECTED, V_WIDTH, V_HEIGHT); if (!display) { printf("Failed to create display!\n"); return -1; } printf("Initializing 4.0\" TFT with Touch and SD Card...\n"); // Initialize the display if (!display->init()) { printf("Display initialization failed!\n"); delete display; return -1; } display->clear(false); // Clear to black LowLevelRenderer renderer(bit_buffer, V_WIDTH, V_HEIGHT); renderer.set_font(&font_5x5_obj); LowLevelGUI gui = LowLevelGUI(&renderer, font_BMplain_obj); LowLevelWindow *w1 = gui.draw_new_window(15, 15, V_WIDTH - 30, V_HEIGHT - 30, "Main Window"); gui.draw_status_bar(w1, 10, 40, 200, "PANELS", "Weekly Average Charge", 65, "190KWH"); gui.draw_circular_gauge(w1, 10, 100 - 10, 200, "SYSTEM EFF.", 68); // Refresh the screen with the rendered GUI refresh_screen(bit_buffer, display); // Initialize touch screen using abstraction LowLevelTouch* 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"); // 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"); } 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(); } else { printf("SD Card initialization failed or no card present\n"); } // Main loop - handle touch events int last_x = -1, last_y = -1; // Touch debouncing uint32_t last_touch_time = 0; 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; printf("Entering main touch loop...\n"); while (1) { // Always sleep to prevent tight loop and allow other operations sleep_us(100); // 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); // Only process if INT pin indicates touch data available if (int_pin_low) { 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; } 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) { last_x = -1; last_y = -1; was_touched = false; } } } return 0; }