Files
basic1/basic1.cpp
2026-01-29 14:16:19 -05:00

301 lines
11 KiB
C++

/*
* 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 "hardware/sync.h"
#include "board_config.h" // Board-specific pin configuration
#include "sd_card.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#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__));
// 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;
// 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
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");
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");
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) {
// 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 if our touch interrupt flag was set
if (!touch_interrupt_flag) {
continue; // Woken by different interrupt, go back to sleep
}
// 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;
}
//printf("Touch interrupt event detected (event_down=%d)\n", touch_event_down);
// 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;
}
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);
}
}
return 0;
}