Repository: HaoboGu/rmk (language: rust)

Issue:
Fix latency after boot
related issue: https://github.com/tweedegolf/sequential-storage/pull/65


Discussion / hints:

Fix patch:
diff --git a/rmk/Cargo.toml b/rmk/Cargo.toml
index e5bb24e7..00b70340 100644
--- a/rmk/Cargo.toml
+++ b/rmk/Cargo.toml
@@ -40,7 +40,7 @@ num_enum = { version = "0.7", default-features = false }
 bitfield-struct = "0.9"
 byteorder = { version = "1", default-features = false }
 futures = { version = "0.3", default-features = false }
-sequential-storage = { version = "3.0.0", features = ["defmt-03"] }
+sequential-storage = { version = "3.0.1", git = "https://github.com/tweedegolf/sequential-storage", features = ["defmt-03"] }
 
 serde = { version = "1", default-features = false, features = ["derive"] }
 postcard = { version = "1", features = ["experimental-derive", "use-defmt"] }
diff --git a/rmk/src/storage/mod.rs b/rmk/src/storage/mod.rs
index f2c2b9ef..1e45975c 100644
--- a/rmk/src/storage/mod.rs
+++ b/rmk/src/storage/mod.rs
@@ -12,7 +12,7 @@ use embedded_storage::nor_flash::NorFlash;
 use embedded_storage_async::nor_flash::NorFlash as AsyncNorFlash;
 use sequential_storage::{
     cache::NoCache,
-    map::{fetch_item, store_item, SerializationError, Value},
+    map::{fetch_all_items, fetch_item, store_item, SerializationError, Value},
     Error as SSError,
 };
 #[cfg(feature = "_nrf_ble")]
@@ -380,7 +380,6 @@ impl<F: AsyncNorFlash, const ROW: usize, const COL: usize, const NUM_LAYER: usiz
             );
             start_addr as u32..(start_addr + config.num_sectors as usize * F::ERASE_SIZE) as u32
         };
-        info!("storage range {}", storage_range);
 
         let mut storage = Self {
             flash,
@@ -562,44 +561,32 @@ impl<F: AsyncNorFlash, const ROW: usize, const COL: usize, const NUM_LAYER: usiz
         &mut self,
         keymap: &mut [[[KeyAction; COL]; ROW]; NUM_LAYER],
     ) -> Result<(), ()> {
-        for (layer, layer_data) in keymap.iter_mut().enumerate() {
-            for (row, row_data) in layer_data.iter_mut().enumerate() {
-                for (col, value) in row_data.iter_mut().enumerate() {
-                    let key = get_keymap_key::<ROW, COL, NUM_LAYER>(row, col, layer);
-                    let item = match fetch_item::<u32, StorageData, _>(
-                        &mut self.flash,
-                        self.storage_range.clone(),
-                        &mut NoCache::new(),
-                        &mut self.buffer,
-                        &key,
-                    )
-                    .await
-                    {
-                        Ok(Some(StorageData::KeymapKey(k))) => k.action,
-                        Ok(None) => {
-                            error!("Got none when reading keymap from storage at (layer,col,row)=({},{},{})", layer, col, row);
-                            return Err(());
-                        }
-                        Err(e) => {
-                            print_storage_error::<F>(e);
-                            error!(
-                                "Load keymap key from storage error: (layer,col,row)=({},{},{})",
-                                layer, col, row
-                            );
-                            return Err(());
-                        }
-                        _ => {
-                            error!(
-                                "Load keymap key from storage error: (layer,col,row)=({},{},{})",
-                                layer, col, row
-                            );
-                            return Err(());
-                        }
-                    };
-                    *value = item;
+        let mut storage_cache = NoCache::new();
+        if let Ok(mut key_iterator) = fetch_all_items::<u32, _, _>(
+            &mut self.flash,
+            self.storage_range.clone(),
+            &mut storage_cache,
+            &mut self.buffer,
+        )
+        .await
+        {
+            // Iterator the storage, read all keymap keys
+            while let Ok(Some((_key, item))) = key_iterator
+                .next::<u32, StorageData>(&mut self.buffer)
+                .await
+            {
+                match item {
+                    StorageData::KeymapKey(key) => {
+                        assert!(key.layer < NUM_LAYER);
+                        assert!(key.row < ROW);
+                        assert!(key.col < COL);
+                        keymap[key.layer][key.row][key.col] = key.action;
+                    }
+                    _ => continue,
                 }
             }
-        }
+        };
+
         Ok(())
     }
 


Test patch: