Browse Source

Optimize MarlinSettings with template methods (#21426)

Ramiro Polla 3 years ago
parent
commit
3ced55aa93
No account linked to committer's email address
2 changed files with 48 additions and 19 deletions
  1. 7
    16
      Marlin/src/module/settings.cpp
  2. 41
    3
      Marlin/src/module/settings.h

+ 7
- 16
Marlin/src/module/settings.cpp View File

@@ -572,13 +572,6 @@ void MarlinSettings::postprocess() {
572 572
 
573 573
 #if ENABLED(EEPROM_SETTINGS)
574 574
 
575
-  #define EEPROM_START()          if (!persistentStore.access_start()) { SERIAL_ECHO_MSG("No EEPROM."); return false; } \
576
-                                  int eeprom_index = EEPROM_OFFSET
577
-  #define EEPROM_FINISH()         persistentStore.access_finish()
578
-  #define EEPROM_SKIP(VAR)        (eeprom_index += sizeof(VAR))
579
-  #define EEPROM_WRITE(VAR)       do{ persistentStore.write_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc);              }while(0)
580
-  #define EEPROM_READ(VAR)        do{ persistentStore.read_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc, !validating);  }while(0)
581
-  #define EEPROM_READ_ALWAYS(VAR) do{ persistentStore.read_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc);               }while(0)
582 575
   #define EEPROM_ASSERT(TST,ERR)  do{ if (!(TST)) { SERIAL_ERROR_MSG(ERR); eeprom_error = true; } }while(0)
583 576
 
584 577
   #if ENABLED(DEBUG_EEPROM_READWRITE)
@@ -594,6 +587,8 @@ void MarlinSettings::postprocess() {
594 587
   const char version[4] = EEPROM_VERSION;
595 588
 
596 589
   bool MarlinSettings::eeprom_error, MarlinSettings::validating;
590
+  int MarlinSettings::eeprom_index;
591
+  uint16_t MarlinSettings::working_crc;
597 592
 
598 593
   bool MarlinSettings::size_error(const uint16_t size) {
599 594
     if (size != datasize()) {
@@ -610,9 +605,7 @@ void MarlinSettings::postprocess() {
610 605
     float dummyf = 0;
611 606
     char ver[4] = "ERR";
612 607
 
613
-    uint16_t working_crc = 0;
614
-
615
-    EEPROM_START();
608
+    if (!EEPROM_START(EEPROM_OFFSET)) return false;
616 609
 
617 610
     eeprom_error = false;
618 611
 
@@ -1456,9 +1449,7 @@ void MarlinSettings::postprocess() {
1456 1449
    * M501 - Retrieve Configuration
1457 1450
    */
1458 1451
   bool MarlinSettings::_load() {
1459
-    uint16_t working_crc = 0;
1460
-
1461
-    EEPROM_START();
1452
+    if (!EEPROM_START(EEPROM_OFFSET)) return false;
1462 1453
 
1463 1454
     char stored_ver[4];
1464 1455
     EEPROM_READ_ALWAYS(stored_ver);
@@ -1496,10 +1487,10 @@ void MarlinSettings::postprocess() {
1496 1487
         uint32_t tmp1[XYZ + esteppers];
1497 1488
         float tmp2[XYZ + esteppers];
1498 1489
         feedRate_t tmp3[XYZ + esteppers];
1499
-        EEPROM_READ(tmp1);                         // max_acceleration_mm_per_s2
1490
+        EEPROM_READ((uint8_t *)tmp1, sizeof(tmp1)); // max_acceleration_mm_per_s2
1500 1491
         EEPROM_READ(planner.settings.min_segment_time_us);
1501
-        EEPROM_READ(tmp2);                         // axis_steps_per_mm
1502
-        EEPROM_READ(tmp3);                         // max_feedrate_mm_s
1492
+        EEPROM_READ((uint8_t *)tmp2, sizeof(tmp2)); // axis_steps_per_mm
1493
+        EEPROM_READ((uint8_t *)tmp3, sizeof(tmp3)); // max_feedrate_mm_s
1503 1494
 
1504 1495
         if (!validating) LOOP_XYZE_N(i) {
1505 1496
           const bool in = (i < esteppers + XYZ);

+ 41
- 3
Marlin/src/module/settings.h View File

@@ -76,12 +76,15 @@ class MarlinSettings {
76 76
         //static void delete_mesh();    // necessary if we have a MAT
77 77
         //static void defrag_meshes();  // "
78 78
       #endif
79
-    #else
79
+
80
+    #else // !EEPROM_SETTINGS
81
+
80 82
       FORCE_INLINE
81 83
       static bool load() { reset(); report(); return true; }
82 84
       FORCE_INLINE
83 85
       static void first_load() { (void)load(); }
84
-    #endif
86
+
87
+    #endif // !EEPROM_SETTINGS
85 88
 
86 89
     #if DISABLED(DISABLE_M503)
87 90
       static void report(const bool forReplay=false);
@@ -105,7 +108,42 @@ class MarlinSettings {
105 108
 
106 109
       static bool _load();
107 110
       static bool size_error(const uint16_t size);
108
-    #endif
111
+
112
+      static int eeprom_index;
113
+      static uint16_t working_crc;
114
+
115
+      static bool EEPROM_START(int eeprom_offset) {
116
+        if (!persistentStore.access_start()) { SERIAL_ECHO_MSG("No EEPROM."); return false; }
117
+        eeprom_index = eeprom_offset;
118
+        working_crc = 0;
119
+        return true;
120
+      }
121
+
122
+      static void EEPROM_FINISH(void) { persistentStore.access_finish(); }
123
+
124
+      template<typename T>
125
+      static void EEPROM_SKIP(const T &VAR) { eeprom_index += sizeof(VAR); }
126
+
127
+      template<typename T>
128
+      static void EEPROM_WRITE(const T &VAR) {
129
+        persistentStore.write_data(eeprom_index, (const uint8_t *) &VAR, sizeof(VAR), &working_crc);
130
+      }
131
+
132
+      template<typename T>
133
+      static void EEPROM_READ(T &VAR) {
134
+        persistentStore.read_data(eeprom_index, (uint8_t *) &VAR, sizeof(VAR), &working_crc, !validating);
135
+      }
136
+
137
+      static void EEPROM_READ(uint8_t *VAR, size_t sizeof_VAR) {
138
+        persistentStore.read_data(eeprom_index, VAR, sizeof_VAR, &working_crc, !validating);
139
+      }
140
+
141
+      template<typename T>
142
+      static void EEPROM_READ_ALWAYS(T &VAR) {
143
+        persistentStore.read_data(eeprom_index, (uint8_t *) &VAR, sizeof(VAR), &working_crc);
144
+      }
145
+
146
+    #endif // EEPROM_SETTINGS
109 147
 };
110 148
 
111 149
 extern MarlinSettings settings;

Loading…
Cancel
Save