From 26423f98bcb72bae58d27daeb6d618631f0b408b Mon Sep 17 00:00:00 2001 From: Marek Bel Date: Fri, 30 Aug 2019 12:24:13 +0200 Subject: [PATCH] Switch to next sheet if currently selected sheet is not initialized. Move next_initialized_sheet() and change_sheet_from_menu() to eeprom.cpp, rename it to eeprom_next_initialized_sheet() and eeprom_switch_to_next_sheet(). Rename default_sheet_name() to eeprom_default_sheet_name(). --- Firmware/eeprom.cpp | 37 ++++++++++++++++++++++++++++++++++--- Firmware/eeprom.h | 6 ++++-- Firmware/ultralcd.cpp | 38 +++++++------------------------------- 3 files changed, 45 insertions(+), 36 deletions(-) diff --git a/Firmware/eeprom.cpp b/Firmware/eeprom.cpp index ed10fdcd1..60b5c946b 100644 --- a/Firmware/eeprom.cpp +++ b/Firmware/eeprom.cpp @@ -41,7 +41,8 @@ bool eeprom_is_uninitialized(char *address) return (0xff == eeprom_read_byte(reinterpret_cast(address))); } -bool is_sheet_initialized(uint8_t sheet_num){ +bool eeprom_is_sheet_initialized(uint8_t sheet_num) +{ return (0xffff != eeprom_read_word(reinterpret_cast(&(EEPROM_Sheets_base-> s[sheet_num].z_offset)))); } @@ -80,13 +81,17 @@ void eeprom_init() if(is_uninitialized) { SheetName sheetName; - default_sheet_name(i,sheetName); + eeprom_default_sheet_name(i,sheetName); for (uint_least8_t a = 0; a < sizeof(Sheet::name); ++a){ eeprom_write(&(EEPROM_Sheets_base->s[i].name[a]), sheetName.c[a]); } } } + if(!eeprom_is_sheet_initialized(eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet)))) + { + eeprom_switch_to_next_sheet(); + } check_babystep(); } @@ -94,7 +99,7 @@ void eeprom_init() //! //! @param[in] index //! @param[out] sheetName -void default_sheet_name(uint8_t index, SheetName &sheetName) +void eeprom_default_sheet_name(uint8_t index, SheetName &sheetName) { sheetName.c[0] = '1' + index; for (uint8_t i = 1; i < (sizeof(sheetName.c)/sizeof(sheetName.c[0])); ++i) @@ -102,3 +107,29 @@ void default_sheet_name(uint8_t index, SheetName &sheetName) sheetName.c[i] = '\0'; } } + +//! @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 +int8_t eeprom_next_initialized_sheet(int8_t sheet) +{ + for (int8_t i = 0; i < static_cast(sizeof(Sheets::s)/sizeof(Sheet)); ++i) + { + ++sheet; + if (sheet >= static_cast(sizeof(Sheets::s)/sizeof(Sheet))) sheet = 0; + if (eeprom_is_sheet_initialized(sheet)) return sheet; + } + return -1; +} + +void eeprom_switch_to_next_sheet() +{ + int8_t sheet = eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet)); + + sheet = eeprom_next_initialized_sheet(sheet); + if (sheet >= 0) eeprom_update_byte(&(EEPROM_Sheets_base->active_sheet), sheet); +} diff --git a/Firmware/eeprom.h b/Firmware/eeprom.h index 732cddffd..482ddf932 100644 --- a/Firmware/eeprom.h +++ b/Firmware/eeprom.h @@ -231,12 +231,14 @@ enum #ifdef __cplusplus void eeprom_init(); -bool is_sheet_initialized(uint8_t sheet_num); +bool eeprom_is_sheet_initialized(uint8_t sheet_num); struct SheetName { char c[sizeof(Sheet::name) + 1]; }; -void default_sheet_name(uint8_t index, SheetName &sheetName); +void eeprom_default_sheet_name(uint8_t index, SheetName &sheetName); +int8_t eeprom_next_initialized_sheet(int8_t sheet); +void eeprom_switch_to_next_sheet(); #endif #endif // EEPROM_H diff --git a/Firmware/ultralcd.cpp b/Firmware/ultralcd.cpp index 96ec847da..6f49f9250 100755 --- a/Firmware/ultralcd.cpp +++ b/Firmware/ultralcd.cpp @@ -3098,7 +3098,7 @@ static void lcd_babystep_z() _md->status = 1; check_babystep(); - if(!is_sheet_initialized(eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet)))){ + if(!eeprom_is_sheet_initialized(eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet)))){ _md->babystepMemZ = 0; } else{ @@ -6512,31 +6512,7 @@ static void change_sheet() menu_back(3); } -//! @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(sizeof(Sheets::s)/sizeof(Sheet)); ++i) - { - ++sheet; - if (sheet >= static_cast(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); -} static void lcd_rename_sheet_menu() { @@ -6585,13 +6561,13 @@ static void lcd_rename_sheet_menu() static void lcd_reset_sheet() { SheetName sheetName; - default_sheet_name(selected_sheet, sheetName); + eeprom_default_sheet_name(selected_sheet, sheetName); eeprom_update_word(reinterpret_cast(&(EEPROM_Sheets_base->s[selected_sheet].z_offset)),0xffff); eeprom_update_block(sheetName.c,EEPROM_Sheets_base->s[selected_sheet].name,sizeof(Sheet::name)); if (selected_sheet == eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet))) { - change_sheet_from_menu(); - if((-1 == next_initialized_sheet(0)) && (CALIBRATION_STATUS_CALIBRATED == calibration_status())) + eeprom_switch_to_next_sheet(); + if((-1 == eeprom_next_initialized_sheet(0)) && (CALIBRATION_STATUS_CALIBRATED == calibration_status())) { calibration_status_store(CALIBRATION_STATUS_LIVE_ADJUST); } @@ -6605,7 +6581,7 @@ static void lcd_sheet_menu() MENU_BEGIN(); MENU_ITEM_BACK_P(_i("Steel sheets")); - if(is_sheet_initialized(selected_sheet)){ + if(eeprom_is_sheet_initialized(selected_sheet)){ MENU_ITEM_SUBMENU_P(_i("Select"), change_sheet); //// c=18 } @@ -6711,10 +6687,10 @@ static void lcd_main_menu() if (!farm_mode) { const int8_t sheet = eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet)); - const int8_t nextSheet = next_initialized_sheet(sheet); + const int8_t nextSheet = eeprom_next_initialized_sheet(sheet); if ((nextSheet >= 0) && (sheet != nextSheet)) // show menu only if we have 2 or more sheets initialized { - MENU_ITEM_FUNCTION_E(EEPROM_Sheets_base->s[sheet], change_sheet_from_menu); + MENU_ITEM_FUNCTION_E(EEPROM_Sheets_base->s[sheet], eeprom_switch_to_next_sheet); } } }