S SmartDocs
Chuỗi bài: Arduino cpp 217 dòng · Cập nhật 2026-01-27

2.4_inch_touch.cpp

Arduino/CCIT4080_CL05_PKPD_Group2/src/Test_components/display_test/2.4_inch_touch/2.4_inch_touch.cpp

/**
 * ST7789 Display + FT6336U Touch Test
 * ESP32-S3 with 240x320 display
 * FT6336U uses default I2C (GPIO 21=SDA, GPIO 22=SCL)
 */

#include <Arduino.h>
#include <TFT_eSPI.h>
#include "RAK14014_FT6336U.h"

#define INT_PIN 2
#define TOUCH_RST 1
#define I2C_SDA 6 // FT6336U SDA pin
#define I2C_SCL 7 // FT6336U SCL pin

TFT_eSPI tft = TFT_eSPI();
FT6336U ft6336u;

static volatile bool intStatus = false; // TP interrupt flag
uint32_t lastUpdate = 0;

// Forward declaration
void tpIntHandle(void);

void setup(void)
{
    // Initialize Serial
    Serial.begin(115200);
    delay(500);
    Serial.println("\n\n========== ST7789 + FT6336U Touch Test ==========");

    // Initialize TFT display
    Serial.println("Initializing TFT display...");
    tft.init();
    tft.setRotation(1); // 0=portrait, 1=landscape
    tft.fillScreen(TFT_BLACK);
    tft.setTextSize(2);
    tft.setTextColor(TFT_WHITE, TFT_BLACK);
    tft.setCursor(10, 10);
    tft.println("Display OK!");
    Serial.println("Display initialized successfully!");

    // Initialize I2C for touch panel
    Serial.println("Initializing I2C for touch panel...");
    Wire.begin(I2C_SDA, I2C_SCL);
    delay(100);

    // Scan I2C devices
    Serial.println("Scanning I2C devices...");
    for (uint8_t addr = 0x01; addr < 0x7F; addr++)
    {
        Wire.beginTransmission(addr);
        if (Wire.endTransmission() == 0)
        {
            Serial.print("  I2C device found at 0x");
            Serial.println(addr, HEX);
        }
    }

    // Reset touch panel
#if TOUCH_RST != -1
    pinMode(TOUCH_RST, OUTPUT);
    digitalWrite(TOUCH_RST, LOW);
    delay(50);
    digitalWrite(TOUCH_RST, HIGH);
    delay(100);
#endif

    Serial.println("Initializing FT6336U touch panel...");
    delay(100);

    // Try to initialize with timeout
    uint32_t timeout = millis() + 5000; // 5 second timeout
    while (ft6336u.begin() == false && millis() < timeout)
    {
        Serial.println("FT6336U is not connected! Retrying...");
        delay(500);
    }

    if (millis() >= timeout)
    {
        Serial.println("ERROR: FT6336U initialization timeout!");
        tft.fillScreen(TFT_BLACK);
        tft.setTextSize(2);
        tft.setTextColor(TFT_RED, TFT_BLACK);
        tft.setCursor(10, 10);
        tft.println("Touch IC Error!");
        tft.setTextSize(1);
        tft.setCursor(10, 50);
        tft.println("Check I2C connection:");
        tft.println("SDA=GPIO6, SCL=GPIO7");
        while (1)
            delay(1000);
    }

    Serial.println("FT6336U connected successfully!");

    // Setup interrupt
    pinMode(INT_PIN, INPUT_PULLUP);
    attachInterrupt(digitalPinToInterrupt(INT_PIN), tpIntHandle, FALLING);

    // Display device info
    Serial.print("FT6336U Firmware Version: ");
    Serial.println(ft6336u.read_firmware_id());
    Serial.print("FT6336U Device Mode: ");
    Serial.println(ft6336u.read_device_mode());

    // Update display
    tft.fillScreen(TFT_BLACK);
    tft.setTextSize(2);
    tft.setTextColor(TFT_GREEN);
    tft.setCursor(10, 20);
    tft.println("FT6336U Test");
    tft.setTextSize(1);
    tft.setTextColor(TFT_WHITE);
    tft.setCursor(10, 70);
    tft.print("FW: ");
    tft.println(ft6336u.read_firmware_id());
    tft.setCursor(10, 100);
    tft.println("Touch screen to test...");

    Serial.println("Setup complete! Ready for touch input.");
}

void loop()
{
    if (intStatus)
    {
        intStatus = false;

        uint8_t td_status = ft6336u.read_td_status();

        Serial.print("TD Status: ");
        Serial.println(td_status);

        // Read touch point 1
        if (td_status >= 1)
        {
            uint16_t x1 = ft6336u.read_touch1_x();
            uint16_t y1 = ft6336u.read_touch1_y();
            uint8_t event1 = ft6336u.read_touch1_event();

            Serial.print("Touch 1: (");
            Serial.print(x1);
            Serial.print(", ");
            Serial.print(y1);
            Serial.print(") Event: ");
            Serial.println(event1);
        }

        // Read touch point 2
        if (td_status >= 2)
        {
            uint16_t x2 = ft6336u.read_touch2_x();
            uint16_t y2 = ft6336u.read_touch2_y();
            uint8_t event2 = ft6336u.read_touch2_event();

            Serial.print("Touch 2: (");
            Serial.print(x2);
            Serial.print(", ");
            Serial.print(y2);
            Serial.print(") Event: ");
            Serial.println(event2);
        }

        // Update display
        if (millis() - lastUpdate > 100)
        {
            lastUpdate = millis();
            tft.fillScreen(TFT_BLACK);

            tft.setTextSize(2);
            tft.setTextColor(TFT_GREEN);
            tft.setCursor(10, 20);
            tft.println("FT6336U Test");

            tft.setTextSize(1);
            tft.setTextColor(TFT_YELLOW);
            tft.setCursor(10, 70);
            tft.print("Touch Points: ");
            tft.println(td_status);

            if (td_status >= 1)
            {
                tft.setCursor(10, 100);
                tft.setTextColor(TFT_WHITE);
                tft.print("P1: (");
                tft.print(ft6336u.read_touch1_x());
                tft.print(", ");
                tft.print(ft6336u.read_touch1_y());
                tft.println(")");
            }

            if (td_status >= 2)
            {
                tft.setCursor(10, 130);
                tft.setTextColor(TFT_WHITE);
                tft.print("P2: (");
                tft.print(ft6336u.read_touch2_x());
                tft.print(", ");
                tft.print(ft6336u.read_touch2_y());
                tft.println(")");
            }

            tft.setCursor(10, 200);
            tft.setTextColor(TFT_CYAN);
            tft.println("Touch screen...");
        }
    }

    delay(50);
}

void tpIntHandle(void)
{
    intStatus = true;
}

Bài viết liên quan