From 98adba28ba7afaec0feb4f005f865751ca09eff4 Mon Sep 17 00:00:00 2001
From: Reed Nightingale <reed.nightingale@gmail.com>
Date: Sat, 15 Feb 2020 14:43:05 -0800
Subject: [PATCH] Have a high level function in charge of finding the active
 submenu

---
 menu.cpp       | 32 ++++++++++++++++++++++++++++++++
 menu.h         |  5 +++++
 menu_main.cpp  | 14 --------------
 menu_utils.cpp | 29 -----------------------------
 menu_utils.h   |  8 --------
 ubitxv6.ino    |  2 +-
 6 files changed, 38 insertions(+), 52 deletions(-)
 create mode 100644 menu.cpp

diff --git a/menu.cpp b/menu.cpp
new file mode 100644
index 0000000..dd00a78
--- /dev/null
+++ b/menu.cpp
@@ -0,0 +1,32 @@
+#include "menu.h"
+#include "menu_main.h"
+
+void runActiveMenu(const ButtonPress_e tuner_button,
+                   const ButtonPress_e touch_button,
+                   const Point touch_point,
+                   const int16_t knob)
+{
+  Menu_t* parent_menu = rootMenu;//rootMenu is it's own parent
+  Menu_t* active_menu = rootMenu;
+  while(nullptr != active_menu->active_submenu){
+    parent_menu = active_menu;
+    active_menu = parent_menu->active_submenu;
+  }
+  MenuReturn_e mr = active_menu->runMenu(tuner_button,touch_button,touch_point,knob);
+  switch(mr){
+    case MenuReturn_e::StillActive://Fallthrough intended
+    case MenuReturn_e::ExitedNoRedraw:
+    {
+      //Nothing to do here - just return
+      break;
+    }
+    default://Fallthrough intended. Default to this menu being active
+    case MenuReturn_e::ExitedRedraw:
+    {
+      //Turn off submenu, redraw, then return
+      parent_menu->active_submenu = nullptr;
+      parent_menu->initMenu();
+      break;
+    }
+  }//end switch
+}
diff --git a/menu.h b/menu.h
index b54e3ff..97d06a6 100644
--- a/menu.h
+++ b/menu.h
@@ -25,3 +25,8 @@ struct Menu_t {
 };
 
 static const uint8_t MENU_KNOB_COUNTS_PER_ITEM = 10;
+
+void runActiveMenu(const ButtonPress_e tuner_button,
+                   const ButtonPress_e touch_button,
+                   const Point touch_point,
+                   const int16_t knob);
diff --git a/menu_main.cpp b/menu_main.cpp
index 9532e0c..fb824ad 100644
--- a/menu_main.cpp
+++ b/menu_main.cpp
@@ -85,20 +85,6 @@ MenuReturn_e runMainMenu(const ButtonPress_e tuner_button,
                          const Point touch_point,
                          const int16_t knob)
 {
-  if(runSubmenu(&mainMenu,
-                drawMainMenu,
-                tuner_button,
-                touch_button,
-                touch_point,
-                knob)){
-    //Submenu processed the input, so return now
-    mainMenuSelectedItemRaw = 0;
-    mainMenuSelecting = false;
-    return MenuReturn_e::StillActive;//main menu always returns StillActive
-  }//end submenu
-
-  //Submenu didn't run, so handle the inputs ourselves
-
   //Check tuner_button
   if(ButtonPress_e::NotPressed != tuner_button){
     switch(tuner_button){
diff --git a/menu_utils.cpp b/menu_utils.cpp
index 11e0fe7..d5fcf6e 100644
--- a/menu_utils.cpp
+++ b/menu_utils.cpp
@@ -8,35 +8,6 @@
 #include "nano_gui.h"
 #include "utils.h"
 
-bool runSubmenu(Menu_t *const current_menu,
-                void(*const redraw_callback)(),
-                const ButtonPress_e tuner_button,
-                const ButtonPress_e touch_button,
-                const Point touch_point,
-                const int16_t knob){
-  if(nullptr != current_menu->active_submenu){
-    auto ret = current_menu->active_submenu->runMenu(tuner_button,touch_button,touch_point,knob);
-    switch(ret){
-      case MenuReturn_e::StillActive://Fallthrough intended
-      case MenuReturn_e::ExitedNoRedraw:
-      {
-        //Nothing to do here - just return
-        break;
-      }
-      default://Fallthrough intended. Default to this menu being active
-      case MenuReturn_e::ExitedRedraw:
-      {
-        //Turn off submenu, redraw, then return
-        current_menu->active_submenu = nullptr;
-        redraw_callback();
-        break;
-      }
-    }//end switch
-    return true;
-  }//end submenu
-  return false;
-}
-
 bool findPressedButton(const Button* const* buttons,
                        const uint8_t num_buttons,
                        Button *const button_out,
diff --git a/menu_utils.h b/menu_utils.h
index 05f43d2..d84be30 100644
--- a/menu_utils.h
+++ b/menu_utils.h
@@ -3,14 +3,6 @@
 #include "button.h"
 #include "menu.h"
 
-//Returns true if submenu was run, false otherwise
-bool runSubmenu(Menu_t* current_menu,
-                void(*const redraw_callback)(),
-                const ButtonPress_e tuner_button,
-                const ButtonPress_e touch_button,
-                const Point touch_point,
-                const int16_t knob);
-
 //Returns true if button was found, false otherwise
 bool findPressedButton(const Button* const* buttons,
                        const uint8_t num_buttons,
diff --git a/ubitxv6.ino b/ubitxv6.ino
index 92ff72a..6c6c5b7 100644
--- a/ubitxv6.ino
+++ b/ubitxv6.ino
@@ -506,5 +506,5 @@ void loop(){
   Point touch_point;
   ButtonPress_e touch_button = checkTouch(&touch_point);
   int16_t knob = enc_read();
-  rootMenu->runMenu(tuner_button,touch_button,touch_point,knob);
+  runActiveMenu(tuner_button,touch_button,touch_point,knob);
 }