From d26399a9dd0c7879f18c799bac72c9ffbb0de3ff Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 3 Jan 2026 22:55:56 +0800 Subject: [PATCH] refactor ws2812 to be dynamic BIO machines & add mac test the BIO is dynamically allocated based on the ws2812 command allowing the BIO to be used for other demos instead of being locked up by the ws2812 object even when not in use. --- apps-dabao/dabao-console/src/cmds/test.rs | 13 +++++++++ apps-dabao/dabao-console/src/cmds/ws2812.rs | 29 ++++++++++++--------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/apps-dabao/dabao-console/src/cmds/test.rs b/apps-dabao/dabao-console/src/cmds/test.rs index fda79c94a..4b9b96eac 100644 --- a/apps-dabao/dabao-console/src/cmds/test.rs +++ b/apps-dabao/dabao-console/src/cmds/test.rs @@ -21,6 +21,19 @@ impl<'a> ShellCmdApi<'a> for Test { if let Some(sub_cmd) = tokens.next() { match sub_cmd { + "mac" => { + let mut md = bio_lib::mac_demo::MacDemo::new().unwrap(); + let a = [0, -1, 32, 6, -10, 5]; + let b = 7; + let hw = md.mac_hw(&a, b); + let sw = md.mac_sw(&a, b); + log::info!("result: {}, {}", hw, sw); + if hw == sw { + writeln!(ret, "pass").unwrap(); + } else { + writeln!(ret, "fail").unwrap(); + } + } "timer" => { let start = _env.ticktimer.elapsed_ms(); log::info!("Starting test"); diff --git a/apps-dabao/dabao-console/src/cmds/ws2812.rs b/apps-dabao/dabao-console/src/cmds/ws2812.rs index fa05f0f22..31875deea 100644 --- a/apps-dabao/dabao-console/src/cmds/ws2812.rs +++ b/apps-dabao/dabao-console/src/cmds/ws2812.rs @@ -8,20 +8,11 @@ const LED_BIO_PIN: u8 = 5; const INIT_STRIP_LENGTH: usize = 6; pub struct Ws2812 { - ws2812: bio_lib::ws2812::Ws2812, len: usize, + pin: arbitrary_int::u5, } impl Ws2812 { - pub fn new() -> Self { - Ws2812 { - ws2812: bio_lib::ws2812::Ws2812::new( - bio_lib::ws2812::LedVariant::B, - arbitrary_int::u5::new(LED_BIO_PIN), - ) - .unwrap(), - len: INIT_STRIP_LENGTH, - } - } + pub fn new() -> Self { Ws2812 { len: INIT_STRIP_LENGTH, pin: arbitrary_int::u5::new(LED_BIO_PIN) } } } impl<'a> ShellCmdApi<'a> for Ws2812 { @@ -36,7 +27,17 @@ impl<'a> ShellCmdApi<'a> for Ws2812 { if let Some(sub_cmd) = tokens.next() { match sub_cmd { + "pin" => { + if let Some(pin_str) = tokens.next() { + let pin = u8::from_str_radix(pin_str, 10).unwrap_or(LED_BIO_PIN); + log::info!("Setting output pin to BIO pin {}", pin); + self.pin = arbitrary_int::u5::new(pin); + } + } "rainbow" => { + let mut ws2812 = + bio_lib::ws2812::Ws2812::new(bio_lib::ws2812::LedVariant::B, self.pin).unwrap(); + let duration = Duration::from_secs(if let Some(duration) = tokens.next() { u64::from_str_radix(duration, 10).unwrap_or(u64::MAX) } else { @@ -63,7 +64,7 @@ impl<'a> ShellCmdApi<'a> for Ws2812 { } } // send - self.ws2812.send(&strip); + ws2812.send(&strip); env.ticktimer.sleep_ms(10).ok(); } } @@ -77,6 +78,8 @@ impl<'a> ShellCmdApi<'a> for Ws2812 { } "hexcolor" => { if let Some(hexcolor) = tokens.next() { + let mut ws2812 = + bio_lib::ws2812::Ws2812::new(bio_lib::ws2812::LedVariant::B, self.pin).unwrap(); if hexcolor.len() != 7 || &hexcolor[0..1] != "#" { write!(ret, "Usage: 'hexcolor #rrggbb'; the # is mandatory").unwrap(); return Ok(Some(ret)); @@ -85,7 +88,7 @@ impl<'a> ShellCmdApi<'a> for Ws2812 { let g = u8::from_str_radix(&hexcolor[3..5], 16).unwrap_or(0); let b = u8::from_str_radix(&hexcolor[5..7], 16).unwrap_or(0); let strip = vec![rgb_to_u32(r, g, b); self.len]; - self.ws2812.send(&strip); + ws2812.send(&strip); } else { write!(ret, "Usage: 'hexcolor #rrggbb'; the # is mandatory").unwrap(); }