Demo of RA8875 TFT touch display on Freescale FRDM-K64F Forked from David Smart https://developer.mbed.org/users/WiredHome/

Dependencies:   RA8875 menu SDFileSystem mbed

Fork of PUB_RA8875_mPaint by David Smart

Revision:
3:3b214426761d
Parent:
2:cf295dad3192
Child:
5:3afc5780b966
--- a/main.cpp	Sat Jan 03 15:41:51 2015 +0000
+++ b/main.cpp	Sat Jan 03 17:12:02 2015 +0000
@@ -105,6 +105,7 @@
 Menu::post_fnc_action_t Tools(uint32_t v);
 Menu::post_fnc_action_t Tools_Type(uint32_t v);
 Menu::post_fnc_action_t PenSize(uint32_t v);
+Menu::post_fnc_action_t HideMenu(uint32_t v);
 
 // Some APIs
 extern "C" void mbed_reset();
@@ -143,6 +144,7 @@
     { "File", File, NULL, NULL, 0, file_menu },
     { "Pen",  NULL, NULL, NULL, 0, pen_menu },
     { "Tools", NULL, NULL, NULL, 0, tools_menu },
+    { "Hide", NULL, NULL, HideMenu, 0, NULL },
     { NULL, NULL, NULL, NULL, 0, NULL },
 };
 
@@ -150,17 +152,20 @@
 
 Menu::post_fnc_action_t File(uint32_t v)
 {
+    (void)v;
     INFO("File");
     return Menu::no_action;
 }
 Menu::post_fnc_action_t File_New(uint32_t v)
 {
+    (void)v;
     INFO("File_New");
     InitDisplay();
     return Menu::no_action;
 }
 Menu::post_fnc_action_t File_Save(uint32_t v)
 {
+    (void)v;
     INFO("File_Save");
     RA8875::LayerMode_T l = lcd.GetLayerMode();
     lcd.SetLayerMode(RA8875::ShowLayer0);
@@ -170,18 +175,21 @@
 }
 Menu::post_fnc_action_t File_Save_All(uint32_t v)
 {
+    (void)v;
     INFO("File_Save_All");
     GetScreenCapture();
     return Menu::close_menu;
 }
 Menu::post_fnc_action_t File_Cal(uint32_t v)
 {
+    (void)v;
     INFO("Tools_Cal");
     CalibrateTS();
     return Menu::no_action;
 }
 Menu::post_fnc_action_t File_Reset(uint32_t v)
 {
+    (void)v;
     INFO("rebooting now...");
     wait_ms(1000);
     mbed_reset();
@@ -189,11 +197,13 @@
 }
 Menu::post_fnc_action_t Edit(uint32_t v)
 {
+    (void)v;
     INFO("Edit");
     return Menu::no_action;
 }
 Menu::post_fnc_action_t Tools(uint32_t v)
 {
+    (void)v;
     INFO("Tools");
     return Menu::no_action;
 }
@@ -222,6 +232,11 @@
     ShowSampleRGB();
     return Menu::close_menu;
 }
+Menu::post_fnc_action_t HideMenu(uint32_t v)
+{
+    (void)v;
+    return Menu::close_menu;
+}
 
 void ShowSampleRGB(void)
 {
@@ -278,11 +293,8 @@
 
 void InitDisplay(void)
 {
-    lcd.cls(3); // both layers
     lcd.SelectDrawingLayer(CANVAS);
-    lcd.fillrect(RGBList[0], Red);
-    lcd.fillrect(RGBList[1], Green);
-    lcd.fillrect(RGBList[2], Blue);
+    lcd.cls();
     lcd.foreground(Blue);
     lcd.background(Black);
 }
@@ -307,16 +319,33 @@
     return 0;
 }
 
-void SeeIfUserSelectingRGBValues(point_t p, TouchCode_t touchcode)
+void ShowRGBSelectors(void)
 {
+    uint16_t curLayer = lcd.GetDrawingLayer();
+    lcd.SelectDrawingLayer(MENUS);
+    lcd.fillrect(RGBList[0], Red);
+    lcd.fillrect(RGBList[1], Green);
+    lcd.fillrect(RGBList[2], Blue);
+    lcd.SelectDrawingLayer(curLayer);
+}
+
+bool SeeIfUserSelectingRGBValues(point_t p, TouchCode_t touchcode)
+{
+    static bool wasIn = false;
+    
     // See if the touch is setting new RGB values
     for (int i=0; i<3; i++) {
         if (lcd.Intersect(RGBList[i], p)) {
             uint8_t mag = (255 * (p.x - RGBList[i].p1.x)) / (RGBList[i].p2.x - RGBList[i].p1.x);
+            wasIn = true;
+            if (touchcode == touch)
+                menu.Show();
+            else if (touchcode == release)
+                menu.Hide();
             rgbVal[i] = mag;
             // update the RGB values
             lcd.SelectDrawingLayer(MENUS);
-            lcd.SetTextCursor(lcd.width() - 100, lcd.height() - 18);
+            lcd.SetTextCursor(lcd.width() - 80, lcd.height() - 16);
             lcd.foreground(Blue);
             lcd.printf("(%02X,%02X,%02X)", rgbVal[0], rgbVal[1], rgbVal[2]);
             // show sample
@@ -325,9 +354,12 @@
             //
             lcd.SelectDrawingLayer(CANVAS);
             lcd.foreground(rgb);
-            break;
+            return true;
         }
     }
+    if (wasIn)
+        menu.Hide();
+    return false;
 }
 
 void ThickLine(point_t origin, point_t p)
@@ -405,9 +437,10 @@
 
     INFO("Turning on display");
     lcd.init();
-    menu.init();
     InitTS();
     InitDisplay();
+    menu.init();
+    ShowRGBSelectors();
 
     INFO("processing loop...");
 
@@ -419,22 +452,17 @@
             int curLayer = lcd.GetDrawingLayer();
             lcd.SelectDrawingLayer(MENUS);
             lcd.foreground(Blue);
-            lcd.SetTextCursor(0, 255);
+            lcd.SetTextCursor(0, lcd.height() - 16);
             lcd.printf("(%3d,%3d) - (%3d,%3d)", origin.x, origin.y, p.x, p.y);
             lcd.SelectDrawingLayer(curLayer);
 
             bool menuHandledIt = menu.HandledTouch(p, touchcode);
             if (menuHandledIt) {
                 // menu handled it
+            } else if (SeeIfUserSelectingRGBValues(p, touchcode)) {
+                // that handled it.
             } else {
-                // app to handle the touch
-                if (!menu.isVisible()) {
-                    SeeIfUserSelectingRGBValues(p, touchcode);
-                    SeeIfUserDrawingOnCanvas(p, touchcode);
-                } else { /* MENU */
-                    WARN("on menu - invalid x,y");
-                    menu.Hide();
-                }
+                SeeIfUserDrawingOnCanvas(p, touchcode);
             }
         } else {
             //non-touch