Browse Source

Convert config code to a static class

pull/1/head
Scott Lahteine 7 years ago
parent
commit
786af73e24
  1. 12
      Marlin/Marlin_main.cpp
  2. 58
      Marlin/configuration_store.cpp
  3. 51
      Marlin/configuration_store.h
  4. 6
      Marlin/ultralcd.cpp

12
Marlin/Marlin_main.cpp

@ -573,7 +573,7 @@ static uint8_t target_extruder;
endstop_adj[ABC] = { 0 }; endstop_adj[ABC] = { 0 };
// These values are loaded or reset at boot time when setup() calls // 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, float delta_radius,
delta_tower_angle_trim[ABC], delta_tower_angle_trim[ABC],
delta_tower[ABC][2], delta_tower[ABC][2],
@ -7898,28 +7898,28 @@ void quickstop_stepper() {
* M500: Store settings in EEPROM * M500: Store settings in EEPROM
*/ */
inline void gcode_M500() { inline void gcode_M500() {
(void)Config_StoreSettings(); (void)settings.save();
} }
/** /**
* M501: Read settings from EEPROM * M501: Read settings from EEPROM
*/ */
inline void gcode_M501() { inline void gcode_M501() {
(void)Config_RetrieveSettings(); (void)settings.load();
} }
/** /**
* M502: Revert to default settings * M502: Revert to default settings
*/ */
inline void gcode_M502() { inline void gcode_M502() {
(void)Config_ResetDefault(); (void)settings.reset();
} }
/** /**
* M503: print settings currently in memory * M503: print settings currently in memory
*/ */
inline void gcode_M503() { 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) #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
@ -11343,7 +11343,7 @@ void setup() {
// Load data from EEPROM if available (or use defaults) // Load data from EEPROM if available (or use defaults)
// This also updates variables in the planner, elsewhere // This also updates variables in the planner, elsewhere
(void)Config_RetrieveSettings(); (void)settings.load();
#if DISABLED(NO_WORKSPACE_OFFSETS) #if DISABLED(NO_WORKSPACE_OFFSETS)
// Initialize current position based on home_offset // Initialize current position based on home_offset

58
Marlin/configuration_store.cpp

@ -23,7 +23,7 @@
/** /**
* configuration_store.cpp * configuration_store.cpp
* *
* Configuration and EEPROM storage * Settings and EEPROM storage
* *
* IMPORTANT: Whenever there are changes made to the variables stored in EEPROM * 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 * in the functions below, also increment the version number. This makes sure that
@ -152,13 +152,16 @@
* 580 Minimum end-point * 580 Minimum end-point
* 1901 (580 + 36 + 9 + 288 + 988) Maximum end-point * 1901 (580 + 36 + 9 + 288 + 988) Maximum end-point
*/ */
#include "configuration_store.h"
MarlinSettings settings;
#include "Marlin.h" #include "Marlin.h"
#include "language.h" #include "language.h"
#include "endstops.h" #include "endstops.h"
#include "planner.h" #include "planner.h"
#include "temperature.h" #include "temperature.h"
#include "ultralcd.h" #include "ultralcd.h"
#include "configuration_store.h"
#if ENABLED(MESH_BED_LEVELING) #if ENABLED(MESH_BED_LEVELING)
#include "mesh_bed_leveling.h" #include "mesh_bed_leveling.h"
@ -179,7 +182,7 @@
/** /**
* Post-process after Retrieve or Reset * 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 // steps per s2 needs to be updated to agree with units per s2
planner.reset_acceleration_rates(); planner.reset_acceleration_rates();
@ -217,12 +220,14 @@ void Config_Postprocess() {
#if ENABLED(EEPROM_SETTINGS) #if ENABLED(EEPROM_SETTINGS)
uint16_t eeprom_checksum;
const char version[4] = EEPROM_VERSION; const char version[4] = EEPROM_VERSION;
bool eeprom_write_error; uint16_t MarlinSettings::eeprom_checksum;
bool MarlinSettings::eeprom_write_error,
MarlinSettings::eeprom_read_error;
void _EEPROM_writeData(int &pos, const uint8_t* value, uint16_t size) { void MarlinSettings::write_data(int &pos, const uint8_t* value, uint16_t size) {
if (eeprom_write_error) return; if (eeprom_write_error) return;
while (size--) { while (size--) {
uint8_t * const p = (uint8_t * const)pos; uint8_t * const p = (uint8_t * const)pos;
@ -243,8 +248,7 @@ void Config_Postprocess() {
value++; value++;
}; };
} }
bool eeprom_read_error; void MarlinSettings::read_data(int &pos, uint8_t* value, uint16_t size) {
void _EEPROM_readData(int &pos, uint8_t* value, uint16_t size) {
do { do {
uint8_t c = eeprom_read_byte((unsigned char*)pos); uint8_t c = eeprom_read_byte((unsigned char*)pos);
if (!eeprom_read_error) *value = c; if (!eeprom_read_error) *value = c;
@ -257,14 +261,14 @@ void Config_Postprocess() {
#define DUMMY_PID_VALUE 3000.0f #define DUMMY_PID_VALUE 3000.0f
#define EEPROM_START() int eeprom_index = EEPROM_OFFSET #define EEPROM_START() int eeprom_index = EEPROM_OFFSET
#define EEPROM_SKIP(VAR) eeprom_index += sizeof(VAR) #define EEPROM_SKIP(VAR) eeprom_index += sizeof(VAR)
#define EEPROM_WRITE(VAR) _EEPROM_writeData(eeprom_index, (uint8_t*)&VAR, sizeof(VAR)) #define EEPROM_WRITE(VAR) write_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR))
#define EEPROM_READ(VAR) _EEPROM_readData(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 () do{ SERIAL_ERROR_START; SERIAL_ERRORLNPGM(ERR); eeprom_read_error |= true; }while(0) #define EEPROM_ASSERT(TST,ERR) if (!(TST)) do{ SERIAL_ERROR_START; SERIAL_ERRORLNPGM(ERR); eeprom_read_error = true; }while(0)
/** /**
* M500 - Store Configuration * M500 - Store Configuration
*/ */
bool Config_StoreSettings() { bool MarlinSettings::save() {
float dummy = 0.0f; float dummy = 0.0f;
char ver[4] = "000"; char ver[4] = "000";
@ -576,7 +580,7 @@ void Config_Postprocess() {
/** /**
* M501 - Retrieve Configuration * M501 - Retrieve Configuration
*/ */
bool Config_RetrieveSettings() { bool MarlinSettings::load() {
EEPROM_START(); EEPROM_START();
eeprom_read_error = false; // If set EEPROM_READ won't write into RAM 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_ECHOPGM("EEPROM version mismatch ");
SERIAL_ECHOPAIR("(EEPROM=", stored_ver); SERIAL_ECHOPAIR("(EEPROM=", stored_ver);
SERIAL_ECHOLNPGM(" Marlin=" EEPROM_VERSION ")"); SERIAL_ECHOLNPGM(" Marlin=" EEPROM_VERSION ")");
Config_ResetDefault(); reset();
} }
else { else {
float dummy = 0; float dummy = 0;
@ -747,6 +751,11 @@ void Config_Postprocess() {
EEPROM_READ(lcd_preheat_bed_temp); EEPROM_READ(lcd_preheat_bed_temp);
EEPROM_READ(lcd_preheat_fan_speed); 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) #if ENABLED(PIDTEMP)
for (uint8_t e = 0; e < MAX_EXTRUDERS; e++) { for (uint8_t e = 0; e < MAX_EXTRUDERS; e++) {
EEPROM_READ(dummy); // Kp EEPROM_READ(dummy); // Kp
@ -869,9 +878,9 @@ void Config_Postprocess() {
if (eeprom_checksum == stored_checksum) { if (eeprom_checksum == stored_checksum) {
if (eeprom_read_error) if (eeprom_read_error)
Config_ResetDefault(); reset();
else { else {
Config_Postprocess(); postprocess();
SERIAL_ECHO_START; SERIAL_ECHO_START;
SERIAL_ECHO(version); SERIAL_ECHO(version);
SERIAL_ECHOPAIR(" stored settings retrieved (", eeprom_index - (EEPROM_OFFSET)); SERIAL_ECHOPAIR(" stored settings retrieved (", eeprom_index - (EEPROM_OFFSET));
@ -881,7 +890,7 @@ void Config_Postprocess() {
else { else {
SERIAL_ERROR_START; SERIAL_ERROR_START;
SERIAL_ERRORLNPGM("EEPROM checksum mismatch"); SERIAL_ERRORLNPGM("EEPROM checksum mismatch");
Config_ResetDefault(); reset();
} }
#if ENABLED(AUTO_BED_LEVELING_UBL) #if ENABLED(AUTO_BED_LEVELING_UBL)
@ -923,7 +932,7 @@ void Config_Postprocess() {
#endif #endif
} }
#if ENABLED(EEPROM_CHITCHAT) #if ENABLED(EEPROM_CHITCHAT)
Config_PrintSettings(); report();
#endif #endif
return !eeprom_read_error; return !eeprom_read_error;
@ -931,7 +940,7 @@ void Config_Postprocess() {
#else // !EEPROM_SETTINGS #else // !EEPROM_SETTINGS
bool Config_StoreSettings() { bool MarlinSettings::save() {
SERIAL_ERROR_START; SERIAL_ERROR_START;
SERIAL_ERRORLNPGM("EEPROM disabled"); SERIAL_ERRORLNPGM("EEPROM disabled");
return false; return false;
@ -942,7 +951,7 @@ void Config_Postprocess() {
/** /**
* M502 - Reset Configuration * M502 - Reset Configuration
*/ */
void Config_ResetDefault() { void MarlinSettings::reset() {
const float tmp1[] = DEFAULT_AXIS_STEPS_PER_UNIT, tmp2[] = DEFAULT_MAX_FEEDRATE; const float tmp1[] = DEFAULT_AXIS_STEPS_PER_UNIT, tmp2[] = DEFAULT_MAX_FEEDRATE;
const uint32_t tmp3[] = DEFAULT_MAX_ACCELERATION; const uint32_t tmp3[] = DEFAULT_MAX_ACCELERATION;
LOOP_XYZE_N(i) { LOOP_XYZE_N(i) {
@ -1118,7 +1127,7 @@ void Config_ResetDefault() {
#endif #endif
#endif #endif
Config_Postprocess(); postprocess();
SERIAL_ECHO_START; SERIAL_ECHO_START;
SERIAL_ECHOLNPGM("Hardcoded Default Settings Loaded"); 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) #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) { void MarlinSettings::report(bool forReplay) {
// Always have this function, even with EEPROM_SETTINGS disabled, the current values will be shown
CONFIG_ECHO_START; CONFIG_ECHO_START;

51
Marlin/configuration_store.h

@ -25,19 +25,38 @@
#include "MarlinConfig.h" #include "MarlinConfig.h"
void Config_ResetDefault(); class MarlinSettings {
bool Config_StoreSettings(); public:
MarlinSettings() { }
#if DISABLED(DISABLE_M503)
void Config_PrintSettings(bool forReplay=false); static void reset();
#else static bool save();
FORCE_INLINE void Config_PrintSettings(bool forReplay=false) {}
#endif #if ENABLED(EEPROM_SETTINGS)
static bool load();
#if ENABLED(EEPROM_SETTINGS) #else
bool Config_RetrieveSettings(); FORCE_INLINE
#else static bool load() { reset(); report(); return true; }
FORCE_INLINE bool Config_RetrieveSettings() { Config_ResetDefault(); Config_PrintSettings(); return true; } #endif
#endif
#if DISABLED(DISABLE_M503)
#endif //CONFIGURATION_STORE_H 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

6
Marlin/ultralcd.cpp

@ -2046,12 +2046,12 @@ void kill_screen(const char* lcd_msg) {
*/ */
#if ENABLED(EEPROM_SETTINGS) #if ENABLED(EEPROM_SETTINGS)
static void lcd_store_settings() { lcd_completion_feedback(Config_StoreSettings()); } static void lcd_store_settings() { lcd_completion_feedback(settings.save()); }
static void lcd_load_settings() { lcd_completion_feedback(Config_RetrieveSettings()); } static void lcd_load_settings() { lcd_completion_feedback(settings.load()); }
#endif #endif
static void lcd_factory_settings() { static void lcd_factory_settings() {
Config_ResetDefault(); settings.reset();
lcd_completion_feedback(); lcd_completion_feedback();
} }

Loading…
Cancel
Save