From 5e30551bf293d4c7aa50515535224638de188a9f Mon Sep 17 00:00:00 2001 From: Reed Nightingale Date: Sun, 9 Feb 2020 17:04:47 -0800 Subject: [PATCH] Move selector logic to menu_utils --- menu_main.cpp | 27 +++++++------------ menu_utils.cpp | 72 +++++++++++++++++++++++++++++++++++++++++--------- menu_utils.h | 21 ++++++++++++--- 3 files changed, 87 insertions(+), 33 deletions(-) diff --git a/menu_main.cpp b/menu_main.cpp index 27e18b8..58b4c97 100644 --- a/menu_main.cpp +++ b/menu_main.cpp @@ -22,7 +22,7 @@ Menu_t mainMenu = { Menu_t* const rootMenu = &mainMenu; bool mainMenuSelecting = false;//Tracks if we're selecting buttons with knob, or adjusting frequency -uint8_t mainMenuSelectedItemRaw = 0; +int16_t mainMenuSelectedItemRaw = 0;//Allow negative only for easier checks on wrap around const Button mainMenuButtons [] PROGMEM = {}; static constexpr uint8_t MAIN_MENU_NUM_BUTTONS = sizeof(mainMenuButtons) / sizeof(mainMenuButtons[0]); @@ -93,7 +93,10 @@ MenuReturn_e runMainMenu(const ButtonPress_e tuner_button, //TODO: activate button } else{ - movePuck(nullptr,&mainMenuButtons[0]); + initSelector(&mainMenuSelectedItemRaw, + mainMenuButtons, + MAIN_MENU_NUM_BUTTONS, + MorsePlaybackType_e::PlayChar); } mainMenuSelecting = !mainMenuSelecting; @@ -132,21 +135,11 @@ MenuReturn_e runMainMenu(const ButtonPress_e tuner_button, else{//Neither button input type found, so handle the knob if(mainMenuSelecting){ - const uint8_t prev_select = mainMenuSelectedItemRaw/MENU_KNOB_COUNTS_PER_ITEM; - mainMenuSelectedItemRaw += LIMIT(mainMenuSelectedItemRaw+knob,0,MAIN_MENU_NUM_BUTTONS*MENU_KNOB_COUNTS_PER_ITEM); - const uint8_t new_select = mainMenuSelectedItemRaw/MENU_KNOB_COUNTS_PER_ITEM; - if(prev_select != new_select){ - movePuck(&mainMenuButtons[prev_select],&mainMenuButtons[new_select]);//TODO - morseLetter(mainMenuButtons[new_select].morse); - int8_t morse_status = 0; - mainMenuButtons[new_select].morse_status(morse_status); - if(morse_status < 0){ - morseBool(false); - } - else if(morse_status > 0){ - morseBool(true); - } - } + adjustSelector(&mainMenuSelectedItemRaw, + knob, + mainMenuButtons, + MAIN_MENU_NUM_BUTTONS, + MorsePlaybackType_e::PlayChar); } else{ mainMenuTune(knob); diff --git a/menu_utils.cpp b/menu_utils.cpp index 7254b81..ff6274c 100644 --- a/menu_utils.cpp +++ b/menu_utils.cpp @@ -1,10 +1,12 @@ -#include "menu.h" +#include "menu_utils.h" #include #include "button.h" #include "color_theme.h" +#include "morse.h" #include "nano_gui.h" +#include "utils.h" bool runSubmenu(Menu_t* const current_menu, void(*const redraw_callback)(), @@ -35,17 +37,6 @@ bool runSubmenu(Menu_t* const current_menu, return false; } -void movePuck(const Button *const b_old, - const Button *const b_new) -{ - if(nullptr != b_old){ - displayRect(b_old->x,b_old->y,b_old->w,b_old->h,COLOR_INACTIVE_BORDER); - } - if(nullptr != b_new){ - displayRect(b_new->x,b_new->y,b_new->w,b_new->h,COLOR_ACTIVE_BORDER); - } -} - bool findPressedButton(const Button *const buttons, const uint8_t num_buttons, Button *const button_out, @@ -63,3 +54,60 @@ bool findPressedButton(const Button *const buttons, return false; } + +void movePuck(const Button *const b_old, + const Button *const b_new) +{ + if(nullptr != b_old){ + displayRect(b_old->x,b_old->y,b_old->w,b_old->h,COLOR_INACTIVE_BORDER); + } + if(nullptr != b_new){ + displayRect(b_new->x,b_new->y,b_new->w,b_new->h,COLOR_ACTIVE_BORDER); + } +} + +void playButtonMorse(const Button *const button, + const MorsePlaybackType_e play_type) +{ + if(MorsePlaybackType_e::PlayText == play_type){ + morseText(button->text); + } + else{ + morseLetter(button->morse); + } + + int8_t morse_status = 0; + button->morse_status(&morse_status); + if(morse_status < 0){ + morseBool(false); + } + else if(morse_status > 0){ + morseBool(true); + } +} + +void initSelector(int16_t *const raw_select_val_in_out, + const Button *const buttons, + const uint8_t num_buttons, + const MorsePlaybackType_e play_type) +{ + *raw_select_val_in_out = 0; + if(0 < num_buttons){ + playButtonMorse(&buttons[0],play_type); + } +} + +void adjustSelector(int16_t *const raw_select_val_in_out, + const int16_t knob, + const Button *const buttons, + const uint8_t num_buttons, + const MorsePlaybackType_e play_type) +{ + const uint8_t prev_select = (*raw_select_val_in_out)/MENU_KNOB_COUNTS_PER_ITEM; + *raw_select_val_in_out += LIMIT((*raw_select_val_in_out)+knob,0,num_buttons*MENU_KNOB_COUNTS_PER_ITEM - 1); + const uint8_t new_select = (*raw_select_val_in_out)/MENU_KNOB_COUNTS_PER_ITEM; + if(prev_select != new_select){ + movePuck(&buttons[prev_select],&buttons[new_select]); + playButtonMorse(&buttons[new_select],play_type); + } +} diff --git a/menu_utils.h b/menu_utils.h index a748b21..4de02d8 100644 --- a/menu_utils.h +++ b/menu_utils.h @@ -1,6 +1,7 @@ #pragma once #include "button.h" +#include "menu.h" //Returns true if submenu was run, false otherwise bool runSubmenu(Menu_t* current_menu, @@ -10,11 +11,23 @@ bool runSubmenu(Menu_t* current_menu, const Point touch_point, const int16_t knob); -void movePuck(const Button *const b_old, - const Button *const b_new); - //Returns true if button was found, false otherwise bool findPressedButton(const Button *const buttons, const uint8_t num_buttons, Button *const button_out, - const Point touch_point); \ No newline at end of file + const Point touch_point); + +enum MorsePlaybackType_e : uint8_t { + PlayChar, + PlayText +}; +void initSelector(int16_t *const raw_select_val_in_out, + const Button *const buttons, + const uint8_t num_buttons, + const MorsePlaybackType_e); + +void adjustSelector(int16_t *const raw_select_val_in_out, + int16_t knob, + const Button *const buttons, + const uint8_t num_buttons, + const MorsePlaybackType_e);