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 Feb 23 16:23:33 2019 +0000
Revision:
1:140215c838ce
Parent:
0:ec2b5129231f
Child:
2:1d3c502e7f23
Updated to latest RA8875.; Updated to latest MBED OS (5.11.4).; Minor changes to comments for clarity.

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 0:ec2b5129231f 8 /// @note Copyright © 2016 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 0:ec2b5129231f 37 RA8875 lcd(p5, p6, p7, p12, NC, p9,p10,p13, "tft"); // SPI:{MOSI,MISO,SCK,/ChipSelect,/reset}, I2C:{SDA,SCL,/IRQ}, name
WiredHome 0:ec2b5129231f 38 #else
WiredHome 0:ec2b5129231f 39 RA8875 lcd(p5, p6, p7, p12, NC, "tft"); // SPI:{MOSI,MISO,SCK,/ChipSelect,/reset}, name
WiredHome 1:140215c838ce 40 void InitTS(void); // Needed for Resistive Touch
WiredHome 1:140215c838ce 41 void CalibrateTS(void); // Needed for Resistive Touch
WiredHome 1:140215c838ce 42 const char * TS_Config = "/local/tpcal.cfg"; // Path and file for storing touch config
WiredHome 1:140215c838ce 43 LocalFileSystem local("local"); // Needed for resistive touch config storage
WiredHome 0:ec2b5129231f 44 #endif
WiredHome 0:ec2b5129231f 45
WiredHome 0:ec2b5129231f 46 #define PC_BAUD 460800 // I like the serial communications to be very fast
WiredHome 0:ec2b5129231f 47
WiredHome 0:ec2b5129231f 48 // // // // // // // // // // // // // // // // // // // // // // // //
WiredHome 0:ec2b5129231f 49 // End of Configuration Section
WiredHome 0:ec2b5129231f 50 // // // // // // // // // // // // // // // // // // // // // // // //
WiredHome 0:ec2b5129231f 51
WiredHome 0:ec2b5129231f 52 Serial pc(USBTX, USBRX); // Not required for display
WiredHome 0:ec2b5129231f 53
WiredHome 0:ec2b5129231f 54 #ifdef BIG_SCREEN
WiredHome 0:ec2b5129231f 55 #define LCD_W 800
WiredHome 0:ec2b5129231f 56 #define LCD_H 480
WiredHome 0:ec2b5129231f 57 #define LCD_C 8 // color - bits per pixel
WiredHome 0:ec2b5129231f 58 #define DEF_RADIUS 50 // default radius of the fingerprint
WiredHome 0:ec2b5129231f 59 #define BL_NORM 25 // Backlight Normal setting (0 to 255)
WiredHome 0:ec2b5129231f 60 #else
WiredHome 0:ec2b5129231f 61 #define LCD_W 480
WiredHome 0:ec2b5129231f 62 #define LCD_H 272
WiredHome 0:ec2b5129231f 63 #define LCD_C 8 // color - bits per pixel
WiredHome 0:ec2b5129231f 64 #define DEF_RADIUS 20 // default radius of the fingerprint
WiredHome 0:ec2b5129231f 65 #define BL_NORM 25 // Backlight Normal setting (0 to 255)
WiredHome 0:ec2b5129231f 66 #endif
WiredHome 0:ec2b5129231f 67
WiredHome 1:140215c838ce 68
WiredHome 1:140215c838ce 69
WiredHome 1:140215c838ce 70
WiredHome 0:ec2b5129231f 71 // When drawing a "fingerprint" under the touch point - the RA8875
WiredHome 1:140215c838ce 72 // cannot clip the drawing at the edge of the screen. Since it cannot
WiredHome 1:140215c838ce 73 // draw an object partially off-screen, this function shrinks the
WiredHome 1:140215c838ce 74 // fingerprint to fit as the touch approaches the edge of the screen.
WiredHome 0:ec2b5129231f 75 //
WiredHome 0:ec2b5129231f 76 int ComputeRadius(point_t p)
WiredHome 0:ec2b5129231f 77 {
WiredHome 0:ec2b5129231f 78 int radius = DEF_RADIUS;
WiredHome 0:ec2b5129231f 79
WiredHome 0:ec2b5129231f 80 if (p.x < radius)
WiredHome 0:ec2b5129231f 81 radius = p.x;
WiredHome 0:ec2b5129231f 82 else if (LCD_W - p.x < radius)
WiredHome 0:ec2b5129231f 83 radius = LCD_W - p.x;
WiredHome 0:ec2b5129231f 84 if (p.y < radius)
WiredHome 0:ec2b5129231f 85 radius = p.y;
WiredHome 0:ec2b5129231f 86 else if (LCD_H - p.y < radius)
WiredHome 0:ec2b5129231f 87 radius = LCD_H - p.y;
WiredHome 0:ec2b5129231f 88 return radius;
WiredHome 0:ec2b5129231f 89 }
WiredHome 0:ec2b5129231f 90
WiredHome 0:ec2b5129231f 91
WiredHome 0:ec2b5129231f 92 // And here is where the fun begins.
WiredHome 0:ec2b5129231f 93 int main()
WiredHome 0:ec2b5129231f 94 {
WiredHome 0:ec2b5129231f 95 color_t fingerColor[5] = {Blue, Red, Green, Yellow, Magenta};
WiredHome 0:ec2b5129231f 96
WiredHome 0:ec2b5129231f 97 pc.baud(PC_BAUD); //I like a snappy terminal, so crank it up!
WiredHome 0:ec2b5129231f 98 pc.printf("\r\nRA8875 Touch Screen Example - Build " __DATE__ " " __TIME__ "\r\n");
WiredHome 0:ec2b5129231f 99
WiredHome 1:140215c838ce 100 lcd.init(LCD_W,LCD_H,LCD_C,40); // 40 is rather dim, but doesn't overload USB ports so easily
WiredHome 0:ec2b5129231f 101 lcd.TouchPanelInit();
WiredHome 1:140215c838ce 102 lcd.foreground(White); // Change to white since it is starting kinda dim.
WiredHome 1:140215c838ce 103 lcd.printf("RA8875 Touch Screen Example - Build " __DATE__ " " __TIME__ "\r\n");
WiredHome 1:140215c838ce 104 lcd.printf("MBED v%d.%d.%d\r\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
WiredHome 0:ec2b5129231f 105
WiredHome 0:ec2b5129231f 106 point_t last[5]; // space for tracking 5 touches
WiredHome 0:ec2b5129231f 107 #ifndef CAP_TOUCH
WiredHome 0:ec2b5129231f 108 InitTS(); // resistive touch calibration
WiredHome 0:ec2b5129231f 109 #endif
WiredHome 0:ec2b5129231f 110
WiredHome 0:ec2b5129231f 111 // draw on one layer and erase the other for smoother transition while
WiredHome 0:ec2b5129231f 112 // is shows both layers.
WiredHome 0:ec2b5129231f 113 lcd.SetLayerMode(RA8875::BooleanOR);
WiredHome 0:ec2b5129231f 114 int layer = 0;
WiredHome 0:ec2b5129231f 115
WiredHome 0:ec2b5129231f 116 while (1) {
WiredHome 0:ec2b5129231f 117 TouchCode_t touch;
WiredHome 0:ec2b5129231f 118
WiredHome 0:ec2b5129231f 119 touch = lcd.TouchPanelReadable(); // any touch to report?
WiredHome 0:ec2b5129231f 120 if (touch) {
WiredHome 0:ec2b5129231f 121 layer++;
WiredHome 0:ec2b5129231f 122 printf("%d: %2X: ", lcd.TouchCount(), lcd.TouchGesture()); // all printf can be removed
WiredHome 0:ec2b5129231f 123
WiredHome 0:ec2b5129231f 124 // TouchChannels reports 1 for resistive panel and 5 for capacitive sense
WiredHome 0:ec2b5129231f 125 for (int i = 0; i < lcd.TouchChannels(); i++) {
WiredHome 0:ec2b5129231f 126 uint8_t id = lcd.TouchID(i); // 'id' tracks the individual touches
WiredHome 0:ec2b5129231f 127 TouchCode_t ev = lcd.TouchCode(i); // 'ev'ent indicates no_touch, touch, held, release, ...
WiredHome 0:ec2b5129231f 128 point_t xy = lcd.TouchCoordinates(i); // and of course the (x,y) coordinates
WiredHome 0:ec2b5129231f 129 int count = lcd.TouchCount(); // how many simultaneous touches
WiredHome 0:ec2b5129231f 130 printf("%2d,%d:(%4d,%4d) ", id, ev, xy.x, xy.y);
WiredHome 0:ec2b5129231f 131 if ((id < 5) || (i < count)) {
WiredHome 0:ec2b5129231f 132 int lastRadius, newRadius;
WiredHome 0:ec2b5129231f 133
WiredHome 0:ec2b5129231f 134 lastRadius = ComputeRadius(last[id]); // To erase the last fingerprint
WiredHome 0:ec2b5129231f 135 newRadius = ComputeRadius(xy); // Shrink near edge of screen
WiredHome 0:ec2b5129231f 136 lcd.SelectDrawingLayer(layer & 1);
WiredHome 0:ec2b5129231f 137 lcd.fillcircle(xy, newRadius, fingerColor[id]); // draw new fingerprint
WiredHome 0:ec2b5129231f 138 lcd.SelectDrawingLayer((layer+1) & 1);
WiredHome 0:ec2b5129231f 139 lcd.fillcircle(last[id], lastRadius, Black); // erase old fingerprint
WiredHome 0:ec2b5129231f 140 last[id] = xy;
WiredHome 0:ec2b5129231f 141 }
WiredHome 0:ec2b5129231f 142 }
WiredHome 0:ec2b5129231f 143 printf("\r\n");
WiredHome 0:ec2b5129231f 144 }
WiredHome 0:ec2b5129231f 145 }
WiredHome 1:140215c838ce 146 }
WiredHome 1:140215c838ce 147
WiredHome 1:140215c838ce 148 //
WiredHome 1:140215c838ce 149 //
WiredHome 1:140215c838ce 150 // For the Reistive Touch Screen, the following are essential.
WiredHome 1:140215c838ce 151 // For the Capacitive Touch Screen, none of the following is required.
WiredHome 1:140215c838ce 152 //
WiredHome 1:140215c838ce 153 //
WiredHome 1:140215c838ce 154 #ifndef CAP_TOUCH
WiredHome 1:140215c838ce 155 // Calibrate the resistive touch screen, and store the data on the
WiredHome 1:140215c838ce 156 // file system.
WiredHome 1:140215c838ce 157 //
WiredHome 1:140215c838ce 158 void CalibrateTS(void)
WiredHome 1:140215c838ce 159 {
WiredHome 1:140215c838ce 160 FILE * fh;
WiredHome 1:140215c838ce 161 tpMatrix_t matrix;
WiredHome 1:140215c838ce 162 RetCode_t r;
WiredHome 1:140215c838ce 163 Timer testperiod;
WiredHome 1:140215c838ce 164
WiredHome 1:140215c838ce 165 r = lcd.TouchPanelCalibrate("Calibrate the touch panel", &matrix);
WiredHome 1:140215c838ce 166 if (r == noerror) {
WiredHome 1:140215c838ce 167 fh = fopen(TS_Config, "wb");
WiredHome 1:140215c838ce 168 if (fh) {
WiredHome 1:140215c838ce 169 fwrite(&matrix, sizeof(tpMatrix_t), 1, fh);
WiredHome 1:140215c838ce 170 fclose(fh);
WiredHome 1:140215c838ce 171 printf(" tp cal written.\r\n");
WiredHome 1:140215c838ce 172 lcd.cls();
WiredHome 1:140215c838ce 173 } else {
WiredHome 1:140215c838ce 174 printf(" couldn't open tpcal file.\r\n");
WiredHome 1:140215c838ce 175 }
WiredHome 1:140215c838ce 176 } else {
WiredHome 1:140215c838ce 177 printf("error return: %d\r\n", r);
WiredHome 1:140215c838ce 178 }
WiredHome 1:140215c838ce 179 lcd.cls();
WiredHome 1:140215c838ce 180 }
WiredHome 1:140215c838ce 181
WiredHome 1:140215c838ce 182 // Try to load a previous resistive touch screen calibration from storage. If it
WiredHome 1:140215c838ce 183 // doesn't exist, activate the touch screen calibration process.
WiredHome 1:140215c838ce 184 //
WiredHome 1:140215c838ce 185 void InitTS(void)
WiredHome 1:140215c838ce 186 {
WiredHome 1:140215c838ce 187 FILE * fh;
WiredHome 1:140215c838ce 188 tpMatrix_t matrix;
WiredHome 1:140215c838ce 189
WiredHome 1:140215c838ce 190 fh = fopen(TS_Config, "rb");
WiredHome 1:140215c838ce 191 if (fh) {
WiredHome 1:140215c838ce 192 fread(&matrix, sizeof(tpMatrix_t), 1, fh);
WiredHome 1:140215c838ce 193 fclose(fh);
WiredHome 1:140215c838ce 194 lcd.TouchPanelSetMatrix(&matrix);
WiredHome 1:140215c838ce 195 printf(" tp cal loaded.\r\n");
WiredHome 1:140215c838ce 196 } else {
WiredHome 1:140215c838ce 197 CalibrateTS();
WiredHome 1:140215c838ce 198 }
WiredHome 1:140215c838ce 199 }
WiredHome 1:140215c838ce 200 #endif // CAP_TOUCH