Ciao! oggi approfondirò l'utilizzo del display TFT 2.8", che abbiamo già visto in questo post, soffermandoci sull'uso della funzionalità touchscreen del display. per utilizzare la funzionalità touchscreen del display, che è basata sul chip XPT2046, è necessario scaricare ed installare oltre alle librerie Adafruit ILI9341 e Adafruit GFX utilizzate per la visualizzazione, anche la libreria XPT2046_Touchscreen. Lo schema di collegamento è più complesso rispetto al solito, lo trovate qui sotto: Qui sotto invece trovate il codice, è derivato dall'esempio "onoffbutton" della libreria Adafruit ILI9341, ho poi fatto delle leggere modifiche per l'utilizzo con il chip touchscreen XPT2046 //This example implements a simple sliding On/Off button. The example // demonstrates drawing and touch operations. // //Thanks to Adafruit forums member Asteroid for the original sketch! // #include #include #include #include #include // This is calibration data for the raw touch data to the screen coordinates #define TS_MINX 350 #define TS_MINY 350 #define TS_MAXX 3800 #define TS_MAXY 4000 #define TS_CS D0 #define TS_TIRQ D1 XPT2046_Touchscreen ts(TS_CS, TS_TIRQ); // Param 2 - Touch IRQ Pin - interrupt enabled polling #define TFT_CS D8 #define TFT_DC D4 Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC); boolean RecordOn = false; #define FRAME_X 210 #define FRAME_Y 180 #define FRAME_W 100 #define FRAME_H 50 #define REDBUTTON_X FRAME_X #define REDBUTTON_Y FRAME_Y #define REDBUTTON_W (FRAME_W/2) #define REDBUTTON_H FRAME_H #define GREENBUTTON_X (REDBUTTON_X + REDBUTTON_W) #define GREENBUTTON_Y FRAME_Y #define GREENBUTTON_W (FRAME_W/2) #define GREENBUTTON_H FRAME_H void drawFrame() { tft.drawRect(FRAME_X, FRAME_Y, FRAME_W, FRAME_H, ILI9341_BLACK); } void redBtn() { tft.fillRect(REDBUTTON_X, REDBUTTON_Y, REDBUTTON_W, REDBUTTON_H, ILI9341_RED); tft.fillRect(GREENBUTTON_X, GREENBUTTON_Y, GREENBUTTON_W, GREENBUTTON_H, ILI9341_BLUE); drawFrame(); tft.setCursor(GREENBUTTON_X + 6 , GREENBUTTON_Y + (GREENBUTTON_H / 2)); tft.setTextColor(ILI9341_WHITE); tft.setTextSize(2); tft.println("ON"); RecordOn = false; } void greenBtn() { tft.fillRect(GREENBUTTON_X, GREENBUTTON_Y, GREENBUTTON_W, GREENBUTTON_H, ILI9341_GREEN); tft.fillRect(REDBUTTON_X, REDBUTTON_Y, REDBUTTON_W, REDBUTTON_H, ILI9341_BLUE); drawFrame(); tft.setCursor(REDBUTTON_X + 6 , REDBUTTON_Y + (REDBUTTON_H / 2)); tft.setTextColor(ILI9341_WHITE); tft.setTextSize(2); tft.println("OFF"); RecordOn = true; } void setup(void) { Serial.begin(9600); tft.begin(); ts.setRotation(2); if (!ts.begin()) { Serial.println("Unable to start touchscreen."); } else { Serial.println("Touchscreen started."); } tft.fillScreen(ILI9341_BLUE); // origin = left,top landscape (USB left upper) tft.setRotation(1); redBtn(); } void loop() { // See if there's any touch data for us if (!ts.bufferEmpty()) { // Retrieve a point TS_Point p = ts.getPoint(); Serial.print("x = "); Serial.print(p.x); Serial.print(", y = "); Serial.print(p.y); delay(30); Serial.println(); // Scale using the calibration #'s // and rotate coordinate system p.x = map(p.x, TS_MINY, TS_MAXY, 0, tft.height()); p.y = map(p.y, TS_MINX, TS_MAXX, 0, tft.width()); int y = tft.height() - p.x; int x = p.y; if (RecordOn) { if ((x > REDBUTTON_X) && (x < (REDBUTTON_X + REDBUTTON_W))) { if ((y > REDBUTTON_Y) && (y <= (REDBUTTON_Y + REDBUTTON_H))) { Serial.println("Red btn hit"); redBtn(); } } } else //Record is off (RecordOn == false) { if ((x > GREENBUTTON_X) && (x < (GREENBUTTON_X + GREENBUTTON_W))) { if ((y > GREENBUTTON_Y) && (y <= (GREENBUTTON_Y + GREENBUTTON_H))) { Serial.println("Green btn hit"); greenBtn(); } } } Serial.println(RecordOn); } } Questo è tutto! per qualsiasi domanda lascia pure un commento, risponderò appena possibile. Seguimi sulle mie pagine per rimanere sempre aggiornato sui nuovi post!