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

Revision:
1:140215c838ce
Parent:
0:ec2b5129231f
Child:
2:1d3c502e7f23
--- a/main.cpp	Tue Aug 02 11:30:58 2016 +0000
+++ b/main.cpp	Sat Feb 23 16:23:33 2019 +0000
@@ -16,8 +16,8 @@
 ///
 /// @author David Smart, Smartware Computing
 //
-#include "mbed.h"       // Last tested: v122
-#include "RA8875.h"     // Last tested: v125
+#include "mbed.h"       // Last tested: v5.11.4
+#include "RA8875.h"     // Last tested: v164
 
 
 // // // // // // // // // // // // // // // // // // // // // // // // 
@@ -26,7 +26,8 @@
 // and port pin assignments.
 // // // // // // // // // // // // // // // // // // // // // // // // 
 
-// Define this for 800x480 panel, undefine for 480x272
+// ############# CRITICAL - Set the display resolution ###############
+// Define BIG_SCREEN for 800x480 panel, undefine for 480x272
 #define BIG_SCREEN
 
 // Define this for Cap touch panel, undefine for resistive
@@ -36,7 +37,10 @@
 RA8875 lcd(p5, p6, p7, p12, NC, p9,p10,p13, "tft"); // SPI:{MOSI,MISO,SCK,/ChipSelect,/reset}, I2C:{SDA,SCL,/IRQ}, name
 #else
 RA8875 lcd(p5, p6, p7, p12, NC, "tft");             // SPI:{MOSI,MISO,SCK,/ChipSelect,/reset}, name
-LocalFileSystem local("local");                     // access to calibration file for resistive touch.
+void InitTS(void);                                  // Needed for Resistive Touch
+void CalibrateTS(void);                             // Needed for Resistive Touch
+const char * TS_Config = "/local/tpcal.cfg";        // Path and file for storing touch config
+LocalFileSystem local("local");                     // Needed for resistive touch config storage
 #endif
 
 #define PC_BAUD 460800  // I like the serial communications to be very fast
@@ -45,7 +49,6 @@
 // End of Configuration Section
 // // // // // // // // // // // // // // // // // // // // // // // // 
 
-
 Serial pc(USBTX, USBRX);    // Not required for display
 
 #ifdef BIG_SCREEN
@@ -62,9 +65,13 @@
     #define BL_NORM 25      // Backlight Normal setting (0 to 255)
 #endif
 
+
+
+
 // When drawing a "fingerprint" under the touch point - the RA8875 
-// cannot draw an object partially off-screen, so this shrinks the 
-// fingerprint as the touch approaches the edge of the screen.
+// cannot clip the drawing at the edge of the screen. Since it cannot
+// draw an object partially off-screen, this function shrinks the 
+// fingerprint to fit as the touch approaches the edge of the screen.
 //
 int ComputeRadius(point_t p)
 {
@@ -81,51 +88,6 @@
     return radius;
 }
 
-// Calibrate the resistive touch screen, and store the data on the
-// local file system.
-//
-void CalibrateTS(void)
-{
-    FILE * fh;
-    tpMatrix_t matrix;
-    RetCode_t r;
-    Timer testperiod;
- 
-    r = lcd.TouchPanelCalibrate("Calibrate the touch panel", &matrix);
-    if (r == noerror) {
-        fh = fopen("/local/tpcal.cfg", "wb");
-        if (fh) {
-            fwrite(&matrix, sizeof(tpMatrix_t), 1, fh);
-            fclose(fh);
-            printf("  tp cal written.\r\n");
-            lcd.cls();
-        } else {
-            printf("  couldn't open tpcal file.\r\n");
-        }
-    } else {
-        printf("error return: %d\r\n", r);
-    }
-    lcd.cls();
-}
-
-// Try to load a previous resistive touch screen calibration from storage. If it
-// doesn't exist, activate the touch screen calibration process.
-//
-void InitTS(void)
-{
-    FILE * fh;
-    tpMatrix_t matrix;
-
-    fh = fopen("/local/tpcal.cfg", "rb");
-    if (fh) {
-        fread(&matrix, sizeof(tpMatrix_t), 1, fh);
-        fclose(fh);
-        lcd.TouchPanelSetMatrix(&matrix);
-        printf("  tp cal loaded.\r\n");
-    } else {
-        CalibrateTS();
-    }
-}
 
 // And here is where the fun begins.
 int main()
@@ -135,9 +97,11 @@
     pc.baud(PC_BAUD);    //I like a snappy terminal, so crank it up!
     pc.printf("\r\nRA8875 Touch Screen Example - Build " __DATE__ " " __TIME__ "\r\n");
 
-    lcd.init(LCD_W,LCD_H,LCD_C);
+    lcd.init(LCD_W,LCD_H,LCD_C,40); // 40 is rather dim, but doesn't overload USB ports so easily
     lcd.TouchPanelInit();
-    lcd.Backlight_u8(BL_NORM);
+    lcd.foreground(White);          // Change to white since it is starting kinda dim.
+    lcd.printf("RA8875 Touch Screen Example - Build " __DATE__ " " __TIME__ "\r\n");
+    lcd.printf("MBED v%d.%d.%d\r\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
     
     point_t last[5];        // space for tracking 5 touches
     #ifndef CAP_TOUCH
@@ -179,4 +143,58 @@
             printf("\r\n");
         }
     }
-}
\ No newline at end of file
+}
+
+//
+//
+// For the Reistive Touch Screen, the following are essential.
+// For the Capacitive Touch Screen, none of the following is required.
+//
+//
+#ifndef CAP_TOUCH
+// Calibrate the resistive touch screen, and store the data on the
+// file system.
+//
+void CalibrateTS(void)
+{
+    FILE * fh;
+    tpMatrix_t matrix;
+    RetCode_t r;
+    Timer testperiod;
+ 
+    r = lcd.TouchPanelCalibrate("Calibrate the touch panel", &matrix);
+    if (r == noerror) {
+        fh = fopen(TS_Config, "wb");
+        if (fh) {
+            fwrite(&matrix, sizeof(tpMatrix_t), 1, fh);
+            fclose(fh);
+            printf("  tp cal written.\r\n");
+            lcd.cls();
+        } else {
+            printf("  couldn't open tpcal file.\r\n");
+        }
+    } else {
+        printf("error return: %d\r\n", r);
+    }
+    lcd.cls();
+}
+
+// Try to load a previous resistive touch screen calibration from storage. If it
+// doesn't exist, activate the touch screen calibration process.
+//
+void InitTS(void)
+{
+    FILE * fh;
+    tpMatrix_t matrix;
+
+    fh = fopen(TS_Config, "rb");
+    if (fh) {
+        fread(&matrix, sizeof(tpMatrix_t), 1, fh);
+        fclose(fh);
+        lcd.TouchPanelSetMatrix(&matrix);
+        printf("  tp cal loaded.\r\n");
+    } else {
+        CalibrateTS();
+    }
+}
+#endif // CAP_TOUCH
\ No newline at end of file