A Touchscreen example program showing the RA8875 driver library. This is easily configured for either the Resistive touch panel or the Capacitive touch panel.

Dependencies:   mbed RA8875

Committer:
WiredHome
Date:
Sat Mar 02 12:43:41 2019 +0000
Revision:
2:1d3c502e7f23
Parent:
1:140215c838ce
Child:
3:ea71f57ca5f8
Integrate support for alternate touch controller based on GSL1680 chip (found in some RA8875-based displays instead of the FT5206 chip).

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 0:ec2b5129231f 1 /// PUB_RA8875_Touch Example.
WiredHome 0:ec2b5129231f 2 ///
WiredHome 0:ec2b5129231f 3 /// This touch screen example shows how easy it is to use the RA8875 library with
WiredHome 0:ec2b5129231f 4 /// either the resistive touch panel, _OR_ the capacitive touch panel
WiredHome 0:ec2b5129231f 5 /// (using the FT5206 controller). When used with the capacitive touch
WiredHome 0:ec2b5129231f 6 /// it tracks 5 fingers simultaneously, and only 1 for the resistive panel.
WiredHome 0:ec2b5129231f 7 ///
WiredHome 2:1d3c502e7f23 8 /// @note Copyright © 2016 - 2019 by Smartware Computing, all rights reserved.
WiredHome 0:ec2b5129231f 9 /// Individuals may use this application for evaluation or non-commercial
WiredHome 0:ec2b5129231f 10 /// purposes. Within this restriction, changes may be made to this application
WiredHome 0:ec2b5129231f 11 /// as long as this copyright notice is retained. The user shall make
WiredHome 0:ec2b5129231f 12 /// clear that their work is a derived work, and not the original.
WiredHome 0:ec2b5129231f 13 /// Users of this application and sources accept this application "as is" and
WiredHome 0:ec2b5129231f 14 /// shall hold harmless Smartware Computing, for any undesired results while
WiredHome 0:ec2b5129231f 15 /// using this application - whether real or imagined.
WiredHome 0:ec2b5129231f 16 ///
WiredHome 0:ec2b5129231f 17 /// @author David Smart, Smartware Computing
WiredHome 0:ec2b5129231f 18 //
WiredHome 1:140215c838ce 19 #include "mbed.h" // Last tested: v5.11.4
WiredHome 1:140215c838ce 20 #include "RA8875.h" // Last tested: v164
WiredHome 0:ec2b5129231f 21
WiredHome 0:ec2b5129231f 22
WiredHome 0:ec2b5129231f 23 // // // // // // // // // // // // // // // // // // // // // // // //
WiredHome 0:ec2b5129231f 24 // Configuration section
WiredHome 0:ec2b5129231f 25 // adjust the following information for the screen size, touch panel technology,
WiredHome 0:ec2b5129231f 26 // and port pin assignments.
WiredHome 0:ec2b5129231f 27 // // // // // // // // // // // // // // // // // // // // // // // //
WiredHome 0:ec2b5129231f 28
WiredHome 1:140215c838ce 29 // ############# CRITICAL - Set the display resolution ###############
WiredHome 1:140215c838ce 30 // Define BIG_SCREEN for 800x480 panel, undefine for 480x272
WiredHome 0:ec2b5129231f 31 #define BIG_SCREEN
WiredHome 0:ec2b5129231f 32
WiredHome 0:ec2b5129231f 33 // Define this for Cap touch panel, undefine for resistive
WiredHome 0:ec2b5129231f 34 #define CAP_TOUCH
WiredHome 0:ec2b5129231f 35
WiredHome 0:ec2b5129231f 36 #ifdef CAP_TOUCH
WiredHome 2:1d3c502e7f23 37 //
WiredHome 2:1d3c502e7f23 38 // Constructor when using the display that has a FT5206 Touch Controller
WiredHome 2:1d3c502e7f23 39 //
WiredHome 2:1d3c502e7f23 40 // LCD[ SPI:{MOSI,MISO,SCK}, /ChipSelect, /reset],
WiredHome 2:1d3c502e7f23 41 // Touch[ I2C:{SDA,SCL}, /IRQ],
WiredHome 2:1d3c502e7f23 42 // name
WiredHome 2:1d3c502e7f23 43 //
WiredHome 2:1d3c502e7f23 44 RA8875 lcd(p5,p6,p7,p12,NC, p9,p10,p13, "tft");
WiredHome 2:1d3c502e7f23 45
WiredHome 2:1d3c502e7f23 46 //
WiredHome 2:1d3c502e7f23 47 // Constructor when using the display that has a GSL1680 Touch Controller
WiredHome 2:1d3c502e7f23 48 //
WiredHome 2:1d3c502e7f23 49 // LCD[ SPI:{MOSI,MISO,SCK}, /ChipSelect, /reset],
WiredHome 2:1d3c502e7f23 50 // Touch[ I2C:{SDA,SCL}, Wake, /IRQ],
WiredHome 2:1d3c502e7f23 51 // name
WiredHome 2:1d3c502e7f23 52 //
WiredHome 2:1d3c502e7f23 53 //RA8875 lcd(p5,p6,p7,p12,NC, p9,p10,p14,p13, "tft");
WiredHome 2:1d3c502e7f23 54 //
WiredHome 0:ec2b5129231f 55 #else
WiredHome 2:1d3c502e7f23 56 //
WiredHome 2:1d3c502e7f23 57 // Constructor when using the basic display with integrated Resistive Touch Controller
WiredHome 2:1d3c502e7f23 58 //
WiredHome 2:1d3c502e7f23 59 // LCD[ SPI:{MOSI,MISO,SCK}, /ChipSelect, /reset],
WiredHome 2:1d3c502e7f23 60 // name
WiredHome 2:1d3c502e7f23 61 //
WiredHome 2:1d3c502e7f23 62 RA8875 lcd(p5, p6, p7, p12, NC, "tft");
WiredHome 2:1d3c502e7f23 63
WiredHome 1:140215c838ce 64 void InitTS(void); // Needed for Resistive Touch
WiredHome 1:140215c838ce 65 void CalibrateTS(void); // Needed for Resistive Touch
WiredHome 1:140215c838ce 66 const char * TS_Config = "/local/tpcal.cfg"; // Path and file for storing touch config
WiredHome 1:140215c838ce 67 LocalFileSystem local("local"); // Needed for resistive touch config storage
WiredHome 0:ec2b5129231f 68 #endif
WiredHome 0:ec2b5129231f 69
WiredHome 0:ec2b5129231f 70 #define PC_BAUD 460800 // I like the serial communications to be very fast
WiredHome 0:ec2b5129231f 71
WiredHome 0:ec2b5129231f 72 // // // // // // // // // // // // // // // // // // // // // // // //
WiredHome 0:ec2b5129231f 73 // End of Configuration Section
WiredHome 0:ec2b5129231f 74 // // // // // // // // // // // // // // // // // // // // // // // //
WiredHome 0:ec2b5129231f 75
WiredHome 0:ec2b5129231f 76 Serial pc(USBTX, USBRX); // Not required for display
WiredHome 0:ec2b5129231f 77
WiredHome 0:ec2b5129231f 78 #ifdef BIG_SCREEN
WiredHome 0:ec2b5129231f 79 #define LCD_W 800
WiredHome 0:ec2b5129231f 80 #define LCD_H 480
WiredHome 0:ec2b5129231f 81 #define LCD_C 8 // color - bits per pixel
WiredHome 0:ec2b5129231f 82 #define DEF_RADIUS 50 // default radius of the fingerprint
WiredHome 0:ec2b5129231f 83 #define BL_NORM 25 // Backlight Normal setting (0 to 255)
WiredHome 0:ec2b5129231f 84 #else
WiredHome 0:ec2b5129231f 85 #define LCD_W 480
WiredHome 0:ec2b5129231f 86 #define LCD_H 272
WiredHome 0:ec2b5129231f 87 #define LCD_C 8 // color - bits per pixel
WiredHome 0:ec2b5129231f 88 #define DEF_RADIUS 20 // default radius of the fingerprint
WiredHome 0:ec2b5129231f 89 #define BL_NORM 25 // Backlight Normal setting (0 to 255)
WiredHome 0:ec2b5129231f 90 #endif
WiredHome 0:ec2b5129231f 91
WiredHome 1:140215c838ce 92
WiredHome 1:140215c838ce 93
WiredHome 1:140215c838ce 94
WiredHome 0:ec2b5129231f 95 // When drawing a "fingerprint" under the touch point - the RA8875
WiredHome 1:140215c838ce 96 // cannot clip the drawing at the edge of the screen. Since it cannot
WiredHome 1:140215c838ce 97 // draw an object partially off-screen, this function shrinks the
WiredHome 1:140215c838ce 98 // fingerprint to fit as the touch approaches the edge of the screen.
WiredHome 0:ec2b5129231f 99 //
WiredHome 0:ec2b5129231f 100 int ComputeRadius(point_t p)
WiredHome 0:ec2b5129231f 101 {
WiredHome 0:ec2b5129231f 102 int radius = DEF_RADIUS;
WiredHome 0:ec2b5129231f 103
WiredHome 0:ec2b5129231f 104 if (p.x < radius)
WiredHome 0:ec2b5129231f 105 radius = p.x;
WiredHome 0:ec2b5129231f 106 else if (LCD_W - p.x < radius)
WiredHome 0:ec2b5129231f 107 radius = LCD_W - p.x;
WiredHome 0:ec2b5129231f 108 if (p.y < radius)
WiredHome 0:ec2b5129231f 109 radius = p.y;
WiredHome 0:ec2b5129231f 110 else if (LCD_H - p.y < radius)
WiredHome 0:ec2b5129231f 111 radius = LCD_H - p.y;
WiredHome 0:ec2b5129231f 112 return radius;
WiredHome 0:ec2b5129231f 113 }
WiredHome 0:ec2b5129231f 114
WiredHome 0:ec2b5129231f 115
WiredHome 0:ec2b5129231f 116 // And here is where the fun begins.
WiredHome 0:ec2b5129231f 117 int main()
WiredHome 0:ec2b5129231f 118 {
WiredHome 0:ec2b5129231f 119 color_t fingerColor[5] = {Blue, Red, Green, Yellow, Magenta};
WiredHome 0:ec2b5129231f 120
WiredHome 0:ec2b5129231f 121 pc.baud(PC_BAUD); //I like a snappy terminal, so crank it up!
WiredHome 0:ec2b5129231f 122 pc.printf("\r\nRA8875 Touch Screen Example - Build " __DATE__ " " __TIME__ "\r\n");
WiredHome 0:ec2b5129231f 123
WiredHome 1:140215c838ce 124 lcd.init(LCD_W,LCD_H,LCD_C,40); // 40 is rather dim, but doesn't overload USB ports so easily
WiredHome 0:ec2b5129231f 125 lcd.TouchPanelInit();
WiredHome 1:140215c838ce 126 lcd.foreground(White); // Change to white since it is starting kinda dim.
WiredHome 1:140215c838ce 127 lcd.printf("RA8875 Touch Screen Example - Build " __DATE__ " " __TIME__ "\r\n");
WiredHome 1:140215c838ce 128 lcd.printf("MBED v%d.%d.%d\r\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
WiredHome 0:ec2b5129231f 129
WiredHome 0:ec2b5129231f 130 point_t last[5]; // space for tracking 5 touches
WiredHome 0:ec2b5129231f 131 #ifndef CAP_TOUCH
WiredHome 0:ec2b5129231f 132 InitTS(); // resistive touch calibration
WiredHome 0:ec2b5129231f 133 #endif
WiredHome 0:ec2b5129231f 134
WiredHome 0:ec2b5129231f 135 // draw on one layer and erase the other for smoother transition while
WiredHome 0:ec2b5129231f 136 // is shows both layers.
WiredHome 0:ec2b5129231f 137 lcd.SetLayerMode(RA8875::BooleanOR);
WiredHome 0:ec2b5129231f 138 int layer = 0;
WiredHome 0:ec2b5129231f 139
WiredHome 0:ec2b5129231f 140 while (1) {
WiredHome 0:ec2b5129231f 141 TouchCode_t touch;
WiredHome 0:ec2b5129231f 142
WiredHome 0:ec2b5129231f 143 touch = lcd.TouchPanelReadable(); // any touch to report?
WiredHome 0:ec2b5129231f 144 if (touch) {
WiredHome 0:ec2b5129231f 145 layer++;
WiredHome 0:ec2b5129231f 146 printf("%d: %2X: ", lcd.TouchCount(), lcd.TouchGesture()); // all printf can be removed
WiredHome 0:ec2b5129231f 147
WiredHome 0:ec2b5129231f 148 // TouchChannels reports 1 for resistive panel and 5 for capacitive sense
WiredHome 0:ec2b5129231f 149 for (int i = 0; i < lcd.TouchChannels(); i++) {
WiredHome 0:ec2b5129231f 150 uint8_t id = lcd.TouchID(i); // 'id' tracks the individual touches
WiredHome 0:ec2b5129231f 151 TouchCode_t ev = lcd.TouchCode(i); // 'ev'ent indicates no_touch, touch, held, release, ...
WiredHome 0:ec2b5129231f 152 point_t xy = lcd.TouchCoordinates(i); // and of course the (x,y) coordinates
WiredHome 0:ec2b5129231f 153 int count = lcd.TouchCount(); // how many simultaneous touches
WiredHome 0:ec2b5129231f 154 printf("%2d,%d:(%4d,%4d) ", id, ev, xy.x, xy.y);
WiredHome 0:ec2b5129231f 155 if ((id < 5) || (i < count)) {
WiredHome 0:ec2b5129231f 156 int lastRadius, newRadius;
WiredHome 0:ec2b5129231f 157
WiredHome 0:ec2b5129231f 158 lastRadius = ComputeRadius(last[id]); // To erase the last fingerprint
WiredHome 0:ec2b5129231f 159 newRadius = ComputeRadius(xy); // Shrink near edge of screen
WiredHome 0:ec2b5129231f 160 lcd.SelectDrawingLayer(layer & 1);
WiredHome 0:ec2b5129231f 161 lcd.fillcircle(xy, newRadius, fingerColor[id]); // draw new fingerprint
WiredHome 0:ec2b5129231f 162 lcd.SelectDrawingLayer((layer+1) & 1);
WiredHome 0:ec2b5129231f 163 lcd.fillcircle(last[id], lastRadius, Black); // erase old fingerprint
WiredHome 0:ec2b5129231f 164 last[id] = xy;
WiredHome 0:ec2b5129231f 165 }
WiredHome 0:ec2b5129231f 166 }
WiredHome 0:ec2b5129231f 167 printf("\r\n");
WiredHome 0:ec2b5129231f 168 }
WiredHome 0:ec2b5129231f 169 }
WiredHome 1:140215c838ce 170 }
WiredHome 1:140215c838ce 171
WiredHome 1:140215c838ce 172 //
WiredHome 1:140215c838ce 173 //
WiredHome 1:140215c838ce 174 // For the Reistive Touch Screen, the following are essential.
WiredHome 1:140215c838ce 175 // For the Capacitive Touch Screen, none of the following is required.
WiredHome 1:140215c838ce 176 //
WiredHome 1:140215c838ce 177 //
WiredHome 1:140215c838ce 178 #ifndef CAP_TOUCH
WiredHome 1:140215c838ce 179 // Calibrate the resistive touch screen, and store the data on the
WiredHome 1:140215c838ce 180 // file system.
WiredHome 1:140215c838ce 181 //
WiredHome 1:140215c838ce 182 void CalibrateTS(void)
WiredHome 1:140215c838ce 183 {
WiredHome 1:140215c838ce 184 FILE * fh;
WiredHome 1:140215c838ce 185 tpMatrix_t matrix;
WiredHome 1:140215c838ce 186 RetCode_t r;
WiredHome 1:140215c838ce 187 Timer testperiod;
WiredHome 1:140215c838ce 188
WiredHome 1:140215c838ce 189 r = lcd.TouchPanelCalibrate("Calibrate the touch panel", &matrix);
WiredHome 1:140215c838ce 190 if (r == noerror) {
WiredHome 1:140215c838ce 191 fh = fopen(TS_Config, "wb");
WiredHome 1:140215c838ce 192 if (fh) {
WiredHome 1:140215c838ce 193 fwrite(&matrix, sizeof(tpMatrix_t), 1, fh);
WiredHome 1:140215c838ce 194 fclose(fh);
WiredHome 1:140215c838ce 195 printf(" tp cal written.\r\n");
WiredHome 1:140215c838ce 196 lcd.cls();
WiredHome 1:140215c838ce 197 } else {
WiredHome 1:140215c838ce 198 printf(" couldn't open tpcal file.\r\n");
WiredHome 1:140215c838ce 199 }
WiredHome 1:140215c838ce 200 } else {
WiredHome 1:140215c838ce 201 printf("error return: %d\r\n", r);
WiredHome 1:140215c838ce 202 }
WiredHome 1:140215c838ce 203 lcd.cls();
WiredHome 1:140215c838ce 204 }
WiredHome 1:140215c838ce 205
WiredHome 1:140215c838ce 206 // Try to load a previous resistive touch screen calibration from storage. If it
WiredHome 1:140215c838ce 207 // doesn't exist, activate the touch screen calibration process.
WiredHome 1:140215c838ce 208 //
WiredHome 1:140215c838ce 209 void InitTS(void)
WiredHome 1:140215c838ce 210 {
WiredHome 1:140215c838ce 211 FILE * fh;
WiredHome 1:140215c838ce 212 tpMatrix_t matrix;
WiredHome 1:140215c838ce 213
WiredHome 1:140215c838ce 214 fh = fopen(TS_Config, "rb");
WiredHome 1:140215c838ce 215 if (fh) {
WiredHome 1:140215c838ce 216 fread(&matrix, sizeof(tpMatrix_t), 1, fh);
WiredHome 1:140215c838ce 217 fclose(fh);
WiredHome 1:140215c838ce 218 lcd.TouchPanelSetMatrix(&matrix);
WiredHome 1:140215c838ce 219 printf(" tp cal loaded.\r\n");
WiredHome 1:140215c838ce 220 } else {
WiredHome 1:140215c838ce 221 CalibrateTS();
WiredHome 1:140215c838ce 222 }
WiredHome 1:140215c838ce 223 }
WiredHome 1:140215c838ce 224 #endif // CAP_TOUCH