GIGA Display Shield Touch Interface

Learn how to use the touch interface on the GIGA Display Shield.

The GIGA Display Shield has an advanced touch interface, supported via the Arduino_GigaDisplayTouch library.

This library is used to return the number of contact points, and the

x,y
coordinates for each of contacts, and in this guide you will learn about different methods to obtain the data.

Hardware & Software Needed

Overview

The Arduino_GigaDisplayTouch library can be used in combination with any of the available graphics libraries (see available options), but is independent from them and works standalone.

The library has two methods of reading sensor data:

  • Polling - continuously read the sensor data through the
    getTouchPoint()
    method.
  • IRQ - only read data when the display is touched through an interrupt and a callback function, using the
    onDetect()
    method.

The number of contacts (fingers touching) can be retrieved and stored as an integer:

1contacts = touchDetector.getTouchPoints(points);

To read the

x
and
y
coordinates, we need to use a specific
struct
called
GDTpoint_t
which contains the values. The example belows simply iterates through the number of
contacts
and stores the coordinates of each contact.

1GDTpoint_t points[5];
2uint8_t contacts;
3
4for (uint8_t i = 0; i < contacts; i++) {
5 x_coordinate = points[i].x;
6 y_coordinate = points[i].y;
7}

Touch Interface

An option to consider

Gesture Detection

As you are able to retrieve multiple touch points at very fast rates, it is possible to create gesture detections. You could for example:

  • change layout when swiping three fingers right,
  • zoom in and out using two fingers,
  • adjust the volume by swiping a finger from the bottom right corner and up,
  • and many more scenarios.

This library can together with the supported graphics libraries create really interactions that we are using in modern day smartphones, tablets etc., with an easy interface and very fast response times.

Polling Example

The polling example demonstrates how to continuously read the touch sensor using the

getTouchPoints()
method. Whenever the display screen is touched, the number of contacts + the coordinates of each contact is printed to the Serial Monitor.

Code Source on Github
1

IRQ Example

The IRQ example demonstrates how to set up an interrupt that triggers a function anytime the screen is touched. The interrupt is set up inside of the

setup()
function, using the
onDetect(function)
method.

Code Source on Github
1

Delay Example

An important factor to consider is that the

loop()
on the GIGA R1 WiFi is executed at a very fast rate, meaning that you will register several touches each time you touch the screen.

This means that whenever you tap the screen, even quickly, you register somewhere between 5-20 touches. So if you want a specific function to only execute once on a specific touch point, you will need to implement a delay in your code.

Using the conventional, but blocking

delay(microseconds)
method is possible but not ideal. The best method to register only a single touch is through using the
millis()
method.

The example below is based on the Polling Example, and limits the if statement to only execute once every

250
milliseconds. This can be edited in the
threshold
variable.

1#include "Arduino_GigaDisplayTouch.h"
2
3Arduino_GigaDisplayTouch touchDetector;
4int lastTouch;
5int threshold = 250; //time in milliseconds
6
7void setup() {
8 Serial.begin(115200);
9 while(!Serial) {}
10
11 if (touchDetector.begin()) {
12 Serial.print("Touch controller init - OK");
13 } else {
14 Serial.print("Touch controller init - FAILED");
15 while(1) ;
16 }
17}
18
19void loop() {
20 uint8_t contacts;
21 GDTpoint_t points[5];
22
23 contacts = touchDetector.getTouchPoints(points);
24
25 // check if more time than the threshold defined has passed
26 if (contacts > 0 && (millis() - lastTouch > threshold) ) {
27 Serial.print("Contacts: ");
28 Serial.println(contacts);
29
30 for (uint8_t i = 0; i < contacts; i++) {
31 Serial.print(points[i].x);
32 Serial.print(" ");
33 Serial.println(points[i].y);
34 }
35 lastTouch = millis(); // register last touch
36 }
37
38 delay(1);
39}

GFX Touch Example

The below example requires uses the Arduino_GigaDisplay_GFX library, and demonstrates how to change a boolean whenever you touch the screen. It implements the

millis()
function to limit the number of executions.

Anytime the screen is touched, the background and text color inverts (black and white).

1#include "Arduino_GigaDisplay_GFX.h"
2#include "Arduino_GigaDisplayTouch.h"
3
4Arduino_GigaDisplayTouch touchDetector;
5GigaDisplay_GFX display;
6
7#define WHITE 0xffff
8#define BLACK 0x0000
9
10#define screen_size_x 480
11#define screen_size_y 800
12
13int touch_x;
14int touch_y;
15
16int lastTouch;
17int threshold = 250;
18
19bool switch_1;
20
21void setup() {
22 // put your setup code here, to run once:
23 Serial.begin(9600);
24 display.begin();
25
26 if (touchDetector.begin()) {
27 Serial.print("Touch controller init - OK");
28 } else {
29 Serial.print("Touch controller init - FAILED");
30 while (1)
31 ;
32 }
33 changeSwitch();
34}
35
36void loop() {
37 uint8_t contacts;
38 GDTpoint_t points[5];
39 contacts = touchDetector.getTouchPoints(points);
40
41 if (contacts > 0 && (millis() - lastTouch > threshold)) {
42 Serial.print("Contacts: ");
43 Serial.println(contacts);
44
45 //record the x,y coordinates
46 for (uint8_t i = 0; i < contacts; i++) {
47 touch_x = points[i].x;
48 touch_y = points[i].y;
49 }
50
51 //as the display is 480x800, anywhere you touch the screen it will trigger
52 if (touch_x < screen_size_x && touch_y < screen_size_y) {
53 switch_1 = !switch_1;
54 Serial.println("switched");
55 changeSwitch();
56 }
57 lastTouch = millis();
58 }
59}
60
61void changeSwitch() {
62 if (switch_1) {
63 display.fillScreen(BLACK);
64 display.setTextColor(WHITE);
65 } else {
66 display.fillScreen(WHITE);
67 display.setTextColor(BLACK);
68 }
69 display.setCursor(50, screen_size_y/2);
70 display.setTextSize(5);
71 display.print("Switched");
72}

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.