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