瀏覽代碼

Optimize MarlinSettings with template methods (#21426)

Ramiro Polla 4 年之前
父節點
當前提交
3ced55aa93
No account linked to committer's email address
共有 2 個文件被更改,包括 48 次插入19 次删除
  1. 7
    16
      Marlin/src/module/settings.cpp
  2. 41
    3
      Marlin/src/module/settings.h

+ 7
- 16
Marlin/src/module/settings.cpp 查看文件

572
 
572
 
573
 #if ENABLED(EEPROM_SETTINGS)
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
   #define EEPROM_ASSERT(TST,ERR)  do{ if (!(TST)) { SERIAL_ERROR_MSG(ERR); eeprom_error = true; } }while(0)
575
   #define EEPROM_ASSERT(TST,ERR)  do{ if (!(TST)) { SERIAL_ERROR_MSG(ERR); eeprom_error = true; } }while(0)
583
 
576
 
584
   #if ENABLED(DEBUG_EEPROM_READWRITE)
577
   #if ENABLED(DEBUG_EEPROM_READWRITE)
594
   const char version[4] = EEPROM_VERSION;
587
   const char version[4] = EEPROM_VERSION;
595
 
588
 
596
   bool MarlinSettings::eeprom_error, MarlinSettings::validating;
589
   bool MarlinSettings::eeprom_error, MarlinSettings::validating;
590
+  int MarlinSettings::eeprom_index;
591
+  uint16_t MarlinSettings::working_crc;
597
 
592
 
598
   bool MarlinSettings::size_error(const uint16_t size) {
593
   bool MarlinSettings::size_error(const uint16_t size) {
599
     if (size != datasize()) {
594
     if (size != datasize()) {
610
     float dummyf = 0;
605
     float dummyf = 0;
611
     char ver[4] = "ERR";
606
     char ver[4] = "ERR";
612
 
607
 
613
-    uint16_t working_crc = 0;
608
+    if (!EEPROM_START(EEPROM_OFFSET)) return false;
614
-
615
-    EEPROM_START();
616
 
609
 
617
     eeprom_error = false;
610
     eeprom_error = false;
618
 
611
 
1456
    * M501 - Retrieve Configuration
1449
    * M501 - Retrieve Configuration
1457
    */
1450
    */
1458
   bool MarlinSettings::_load() {
1451
   bool MarlinSettings::_load() {
1459
-    uint16_t working_crc = 0;
1452
+    if (!EEPROM_START(EEPROM_OFFSET)) return false;
1460
-
1461
-    EEPROM_START();
1462
 
1453
 
1463
     char stored_ver[4];
1454
     char stored_ver[4];
1464
     EEPROM_READ_ALWAYS(stored_ver);
1455
     EEPROM_READ_ALWAYS(stored_ver);
1496
         uint32_t tmp1[XYZ + esteppers];
1487
         uint32_t tmp1[XYZ + esteppers];
1497
         float tmp2[XYZ + esteppers];
1488
         float tmp2[XYZ + esteppers];
1498
         feedRate_t tmp3[XYZ + esteppers];
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
         EEPROM_READ(planner.settings.min_segment_time_us);
1491
         EEPROM_READ(planner.settings.min_segment_time_us);
1501
-        EEPROM_READ(tmp2);                         // axis_steps_per_mm
1492
+        EEPROM_READ((uint8_t *)tmp2, sizeof(tmp2)); // axis_steps_per_mm
1502
-        EEPROM_READ(tmp3);                         // max_feedrate_mm_s
1493
+        EEPROM_READ((uint8_t *)tmp3, sizeof(tmp3)); // max_feedrate_mm_s
1503
 
1494
 
1504
         if (!validating) LOOP_XYZE_N(i) {
1495
         if (!validating) LOOP_XYZE_N(i) {
1505
           const bool in = (i < esteppers + XYZ);
1496
           const bool in = (i < esteppers + XYZ);

+ 41
- 3
Marlin/src/module/settings.h 查看文件

76
         //static void delete_mesh();    // necessary if we have a MAT
76
         //static void delete_mesh();    // necessary if we have a MAT
77
         //static void defrag_meshes();  // "
77
         //static void defrag_meshes();  // "
78
       #endif
78
       #endif
79
-    #else
79
+
80
+    #else // !EEPROM_SETTINGS
81
+
80
       FORCE_INLINE
82
       FORCE_INLINE
81
       static bool load() { reset(); report(); return true; }
83
       static bool load() { reset(); report(); return true; }
82
       FORCE_INLINE
84
       FORCE_INLINE
83
       static void first_load() { (void)load(); }
85
       static void first_load() { (void)load(); }
84
-    #endif
86
+
87
+    #endif // !EEPROM_SETTINGS
85
 
88
 
86
     #if DISABLED(DISABLE_M503)
89
     #if DISABLED(DISABLE_M503)
87
       static void report(const bool forReplay=false);
90
       static void report(const bool forReplay=false);
105
 
108
 
106
       static bool _load();
109
       static bool _load();
107
       static bool size_error(const uint16_t size);
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
 extern MarlinSettings settings;
149
 extern MarlinSettings settings;

Loading…
取消
儲存