Extract function next_initialized_sheet from change_sheet_from_menu(). Make it more general to depend on sizeof(Sheets::s) instead of hard coded value of 2.

This commit is contained in:
Marek Bel 2019-08-28 18:59:48 +02:00
parent 33675c6d1e
commit 3903dc819a
1 changed files with 25 additions and 16 deletions

View File

@ -6515,22 +6515,31 @@ static void change_sheet()
menu_back(3); menu_back(3);
} }
static void change_sheet_from_menu(){ //! @brief Get next initialized sheet
uint8_t next_sheet = eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet))+1; //!
while(true){ //! If current sheet is the only sheet initialized, current sheet is returned.
if(next_sheet > 2) next_sheet = 0; //!
if(is_sheet_initialized(next_sheet)){ //! @param sheet Current sheet
eeprom_update_byte(&(EEPROM_Sheets_base->active_sheet), next_sheet); //! @return next initialized sheet
selected_sheet = next_sheet; //! @retval -1 no sheet is initialized
break; static int8_t next_initialized_sheet(int8_t sheet)
} {
else if (next_sheet == selected_sheet){ for (int8_t i = 0; i < static_cast<int8_t>(sizeof(Sheets::s)/sizeof(Sheet)); ++i)
break; {
} ++sheet;
else{ if (sheet >= static_cast<int8_t>(sizeof(Sheets::s)/sizeof(Sheet))) sheet = 0;
next_sheet++; if (is_sheet_initialized(sheet)) return sheet;
} }
} return -1;
}
static void change_sheet_from_menu()
{
int8_t sheet = eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet));
sheet = next_initialized_sheet(sheet);
if (sheet >= 0) eeprom_update_byte(&(EEPROM_Sheets_base->active_sheet), sheet);
menu_back(); menu_back();
} }