Onscreen QWERTY keypad for RA8857 based display. Uses the resistive touch panel.

Dependents:   RA8875_KeyPadDemo PUB_RA8875_Keypad IAC_Final_Monil_copy

Revision:
4:edb5155f1b6f
Parent:
3:402f1126a3ec
Child:
5:7ccb8f7386fa
--- a/Keypad.cpp	Sat Mar 05 16:47:58 2016 +0000
+++ b/Keypad.cpp	Sat Aug 06 20:07:12 2016 +0000
@@ -39,6 +39,8 @@
     shift = false;
     enter_key = KYBD_SYM_ENTER;
     esc_key   = KYBD_SYM_ESCAPE;
+    user_font = NULL;
+    user_font_scale = 0;
 }
 
 Keypad::~Keypad() {
@@ -54,6 +56,14 @@
     return true;
 }
 
+bool Keypad::SetKeyboardFont(const uint8_t * _font, int _fontscale) {
+    if (_font)
+        user_font = _font;
+    else
+        user_font_scale = _fontscale;
+    return true;
+}
+
 void Keypad::DrawKey(rect_t r, char c, bool invert) {
     ra.fillroundrect(r, 2, 2, invert ? fore : back);
     ra.roundrect(r, 2, 2, invert ? back : fore);
@@ -74,13 +84,26 @@
     dim_t kH = ra.fontheight() + 4;
     rect_t r = ComputeKeypadRect();
     
-    ra.fillrect(r, c);    
+    ra.fillrect(r, c);
+    if (user_font)
+        ra.SelectUserFont(restore_font);                   // restore
+    else if (restore_hScale | restore_vScale)
+        ra.SetTextFontSize(restore_hScale, restore_vScale);
+    printf("Restore: font: %p, scale: %d,%d\r\n", user_font, restore_hScale, restore_vScale);    
 }
 
 void Keypad::DrawInputPanel(const char * prompt) {
-    dim_t kH = ra.fontheight() + 4;
-    rect_t r = ComputeKeypadRect();
-
+    rect_t r;
+    
+    if (user_font || user_font_scale) {
+        restore_font = ra.GetUserFont();   // save to later restore
+        ra.GetTextFontSize(&restore_hScale, &restore_vScale);
+        printf("Save   : font: %p, scale: %d,%d\r\n", user_font, restore_hScale, restore_vScale);
+    }
+    if (!user_font && user_font_scale) {
+        ra.SetTextFontSize(user_font_scale);
+    }
+    r = ComputeKeypadRect();
     ra.fillrect(r, back);
     ra.foreground(fore);
     ra.background(back);
@@ -115,10 +138,14 @@
 }
 
 void Keypad::DrawKeypad(void) {
-    dim_t fW = ra.fontwidth();
-    dim_t fH = ra.fontheight();
-    dim_t kW;
-    dim_t kH = fH + 4;
+    dim_t fW;
+    dim_t fH;
+    dim_t kW;       // key width
+    dim_t kH;       // key Height
+    
+    fW = ra.fontwidth();
+    fH = ra.fontheight();
+    kH = fH + 4;
     
     const char * p = kbd->keydef1;
     rect_t r = ComputeKeypadRect();
@@ -187,9 +214,10 @@
     ra.SetTextCursorControl(RA8875::UNDER);
 }
 
-bool Keypad::GetString(char * buffer, size_t size, const char * prompt, char mask) {
+bool Keypad::GetString(char * buffer, size_t size, const char * prompt, char mask, bool auto_close) {
     point_t point = {0, 0};
-
+    bool success = false;
+    
     pStart = pNext = buffer;
     bufSize = size;
     DrawInputPanel(prompt);
@@ -205,19 +233,23 @@
                 TouchCode_t is;
                 do {
                     is = ra.TouchPanelReadable(NULL);
-                    //printf("is: %d\r\n", is);
+                    printf("is: %d\r\n", is);
+                    
                 } while (is != no_touch);
                 DrawKey(touchRect, key);
             }
-            //printf("Touch %02X at (%d,%d)\r\n", key, point.x, point.y);
+            printf("Touch %02X at (%d,%d)\r\n", key, point.x, point.y);
+            printf("Touch %02X at (%d,%d)\r\n", key, point.x, point.y);
             if (key == enter_key) {
                 *pNext = '\0';
                 ra.SetTextCursorControl();
-                return true;
+                success = true;
+                break;
             } else if (key == esc_key) {
                 *buffer = '\0';
                 ra.SetTextCursorControl();
-                return false;
+                success = false;
+                break;
             } else if (key == KYBD_SYM_BS) {
                 if (pNext > buffer) {
                     pNext--;
@@ -246,9 +278,13 @@
             if ((pNext - buffer) >= (size - 1)) {
                 *pNext = '\0';
                 ra.SetTextCursorControl();
-                return true;
+                success = true;
+                break;
             }
             ShowBufferMetrics();
         }
     }
+    if (auto_close)
+        Erase(back);
+    return success;
 }