From ab8ba66067684fd9f86b841ade210550ebff3007 Mon Sep 17 00:00:00 2001 From: Reed Nightingale Date: Sun, 26 Apr 2020 22:43:47 -0700 Subject: [PATCH] Reuse menu running code between quicklist and numpad --- menu_np_ql_shared.cpp | 52 +++++++++++++++++++++++++++++++++++++++++ menu_np_ql_shared.h | 14 +++++++++++ menu_numpad.cpp | 45 ++++++++--------------------------- menu_numpad_buttons.cpp | 6 ++--- menu_numpad_buttons.h | 2 ++ menu_quicklist.cpp | 44 +++++++--------------------------- 6 files changed, 90 insertions(+), 73 deletions(-) create mode 100644 menu_np_ql_shared.cpp create mode 100644 menu_np_ql_shared.h diff --git a/menu_np_ql_shared.cpp b/menu_np_ql_shared.cpp new file mode 100644 index 0000000..fc988d1 --- /dev/null +++ b/menu_np_ql_shared.cpp @@ -0,0 +1,52 @@ +#include "menu_np_ql_shared.h" + +#include + +#include "button.h" +#include "menu_utils.h" + +MenuReturn_e runNpQlShared(const ButtonPress_e tuner_button, + const ButtonPress_e touch_button, + const Point touch_point, + const int16_t knob, + int16_t *const menuSelectedItemRaw, + const Button *const *const menu_buttons, + const uint8_t menu_num_buttons, + ButtonPress_e *const selection_mode) +{ + if(ButtonPress_e::NotPressed != tuner_button){ + uint8_t menu_index = *menuSelectedItemRaw/MENU_KNOB_COUNTS_PER_ITEM; + Button button; + Button* bp; + memcpy_P(&bp,&(menu_buttons[menu_index]),sizeof(bp)); + memcpy_P(&button,bp,sizeof(button)); + *selection_mode = tuner_button; + button.on_select(); + }//tuner_button + + else if(ButtonPress_e::NotPressed != touch_button){ + Button button; + if(findPressedButton(menu_buttons,menu_num_buttons,&button,touch_point)){ + *selection_mode = touch_button; + button.on_select(); + } + else{ + //Touch detected, but not on our buttons, so ignore + } + }//touch_button + + else{//Neither button input type found, so handle the knob + adjustSelector(menuSelectedItemRaw, + knob, + menu_buttons, + menu_num_buttons, + MorsePlaybackType_e::PlayChar); + } + + if(ButtonPress_e::NotPressed == *selection_mode){ + return MenuReturn_e::ExitedRedraw; + } + else{ + return MenuReturn_e::StillActive; + } +} diff --git a/menu_np_ql_shared.h b/menu_np_ql_shared.h new file mode 100644 index 0000000..9a260c9 --- /dev/null +++ b/menu_np_ql_shared.h @@ -0,0 +1,14 @@ +#pragma once + +#include "button.h" +#include "menu.h" + +MenuReturn_e runNpQlShared(const ButtonPress_e tuner_button, + const ButtonPress_e touch_button, + const Point touch_point, + const int16_t knob, + int16_t *const menuSelectedItemRaw, + const Button *const *const menu_buttons, + const uint8_t menu_num_buttons, + ButtonPress_e *const selection_mode); + diff --git a/menu_numpad.cpp b/menu_numpad.cpp index 437bfc3..1fa060f 100644 --- a/menu_numpad.cpp +++ b/menu_numpad.cpp @@ -4,6 +4,7 @@ #include #include "color_theme.h" +#include "menu_np_ql_shared.h" #include "menu_utils.h" #include "nano_gui.h" @@ -36,6 +37,7 @@ void drawNumpad(void) void initNumpad(void) { numpadMenuFrequency = 0; + numpadSelectionMode = ButtonPress_e::LongPress;//Anything except NotPressed drawNumpad(); initSelector(&numpadMenuSelectedItemRaw, numpadMenuButtons, @@ -48,39 +50,12 @@ MenuReturn_e runNumpad(const ButtonPress_e tuner_button, const Point touch_point, const int16_t knob) { - if(ButtonPress_e::NotPressed != tuner_button){ - //We treat long and short presses the same, so no need to have a switch - uint8_t menu_index = numpadMenuSelectedItemRaw/MENU_KNOB_COUNTS_PER_ITEM; - Button button; - Button* bp; - memcpy_P(&bp,&(numpadMenuButtons[menu_index]),sizeof(bp)); - memcpy_P(&button,bp,sizeof(button)); - button.on_select(); - }//tuner_button - - else if(ButtonPress_e::NotPressed != touch_button){ - //We treat long and short presses the same, so no need to have a switch - Button button; - if(findPressedButton(numpadMenuButtons,NUMPAD_MENU_NUM_BUTTONS,&button,touch_point)){ - button.on_select(); - } - else{ - //Touch detected, but not on our buttons, so ignore - } - }//touch_button - - else{//Neither button input type found, so handle the knob - adjustSelector(&numpadMenuSelectedItemRaw, - knob, - numpadMenuButtons, - NUMPAD_MENU_NUM_BUTTONS, - MorsePlaybackType_e::PlayChar); - } - - if(NUMPAD_MENU_EXIT_FREQ == numpadMenuFrequency){ - return MenuReturn_e::ExitedRedraw; - } - else{ - return MenuReturn_e::StillActive; - } + return runNpQlShared(tuner_button, + touch_button, + touch_point, + knob, + &numpadMenuSelectedItemRaw, + numpadMenuButtons, + NUMPAD_MENU_NUM_BUTTONS, + &numpadSelectionMode); } \ No newline at end of file diff --git a/menu_numpad_buttons.cpp b/menu_numpad_buttons.cpp index 252eaa6..c9cdabe 100644 --- a/menu_numpad_buttons.cpp +++ b/menu_numpad_buttons.cpp @@ -22,8 +22,8 @@ static const unsigned int LAYOUT_BUTTON_HEIGHT = 36; static const unsigned int LAYOUT_BUTTON_PITCH_X = 64; static const unsigned int LAYOUT_BUTTON_PITCH_Y = 40; -const uint32_t NUMPAD_MENU_EXIT_FREQ = -1; uint32_t numpadMenuFrequency; +ButtonPress_e numpadSelectionMode; #define D_STRINGIFY(x) #x #define D_STRING(x) D_STRINGIFY(x) @@ -152,10 +152,10 @@ void osOk(void) SetActiveVfoFreq(numpadMenuFrequency); SaveSettingsToEeprom(); setFrequency(numpadMenuFrequency); - numpadMenuFrequency = NUMPAD_MENU_EXIT_FREQ; + numpadSelectionMode = ButtonPress_e::NotPressed; } void osCancel(void) { - numpadMenuFrequency = NUMPAD_MENU_EXIT_FREQ; + numpadSelectionMode = ButtonPress_e::NotPressed; } diff --git a/menu_numpad_buttons.h b/menu_numpad_buttons.h index a7d555e..3628a3f 100644 --- a/menu_numpad_buttons.h +++ b/menu_numpad_buttons.h @@ -3,9 +3,11 @@ #include #include "button.h" +#include "button_press_e.h" extern const Button* const numpadMenuButtons[]; extern const uint8_t NUMPAD_MENU_NUM_BUTTONS; extern const uint32_t NUMPAD_MENU_EXIT_FREQ; extern uint32_t numpadMenuFrequency; +extern ButtonPress_e numpadSelectionMode;//NotPressed means exit menu. Other press types are consumed by selectors diff --git a/menu_quicklist.cpp b/menu_quicklist.cpp index 062c1e3..63526af 100644 --- a/menu_quicklist.cpp +++ b/menu_quicklist.cpp @@ -5,6 +5,7 @@ #include //F() #include "color_theme.h" +#include "menu_np_ql_shared.h" #include "menu_utils.h" #include "nano_gui.h" #include "scratch_space.h" @@ -52,39 +53,12 @@ MenuReturn_e runQuickList(const ButtonPress_e tuner_button, const Point touch_point, const int16_t knob) { - if(ButtonPress_e::NotPressed != tuner_button){ - uint8_t menu_index = quickListMenuSelectedItemRaw/MENU_KNOB_COUNTS_PER_ITEM; - Button button; - Button* bp; - memcpy_P(&bp,&(quickListMenuButtons[menu_index]),sizeof(bp)); - memcpy_P(&button,bp,sizeof(button)); - quickListSelectionMode = tuner_button; - button.on_select(); - }//tuner_button - - else if(ButtonPress_e::NotPressed != touch_button){ - Button button; - if(findPressedButton(quickListMenuButtons,QUICKLIST_MENU_NUM_BUTTONS,&button,touch_point)){ - quickListSelectionMode = touch_button; - button.on_select(); - } - else{ - //Touch detected, but not on our buttons, so ignore - } - }//touch_button - - else{//Neither button input type found, so handle the knob - adjustSelector(&quickListMenuSelectedItemRaw, - knob, - quickListMenuButtons, - QUICKLIST_MENU_NUM_BUTTONS, - MorsePlaybackType_e::PlayChar); - } - - if(ButtonPress_e::NotPressed == quickListSelectionMode){ - return MenuReturn_e::ExitedRedraw; - } - else{ - return MenuReturn_e::StillActive; - } + return runNpQlShared(tuner_button, + touch_button, + touch_point, + knob, + &quickListMenuSelectedItemRaw, + quickListMenuButtons, + QUICKLIST_MENU_NUM_BUTTONS, + &quickListSelectionMode); } \ No newline at end of file