witnessmenow / ESP32-Cheap-Yellow-Display

Building a community around a cheap ESP32 Display with a touch screen
MIT License
1.26k stars 128 forks source link

how to get button clicks recognised #174

Closed Bazza1959 closed 1 month ago

Bazza1959 commented 2 months ago

can anyone help me, I have tried both of the following void loop() { // Check for button presses int touchX, touchY; if (tft.getTouchRaw(&touchX, &touchY)) { int btnIndex = touchX / (TFT_WIDTH / NUM_BUTTONS); if (btnIndex >= 0 && btnIndex < NUM_BUTTONS) { btnClickHandlers[btnIndex](); } } and also void loop() { // Check for button presses if (tft.getTouch(&touchX, &touchY)) { int btnIndex = touchX / (TFT_WIDTH / NUM_BUTTONS); if (btnIndex >= 0 && btnIndex < NUM_BUTTONS) { btnClickHandlers[btnIndex](); } } } and get the following error when compiling

no matching function for call to 'TFT_eSPI: :getTouch (int, int)
and this one no matching function for call to 'TFT_eSPI: :getTouchRaw (int, int)

BTW the code is being written by ChatGPT and is as follows

include

include

include

define TFT_WIDTH 800

define TFT_HEIGHT 480

TFT_eSPI tft = TFT_eSPI(); // Initialize the display

// Button positions

define BTN_WIDTH 150

define BTN_HEIGHT 50

define BTN_MARGIN 20

define BTN_START_X 50

define BTN_START_Y 50

// Button colors

define BTN_COLOR 0x7E0 // Green

define BTN_TEXT_COLOR TFT_WHITE

// Button variables

define NUM_BUTTONS 4

const char *buttonLabels[NUM_BUTTONS] = {"Homing", "Speed +", "Speed -", "Start"}; const uint16_t buttonColors[NUM_BUTTONS] = {TFT_RED, TFT_BLUE, TFT_BLUE, TFT_GREEN}; const uint16_t buttonTextColor[NUM_BUTTONS] = {TFT_WHITE, TFT_WHITE, TFT_WHITE, TFT_BLACK};

// WiFi credentials const char ssid = "YourWiFiSSID"; const char password = "YourWiFiPassword";

WiFiServer server(80);

// Button click handlers void btnHomingClicked() { // Handle homing button click Serial.println("Homing button clicked"); }

void btnSpeedIncClicked() { // Handle speed increase button click Serial.println("Speed increase button clicked"); }

void btnSpeedDecClicked() { // Handle speed decrease button click Serial.println("Speed decrease button clicked"); }

void btnStartClicked() { // Handle start button click Serial.println("Start button clicked"); }

// Button click handlers array void (*btnClickHandlers[NUM_BUTTONS])() = {btnHomingClicked, btnSpeedIncClicked, btnSpeedDecClicked, btnStartClicked};

void setup() { Serial.begin(115200);

// Initialize TFT display
tft.begin();
tft.setRotation(3); // Adjust display rotation if needed
tft.fillScreen(TFT_BLACK);

// Draw buttons
for (int i = 0; i < NUM_BUTTONS; i++) {
    int btnX = BTN_START_X;
    int btnY = BTN_START_Y + (BTN_HEIGHT + BTN_MARGIN) * i;
    tft.fillRect(btnX, btnY, BTN_WIDTH, BTN_HEIGHT, buttonColors[i]);
    tft.drawRect(btnX, btnY, BTN_WIDTH, BTN_HEIGHT, TFT_WHITE);
    tft.setTextColor(buttonTextColor[i]);
    tft.setTextSize(2);
    tft.setTextDatum(MC_DATUM);
    tft.drawString(buttonLabels[i], btnX + BTN_WIDTH / 2, btnY + BTN_HEIGHT / 2);
}

// Connect to WiFi network
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
}
Serial.println("WiFi connected");

// Start server
server.begin();

}

void loop() { // Check for button presses int touchX, touchY; if (tft.getTouchRaw(&touchX, &touchY)) { int btnIndex = touchX / (TFT_WIDTH / NUM_BUTTONS); if (btnIndex >= 0 && btnIndex < NUM_BUTTONS) { btnClickHandlers[btnIndex](); } } }

TheNitek commented 1 month ago

I added a new example covering this: https://github.com/witnessmenow/ESP32-Cheap-Yellow-Display/tree/main/Examples/Basics/8-Buttons