Browse Source

Convert config code to a static class

Scott Lahteine 7 years ago
parent
commit
786af73e24
4 changed files with 78 additions and 49 deletions
  1. 6
    6
      Marlin/Marlin_main.cpp
  2. 34
    24
      Marlin/configuration_store.cpp
  3. 35
    16
      Marlin/configuration_store.h
  4. 3
    3
      Marlin/ultralcd.cpp

+ 6
- 6
Marlin/Marlin_main.cpp View File

573
         endstop_adj[ABC] = { 0 };
573
         endstop_adj[ABC] = { 0 };
574
 
574
 
575
   // These values are loaded or reset at boot time when setup() calls
575
   // These values are loaded or reset at boot time when setup() calls
576
-  // Config_RetrieveSettings(), which calls recalc_delta_settings().
576
+  // settings.load(), which calls recalc_delta_settings().
577
   float delta_radius,
577
   float delta_radius,
578
         delta_tower_angle_trim[ABC],
578
         delta_tower_angle_trim[ABC],
579
         delta_tower[ABC][2],
579
         delta_tower[ABC][2],
7898
  * M500: Store settings in EEPROM
7898
  * M500: Store settings in EEPROM
7899
  */
7899
  */
7900
 inline void gcode_M500() {
7900
 inline void gcode_M500() {
7901
-  (void)Config_StoreSettings();
7901
+  (void)settings.save();
7902
 }
7902
 }
7903
 
7903
 
7904
 /**
7904
 /**
7905
  * M501: Read settings from EEPROM
7905
  * M501: Read settings from EEPROM
7906
  */
7906
  */
7907
 inline void gcode_M501() {
7907
 inline void gcode_M501() {
7908
-  (void)Config_RetrieveSettings();
7908
+  (void)settings.load();
7909
 }
7909
 }
7910
 
7910
 
7911
 /**
7911
 /**
7912
  * M502: Revert to default settings
7912
  * M502: Revert to default settings
7913
  */
7913
  */
7914
 inline void gcode_M502() {
7914
 inline void gcode_M502() {
7915
-  (void)Config_ResetDefault();
7915
+  (void)settings.reset();
7916
 }
7916
 }
7917
 
7917
 
7918
 /**
7918
 /**
7919
  * M503: print settings currently in memory
7919
  * M503: print settings currently in memory
7920
  */
7920
  */
7921
 inline void gcode_M503() {
7921
 inline void gcode_M503() {
7922
-  (void)Config_PrintSettings(code_seen('S') && !code_value_bool());
7922
+  (void)settings.report(code_seen('S') && !code_value_bool());
7923
 }
7923
 }
7924
 
7924
 
7925
 #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
7925
 #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
11343
 
11343
 
11344
   // Load data from EEPROM if available (or use defaults)
11344
   // Load data from EEPROM if available (or use defaults)
11345
   // This also updates variables in the planner, elsewhere
11345
   // This also updates variables in the planner, elsewhere
11346
-  (void)Config_RetrieveSettings();
11346
+  (void)settings.load();
11347
 
11347
 
11348
   #if DISABLED(NO_WORKSPACE_OFFSETS)
11348
   #if DISABLED(NO_WORKSPACE_OFFSETS)
11349
     // Initialize current position based on home_offset
11349
     // Initialize current position based on home_offset

+ 34
- 24
Marlin/configuration_store.cpp View File

23
 /**
23
 /**
24
  * configuration_store.cpp
24
  * configuration_store.cpp
25
  *
25
  *
26
- * Configuration and EEPROM storage
26
+ * Settings and EEPROM storage
27
  *
27
  *
28
  * IMPORTANT:  Whenever there are changes made to the variables stored in EEPROM
28
  * IMPORTANT:  Whenever there are changes made to the variables stored in EEPROM
29
  * in the functions below, also increment the version number. This makes sure that
29
  * in the functions below, also increment the version number. This makes sure that
152
  *  580                                Minimum end-point
152
  *  580                                Minimum end-point
153
  * 1901 (580 + 36 + 9 + 288 + 988)     Maximum end-point
153
  * 1901 (580 + 36 + 9 + 288 + 988)     Maximum end-point
154
  */
154
  */
155
+#include "configuration_store.h"
156
+
157
+MarlinSettings settings;
158
+
155
 #include "Marlin.h"
159
 #include "Marlin.h"
156
 #include "language.h"
160
 #include "language.h"
157
 #include "endstops.h"
161
 #include "endstops.h"
158
 #include "planner.h"
162
 #include "planner.h"
159
 #include "temperature.h"
163
 #include "temperature.h"
160
 #include "ultralcd.h"
164
 #include "ultralcd.h"
161
-#include "configuration_store.h"
162
 
165
 
163
 #if ENABLED(MESH_BED_LEVELING)
166
 #if ENABLED(MESH_BED_LEVELING)
164
   #include "mesh_bed_leveling.h"
167
   #include "mesh_bed_leveling.h"
179
 /**
182
 /**
180
  * Post-process after Retrieve or Reset
183
  * Post-process after Retrieve or Reset
181
  */
184
  */
182
-void Config_Postprocess() {
185
+void MarlinSettings::postprocess() {
183
   // steps per s2 needs to be updated to agree with units per s2
186
   // steps per s2 needs to be updated to agree with units per s2
184
   planner.reset_acceleration_rates();
187
   planner.reset_acceleration_rates();
185
 
188
 
217
 
220
 
218
 #if ENABLED(EEPROM_SETTINGS)
221
 #if ENABLED(EEPROM_SETTINGS)
219
 
222
 
220
-  uint16_t eeprom_checksum;
221
   const char version[4] = EEPROM_VERSION;
223
   const char version[4] = EEPROM_VERSION;
222
 
224
 
223
-  bool eeprom_write_error;
225
+  uint16_t MarlinSettings::eeprom_checksum;
226
+
227
+  bool MarlinSettings::eeprom_write_error,
228
+       MarlinSettings::eeprom_read_error;
224
 
229
 
225
-  void _EEPROM_writeData(int &pos, const uint8_t* value, uint16_t size) {
230
+  void MarlinSettings::write_data(int &pos, const uint8_t* value, uint16_t size) {
226
     if (eeprom_write_error) return;
231
     if (eeprom_write_error) return;
227
     while (size--) {
232
     while (size--) {
228
       uint8_t * const p = (uint8_t * const)pos;
233
       uint8_t * const p = (uint8_t * const)pos;
243
       value++;
248
       value++;
244
     };
249
     };
245
   }
250
   }
246
-  bool eeprom_read_error;
247
-  void _EEPROM_readData(int &pos, uint8_t* value, uint16_t size) {
251
+  void MarlinSettings::read_data(int &pos, uint8_t* value, uint16_t size) {
248
     do {
252
     do {
249
       uint8_t c = eeprom_read_byte((unsigned char*)pos);
253
       uint8_t c = eeprom_read_byte((unsigned char*)pos);
250
       if (!eeprom_read_error) *value = c;
254
       if (!eeprom_read_error) *value = c;
257
   #define DUMMY_PID_VALUE 3000.0f
261
   #define DUMMY_PID_VALUE 3000.0f
258
   #define EEPROM_START() int eeprom_index = EEPROM_OFFSET
262
   #define EEPROM_START() int eeprom_index = EEPROM_OFFSET
259
   #define EEPROM_SKIP(VAR) eeprom_index += sizeof(VAR)
263
   #define EEPROM_SKIP(VAR) eeprom_index += sizeof(VAR)
260
-  #define EEPROM_WRITE(VAR) _EEPROM_writeData(eeprom_index, (uint8_t*)&VAR, sizeof(VAR))
261
-  #define EEPROM_READ(VAR) _EEPROM_readData(eeprom_index, (uint8_t*)&VAR, sizeof(VAR))
262
-  #define EEPROM_ASSERT(TST,ERR) if () do{ SERIAL_ERROR_START; SERIAL_ERRORLNPGM(ERR); eeprom_read_error |= true; }while(0)
264
+  #define EEPROM_WRITE(VAR) write_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR))
265
+  #define EEPROM_READ(VAR) read_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR))
266
+  #define EEPROM_ASSERT(TST,ERR) if (!(TST)) do{ SERIAL_ERROR_START; SERIAL_ERRORLNPGM(ERR); eeprom_read_error = true; }while(0)
263
 
267
 
264
   /**
268
   /**
265
    * M500 - Store Configuration
269
    * M500 - Store Configuration
266
    */
270
    */
267
-  bool Config_StoreSettings() {
271
+  bool MarlinSettings::save() {
268
     float dummy = 0.0f;
272
     float dummy = 0.0f;
269
     char ver[4] = "000";
273
     char ver[4] = "000";
270
 
274
 
576
   /**
580
   /**
577
    * M501 - Retrieve Configuration
581
    * M501 - Retrieve Configuration
578
    */
582
    */
579
-  bool Config_RetrieveSettings() {
583
+  bool MarlinSettings::load() {
580
 
584
 
581
     EEPROM_START();
585
     EEPROM_START();
582
     eeprom_read_error = false; // If set EEPROM_READ won't write into RAM
586
     eeprom_read_error = false; // If set EEPROM_READ won't write into RAM
597
       SERIAL_ECHOPGM("EEPROM version mismatch ");
601
       SERIAL_ECHOPGM("EEPROM version mismatch ");
598
       SERIAL_ECHOPAIR("(EEPROM=", stored_ver);
602
       SERIAL_ECHOPAIR("(EEPROM=", stored_ver);
599
       SERIAL_ECHOLNPGM(" Marlin=" EEPROM_VERSION ")");
603
       SERIAL_ECHOLNPGM(" Marlin=" EEPROM_VERSION ")");
600
-      Config_ResetDefault();
604
+      reset();
601
     }
605
     }
602
     else {
606
     else {
603
       float dummy = 0;
607
       float dummy = 0;
747
       EEPROM_READ(lcd_preheat_bed_temp);
751
       EEPROM_READ(lcd_preheat_bed_temp);
748
       EEPROM_READ(lcd_preheat_fan_speed);
752
       EEPROM_READ(lcd_preheat_fan_speed);
749
 
753
 
754
+      //EEPROM_ASSERT(
755
+      //  WITHIN(lcd_preheat_fan_speed, 0, 255),
756
+      //  "lcd_preheat_fan_speed out of range"
757
+      //);
758
+
750
       #if ENABLED(PIDTEMP)
759
       #if ENABLED(PIDTEMP)
751
         for (uint8_t e = 0; e < MAX_EXTRUDERS; e++) {
760
         for (uint8_t e = 0; e < MAX_EXTRUDERS; e++) {
752
           EEPROM_READ(dummy); // Kp
761
           EEPROM_READ(dummy); // Kp
869
 
878
 
870
       if (eeprom_checksum == stored_checksum) {
879
       if (eeprom_checksum == stored_checksum) {
871
         if (eeprom_read_error)
880
         if (eeprom_read_error)
872
-          Config_ResetDefault();
881
+          reset();
873
         else {
882
         else {
874
-          Config_Postprocess();
883
+          postprocess();
875
           SERIAL_ECHO_START;
884
           SERIAL_ECHO_START;
876
           SERIAL_ECHO(version);
885
           SERIAL_ECHO(version);
877
           SERIAL_ECHOPAIR(" stored settings retrieved (", eeprom_index - (EEPROM_OFFSET));
886
           SERIAL_ECHOPAIR(" stored settings retrieved (", eeprom_index - (EEPROM_OFFSET));
881
       else {
890
       else {
882
         SERIAL_ERROR_START;
891
         SERIAL_ERROR_START;
883
         SERIAL_ERRORLNPGM("EEPROM checksum mismatch");
892
         SERIAL_ERRORLNPGM("EEPROM checksum mismatch");
884
-        Config_ResetDefault();
893
+        reset();
885
       }
894
       }
886
 
895
 
887
       #if ENABLED(AUTO_BED_LEVELING_UBL)
896
       #if ENABLED(AUTO_BED_LEVELING_UBL)
923
       #endif
932
       #endif
924
     }
933
     }
925
     #if ENABLED(EEPROM_CHITCHAT)
934
     #if ENABLED(EEPROM_CHITCHAT)
926
-      Config_PrintSettings();
935
+      report();
927
     #endif
936
     #endif
928
 
937
 
929
     return !eeprom_read_error;
938
     return !eeprom_read_error;
931
 
940
 
932
 #else // !EEPROM_SETTINGS
941
 #else // !EEPROM_SETTINGS
933
 
942
 
934
-  bool Config_StoreSettings() {
943
+  bool MarlinSettings::save() {
935
     SERIAL_ERROR_START;
944
     SERIAL_ERROR_START;
936
     SERIAL_ERRORLNPGM("EEPROM disabled");
945
     SERIAL_ERRORLNPGM("EEPROM disabled");
937
     return false;
946
     return false;
942
 /**
951
 /**
943
  * M502 - Reset Configuration
952
  * M502 - Reset Configuration
944
  */
953
  */
945
-void Config_ResetDefault() {
954
+void MarlinSettings::reset() {
946
   const float tmp1[] = DEFAULT_AXIS_STEPS_PER_UNIT, tmp2[] = DEFAULT_MAX_FEEDRATE;
955
   const float tmp1[] = DEFAULT_AXIS_STEPS_PER_UNIT, tmp2[] = DEFAULT_MAX_FEEDRATE;
947
   const uint32_t tmp3[] = DEFAULT_MAX_ACCELERATION;
956
   const uint32_t tmp3[] = DEFAULT_MAX_ACCELERATION;
948
   LOOP_XYZE_N(i) {
957
   LOOP_XYZE_N(i) {
1118
     #endif
1127
     #endif
1119
   #endif
1128
   #endif
1120
 
1129
 
1121
-  Config_Postprocess();
1130
+  postprocess();
1122
 
1131
 
1123
   SERIAL_ECHO_START;
1132
   SERIAL_ECHO_START;
1124
   SERIAL_ECHOLNPGM("Hardcoded Default Settings Loaded");
1133
   SERIAL_ECHOLNPGM("Hardcoded Default Settings Loaded");
1129
   #define CONFIG_ECHO_START do{ if (!forReplay) SERIAL_ECHO_START; }while(0)
1138
   #define CONFIG_ECHO_START do{ if (!forReplay) SERIAL_ECHO_START; }while(0)
1130
 
1139
 
1131
   /**
1140
   /**
1132
-   * M503 - Print Configuration
1141
+   * M503 - Report current settings in RAM
1142
+   *   
1143
+   * Unless specifically disabled, M503 is available even without EEPROM
1133
    */
1144
    */
1134
-  void Config_PrintSettings(bool forReplay) {
1135
-    // Always have this function, even with EEPROM_SETTINGS disabled, the current values will be shown
1145
+  void MarlinSettings::report(bool forReplay) {
1136
 
1146
 
1137
     CONFIG_ECHO_START;
1147
     CONFIG_ECHO_START;
1138
 
1148
 

+ 35
- 16
Marlin/configuration_store.h View File

25
 
25
 
26
 #include "MarlinConfig.h"
26
 #include "MarlinConfig.h"
27
 
27
 
28
-void Config_ResetDefault();
29
-bool Config_StoreSettings();
30
-
31
-#if DISABLED(DISABLE_M503)
32
-  void Config_PrintSettings(bool forReplay=false);
33
-#else
34
-  FORCE_INLINE void Config_PrintSettings(bool forReplay=false) {}
35
-#endif
36
-
37
-#if ENABLED(EEPROM_SETTINGS)
38
-  bool Config_RetrieveSettings();
39
-#else
40
-  FORCE_INLINE bool Config_RetrieveSettings() { Config_ResetDefault(); Config_PrintSettings(); return true; }
41
-#endif
42
-
43
-#endif //CONFIGURATION_STORE_H
28
+class MarlinSettings {
29
+  public:
30
+    MarlinSettings() { }
31
+
32
+    static void reset();
33
+    static bool save();
34
+
35
+    #if ENABLED(EEPROM_SETTINGS)
36
+      static bool load();
37
+    #else
38
+      FORCE_INLINE
39
+      static bool load() { reset(); report(); return true; }
40
+    #endif
41
+
42
+    #if DISABLED(DISABLE_M503)
43
+      static void report(bool forReplay=false);
44
+    #else
45
+      FORCE_INLINE
46
+      static void report(bool forReplay=false) { }
47
+    #endif
48
+
49
+  private:
50
+    static void postprocess();
51
+
52
+    #if ENABLED(EEPROM_SETTINGS)
53
+      static uint16_t eeprom_checksum;
54
+      static bool eeprom_read_error, eeprom_write_error;
55
+      static void write_data(int &pos, const uint8_t* value, uint16_t size);
56
+      static void read_data(int &pos, uint8_t* value, uint16_t size);
57
+    #endif
58
+};
59
+
60
+extern MarlinSettings settings;
61
+
62
+#endif // CONFIGURATION_STORE_H

+ 3
- 3
Marlin/ultralcd.cpp View File

2046
    */
2046
    */
2047
 
2047
 
2048
   #if ENABLED(EEPROM_SETTINGS)
2048
   #if ENABLED(EEPROM_SETTINGS)
2049
-    static void lcd_store_settings()   { lcd_completion_feedback(Config_StoreSettings()); }
2050
-    static void lcd_load_settings()    { lcd_completion_feedback(Config_RetrieveSettings()); }
2049
+    static void lcd_store_settings()   { lcd_completion_feedback(settings.save()); }
2050
+    static void lcd_load_settings()    { lcd_completion_feedback(settings.load()); }
2051
   #endif
2051
   #endif
2052
 
2052
 
2053
   static void lcd_factory_settings() {
2053
   static void lcd_factory_settings() {
2054
-    Config_ResetDefault();
2054
+    settings.reset();
2055
     lcd_completion_feedback();
2055
     lcd_completion_feedback();
2056
   }
2056
   }
2057
 
2057
 

Loading…
Cancel
Save