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().

This commit is contained in:
Marek Bel 2019-08-30 12:24:13 +02:00
parent 6c78c2fad5
commit 26423f98bc
3 changed files with 45 additions and 36 deletions

View file

@ -41,7 +41,8 @@ bool eeprom_is_uninitialized<char>(char *address)
return (0xff == eeprom_read_byte(reinterpret_cast<uint8_t*>(address))); return (0xff == eeprom_read_byte(reinterpret_cast<uint8_t*>(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<uint16_t*>(&(EEPROM_Sheets_base-> return (0xffff != eeprom_read_word(reinterpret_cast<uint16_t*>(&(EEPROM_Sheets_base->
s[sheet_num].z_offset)))); s[sheet_num].z_offset))));
} }
@ -80,13 +81,17 @@ void eeprom_init()
if(is_uninitialized) if(is_uninitialized)
{ {
SheetName sheetName; SheetName sheetName;
default_sheet_name(i,sheetName); eeprom_default_sheet_name(i,sheetName);
for (uint_least8_t a = 0; a < sizeof(Sheet::name); ++a){ for (uint_least8_t a = 0; a < sizeof(Sheet::name); ++a){
eeprom_write(&(EEPROM_Sheets_base->s[i].name[a]), sheetName.c[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(); check_babystep();
} }
@ -94,7 +99,7 @@ void eeprom_init()
//! //!
//! @param[in] index //! @param[in] index
//! @param[out] sheetName //! @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; sheetName.c[0] = '1' + index;
for (uint8_t i = 1; i < (sizeof(sheetName.c)/sizeof(sheetName.c[0])); ++i) 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'; 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<int8_t>(sizeof(Sheets::s)/sizeof(Sheet)); ++i)
{
++sheet;
if (sheet >= static_cast<int8_t>(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);
}

View file

@ -231,12 +231,14 @@ enum
#ifdef __cplusplus #ifdef __cplusplus
void eeprom_init(); void eeprom_init();
bool is_sheet_initialized(uint8_t sheet_num); bool eeprom_is_sheet_initialized(uint8_t sheet_num);
struct SheetName struct SheetName
{ {
char c[sizeof(Sheet::name) + 1]; 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
#endif // EEPROM_H #endif // EEPROM_H

View file

@ -3098,7 +3098,7 @@ static void lcd_babystep_z()
_md->status = 1; _md->status = 1;
check_babystep(); 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; _md->babystepMemZ = 0;
} }
else{ else{
@ -6512,31 +6512,7 @@ static void change_sheet()
menu_back(3); 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<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);
}
static void lcd_rename_sheet_menu() static void lcd_rename_sheet_menu()
{ {
@ -6585,13 +6561,13 @@ static void lcd_rename_sheet_menu()
static void lcd_reset_sheet() static void lcd_reset_sheet()
{ {
SheetName sheetName; SheetName sheetName;
default_sheet_name(selected_sheet, sheetName); eeprom_default_sheet_name(selected_sheet, sheetName);
eeprom_update_word(reinterpret_cast<uint16_t *>(&(EEPROM_Sheets_base->s[selected_sheet].z_offset)),0xffff); eeprom_update_word(reinterpret_cast<uint16_t *>(&(EEPROM_Sheets_base->s[selected_sheet].z_offset)),0xffff);
eeprom_update_block(sheetName.c,EEPROM_Sheets_base->s[selected_sheet].name,sizeof(Sheet::name)); 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))) if (selected_sheet == eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet)))
{ {
change_sheet_from_menu(); eeprom_switch_to_next_sheet();
if((-1 == next_initialized_sheet(0)) && (CALIBRATION_STATUS_CALIBRATED == calibration_status())) if((-1 == eeprom_next_initialized_sheet(0)) && (CALIBRATION_STATUS_CALIBRATED == calibration_status()))
{ {
calibration_status_store(CALIBRATION_STATUS_LIVE_ADJUST); calibration_status_store(CALIBRATION_STATUS_LIVE_ADJUST);
} }
@ -6605,7 +6581,7 @@ static void lcd_sheet_menu()
MENU_BEGIN(); MENU_BEGIN();
MENU_ITEM_BACK_P(_i("Steel sheets")); 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 MENU_ITEM_SUBMENU_P(_i("Select"), change_sheet); //// c=18
} }
@ -6711,10 +6687,10 @@ static void lcd_main_menu()
if (!farm_mode) if (!farm_mode)
{ {
const int8_t sheet = eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet)); 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 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);
} }
} }
} }