From 786af73e24215da94cdc05c277b0ec05b7cc37d2 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sun, 9 Apr 2017 21:47:49 -0500 Subject: [PATCH] Convert config code to a static class --- Marlin/Marlin_main.cpp | 12 +++---- Marlin/configuration_store.cpp | 58 ++++++++++++++++++++-------------- Marlin/configuration_store.h | 45 ++++++++++++++++++-------- Marlin/ultralcd.cpp | 6 ++-- 4 files changed, 75 insertions(+), 46 deletions(-) diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp index 42ad37d50..088b0d5d9 100644 --- a/Marlin/Marlin_main.cpp +++ b/Marlin/Marlin_main.cpp @@ -573,7 +573,7 @@ static uint8_t target_extruder; endstop_adj[ABC] = { 0 }; // These values are loaded or reset at boot time when setup() calls - // Config_RetrieveSettings(), which calls recalc_delta_settings(). + // settings.load(), which calls recalc_delta_settings(). float delta_radius, delta_tower_angle_trim[ABC], delta_tower[ABC][2], @@ -7898,28 +7898,28 @@ void quickstop_stepper() { * M500: Store settings in EEPROM */ inline void gcode_M500() { - (void)Config_StoreSettings(); + (void)settings.save(); } /** * M501: Read settings from EEPROM */ inline void gcode_M501() { - (void)Config_RetrieveSettings(); + (void)settings.load(); } /** * M502: Revert to default settings */ inline void gcode_M502() { - (void)Config_ResetDefault(); + (void)settings.reset(); } /** * M503: print settings currently in memory */ inline void gcode_M503() { - (void)Config_PrintSettings(code_seen('S') && !code_value_bool()); + (void)settings.report(code_seen('S') && !code_value_bool()); } #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) @@ -11343,7 +11343,7 @@ void setup() { // Load data from EEPROM if available (or use defaults) // This also updates variables in the planner, elsewhere - (void)Config_RetrieveSettings(); + (void)settings.load(); #if DISABLED(NO_WORKSPACE_OFFSETS) // Initialize current position based on home_offset diff --git a/Marlin/configuration_store.cpp b/Marlin/configuration_store.cpp index 2eda9ae47..a5c47dc1d 100644 --- a/Marlin/configuration_store.cpp +++ b/Marlin/configuration_store.cpp @@ -23,7 +23,7 @@ /** * configuration_store.cpp * - * Configuration and EEPROM storage + * Settings and EEPROM storage * * IMPORTANT: Whenever there are changes made to the variables stored in EEPROM * in the functions below, also increment the version number. This makes sure that @@ -152,13 +152,16 @@ * 580 Minimum end-point * 1901 (580 + 36 + 9 + 288 + 988) Maximum end-point */ +#include "configuration_store.h" + +MarlinSettings settings; + #include "Marlin.h" #include "language.h" #include "endstops.h" #include "planner.h" #include "temperature.h" #include "ultralcd.h" -#include "configuration_store.h" #if ENABLED(MESH_BED_LEVELING) #include "mesh_bed_leveling.h" @@ -179,7 +182,7 @@ /** * Post-process after Retrieve or Reset */ -void Config_Postprocess() { +void MarlinSettings::postprocess() { // steps per s2 needs to be updated to agree with units per s2 planner.reset_acceleration_rates(); @@ -217,12 +220,14 @@ void Config_Postprocess() { #if ENABLED(EEPROM_SETTINGS) - uint16_t eeprom_checksum; const char version[4] = EEPROM_VERSION; - bool eeprom_write_error; + uint16_t MarlinSettings::eeprom_checksum; - void _EEPROM_writeData(int &pos, const uint8_t* value, uint16_t size) { + bool MarlinSettings::eeprom_write_error, + MarlinSettings::eeprom_read_error; + + void MarlinSettings::write_data(int &pos, const uint8_t* value, uint16_t size) { if (eeprom_write_error) return; while (size--) { uint8_t * const p = (uint8_t * const)pos; @@ -243,8 +248,7 @@ void Config_Postprocess() { value++; }; } - bool eeprom_read_error; - void _EEPROM_readData(int &pos, uint8_t* value, uint16_t size) { + void MarlinSettings::read_data(int &pos, uint8_t* value, uint16_t size) { do { uint8_t c = eeprom_read_byte((unsigned char*)pos); if (!eeprom_read_error) *value = c; @@ -257,14 +261,14 @@ void Config_Postprocess() { #define DUMMY_PID_VALUE 3000.0f #define EEPROM_START() int eeprom_index = EEPROM_OFFSET #define EEPROM_SKIP(VAR) eeprom_index += sizeof(VAR) - #define EEPROM_WRITE(VAR) _EEPROM_writeData(eeprom_index, (uint8_t*)&VAR, sizeof(VAR)) - #define EEPROM_READ(VAR) _EEPROM_readData(eeprom_index, (uint8_t*)&VAR, sizeof(VAR)) - #define EEPROM_ASSERT(TST,ERR) if () do{ SERIAL_ERROR_START; SERIAL_ERRORLNPGM(ERR); eeprom_read_error |= true; }while(0) + #define EEPROM_WRITE(VAR) write_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR)) + #define EEPROM_READ(VAR) read_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR)) + #define EEPROM_ASSERT(TST,ERR) if (!(TST)) do{ SERIAL_ERROR_START; SERIAL_ERRORLNPGM(ERR); eeprom_read_error = true; }while(0) /** * M500 - Store Configuration */ - bool Config_StoreSettings() { + bool MarlinSettings::save() { float dummy = 0.0f; char ver[4] = "000"; @@ -576,7 +580,7 @@ void Config_Postprocess() { /** * M501 - Retrieve Configuration */ - bool Config_RetrieveSettings() { + bool MarlinSettings::load() { EEPROM_START(); eeprom_read_error = false; // If set EEPROM_READ won't write into RAM @@ -597,7 +601,7 @@ void Config_Postprocess() { SERIAL_ECHOPGM("EEPROM version mismatch "); SERIAL_ECHOPAIR("(EEPROM=", stored_ver); SERIAL_ECHOLNPGM(" Marlin=" EEPROM_VERSION ")"); - Config_ResetDefault(); + reset(); } else { float dummy = 0; @@ -747,6 +751,11 @@ void Config_Postprocess() { EEPROM_READ(lcd_preheat_bed_temp); EEPROM_READ(lcd_preheat_fan_speed); + //EEPROM_ASSERT( + // WITHIN(lcd_preheat_fan_speed, 0, 255), + // "lcd_preheat_fan_speed out of range" + //); + #if ENABLED(PIDTEMP) for (uint8_t e = 0; e < MAX_EXTRUDERS; e++) { EEPROM_READ(dummy); // Kp @@ -869,9 +878,9 @@ void Config_Postprocess() { if (eeprom_checksum == stored_checksum) { if (eeprom_read_error) - Config_ResetDefault(); + reset(); else { - Config_Postprocess(); + postprocess(); SERIAL_ECHO_START; SERIAL_ECHO(version); SERIAL_ECHOPAIR(" stored settings retrieved (", eeprom_index - (EEPROM_OFFSET)); @@ -881,7 +890,7 @@ void Config_Postprocess() { else { SERIAL_ERROR_START; SERIAL_ERRORLNPGM("EEPROM checksum mismatch"); - Config_ResetDefault(); + reset(); } #if ENABLED(AUTO_BED_LEVELING_UBL) @@ -923,7 +932,7 @@ void Config_Postprocess() { #endif } #if ENABLED(EEPROM_CHITCHAT) - Config_PrintSettings(); + report(); #endif return !eeprom_read_error; @@ -931,7 +940,7 @@ void Config_Postprocess() { #else // !EEPROM_SETTINGS - bool Config_StoreSettings() { + bool MarlinSettings::save() { SERIAL_ERROR_START; SERIAL_ERRORLNPGM("EEPROM disabled"); return false; @@ -942,7 +951,7 @@ void Config_Postprocess() { /** * M502 - Reset Configuration */ -void Config_ResetDefault() { +void MarlinSettings::reset() { const float tmp1[] = DEFAULT_AXIS_STEPS_PER_UNIT, tmp2[] = DEFAULT_MAX_FEEDRATE; const uint32_t tmp3[] = DEFAULT_MAX_ACCELERATION; LOOP_XYZE_N(i) { @@ -1118,7 +1127,7 @@ void Config_ResetDefault() { #endif #endif - Config_Postprocess(); + postprocess(); SERIAL_ECHO_START; SERIAL_ECHOLNPGM("Hardcoded Default Settings Loaded"); @@ -1129,10 +1138,11 @@ void Config_ResetDefault() { #define CONFIG_ECHO_START do{ if (!forReplay) SERIAL_ECHO_START; }while(0) /** - * M503 - Print Configuration + * M503 - Report current settings in RAM + * + * Unless specifically disabled, M503 is available even without EEPROM */ - void Config_PrintSettings(bool forReplay) { - // Always have this function, even with EEPROM_SETTINGS disabled, the current values will be shown + void MarlinSettings::report(bool forReplay) { CONFIG_ECHO_START; diff --git a/Marlin/configuration_store.h b/Marlin/configuration_store.h index 93dbb7404..e31d20b5c 100644 --- a/Marlin/configuration_store.h +++ b/Marlin/configuration_store.h @@ -25,19 +25,38 @@ #include "MarlinConfig.h" -void Config_ResetDefault(); -bool Config_StoreSettings(); +class MarlinSettings { + public: + MarlinSettings() { } -#if DISABLED(DISABLE_M503) - void Config_PrintSettings(bool forReplay=false); -#else - FORCE_INLINE void Config_PrintSettings(bool forReplay=false) {} -#endif + static void reset(); + static bool save(); -#if ENABLED(EEPROM_SETTINGS) - bool Config_RetrieveSettings(); -#else - FORCE_INLINE bool Config_RetrieveSettings() { Config_ResetDefault(); Config_PrintSettings(); return true; } -#endif + #if ENABLED(EEPROM_SETTINGS) + static bool load(); + #else + FORCE_INLINE + static bool load() { reset(); report(); return true; } + #endif -#endif //CONFIGURATION_STORE_H + #if DISABLED(DISABLE_M503) + static void report(bool forReplay=false); + #else + FORCE_INLINE + static void report(bool forReplay=false) { } + #endif + + private: + static void postprocess(); + + #if ENABLED(EEPROM_SETTINGS) + static uint16_t eeprom_checksum; + static bool eeprom_read_error, eeprom_write_error; + static void write_data(int &pos, const uint8_t* value, uint16_t size); + static void read_data(int &pos, uint8_t* value, uint16_t size); + #endif +}; + +extern MarlinSettings settings; + +#endif // CONFIGURATION_STORE_H diff --git a/Marlin/ultralcd.cpp b/Marlin/ultralcd.cpp index afba70a10..7294a004d 100755 --- a/Marlin/ultralcd.cpp +++ b/Marlin/ultralcd.cpp @@ -2046,12 +2046,12 @@ void kill_screen(const char* lcd_msg) { */ #if ENABLED(EEPROM_SETTINGS) - static void lcd_store_settings() { lcd_completion_feedback(Config_StoreSettings()); } - static void lcd_load_settings() { lcd_completion_feedback(Config_RetrieveSettings()); } + static void lcd_store_settings() { lcd_completion_feedback(settings.save()); } + static void lcd_load_settings() { lcd_completion_feedback(settings.load()); } #endif static void lcd_factory_settings() { - Config_ResetDefault(); + settings.reset(); lcd_completion_feedback(); }