/* 2.8" ESP32-32E ILI9341 20-Tasten Touch Keyboard 16 normale Tasten + 4 Sondertasten Touch: XPT2046 / TFT_Touch Ausgabe: Display + Serial Basis: funktionierender Hersteller-Demo-Code */ #include #include "TFT_eSPI.h" #include // ------------------------------------------------------------ // TOUCH-PINS – aus der funktionierenden Hersteller-Demo // ------------------------------------------------------------ #define RTP_DOUT 39 #define RTP_DIN 32 #define RTP_SCK 25 #define RTP_CS 33 #define RTP_IRQ 36 // ------------------------------------------------------------ // DISPLAY // ------------------------------------------------------------ TFT_eSPI tft = TFT_eSPI(); TFT_Touch my_touch = TFT_Touch( RTP_CS, RTP_SCK, RTP_DIN, RTP_DOUT ); // ------------------------------------------------------------ // TASTEN // ------------------------------------------------------------ #define NUM_KEYS 20 // 4 Spalten x 5 Reihen #define COLS 4 #define ROWS 5 #define KEY_W 68 #define KEY_H 32 #define KEY_X0 20 //36 von links #define KEY_Y0 28 // 43 von oben #define KEY_GAP_X 8 #define KEY_GAP_Y 7 // ------------------------------------------------------------ // TASTENBESCHRIFTUNG // ------------------------------------------------------------ const char *keyLabel[NUM_KEYS] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "S1", "S2", "S3", "S4" }; // ------------------------------------------------------------ // TASTENCODES // // HIER kannst du später deine echten Codes eintragen. // ------------------------------------------------------------ const uint8_t keyCode[NUM_KEYS] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0xF1, 0xF2, 0xF3, 0xF4 }; // ------------------------------------------------------------ // BUTTON-OBJEKTE // ------------------------------------------------------------ TFT_eSPI_Button key_b[NUM_KEYS]; // verhindert mehrfaches Auslösen bei gehaltenem Finger int lastKey = -1; // ------------------------------------------------------------ // SETUP // ------------------------------------------------------------ void setup() { Serial.begin(115200); // Display starten tft.begin(); // Landscape 320 x 240 tft.setRotation(1); // ---------------------------------------------------------- // DEINE FUNKTIONIERENDE TOUCH-KALIBRIERUNG // ---------------------------------------------------------- my_touch.setCal( 495, 3398, 721, 3448, 320, 240, 1 ); pinMode(RTP_IRQ, INPUT); // ---------------------------------------------------------- // DISPLAY LÖSCHEN // ---------------------------------------------------------- tft.fillScreen(TFT_BLACK); // Titel tft.setTextColor(TFT_WHITE, TFT_BLACK); tft.setTextFont(2); tft.setTextSize(1); tft.setCursor(8, 5); tft.print("TOUCH KEYBOARD"); // ---------------------------------------------------------- // TASTEN ZEICHNEN // ---------------------------------------------------------- for (int i = 0; i < NUM_KEYS; i++) { int col = i % COLS; int row = i / COLS; int x = KEY_X0 + col * (KEY_W + KEY_GAP_X); int y = KEY_Y0 + row * (KEY_H + KEY_GAP_Y); key_b[i].initButton( &tft, x + KEY_W / 2, y + KEY_H / 2, KEY_W, KEY_H, TFT_WHITE, // Rahmen TFT_DARKGREY, // normal TFT_WHITE, // Text "", 1 ); key_b[i].drawButton(false, keyLabel[i]); } // ---------------------------------------------------------- // STATUSZEILE // ---------------------------------------------------------- tft.drawFastHLine(5, 220, 310, TFT_DARKGREY);// statuszeile linie tft.setTextFont(2); tft.setTextColor(TFT_YELLOW, TFT_BLACK); tft.setCursor(8, 224); //statuszeile text tft.print("Taste: - Code: ----"); Serial.println(); Serial.println("================================"); Serial.println("20-Key Touch Keyboard"); Serial.println("Bereit."); Serial.println("================================"); } // ------------------------------------------------------------ // TASTE AUSGEBEN // ------------------------------------------------------------ void sendKey(int key) { Serial.print("Taste: "); Serial.print(keyLabel[key]); Serial.print(" Code: 0x"); if (keyCode[key] < 0x10) Serial.print("0"); Serial.println(keyCode[key], HEX); // ---------------------------------------------------------- // STATUS AUF DISPLAY // ---------------------------------------------------------- tft.fillRect(0, 226, 320, 14, TFT_BLACK); tft.setTextFont(2); tft.setTextColor(TFT_YELLOW, TFT_BLACK); tft.setCursor(8, 224); //statuszeile text tft.print("Taste: "); tft.print(keyLabel[key]); tft.print(" Code: 0x"); if (keyCode[key] < 0x10) tft.print("0"); tft.print(keyCode[key], HEX); // ---------------------------------------------------------- // HIER WIRD DER EIGENTLICHE CODE ALS BYTE GESENDET // // Aktuell über Serial. // ---------------------------------------------------------- Serial.write(keyCode[key]); } // ------------------------------------------------------------ // LOOP // ------------------------------------------------------------ void loop() { bool pressed = my_touch.Pressed(); // ---------------------------------------------------------- // KEIN TOUCH // ---------------------------------------------------------- if (!pressed) { if (lastKey >= 0) { key_b[lastKey].press(false); key_b[lastKey].drawButton(false, keyLabel[lastKey]); lastKey = -1; } delay(10); return; } // ---------------------------------------------------------- // TOUCH-KOORDINATEN EINMAL LESEN // ---------------------------------------------------------- uint16_t x = my_touch.X(); uint16_t y = my_touch.Y(); // ---------------------------------------------------------- // PASSENDE TASTE SUCHEN // ---------------------------------------------------------- int currentKey = -1; for (int i = 0; i < NUM_KEYS; i++) { if (key_b[i].contains(x, y)) { currentKey = i; break; } } // ---------------------------------------------------------- // NICHT AUF EINER TASTE // ---------------------------------------------------------- if (currentKey < 0) { delay(10); return; } // ---------------------------------------------------------- // ANDERE TASTE LOSLASSEN // ---------------------------------------------------------- if (lastKey >= 0 && lastKey != currentKey) { key_b[lastKey].press(false); key_b[lastKey].drawButton(false, keyLabel[lastKey]); } // ---------------------------------------------------------- // NEUE TASTE // ---------------------------------------------------------- if (currentKey != lastKey) { key_b[currentKey].press(true); key_b[currentKey].drawButton(true, keyLabel[currentKey]); sendKey(currentKey); lastKey = currentKey; } delay(10); }