1
+ // ///////////////////////////////////////////////////////////////
2
+ /*
3
+ ESP32 | Multi-Touch Test(ft. FT6236)
4
+ Video Tutorial: https://youtu.be/8sp4LKYSdEk
5
+ Created by Eric N. (ThatProject)
6
+ */
7
+ // ///////////////////////////////////////////////////////////////
8
+
9
+ #include < TFT_eSPI.h>
10
+ #include < FT6236.h>
11
+
12
+ #define TOUCH_THRESHOLD 80
13
+ #define I2C_SDA 18
14
+ #define I2C_SCL 5
15
+ #define STAR_WIDTH 70
16
+ #define STAR_HEIGHT 80
17
+
18
+ TFT_eSPI tft = TFT_eSPI();
19
+ TFT_eSprite sprImg = TFT_eSprite(&tft);
20
+
21
+ FT6236 ts = FT6236();
22
+ unsigned long previousTouchTimestamp = 0 ;
23
+ unsigned long touchCheckInterval = 50 ;
24
+
25
+ void setup (){
26
+
27
+ Serial.begin (115200 );
28
+
29
+ if (!ts.begin (TOUCH_THRESHOLD, I2C_SDA, I2C_SCL)){
30
+ Serial.println (" Unable to start the capacitive touchscreen." );
31
+ }
32
+
33
+ tft.init ();
34
+ tft.setRotation (3 );
35
+ tft.fillScreen (TFT_BLACK);
36
+ }
37
+
38
+ void loop () {
39
+ getTouchEvent ();
40
+ }
41
+
42
+ void getTouchEvent (){
43
+
44
+ unsigned long currentTouchTimestamp = millis ();
45
+ bool needRefresh = true ;
46
+
47
+ if (currentTouchTimestamp - previousTouchTimestamp > touchCheckInterval) {
48
+ previousTouchTimestamp = currentTouchTimestamp;
49
+
50
+ for (int i=0 ; i< ts.touched (); i++){
51
+ TS_Point p = ts.getPoint (i);
52
+ int16_t x = tft.width () - p.y ;
53
+ int16_t y = p.x ;
54
+
55
+ drawStar (i, x, y, random (0x10000 ));
56
+ needRefresh = false ;
57
+ }
58
+
59
+ if (needRefresh) tft.fillScreen (TFT_BLACK);
60
+ }
61
+ }
62
+
63
+ void drawStar (int id, int x, int y, int star_color)
64
+ {
65
+ // Create an 8 bit sprite 70x 80 pixels (uses 5600 bytes of RAM)
66
+ sprImg.setColorDepth (8 );
67
+ sprImg.createSprite (STAR_WIDTH, STAR_HEIGHT);
68
+
69
+ // Fill Sprite with a "transparent" colour
70
+ // TFT_TRANSPARENT is already defined for convenience
71
+ // We could also fill with any colour as "transparent" and later specify that
72
+ // same colour when we push the Sprite onto the screen.
73
+ sprImg.fillSprite (TFT_TRANSPARENT);
74
+
75
+ // Draw 2 triangles to create a filled in star
76
+ sprImg.fillTriangle (35 , 0 , 0 ,59 , 69 ,59 , star_color);
77
+ sprImg.fillTriangle (35 ,79 , 0 ,20 , 69 ,20 , star_color);
78
+
79
+ // Punch a star shaped hole in the middle with a smaller transparent star
80
+ sprImg.fillTriangle (35 , 7 , 6 ,56 , 63 ,56 , TFT_TRANSPARENT);
81
+ sprImg.fillTriangle (35 ,73 , 6 ,24 , 63 ,24 , TFT_TRANSPARENT);
82
+
83
+ // Push sprite to TFT screen CGRAM at coordinate x,y (top left corner)
84
+ // Specify what colour is to be treated as transparent.
85
+
86
+ sprImg.drawString ((String)id, 0 , 0 , 4 ); // Continue printing from new x position
87
+ sprImg.pushSprite (x - STAR_WIDTH /2 , y - STAR_HEIGHT/2 , TFT_TRANSPARENT);
88
+
89
+ // Delete it to free memory
90
+ sprImg.deleteSprite ();
91
+ }
0 commit comments