From 60f7cd9e5744daf3e55ec09fedee984e7d37aee4 Mon Sep 17 00:00:00 2001 From: JatoMixo Date: Sat, 31 Aug 2024 16:43:34 +0200 Subject: [PATCH 1/4] feat: added log_u16 and log_u16_array methods to logger --- libs/logging/src/lib.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/libs/logging/src/lib.rs b/libs/logging/src/lib.rs index 6445373..0ae46d8 100644 --- a/libs/logging/src/lib.rs +++ b/libs/logging/src/lib.rs @@ -25,6 +25,36 @@ impl<'a> Logger<'a> { .unwrap(); } } + + pub fn log_u16(&mut self, val: &u16) { + for digit_index in (1..6).rev() { + + // We have to temporarily convert the u16 to u32, otherwise doing 10⁶5 would cause an + // overflow in a u16 (100,000 > 65,535) + let digit = ((*val as u32 % 10u32.pow(digit_index as u32)) / 10u32.pow(digit_index as u32 - 1)) as u16; + let digit_character = digit + 48; // 48 is the offset of the numbers in the ASCII table + + block!(match self.uart.bwrite_all(&[digit_character as u8]) { + Ok(_) => Ok(()), + Err(_) => Err(nb::Error::Other(())), + }) + .unwrap(); + } + } + + pub fn log_u16_array(&mut self, array: &[u16]) { + self.log("["); + + array.iter().enumerate().for_each(|(index, &value)| { + self.log_u16(&value); + + if index != array.len() - 1 { + self.log(", "); + } + }); + + self.log("]"); + } } #[cfg(test)] From 2718cf19b5a9a7484854e4d6d1263a614c2609f5 Mon Sep 17 00:00:00 2001 From: JatoMixo Date: Sat, 31 Aug 2024 16:51:13 +0200 Subject: [PATCH 2/4] fixed typo in comment and added tests --- libs/logging/src/lib.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/libs/logging/src/lib.rs b/libs/logging/src/lib.rs index 0ae46d8..05690cc 100644 --- a/libs/logging/src/lib.rs +++ b/libs/logging/src/lib.rs @@ -29,7 +29,7 @@ impl<'a> Logger<'a> { pub fn log_u16(&mut self, val: &u16) { for digit_index in (1..6).rev() { - // We have to temporarily convert the u16 to u32, otherwise doing 10⁶5 would cause an + // We have to temporarily convert the u16 to u32, otherwise doing 10^5 would cause an // overflow in a u16 (100,000 > 65,535) let digit = ((*val as u32 % 10u32.pow(digit_index as u32)) / 10u32.pow(digit_index as u32 - 1)) as u16; let digit_character = digit + 48; // 48 is the offset of the numbers in the ASCII table @@ -108,4 +108,20 @@ mod tests { logger.log("Hello"); assert_eq!(mock_writer.get_string(), "Hello"); } + + #[test] + fn test_log_u16() { + let mut mock_writer = MockWriter::new(); + let mut logger = Logger::new(&mut mock_writer); + logger.log_u16(&3456u16); + assert_eq!(mock_writer.get_string(), "03456"); + } + + #[test] + fn test_log_u16_array() { + let mut mock_writer = MockWriter::new(); + let mut logger = Logger::new(&mut mock_writer); + logger.log_u16_array(&[100u16, 250u16, 500u16]); + assert_eq!(mock_writer.get_string(), "[00100, 00250, 00500]"); + } } From eecf7dd0d0218af6962cd8e2d28446d9ac2daade Mon Sep 17 00:00:00 2001 From: JatoMixo Date: Sun, 1 Sep 2024 17:17:08 +0200 Subject: [PATCH 3/4] small refactor --- libs/logging/src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libs/logging/src/lib.rs b/libs/logging/src/lib.rs index 05690cc..52510ed 100644 --- a/libs/logging/src/lib.rs +++ b/libs/logging/src/lib.rs @@ -29,12 +29,12 @@ impl<'a> Logger<'a> { pub fn log_u16(&mut self, val: &u16) { for digit_index in (1..6).rev() { - // We have to temporarily convert the u16 to u32, otherwise doing 10^5 would cause an + // We have to temporarily the u16 to u32, otherwise doing 10^5 would cause an // overflow in a u16 (100,000 > 65,535) - let digit = ((*val as u32 % 10u32.pow(digit_index as u32)) / 10u32.pow(digit_index as u32 - 1)) as u16; - let digit_character = digit + 48; // 48 is the offset of the numbers in the ASCII table + let digit = (*val as u32 % 10u32.pow(digit_index as u32)) / 10u32.pow(digit_index as u32 - 1); + let digit_character: u8 = digit as u8 + 48; // 48 is the offset of the numbers in the ASCII table - block!(match self.uart.bwrite_all(&[digit_character as u8]) { + block!(match self.uart.bwrite_all(&[digit_character]) { Ok(_) => Ok(()), Err(_) => Err(nb::Error::Other(())), }) From 8d6237f2008e1f85f74ca4c08f6af2f541fa069f Mon Sep 17 00:00:00 2001 From: JatoMixo Date: Sat, 14 Sep 2024 13:13:40 +0200 Subject: [PATCH 4/4] removed probe-rs-tools from github actions it was giving problems when installing it as a dependency, so i removed it to see if the problem dissappears --- .github/workflows/rust.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index fee02a7..e6f7a63 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -39,7 +39,6 @@ jobs: run: | rustup component add llvm-tools-preview rustup target add thumbv7m-none-eabi - cargo install probe-rs-tools cargo install cargo-binutils cargo-expand cargo install cargo-generate