KSM edits

Dependencies:   mbed RA8875

Files at this revision

API Documentation at this revision

Comitter:
kerrysmartin
Date:
Thu Jul 11 14:04:19 2019 +0000
Parent:
19:fee3f71fab2d
Commit message:
Initial check in

Changed in this revision

Button.cpp Show annotated file Show diff for this revision Revisions of this file
Button.h Show annotated file Show diff for this revision Revisions of this file
RA8875.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r fee3f71fab2d -r d25fb9c55781 Button.cpp
--- a/Button.cpp	Wed Jul 10 13:30:27 2019 +0000
+++ b/Button.cpp	Thu Jul 11 14:04:19 2019 +0000
@@ -6,12 +6,8 @@
   *     
   *
   */
-Button::Button(loc_t arg_corner1_x, loc_t arg_corner1_y, loc_t arg_corner2_x, loc_t arg_corner2_y, color_t arg_color, RA8875 *arg_display, const char * arg_name)
-: corner1_x(arg_corner1_x),
-  corner1_y(arg_corner1_y),
-  corner2_x(arg_corner2_x),
-  corner2_y(arg_corner2_y),
-  //button_name(arg_name),
+Button::Button(rect_t arg_rect, color_t arg_color, RA8875 *arg_display, const char * arg_name)
+: button_rect(arg_rect),
   button_color(arg_color),
   lcd(arg_display)
 {
@@ -20,6 +16,28 @@
     snprintf(button_name, len, "%s", arg_name);
 }
 
+Button::Button(loc_t arg_corner1_x, loc_t arg_corner1_y, loc_t arg_corner2_x, loc_t arg_corner2_y, color_t arg_color, RA8875 *arg_display, const char * arg_name)
+{
+
+
+    //button_name(arg_name),
+    button_color = arg_color;
+    lcd = arg_display;
+    button_rect.p1.x = arg_corner1_x; 
+    button_rect.p1.y = arg_corner1_y;
+    button_rect.p2.x = arg_corner2_x;
+    button_rect.p2.y = arg_corner2_y;
+    int len = strlen(arg_name) + 1;
+    button_name = new char[len];
+    snprintf(button_name, len, "%s", arg_name);
+    
+
+}
+
+
+/*
+
+*/
 //Button::Button()
 //: Button(0, 0, 0, 0, RGB(0,0,0), RA8875(), string("noname"))
 //{
@@ -30,11 +48,14 @@
   */
 bool Button::isPressed(loc_t cur_x, loc_t cur_y)
 {
-    if(corner1_x < cur_x && corner2_x > cur_x && corner1_y < cur_y && corner2_y > cur_y)
+    /*if(corner1_x < cur_x && corner2_x > cur_x && corner1_y < cur_y && corner2_y > cur_y)
     {
         return true;
-    }
-    return false;
+    }*/
+    point_t touch_pt; 
+    touch_pt.x = cur_x;
+    touch_pt.y = cur_y;
+    return lcd->Intersect(button_rect, touch_pt);
 }
 
 /** draw
@@ -43,8 +64,8 @@
   */
 void Button::draw()
 {
-    lcd->rect(corner1_x, corner1_y, corner2_x, corner2_y, button_color);
-    lcd->puts(corner1_x, corner2_y, button_name);
+    lcd->rect(button_rect, button_color);
+    lcd->puts(button_rect.p1.x, button_rect.p1.y, button_name);
 }
 
 /** toString
diff -r fee3f71fab2d -r d25fb9c55781 Button.h
--- a/Button.h	Wed Jul 10 13:30:27 2019 +0000
+++ b/Button.h	Thu Jul 11 14:04:19 2019 +0000
@@ -14,13 +14,15 @@
     public:
     //Button();
     Button(loc_t arg_corner1_x, loc_t arg_corner1_y, loc_t arg_corner2_x, loc_t arg_corner2_y, color_t arg_color, RA8875 *arg_display, const char * arg_name);
+    Button(rect_t arg_rect, color_t arg_color, RA8875 *arg_display, const char * arg_name);
     bool isPressed(loc_t cur_x, loc_t cur_y);
     void draw();
     string toString();
+    color_t button_color;
     private:
-    const loc_t corner1_x, corner1_y, corner2_x, corner2_y;
+    rect_t button_rect;
     char * button_name;
-    const color_t button_color;
+    
     RA8875 *lcd;
     //string button_name;
 };
diff -r fee3f71fab2d -r d25fb9c55781 RA8875.lib
--- a/RA8875.lib	Wed Jul 10 13:30:27 2019 +0000
+++ b/RA8875.lib	Thu Jul 11 14:04:19 2019 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/WiredHome/code/RA8875/#4ab96d33a8ec
+https://os.mbed.com/users/kerrysmartin/code/RA8875/#8620cdfcdbf2
diff -r fee3f71fab2d -r d25fb9c55781 main.cpp
--- 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