Browse Source

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.

Marek Bel 5 years ago
parent
commit
3903dc819a
1 changed files with 25 additions and 16 deletions
  1. 25 16
      Firmware/ultralcd.cpp

+ 25 - 16
Firmware/ultralcd.cpp

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