From 4b5a42f86a08a3233871628818a885058ad93d06 Mon Sep 17 00:00:00 2001
From: Scott Lahteine <github@thinkyhead.com>
Date: Sat, 10 Mar 2018 03:07:50 -0600
Subject: [PATCH] Tweaks to E2END, mesh slot offset

---
 Marlin/configuration_store.cpp | 9 ++++++---
 Marlin/configuration_store.h   | 6 +++---
 Marlin/ubl_G29.cpp             | 4 ++--
 3 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/Marlin/configuration_store.cpp b/Marlin/configuration_store.cpp
index 90f567ab4ae..fbde923ea97 100644
--- a/Marlin/configuration_store.cpp
+++ b/Marlin/configuration_store.cpp
@@ -1482,6 +1482,10 @@ void MarlinSettings::postprocess() {
       return (meshes_end - meshes_start_index()) / sizeof(ubl.z_values);
     }
 
+    int MarlinSettings::mesh_slot_offset(const int8_t slot) {
+      return meshes_end - (slot + 1) * sizeof(ubl.z_values);
+    }
+
     void MarlinSettings::store_mesh(const int8_t slot) {
 
       #if ENABLED(AUTO_BED_LEVELING_UBL)
@@ -1497,9 +1501,8 @@ void MarlinSettings::postprocess() {
           return;
         }
 
+        int pos = mesh_slot_offset(slot);
         uint16_t crc = 0;
-        int pos = meshes_end - (slot + 1) * sizeof(ubl.z_values);
-
         write_data(pos, (uint8_t *)&ubl.z_values, sizeof(ubl.z_values), &crc);
 
         // Write crc to MAT along with other data, or just tack on to the beginning or end
@@ -1528,8 +1531,8 @@ void MarlinSettings::postprocess() {
           return;
         }
 
+        int pos = mesh_slot_offset(slot);
         uint16_t crc = 0;
-        int pos = meshes_end - (slot + 1) * sizeof(ubl.z_values);
         uint8_t * const dest = into ? (uint8_t*)into : (uint8_t*)&ubl.z_values;
         read_data(pos, dest, sizeof(ubl.z_values), &crc);
 
diff --git a/Marlin/configuration_store.h b/Marlin/configuration_store.h
index 54102807b79..1f0a33c5998 100644
--- a/Marlin/configuration_store.h
+++ b/Marlin/configuration_store.h
@@ -55,6 +55,7 @@ class MarlinSettings {
         static int16_t meshes_start_index();
         FORCE_INLINE static int16_t meshes_end_index() { return meshes_end; }
         static uint16_t calc_num_meshes();
+        static int mesh_slot_offset(const int8_t slot);
         static void store_mesh(const int8_t slot);
         static void load_mesh(const int8_t slot, void * const into=NULL);
 
@@ -82,9 +83,8 @@ class MarlinSettings {
 
       #if ENABLED(AUTO_BED_LEVELING_UBL) // Eventually make these available if any leveling system
                                          // That can store is enabled
-        static int16_t meshes_begin;
-        const static int16_t meshes_end = E2END - 128; // 128 is a placeholder for the size of the MAT; the MAT will always
-                                                       // live at the very end of the eeprom
+        static constexpr int16_t meshes_end = E2END - 128; // 128 is a placeholder for the size of the MAT; the MAT will always
+                                                           // live at the very end of the eeprom
 
       #endif
 
diff --git a/Marlin/ubl_G29.cpp b/Marlin/ubl_G29.cpp
index d4262ed0be8..cdca64c765f 100644
--- a/Marlin/ubl_G29.cpp
+++ b/Marlin/ubl_G29.cpp
@@ -1163,13 +1163,13 @@
 
     SERIAL_ECHO_START();
     SERIAL_ECHOLNPGM("EEPROM Dump:");
-    for (uint16_t i = 0; i < E2END + 1; i += 16) {
+    for (uint16_t i = 0; i <= E2END; i += 16) {
       if (!(i & 0x3)) idle();
       print_hex_word(i);
       SERIAL_ECHOPGM(": ");
       for (uint16_t j = 0; j < 16; j++) {
         kkkk = i + j;
-        eeprom_read_block(&cccc, (const void *) kkkk, sizeof(unsigned char));
+        eeprom_read_block(&cccc, (const void *)kkkk, sizeof(unsigned char));
         print_hex_byte(cccc);
         SERIAL_ECHO(' ');
       }