Bläddra i källkod

Convert config code to a static class

Scott Lahteine 7 år sedan
förälder
incheckning
786af73e24
4 ändrade filer med 78 tillägg och 49 borttagningar
  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 Visa fil

@@ -573,7 +573,7 @@ static uint8_t target_extruder;
573 573
         endstop_adj[ABC] = { 0 };
574 574
 
575 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 577
   float delta_radius,
578 578
         delta_tower_angle_trim[ABC],
579 579
         delta_tower[ABC][2],
@@ -7898,28 +7898,28 @@ void quickstop_stepper() {
7898 7898
  * M500: Store settings in EEPROM
7899 7899
  */
7900 7900
 inline void gcode_M500() {
7901
-  (void)Config_StoreSettings();
7901
+  (void)settings.save();
7902 7902
 }
7903 7903
 
7904 7904
 /**
7905 7905
  * M501: Read settings from EEPROM
7906 7906
  */
7907 7907
 inline void gcode_M501() {
7908
-  (void)Config_RetrieveSettings();
7908
+  (void)settings.load();
7909 7909
 }
7910 7910
 
7911 7911
 /**
7912 7912
  * M502: Revert to default settings
7913 7913
  */
7914 7914
 inline void gcode_M502() {
7915
-  (void)Config_ResetDefault();
7915
+  (void)settings.reset();
7916 7916
 }
7917 7917
 
7918 7918
 /**
7919 7919
  * M503: print settings currently in memory
7920 7920
  */
7921 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 7925
 #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
@@ -11343,7 +11343,7 @@ void setup() {
11343 11343
 
11344 11344
   // Load data from EEPROM if available (or use defaults)
11345 11345
   // This also updates variables in the planner, elsewhere
11346
-  (void)Config_RetrieveSettings();
11346
+  (void)settings.load();
11347 11347
 
11348 11348
   #if DISABLED(NO_WORKSPACE_OFFSETS)
11349 11349
     // Initialize current position based on home_offset

+ 34
- 24
Marlin/configuration_store.cpp Visa fil

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

+ 35
- 16
Marlin/configuration_store.h Visa fil

@@ -25,19 +25,38 @@
25 25
 
26 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 Visa fil

@@ -2046,12 +2046,12 @@ void kill_screen(const char* lcd_msg) {
2046 2046
    */
2047 2047
 
2048 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 2051
   #endif
2052 2052
 
2053 2053
   static void lcd_factory_settings() {
2054
-    Config_ResetDefault();
2054
+    settings.reset();
2055 2055
     lcd_completion_feedback();
2056 2056
   }
2057 2057
 

Laddar…
Avbryt
Spara