Optimize MarlinSettings with template methods (#21426)

This commit is contained in:
Ramiro Polla 2021-03-24 13:32:08 +01:00 committed by Scott Lahteine
parent c6774812fa
commit 5c8f7c7ea3
2 changed files with 48 additions and 19 deletions

View file

@ -572,13 +572,6 @@ void MarlinSettings::postprocess() {
#if ENABLED(EEPROM_SETTINGS) #if ENABLED(EEPROM_SETTINGS)
#define EEPROM_START() if (!persistentStore.access_start()) { SERIAL_ECHO_MSG("No EEPROM."); return false; } \
int eeprom_index = EEPROM_OFFSET
#define EEPROM_FINISH() persistentStore.access_finish()
#define EEPROM_SKIP(VAR) (eeprom_index += sizeof(VAR))
#define EEPROM_WRITE(VAR) do{ persistentStore.write_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc); }while(0)
#define EEPROM_READ(VAR) do{ persistentStore.read_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc, !validating); }while(0)
#define EEPROM_READ_ALWAYS(VAR) do{ persistentStore.read_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc); }while(0)
#define EEPROM_ASSERT(TST,ERR) do{ if (!(TST)) { SERIAL_ERROR_MSG(ERR); eeprom_error = true; } }while(0) #define EEPROM_ASSERT(TST,ERR) do{ if (!(TST)) { SERIAL_ERROR_MSG(ERR); eeprom_error = true; } }while(0)
#if ENABLED(DEBUG_EEPROM_READWRITE) #if ENABLED(DEBUG_EEPROM_READWRITE)
@ -594,6 +587,8 @@ void MarlinSettings::postprocess() {
const char version[4] = EEPROM_VERSION; const char version[4] = EEPROM_VERSION;
bool MarlinSettings::eeprom_error, MarlinSettings::validating; bool MarlinSettings::eeprom_error, MarlinSettings::validating;
int MarlinSettings::eeprom_index;
uint16_t MarlinSettings::working_crc;
bool MarlinSettings::size_error(const uint16_t size) { bool MarlinSettings::size_error(const uint16_t size) {
if (size != datasize()) { if (size != datasize()) {
@ -610,9 +605,7 @@ void MarlinSettings::postprocess() {
float dummyf = 0; float dummyf = 0;
char ver[4] = "ERR"; char ver[4] = "ERR";
uint16_t working_crc = 0; if (!EEPROM_START(EEPROM_OFFSET)) return false;
EEPROM_START();
eeprom_error = false; eeprom_error = false;
@ -1456,9 +1449,7 @@ void MarlinSettings::postprocess() {
* M501 - Retrieve Configuration * M501 - Retrieve Configuration
*/ */
bool MarlinSettings::_load() { bool MarlinSettings::_load() {
uint16_t working_crc = 0; if (!EEPROM_START(EEPROM_OFFSET)) return false;
EEPROM_START();
char stored_ver[4]; char stored_ver[4];
EEPROM_READ_ALWAYS(stored_ver); EEPROM_READ_ALWAYS(stored_ver);
@ -1496,10 +1487,10 @@ void MarlinSettings::postprocess() {
uint32_t tmp1[XYZ + esteppers]; uint32_t tmp1[XYZ + esteppers];
float tmp2[XYZ + esteppers]; float tmp2[XYZ + esteppers];
feedRate_t tmp3[XYZ + esteppers]; feedRate_t tmp3[XYZ + esteppers];
EEPROM_READ(tmp1); // max_acceleration_mm_per_s2 EEPROM_READ((uint8_t *)tmp1, sizeof(tmp1)); // max_acceleration_mm_per_s2
EEPROM_READ(planner.settings.min_segment_time_us); EEPROM_READ(planner.settings.min_segment_time_us);
EEPROM_READ(tmp2); // axis_steps_per_mm EEPROM_READ((uint8_t *)tmp2, sizeof(tmp2)); // axis_steps_per_mm
EEPROM_READ(tmp3); // max_feedrate_mm_s EEPROM_READ((uint8_t *)tmp3, sizeof(tmp3)); // max_feedrate_mm_s
if (!validating) LOOP_XYZE_N(i) { if (!validating) LOOP_XYZE_N(i) {
const bool in = (i < esteppers + XYZ); const bool in = (i < esteppers + XYZ);

View file

@ -76,12 +76,15 @@ class MarlinSettings {
//static void delete_mesh(); // necessary if we have a MAT //static void delete_mesh(); // necessary if we have a MAT
//static void defrag_meshes(); // " //static void defrag_meshes(); // "
#endif #endif
#else
#else // !EEPROM_SETTINGS
FORCE_INLINE FORCE_INLINE
static bool load() { reset(); report(); return true; } static bool load() { reset(); report(); return true; }
FORCE_INLINE FORCE_INLINE
static void first_load() { (void)load(); } static void first_load() { (void)load(); }
#endif
#endif // !EEPROM_SETTINGS
#if DISABLED(DISABLE_M503) #if DISABLED(DISABLE_M503)
static void report(const bool forReplay=false); static void report(const bool forReplay=false);
@ -105,7 +108,42 @@ class MarlinSettings {
static bool _load(); static bool _load();
static bool size_error(const uint16_t size); static bool size_error(const uint16_t size);
#endif
static int eeprom_index;
static uint16_t working_crc;
static bool EEPROM_START(int eeprom_offset) {
if (!persistentStore.access_start()) { SERIAL_ECHO_MSG("No EEPROM."); return false; }
eeprom_index = eeprom_offset;
working_crc = 0;
return true;
}
static void EEPROM_FINISH(void) { persistentStore.access_finish(); }
template<typename T>
static void EEPROM_SKIP(const T &VAR) { eeprom_index += sizeof(VAR); }
template<typename T>
static void EEPROM_WRITE(const T &VAR) {
persistentStore.write_data(eeprom_index, (const uint8_t *) &VAR, sizeof(VAR), &working_crc);
}
template<typename T>
static void EEPROM_READ(T &VAR) {
persistentStore.read_data(eeprom_index, (uint8_t *) &VAR, sizeof(VAR), &working_crc, !validating);
}
static void EEPROM_READ(uint8_t *VAR, size_t sizeof_VAR) {
persistentStore.read_data(eeprom_index, VAR, sizeof_VAR, &working_crc, !validating);
}
template<typename T>
static void EEPROM_READ_ALWAYS(T &VAR) {
persistentStore.read_data(eeprom_index, (uint8_t *) &VAR, sizeof(VAR), &working_crc);
}
#endif // EEPROM_SETTINGS
}; };
extern MarlinSettings settings; extern MarlinSettings settings;