From ac1d6e08a20520c42b4a653aca43ce2253e5d0b0 Mon Sep 17 00:00:00 2001 From: Naxdy Date: Wed, 3 Apr 2024 17:21:10 +0200 Subject: [PATCH] chore(config): streamline flash interactions --- src/config.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/config.rs b/src/config.rs index 228b908..473d3e1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -554,7 +554,7 @@ impl ControllerConfig { pub fn from_flash_memory( mut flash: &mut Flash<'static, FLASH, Async, FLASH_SIZE>, ) -> Result { - let mut controller_config_packed: ::ByteArray = [0u8; 659]; // ControllerConfig byte size + let mut controller_config_packed: ::ByteArray = ControllerConfig::default().pack().unwrap(); let r = flash.blocking_read(ADDR_OFFSET, &mut controller_config_packed); @@ -569,23 +569,21 @@ impl ControllerConfig { Ok(cfg) => match cfg { a if a.config_revision == CONTROLLER_CONFIG_REVISION => { info!("Controller config loaded from flash: {}", a); - Ok(a) + return Ok(a); } a => { warn!("Outdated controller config detected ({:02X}), or controller config was never present, using default.", a.config_revision); - let cfg = ControllerConfig::default(); - info!("Going to save default controller config."); - cfg.write_to_flash(&mut flash)?; - Ok(cfg) } }, Err(_) => { - let cfg = ControllerConfig::default(); - info!("Going to save default controller config."); - cfg.write_to_flash(&mut flash)?; - Ok(cfg) + warn!("Controller config corrupted or flash uninitialized, using default."); } - } + }; + + let cfg = ControllerConfig::default(); + info!("Going to save default controller config."); + cfg.write_to_flash(&mut flash)?; + Ok(cfg) } pub fn write_to_flash(