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:
Tue Aug 02 11:30:58 2016 +0000
Revision:
0:ec2b5129231f
Child:
1:140215c838ce
Example using either the RA8875 Resistive touch panel, _OR_ the FT5206 controller based Capacitive touch panel.

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