Skip to content

Commit

Permalink
refactor(log): use defmt instead of log crate, to reduce binary size
Browse files Browse the repository at this point in the history
Signed-off-by: Haobo Gu <haobogu@outlook.com>
  • Loading branch information
HaoboGu committed Jan 24, 2024
1 parent a5c3eca commit eae2df4
Show file tree
Hide file tree
Showing 20 changed files with 116 additions and 230 deletions.
13 changes: 13 additions & 0 deletions boards/rp2040/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion boards/rp2040/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ cortex-m-rt = "0.7.3"
embedded-storage = "0.3"
portable-atomic = { version = "1.5", features = ["critical-section"] }
packed_struct = { version = "0.10.1", default-features = false }
static_cell = { version = "2" }
static_cell = "2"
defmt = "0.3"
defmt-rtt = "0.4"
panic-probe = { version = "0.3", features = ["print-defmt"] }
Expand Down
2 changes: 1 addition & 1 deletion boards/stm32f4/.cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ target = "thumbv7em-none-eabihf" # Cortex-M4F and Cortex-M7F (with FPU)
# target = "thumbv8m.main-none-eabihf" # Cortex-M33 (with FPU)

[env]
DEFMT_LOG = "info"
DEFMT_LOG = "debug"
12 changes: 12 additions & 0 deletions boards/stm32f4/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions boards/stm32f4/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ embassy-executor = { version = "0.5", features = [
"integrated-timers",
] }
packed_struct = { version = "0.10.1", default-features = false }
embedded-storage = { version = "0.3" }
static_cell = { version = "2" }
embedded-storage = "0.3"
static_cell = "2"

# defmt deps
defmt = "0.3"
Expand Down
2 changes: 1 addition & 1 deletion boards/stm32h7/.cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ target = "thumbv7em-none-eabihf" # Cortex-M4F and Cortex-M7F (with FPU)
# target = "thumbv8m.main-none-eabihf" # Cortex-M33 (with FPU)

[env]
DEFMT_LOG = "info"
DEFMT_LOG = "debug"
12 changes: 12 additions & 0 deletions boards/stm32h7/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions boards/stm32h7/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ embassy-executor = { version = "0.5", features = [
"integrated-timers",
] }
packed_struct = { version = "0.10.1", default-features = false }
embedded-storage = { version = "0.3" }
static_cell = { version = "2" }
embedded-storage = "0.3"
static_cell = "2"

# rtt-target = "0.5.0"
# panic-rtt-target = { version = "0.1.2", features = ["cortex-m"] }
Expand Down
16 changes: 9 additions & 7 deletions rmk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,23 @@ edition = "2021"
license = "MIT OR Apache-2.0"

[dependencies]
embedded-hal = "1.0.0"
embedded-hal = { version = "1.0.0", features = ["defmt-03"] }
embedded-storage = "0.3"
embedded-alloc = "0.5"
embassy-time = { version = "0.3" }
embassy-time = { version = "0.3", features = ["defmt"] }
embassy-usb = { version = "0.1", features = [
"defmt",
"usbd-hid",
"max-interface-count-8",
"max-handler-count-8",
] }
embassy-sync = { version = "0.5" }
embassy-futures = { version = "0.1" }
num_enum = { version = "0.7", default-features = false }
embassy-sync = { version = "0.5", features = ["defmt"] }
embassy-futures = { version = "0.1", features = ["defmt"] }
usbd-hid = { version = "0.6.1", features = ["defmt"] }
defmt = "0.3"
log = "0.4"
static_cell = { version = "2" }
usbd-hid = "0.6.1"
static_cell = "2"
num_enum = { version = "0.7", default-features = false }
packed_struct = { version = "0.10", default-features = false }
byteorder = { version = "1.4", default-features = false }

Expand Down
8 changes: 4 additions & 4 deletions rmk/src/action.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::keycode::{KeyCode, ModifierCombination};
use log::{error, warn};
use defmt::{error, warn, Format};
use num_enum::FromPrimitive;
use packed_struct::PackedStructSlice;

Expand All @@ -15,7 +15,7 @@ use packed_struct::PackedStructSlice;
/// The `BasicAction` represents only a single key action of keycodes defined in HID spec. The `Action` represents all actions defined in the following `Action` enum, including modifier combination and layer switch.
///
/// The KeyActionType bits varies between different types of a KeyAction, see docs of each enum variant.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[derive(Format, Copy, Clone, PartialEq, Eq)]
pub enum KeyAction {
/// No action. Serialized as 0x0000.
No,
Expand Down Expand Up @@ -120,7 +120,7 @@ impl KeyAction {

/// A single basic action that a keyboard can execute.
/// An Action can be represented in 12 bits, aka 0x000 ~ 0xFFF
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[derive(Debug, Format, Copy, Clone, PartialEq, Eq)]
pub enum Action {
/// A normal key stroke, uses for all keycodes defined in `KeyCode` enum, including mouse key, consumer/system control, etc.
///
Expand Down Expand Up @@ -177,7 +177,7 @@ impl Action {
Action::LayerToggle(layer)
}
_ => {
warn!("Not a valid 12-bit action code: {:X}", action_code);
warn!("Not a valid 12-bit action code: {:#X}", action_code);
Action::Key(KeyCode::No)
}
}
Expand Down
27 changes: 11 additions & 16 deletions rmk/src/eeprom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use self::eeconfig::{Eeconfig, EEPROM_MAGIC};
use crate::{action::KeyAction, keymap::KeyMapConfig};
use alloc::vec;
use core::sync::atomic::{AtomicBool, Ordering::SeqCst};
use defmt::{debug, error, info, warn};
use embedded_storage::nor_flash::NorFlash;
use log::{debug, error, info, warn};

/// A record in the eeprom, with 2-byte address and 2-byte data
/// A record is 4-byte long, so the tracking pos in the `Eeprom` implementation must be a multiple of 4
Expand Down Expand Up @@ -133,11 +133,8 @@ impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {
eeprom.cache[record.address as usize + 1] = record.data as u8;
eeprom.pos += eeprom.storage_config.page_size;
}
Err(e) => {
error!(
"Restore eeprom value at pos {:X} error: {:?}",
eeprom.pos, e
);
Err(_) => {
error!("Restore eeprom value at pos {} error", eeprom.pos);
break;
}
}
Expand Down Expand Up @@ -212,17 +209,16 @@ impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {
break;
} else {
warn!(
"Writing addr {:X} is not 0xFF",
"Writing addr {:#X} is not 0xFF",
self.storage_config.start_addr + self.pos
);
self.pos += self.storage_config.page_size;
}
}
Err(e) => {
Err(_) => {
warn!(
"Check addr {:X} error before writing: {:?}",
self.storage_config.start_addr + self.pos,
e
"Check addr {:#X} error before writing",
self.storage_config.start_addr + self.pos
);
// Go to next possible addr
self.pos += self.storage_config.page_size;
Expand All @@ -234,7 +230,7 @@ impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {
let mut buf = vec![0xFF_u8; self.storage_config.page_size as usize];
buf[..bytes.len()].copy_from_slice(&bytes);
debug!(
"EEPROM write storage length {:?} at 0x{:X}: {:X?} ",
"EEPROM write storage length {} at {:#X}: {=[u8]:#X} ",
buf.len(),
self.storage_config.start_addr + self.pos,
buf,
Expand All @@ -246,11 +242,10 @@ impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {
{
// `pos` should be aligned by write size
Ok(_) => self.pos += self.storage_config.page_size,
Err(e) => {
Err(_) => {
error!(
"Failed to write record to storage at 0x{:X}: {:?}",
self.storage_config.start_addr + self.pos,
e
"Failed to write record to storage at 0x{:#X}",
self.storage_config.start_addr + self.pos
)
}
}
Expand Down
Loading

0 comments on commit eae2df4

Please sign in to comment.