1+ /*
2+ This example uses the Arduino_GigaDisplayTouch and
3+ Arduino_GigaDisplay_GFX library to change the
4+ display whenever it is touched, inverting the colors.
5+
6+ The circuit:
7+ - GIGA R1 WiFi
8+ - GIGA Display Shield
9+
10+ created 10 sept 2023
11+ by Karl Söderby
12+
13+ This example code is in the public domain.
14+ */
15+
16+ #include " Arduino_GigaDisplay_GFX.h"
17+ #include " Arduino_GigaDisplayTouch.h"
18+
19+ Arduino_GigaDisplayTouch touchDetector;
20+ GigaDisplay_GFX display;
21+
22+ #define WHITE 0xffff
23+ #define BLACK 0x0000
24+
25+ #define screen_size_x 480
26+ #define screen_size_y 800
27+
28+ int touch_x;
29+ int touch_y;
30+
31+ int lastTouch;
32+ int threshold = 250 ;
33+
34+ bool switch_1;
35+
36+ void setup () {
37+ // put your setup code here, to run once:
38+ Serial.begin (9600 );
39+ display.begin ();
40+
41+ if (touchDetector.begin ()) {
42+ Serial.print (" Touch controller init - OK" );
43+ } else {
44+ Serial.print (" Touch controller init - FAILED" );
45+ while (1 )
46+ ;
47+ }
48+ changeSwitch ();
49+ }
50+
51+ void loop () {
52+ uint8_t contacts;
53+ GDTpoint_t points[5 ];
54+ contacts = touchDetector.getTouchPoints (points);
55+
56+ if (contacts > 0 && (millis () - lastTouch > threshold)) {
57+ Serial.print (" Contacts: " );
58+ Serial.println (contacts);
59+
60+ // record the x,y coordinates
61+ for (uint8_t i = 0 ; i < contacts; i++) {
62+ touch_x = points[i].x ;
63+ touch_y = points[i].y ;
64+ }
65+
66+ // as the display is 480x800, anywhere you touch the screen it will trigger
67+ if (touch_x < screen_size_x && touch_y < screen_size_y) {
68+ switch_1 = !switch_1;
69+ Serial.println (" switched" );
70+ changeSwitch ();
71+ }
72+ lastTouch = millis ();
73+ }
74+ }
75+
76+ void changeSwitch () {
77+ if (switch_1) {
78+ display.fillScreen (BLACK);
79+ display.setTextColor (WHITE);
80+ } else {
81+ display.fillScreen (WHITE);
82+ display.setTextColor (BLACK);
83+ }
84+ display.setCursor (50 , screen_size_y/2 );
85+ display.setTextSize (5 );
86+ display.print (" Switched" );
87+ }
0 commit comments