Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove extra redundancies #27380

Open
wants to merge 11 commits into
base: bugfix-2.1.x
Choose a base branch
from
133 changes: 58 additions & 75 deletions Marlin/src/module/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3150,96 +3150,79 @@ void MarlinSettings::postprocess() {
}

void MarlinSettings::store_mesh(const int8_t slot) {
const int16_t a = calc_num_meshes();
if (!WITHIN(slot, 0, a - 1)) {
ubl_invalid_slot(a);
DEBUG_ECHOLNPGM("E2END=", persistentStore.capacity() - 1, " meshes_end=", meshes_end, " slot=", slot);
DEBUG_EOL();
return;
}

#if ENABLED(AUTO_BED_LEVELING_UBL)
const int16_t a = calc_num_meshes();
if (!WITHIN(slot, 0, a - 1)) {
ubl_invalid_slot(a);
DEBUG_ECHOLNPGM("E2END=", persistentStore.capacity() - 1, " meshes_end=", meshes_end, " slot=", slot);
DEBUG_EOL();
return;
}

int pos = mesh_slot_offset(slot);
uint16_t crc = 0;

#if ENABLED(OPTIMIZED_MESH_STORAGE)
int16_t z_mesh_store[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y];
bedlevel.set_store_from_mesh(bedlevel.z_values, z_mesh_store);
uint8_t * const src = (uint8_t*)&z_mesh_store;
#else
uint8_t * const src = (uint8_t*)&bedlevel.z_values;
#endif

// Write crc to MAT along with other data, or just tack on to the beginning or end
persistentStore.access_start();
const bool status = persistentStore.write_data(pos, src, MESH_STORE_SIZE, &crc);
persistentStore.access_finish();

if (status) SERIAL_ECHOLNPGM("?Unable to save mesh data.");
else DEBUG_ECHOLNPGM("Mesh saved in slot ", slot);
int pos = mesh_slot_offset(slot);
uint16_t crc = 0;

#if ENABLED(OPTIMIZED_MESH_STORAGE)
int16_t z_mesh_store[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y];
bedlevel.set_store_from_mesh(bedlevel.z_values, z_mesh_store);
uint8_t * const src = (uint8_t*)&z_mesh_store;
#else
uint8_t * const src = (uint8_t*)&bedlevel.z_values;
#endif

// Other mesh types
// Write crc to MAT along with other data, or just tack on to the beginning or end
persistentStore.access_start();
const bool status = persistentStore.write_data(pos, src, MESH_STORE_SIZE, &crc);
persistentStore.access_finish();

#endif
if (status) SERIAL_ECHOLNPGM("?Unable to save mesh data.");
else DEBUG_ECHOLNPGM("Mesh saved in slot ", slot);
}

void MarlinSettings::load_mesh(const int8_t slot, void * const into/*=nullptr*/) {
const int16_t a = settings.calc_num_meshes();

#if ENABLED(AUTO_BED_LEVELING_UBL)

const int16_t a = settings.calc_num_meshes();

if (!WITHIN(slot, 0, a - 1)) {
ubl_invalid_slot(a);
return;
}

int pos = mesh_slot_offset(slot);
uint16_t crc = 0;
#if ENABLED(OPTIMIZED_MESH_STORAGE)
int16_t z_mesh_store[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y];
uint8_t * const dest = (uint8_t*)&z_mesh_store;
#else
uint8_t * const dest = into ? (uint8_t*)into : (uint8_t*)&bedlevel.z_values;
#endif

persistentStore.access_start();
uint16_t status = persistentStore.read_data(pos, dest, MESH_STORE_SIZE, &crc);
persistentStore.access_finish();

#if ENABLED(OPTIMIZED_MESH_STORAGE)
if (into) {
float z_values[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y];
bedlevel.set_mesh_from_store(z_mesh_store, z_values);
memcpy(into, z_values, sizeof(z_values));
}
else
bedlevel.set_mesh_from_store(z_mesh_store, bedlevel.z_values);
#endif
if (!WITHIN(slot, 0, a - 1)) {
ubl_invalid_slot(a);
return;
}

#if ENABLED(DWIN_LCD_PROUI)
status = !bedLevelTools.meshValidate();
if (status) {
bedlevel.invalidate();
LCD_MESSAGE(MSG_UBL_MESH_INVALID);
}
else
ui.status_printf(0, GET_TEXT_F(MSG_MESH_LOADED), bedlevel.storage_slot);
#endif
int pos = mesh_slot_offset(slot);
uint16_t crc = 0;
#if ENABLED(OPTIMIZED_MESH_STORAGE)
int16_t z_mesh_store[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y];
uint8_t * const dest = (uint8_t*)&z_mesh_store;
#else
uint8_t * const dest = into ? (uint8_t*)into : (uint8_t*)&bedlevel.z_values;
#endif

if (status) SERIAL_ECHOLNPGM("?Unable to load mesh data.");
else DEBUG_ECHOLNPGM("Mesh loaded from slot ", slot);
persistentStore.access_start();
uint16_t status = persistentStore.read_data(pos, dest, MESH_STORE_SIZE, &crc);
persistentStore.access_finish();

EEPROM_FINISH();
#if ENABLED(OPTIMIZED_MESH_STORAGE)
if (into) {
float z_values[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y];
bedlevel.set_mesh_from_store(z_mesh_store, z_values);
memcpy(into, z_values, sizeof(z_values));
}
else
bedlevel.set_mesh_from_store(z_mesh_store, bedlevel.z_values);
#endif

#else
#if ENABLED(DWIN_LCD_PROUI)
status = !bedLevelTools.meshValidate();
if (status) {
bedlevel.invalidate();
LCD_MESSAGE(MSG_UBL_MESH_INVALID);
}
else
ui.status_printf(0, GET_TEXT_F(MSG_MESH_LOADED), bedlevel.storage_slot);
#endif

// Other mesh types
if (status) SERIAL_ECHOLNPGM("?Unable to load mesh data.");
else DEBUG_ECHOLNPGM("Mesh loaded from slot ", slot);

#endif
EEPROM_FINISH();
}

//void MarlinSettings::delete_mesh() { return; }
Expand Down