From afcf71dc4f8e48b5b13d45f40fbe8f91e13ab632 Mon Sep 17 00:00:00 2001 From: Matt Johnston <matt@ucc.asn.au> Date: Sat, 1 Oct 2022 15:18:33 +0800 Subject: [PATCH] Moved to step() function Changed to separate LOOP vs BUF. No change to algorithm --- examples/rp/src/bin/usb_serial.rs | 49 +++++++++++++++++-------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/examples/rp/src/bin/usb_serial.rs b/examples/rp/src/bin/usb_serial.rs index 6f1465e8..92d35904 100644 --- a/examples/rp/src/bin/usb_serial.rs +++ b/examples/rp/src/bin/usb_serial.rs @@ -117,12 +117,21 @@ async fn run<'d, P: Pin>(pin: &mut Flex<'d, P>, syst: &mut SYST) -> Result<(), D loop { const LOOP: usize = 20; + const BUF: usize = 16; const T: u32 = 8192; - let mut dels = [0u32; LOOP]; - let mut ups = [0u32; LOOP]; - let mut downs = [0u32; LOOP]; - let mut upx = [0u32; LOOP]; - let mut downx = [0u32; LOOP]; + let mut dels = [0u32; BUF]; + let mut ups = [0u32; BUF]; + let mut downs = [0u32; BUF]; + let mut upx = [0u32; BUF]; + let mut downx = [0u32; BUF]; + + let step = |prev: u32, meas: u32| { + if meas < T || prev < T { + prev * 2 + } else { + prev.saturating_sub(T / 2) + }.max(1) + }; critical_section::with(|_cs| { @@ -135,11 +144,7 @@ async fn run<'d, P: Pin>(pin: &mut Flex<'d, P>, syst: &mut SYST) -> Result<(), D let t2 = SYST::get_current(); up = t1 - t2; - up_x = if down < T { - up_x * 2 - } else { - up_x.saturating_sub(down/2) - }.max(1); + up_x = step(up_x, down); cortex_m::asm::delay(up_x); pin.set_pull(Pull::Down); @@ -149,22 +154,22 @@ async fn run<'d, P: Pin>(pin: &mut Flex<'d, P>, syst: &mut SYST) -> Result<(), D let t2 = SYST::get_current(); down = t1 - t2; - down_x = if up < T { - down_x * 2 - } else { - down_x.saturating_sub(up/2) - }.max(1); - - ups[i] = up; - upx[i] = up_x; - downs[i] = down; - downx[i] = down_x; - dels[i] = up + down; + down_x = step(down_x, up); + + if i < BUF { + let n = i; + ups[n] = up; + upx[n] = up_x; + downs[n] = down; + downx[n] = down_x; + dels[n] = up + down; + } + } pin.set_pull(Pull::None); }); - for i in 0..LOOP { + for i in 0..BUF { info!("{} U {} x {} D {} x {}", dels[i], ups[i], upx[i], downs[i], downx[i]); } info!("==="); -- GitLab