From 89a85c8dfb9d75377c591ccf35d6d631cba1d892 Mon Sep 17 00:00:00 2001 From: JadKHaddad Date: Sat, 21 Sep 2024 10:14:06 +0200 Subject: [PATCH 1/2] feat(Cargo): mappings feature is now enabled by default Signed-off-by: JadKHaddad --- tm1637/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tm1637/Cargo.toml b/tm1637/Cargo.toml index 1292ddc..3b9e0fc 100644 --- a/tm1637/Cargo.toml +++ b/tm1637/Cargo.toml @@ -10,7 +10,7 @@ keywords = ["tm1637", "embedded-hal", "no-std", "embedded"] readme = "../README.md" [features] -default = ["async", "impl-debug"] +default = ["async", "impl-debug", "mappings"] async = ["dep:embedded-hal", "dep:embedded-hal-async"] blocking = ["dep:embedded-hal"] mappings = [] From 411e0bc310d5ba24f94c66441236224dd9ca5775 Mon Sep 17 00:00:00 2001 From: JadKHaddad Date: Sat, 21 Sep 2024 10:26:43 +0200 Subject: [PATCH 2/2] feat(str): added move_ascii_str function Signed-off-by: JadKHaddad --- tm1637/src/device.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tm1637/src/device.rs b/tm1637/src/device.rs index f5fe4f1..eb7221a 100644 --- a/tm1637/src/device.rs +++ b/tm1637/src/device.rs @@ -311,6 +311,43 @@ pub mod module { Ok(()) } + + /// Convert an `ASCII` string to a byte array using [`from_ascii_byte`](crate::mappings::from_ascii_byte) and move the segments across the display using [`TM1637::move_segments_raw`]. + /// + /// - `N` is the size of the internal window used to move the segments. See [`TM1637::move_segments_raw`] for more information. + /// - `M` is the maximum number of bytes that can be converted from the `ASCII` string. + /// + /// # Example + /// + /// Move the string `"HELLO "` across a `4-digit display`: + /// + /// ```rust, ignore + /// let mut tm = TM1637::builder(clk_pin, dio_pin, delay) + /// .brightness(Brightness::L3) + /// .build(); + /// + /// tm.init().ok(); + /// + /// tm.move_ascii_str::<4, 6>(0, "HELLO ", 500).ok(); + /// ``` + #[cfg(feature = "mappings")] + pub async fn move_ascii_str( + &mut self, + position: u8, + ascii_str: &str, + delay_ms: u32, + ) -> Result<(), ERR> { + let mut bytes = [0u8; M]; + ascii_str + .as_bytes() + .iter() + .take(M) + .enumerate() + .for_each(|(i, &byte)| bytes[i] = crate::mappings::from_ascii_byte(byte)); + + self.move_segments_raw::(position, &bytes, delay_ms) + .await + } } }