From 57e194e39d87e4c94bf2f775e8166e9535ed8fec Mon Sep 17 00:00:00 2001
From: Thomas Hooge <thomas@hoogi.de>
Date: Sat, 7 Dec 2024 11:11:07 +0100
Subject: [PATCH] Add new system page

---
 lib/obp60task/OBP60Keypad.h  |   9 +++
 lib/obp60task/PageSystem.cpp | 104 +++++++++++++++++++++++++++++++++++
 lib/obp60task/obp60task.cpp  |  56 +++++++++++++------
 3 files changed, 151 insertions(+), 18 deletions(-)
 create mode 100644 lib/obp60task/PageSystem.cpp

diff --git a/lib/obp60task/OBP60Keypad.h b/lib/obp60task/OBP60Keypad.h
index e51fe9d..c3a11d2 100644
--- a/lib/obp60task/OBP60Keypad.h
+++ b/lib/obp60task/OBP60Keypad.h
@@ -128,6 +128,15 @@ int readKeypad(uint thSensitivity) {
     }
   }
 
+  // System page with key 5 and 4 in fast series
+  if (keycode2 == 5 && keycodeold2 == 4) {
+    keycode = 0;
+    keycodeold = 0;
+    keycode2 = 0;
+    keycodeold2 = 0;
+    keystatus = 12;
+  }
+
   // Key lock with key 1 and 6 or 6 and 1 in fast series
   if((keycode2 == 1 && keycodeold2 == 6) || (keycode2 == 6 && keycodeold2 == 1)) {
     keycode = 0;
diff --git a/lib/obp60task/PageSystem.cpp b/lib/obp60task/PageSystem.cpp
new file mode 100644
index 0000000..fa70a78
--- /dev/null
+++ b/lib/obp60task/PageSystem.cpp
@@ -0,0 +1,104 @@
+#ifdef BOARD_OBP60S3
+
+#include "Pagedata.h"
+#include "OBP60Extensions.h"
+
+/*
+ * Special system page, called directly with fast key sequence 5,4
+ * Out of normal page order.
+ */
+
+class PageSystem : public Page{
+    bool keylock = false;
+
+    public:
+    PageSystem(CommonData &common){
+        common.logger->logDebug(GwLog::LOG,"Show PageSystem");
+    }
+
+    virtual int handleKey(int key){
+        // Code for keylock
+        if (key == 11) {
+            keylock = !keylock;
+            return 0;
+        }
+        return key;
+    }
+
+    virtual void displayPage(CommonData &commonData, PageData &pageData){
+        GwConfigHandler *config = commonData.config;
+        GwLog *logger=commonData.logger;
+
+        // Get config data
+        String displaycolor = config->getString(config->displaycolor);
+        String backlightMode = config->getString(config->backlight);
+        String flashLED = config->getString(config->flashLED);
+
+        // Optical warning by limit violation (unused)
+        if(String(flashLED) == "Limit Violation"){
+            setBlinkingLED(false);
+            setFlashLED(false); 
+        }
+
+        // Logging boat values
+        LOG_DEBUG(GwLog::LOG,"Drawing at PageSystem");
+
+        // Draw page
+        //***********************************************************
+
+        // Set colors
+        int fgcolor = GxEPD_BLACK;
+        int bgcolor = GxEPD_WHITE;
+        if (displaycolor != "Normal") {
+            fgcolor = GxEPD_WHITE;
+            bgcolor = GxEPD_BLACK;
+        }
+
+        // Set display in partial refresh mode
+        getdisplay().setPartialWindow(0, 0, getdisplay().width(), getdisplay().height()); // Set partial update
+
+        getdisplay().setFont(&Ubuntu_Bold12pt7b);
+        getdisplay().setCursor(20, 60);
+        getdisplay().print("System Information and Settings");
+
+        // Key Layout
+        getdisplay().setTextColor(fgcolor);
+        getdisplay().setFont(&Ubuntu_Bold8pt7b);
+        if (keylock == false) {
+            getdisplay().setCursor(10, 290);
+            getdisplay().print("[STBY]");
+            if (String(backlightMode) == "Control by Key") {
+                getdisplay().setCursor(343, 290);
+                getdisplay().print("[ILUM]");
+            }
+        }
+        else {
+            getdisplay().setCursor(130, 290);
+            getdisplay().print(" [    Keylock active    ]");
+        }
+
+        // Update display
+        getdisplay().nextPage();    // Partial update (fast)
+
+    };
+};
+
+static Page* createPage(CommonData &common){
+    return new PageSystem(common);
+}
+
+/**
+ * with the code below we make this page known to the PageTask
+ * we give it a type (name) that can be selected in the config
+ * we define which function is to be called
+ * and we provide the number of user parameters we expect
+ * this will be number of BoatValue pointers in pageData.values
+ */
+PageDescription registerPageSystem(
+    "System",   // Page name
+    createPage, // Action
+    0,          // No bus values
+    true        // Headers are anabled so far
+);
+
+#endif
diff --git a/lib/obp60task/obp60task.cpp b/lib/obp60task/obp60task.cpp
index 3e1dacf..2e7ae83 100644
--- a/lib/obp60task/obp60task.cpp
+++ b/lib/obp60task/obp60task.cpp
@@ -201,8 +201,10 @@ void registerAllPages(PageList &list){
     //this way this separate source file can be compiled by it's own
     //and has no access to any of our data except the one that we
     //give as a parameter to the page function
-    extern PageDescription registerPageOneValue;
+    extern PageDescription registerPageSystem;
     //we add the variable to our list
+    list.add(&registerPageSystem);
+    extern PageDescription registerPageOneValue;
     list.add(&registerPageOneValue);
     extern PageDescription registerPageTwoValues;
     list.add(&registerPageTwoValues);
@@ -470,11 +472,11 @@ void OBP60Task(GwApi *api){
     
     // Main loop runs with 100ms
     //####################################################################################
-    
+
+    bool systemPage = false;
     while (true){
         delay(100);     // Delay 100ms (loop time)
 
-    
         // Undervoltage detection
         if(uvoltage == true){
             underVoltageDetection(api);
@@ -515,9 +517,19 @@ void OBP60Task(GwApi *api){
             int keyboardMessage=0;
             while (xQueueReceive(allParameters.queue,&keyboardMessage,0)){
                 LOG_DEBUG(GwLog::LOG,"new key from keyboard %d",keyboardMessage);
-                
-                Page *currentPage=pages[pageNumber].page;
-                if (currentPage ){
+
+                Page *currentPage;
+                if (keyboardMessage == 12) {
+                    LOG_DEBUG(GwLog::LOG, "Calling system page");
+                    systemPage = true; // System page is out of band
+                    currentPage = allPages.pages[0]->creator(commonData);
+                }
+                else {
+                    systemPage = false;
+                    currentPage  = pages[pageNumber].page;
+                }
+
+                if (currentPage) {
                     keyboardMessage=currentPage->handleKey(keyboardMessage);
                 }
                 if (keyboardMessage > 0)
@@ -641,19 +653,27 @@ void OBP60Task(GwApi *api){
                 }
                 
                 // Call the particular page
-                Page *currentPage=pages[pageNumber].page;
-                if (currentPage == NULL){
-                    LOG_DEBUG(GwLog::ERROR,"page number %d not found",pageNumber);
-                    // Error handling for missing page
+                Page *currentPage;
+                if (systemPage) {
+                    currentPage = allPages.pages[0]->creator(commonData);
+                    PageData sysparams;
+                    currentPage->displayPage(commonData, sysparams);
                 }
-                else{
-                    if (lastPage != pageNumber){
-                        currentPage->displayNew(commonData,pages[pageNumber].parameters);
-                        lastPage=pageNumber;
+                else {
+                    currentPage = pages[pageNumber].page;
+                    if (currentPage == NULL){
+                        LOG_DEBUG(GwLog::ERROR,"page number %d not found",pageNumber);
+                        // Error handling for missing page
+                    }
+                    else{
+                        if (lastPage != pageNumber){
+                            currentPage->displayNew(commonData,pages[pageNumber].parameters);
+                            lastPage=pageNumber;
+                        }
+                        //call the page code
+                        LOG_DEBUG(GwLog::DEBUG,"calling page %d",pageNumber);
+                        currentPage->displayPage(commonData,pages[pageNumber].parameters);
                     }
-                    //call the page code
-                    LOG_DEBUG(GwLog::DEBUG,"calling page %d",pageNumber);
-                    currentPage->displayPage(commonData,pages[pageNumber].parameters);
                 }
             }
         }
@@ -662,4 +682,4 @@ void OBP60Task(GwApi *api){
 
 }
 
-#endif
\ No newline at end of file
+#endif