Skip to content

Commit

Permalink
refactor: move eeprom storage config calculation to it's constructor
Browse files Browse the repository at this point in the history
Signed-off-by: HaoboGu <[email protected]>
  • Loading branch information
HaoboGu committed Jan 30, 2024
1 parent 554ba8c commit c2020ec
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
10 changes: 9 additions & 1 deletion rmk/src/eeprom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,18 @@ pub(crate) struct Eeprom<F: NorFlash, const EEPROM_SIZE: usize> {
impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {
pub(crate) fn new<const ROW: usize, const COL: usize, const NUM_LAYER: usize>(
storage: F,
storage_config: EepromStorageConfig,
eeconfig: Option<Eeconfig>,
keymap: &[[[KeyAction; COL]; ROW]; NUM_LAYER],
) -> Option<Self> {
let mut storage_config = EepromStorageConfig {
start_addr: (F::ERASE_SIZE * (storage.capacity() / F::ERASE_SIZE - 1)) as u32,
storage_size: F::ERASE_SIZE as u32,
page_size: F::WRITE_SIZE as u32,
};
// At least write 4 byte(1 record)
if storage_config.page_size < 4 {
storage_config.page_size = 4;
}
// Check backend storage config
if (!is_power_of_two(storage_config.page_size))
|| storage_config.start_addr == 0
Expand Down
14 changes: 2 additions & 12 deletions rmk/src/keymap.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
action::KeyAction,
eeprom::{eeconfig::Eeconfig, Eeprom, EepromStorageConfig},
eeprom::{eeconfig::Eeconfig, Eeprom},
matrix::KeyState,
};
use defmt::warn;
Expand Down Expand Up @@ -70,17 +70,7 @@ impl<
// Initialize eeprom, if success, re-load keymap from it
let eeprom = match storage {
Some(s) => {
// TODO: Refactor Eeprom so that we don't need this anymore
let mut eeprom_storage_config = EepromStorageConfig {
start_addr: (F::ERASE_SIZE * (s.capacity() / F::ERASE_SIZE - 1)) as u32,
storage_size: F::ERASE_SIZE as u32,
page_size: F::WRITE_SIZE as u32,
};
// At least write 4 byte(1 record)
if eeprom_storage_config.page_size < 4 {
eeprom_storage_config.page_size = 4;
}
let e = Eeprom::new(s, eeprom_storage_config, eeconfig, &mut action_map);
let e = Eeprom::new(s, eeconfig, &mut action_map);
// If eeprom is initialized, read keymap from it.
match e {
Some(e) => {
Expand Down

0 comments on commit c2020ec

Please sign in to comment.