22
33#include < puara/gestures.h>
44
5- #include < Adafruit_FT6206.h>
5+ #include < Adafruit_FT6206.h> // Touchscreen library
66#include < Adafruit_GFX.h> // Core graphics library
7- #include < Adafruit_ILI9341.h> // Hardware-specific library for ST7789
7+ #include < Adafruit_ILI9341.h> // Display library
88#include < SPI.h>
99
1010/* *
1111 * @class TinyTouch
12- * @brief A class to handle touchscreen interactions using the Adafruit ILI9341 and FT6206 libraries.
12+ * @brief A class to demonstrate how to detect gestures on a touch-array like device (here using
13+ * a capacitive touchscreen available in Wokwi) ran on a TinyPICO board.
1314 *
14- * This class is designed to test the Puara gesture framework by providing a simple interface
15- * for initializing the screen, displaying introductory messages, detecting touch events, and
16- * drawing rectangles on the screen. It uses the Puara Gesture library to recognise gestures.
15+ * This class is designed to demonstrate how to use the Puara gesture framework by providing a
16+ * simple interface for displaying an introductory message, drawing rectangles on the screen to
17+ * simulate a touch array, and detecting touch events on this array. It uses the
18+ * TouchArrayGestureDetector to recognise gestures.
19+ *
20+ * The associated wokwi diagram has one `board-ili9341-cap-touch` touch screen device, which is
21+ * split into its display and touch components in class members `Adafruit_ILI9341 display`
22+ * and `Adafruit_FT6206 touchScreen`.
1723 */
1824class TinyTouch
1925{
@@ -29,13 +35,15 @@ class TinyTouch
2935 */
3036 void initScreen ()
3137 {
32- tft.begin ();
33- screenHeight = tft.height ();
34- screenWidth = tft.width ();
38+ // initialize the display
39+ display.begin ();
40+ screenHeight = display.height ();
41+ screenWidth = display.width ();
3542 rectangleHeight = screenHeight / RECT_COUNT;
3643 Serial.println (F (" Touchscreen is initialized" ));
3744
38- if (!ctp.begin ())
45+ // initialize the touchscreen
46+ if (!touchScreen.begin ())
3947 {
4048 Serial.println (" Couldn't start touchscreen controller" );
4149 while (1 )
@@ -49,56 +57,54 @@ class TinyTouch
4957 */
5058 void printIntro ()
5159 {
52- tft .fillScreen (ILI9341_BLACK);
53- tft .setTextColor (ILI9341_WHITE);
54- tft .setTextSize (2 );
55- tft .setCursor (10 , 100 );
56- tft .println (" Puara Gesture Test!" );
57- tft .setTextSize (1 );
58- tft .setCursor (10 , 130 );
59- tft .println (" Touch the screen to begin." );
60+ display .fillScreen (ILI9341_BLACK);
61+ display .setTextColor (ILI9341_WHITE);
62+ display .setTextSize (2 );
63+ display .setCursor (10 , 100 );
64+ display .println (" Puara Gesture Test!" );
65+ display .setTextSize (1 );
66+ display .setCursor (10 , 130 );
67+ display .println (" Touch the screen to begin." );
6068 }
6169
6270 /* *
6371 * @brief Checks if the touchscreen is currently being touched.
6472 * @return True if the touchscreen is being touched, false otherwise.
6573 */
66- bool touched () { return ctp .touched (); }
74+ bool touched () { return touchScreen .touched (); }
6775
6876 /* *
6977 * @brief Draws rectangles on the screen.
7078 *
7179 * This function divides the screen into a series of rectangles and draws them. These
72- * discrete rectangles will be used by puara to calculate various touch gestures.
80+ * discrete rectangles will simulate stripes in a touch array, which will be used
81+ * by the TouchArrayGestureDetector to calculate various gestures.
7382 */
7483 void drawRectangles ()
7584 {
7685 // Draw rectangles
77- tft .fillScreen (ILI9341_BLACK);
86+ display .fillScreen (ILI9341_BLACK);
7887 for (int i = 0 ; i < RECT_COUNT; i++)
79- tft .drawRect (0 , i * rectangleHeight, screenWidth, rectangleHeight, ILI9341_WHITE);
88+ display .drawRect (0 , i * rectangleHeight, screenWidth, rectangleHeight, ILI9341_WHITE);
8089 }
8190
8291 /* *
83- * @brief Updates the touch state of rectangles and redraws them on the screen .
92+ * @brief Updates the touch state of rectangles and the gestures in the TouchArrayGestureDetector .
8493 *
85- * This function resets the current touch states , checks for new touch inputs,
94+ * This function resets the current touched rectangles , checks for new touch inputs,
8695 * and updates the screen and internal state to reflect changes. Touched
8796 * rectangles are filled with white, while untapped ones are filled with black
8897 * and outlined in white.
89- *
90- * @pre `ctp` must be initialized, and rectangle dimensions must match screen setup.
91- * @post The screen reflects the updated touch states.
9298 */
93- void updateRectangles ()
99+ void update ()
94100 {
95101 // Reset the current touch array
96102 std::fill_n (touchedRectangles, RECT_COUNT, 0 );
97103
98104 // Check for touches
99- if (ctp .touched ())
105+ if (touchScreen .touched ())
100106 {
101- TS_Point p{ctp .getPoint ()};
107+ TS_Point p{touchScreen .getPoint ()};
102108 p.x = map (p.x , 0 , 240 , 240 , 0 );
103109 p.y = map (p.y , 0 , 320 , 320 , 0 );
104110
@@ -113,14 +119,14 @@ class TinyTouch
113119 {
114120 if (touchedRectangles[i] == 1 && previouslyTouchedRectangles[i] == 0 )
115121 {
116- // Rectangle is touched - make it white
117- tft .fillRect (0 , i * rectangleHeight, screenWidth, rectangleHeight, ILI9341_WHITE);
122+ // Rectangle was just touched - make it white
123+ display .fillRect (0 , i * rectangleHeight, screenWidth, rectangleHeight, ILI9341_WHITE);
118124 }
119125 else if (touchedRectangles[i] == 0 && previouslyTouchedRectangles[i] == 1 )
120126 {
121127 // Rectangle no longer touched - make it black
122- tft .fillRect (0 , i * rectangleHeight, screenWidth, rectangleHeight, ILI9341_BLACK);
123- tft .drawRect (0 , i * rectangleHeight, screenWidth, rectangleHeight, ILI9341_WHITE);
128+ display .fillRect (0 , i * rectangleHeight, screenWidth, rectangleHeight, ILI9341_BLACK);
129+ display .drawRect (0 , i * rectangleHeight, screenWidth, rectangleHeight, ILI9341_WHITE);
124130 }
125131 }
126132
@@ -149,8 +155,8 @@ class TinyTouch
149155 int screenWidth{-1 };
150156 int rectangleHeight{-1 };
151157
152- Adafruit_ILI9341 tft {TinyTouch::PIN_CS, TinyTouch::PIN_DC};
153- Adafruit_FT6206 ctp ;
158+ Adafruit_ILI9341 display {TinyTouch::PIN_CS, TinyTouch::PIN_DC};
159+ Adafruit_FT6206 touchScreen ;
154160
155161 // ======================== setup puara =======================
156162 static constexpr int RECT_COUNT{16 };
0 commit comments