KSM edits

Dependencies:   mbed RA8875

Revision:
20:d25fb9c55781
Parent:
19:fee3f71fab2d
--- a/main.cpp	Wed Jul 10 13:30:27 2019 +0000
+++ b/main.cpp	Thu Jul 11 14:04:19 2019 +0000
@@ -25,13 +25,18 @@
 #define LCD_C 16         // color - bits per pixel
                              // Needed for Resistive Touch
 void CalibrateTS(void);
-
+void InitTS(void); 
 #ifdef CAP_TOUCH
 RA8875 lcd(p5, p6, p7, p12, NC, p9,p10,p13, "tft"); // MOSI,MISO,SCK,/ChipSelect,/reset, SDA,SCL,/IRQ, name
+LocalFileSystem local("local");                     // access to calibration file for resistive touch and printscreen
+
 #else
 RA8875 lcd(p5, p6, p7, p12, NC, "tft");             //MOSI, MISO, SCK, /ChipSelect, /reset, name
+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
-LocalFileSystem local("local");                     // access to calibration file for resistive touch and printscreen
 
 #ifdef BIG_SCREEN
 #define LCD_W 800
@@ -43,7 +48,9 @@
 
 Serial pc(USBTX, USBRX);            // And a little feedback
 Timer measurement;
-Button button_calibration((loc_t)10, (loc_t)10, (loc_t)20, (loc_t)20, (color_t)RGB(255,255,0), &lcd, "CAL");
+// 
+const rect_t button_1 = {10,10,50,50}; // if intersect button_1, poin_of_touch
+Button button_calibration((loc_t)10, (loc_t)10, (loc_t)50, (loc_t)50, (color_t)RGB(0,255,255), &lcd, "CAL");
 Button button_data_rate_select((loc_t)10, (loc_t)10, (loc_t)20, (loc_t)20, (color_t)RGB(255,255,0), &lcd, "250");
 
 RetCode_t callback(RA8875::filecmd_t cmd, uint8_t * buffer, uint16_t size)
@@ -88,6 +95,10 @@
     lcd.frequency(500000);
     lcd.Reset();
     lcd.init(LCD_W, LCD_H, LCD_C, 40);
+    lcd.TouchPanelInit();
+    #ifndef CAP_TOUCH
+    InitTS();               // resistive touch calibration
+    #endif 
     lcd.Backlight(.9);
     // **************************
     //RunTestSet(lcd, pc);  // If the library was compiled for test mode...
@@ -117,29 +128,117 @@
 
     lcd.rect(0,0, 10,10, BrightRed);
 
-    pc.printf("PrintScreen activated ...\r\n");
-    RetCode_t r = lcd.PrintScreen(0,0,LCD_W,LCD_H,"/local/file.bmp", 8);
-    pc.printf("  PrintScreen returned %d - %s\r\n", r, lcd.GetErrorMessage(r));
+    //pc.printf("PrintScreen activated ...\r\n");
+    //RetCode_t r = lcd.PrintScreen(0,0,LCD_W,LCD_H,"/local/file.bmp", 8);
+    //pc.printf("  PrintScreen returned %d - %s\r\n", r, lcd.GetErrorMessage(r));
 
     //lcd.AttachPrintHandler(callback);
     //lcd.PrintScreen(0,0,LCD_W,LCD_H,8);
+    lcd.SelectUserFont(Dave_Smart18x32);
     button_calibration.draw();
 
+    int cycle = 0;
+
+    int xxxx, yyyy;
+    xxxx = 0;
+    yyyy = 0;
+    
     while(1) 
     {
+        
         TouchCode_t touch;
         
         touch = lcd.TouchPanelReadable();
         if(touch)
         {
+            cycle += 1;
+            
             for(int i = 0; i < lcd.TouchChannels(); i++)
             {
-                point_t xy = lcd.TouchCoordinates(i);
+                TouchCode_t ev = lcd.TouchCode(i);
+                point_t xy = lcd.TouchCoordinates(i); 
+                uint8_t id = lcd.TouchID(i);                        // 'id' tracks the individual touches
+                //TouchCode_t ev = lcd.TouchCode(i);                  // 'ev'ent indicates no_touch, touch, held, release, ...
+                //point_t xy = lcd.TouchCoordinates(i);               // and of course the (x,y) coordinates
+                int count = lcd.TouchCount();                       // how many simultaneous touches
+                printf("%2d,%d:(%4d,%4d) \n\r", id, ev, xy.x, xy.y); 
                 if(button_calibration.isPressed(xy.x, xy.y))
                 {
-                    lcd.rect(0,0, LCD_W,LCD_H, BrightCyan);
+                    //lcd.rect(0,0, LCD_W,LCD_H, BrightCyan);
+                    //lcd.rect(xy.x, xy.y, xy.x+20, xy.y+20, BrightCyan);
+                    if(ev == touch)
+                    {
+                        button_calibration.button_color = Magenta;
+                    }
+                    if(ev == release)
+                    {
+                        button_calibration.button_color = BrightCyan;
+                    }
+                    xxxx = xy.x;
+                    yyyy = xy.y;
                 };       // end
+                
+                
             }
+            
+            lcd.printf("x: %d, y: %d, cy: %d\n\r", xxxx, yyyy, cycle);
+            button_calibration.draw();
         }
+        
+        //lcd.printf("Cycle: %i, Touch: %i\n\r", cycle, touch);
     }
-}
\ No newline at end of file
+}
+
+//
+//
+// For the Resistive 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