From 0f3d95f2c86362160fdcc9047380a072756d26d4 Mon Sep 17 00:00:00 2001
From: Matt Johnston <matt@ucc.asn.au>
Date: Sat, 1 Oct 2022 10:20:52 +0800
Subject: [PATCH] jagged

up_x = if down < T {
    up_x * 2
} else {
    up_x.saturating_sub(down/2)
}.max(1);
---
 examples/rp/src/bin/usb_serial.rs | 35 +++++++++++++++++++++----------
 1 file changed, 24 insertions(+), 11 deletions(-)

diff --git a/examples/rp/src/bin/usb_serial.rs b/examples/rp/src/bin/usb_serial.rs
index f6652dd6..6f1465e8 100644
--- a/examples/rp/src/bin/usb_serial.rs
+++ b/examples/rp/src/bin/usb_serial.rs
@@ -110,18 +110,21 @@ async fn run<'d, P: Pin>(pin: &mut Flex<'d, P>, syst: &mut SYST) -> Result<(), D
         while pin.is_high() {}
     });
 
+    let mut up_x = 0u32;
+    let mut down_x = 0u32;
+    let mut up = 0u32;
+    let mut down = 0u32;
+
     loop {
         const LOOP: usize = 20;
-        const T: u32 = 10000;
+        const T: u32 = 8192;
         let mut dels = [0u32; LOOP];
         let mut ups = [0u32; LOOP];
         let mut downs = [0u32; LOOP];
-        critical_section::with(|_cs| {
+        let mut upx = [0u32; LOOP];
+        let mut downx = [0u32; LOOP];
 
-            let mut up = 0u32;
-            let mut up_x = 0u32;
-            let mut down = 0u32;
-            let mut down_x = 0u32;
+        critical_section::with(|_cs| {
 
             for i in 0..LOOP {
                 cortex_m::asm::delay(down_x);
@@ -131,8 +134,12 @@ async fn run<'d, P: Pin>(pin: &mut Flex<'d, P>, syst: &mut SYST) -> Result<(), D
                 while pin.is_low() {}
                 let t2 = SYST::get_current();
                 up = t1 - t2;
-                up_x = up_x + T - down;
-                up_x = up_x.min(T * 4);
+
+                up_x = if down < T {
+                    up_x * 2
+                } else {
+                    up_x.saturating_sub(down/2)
+                }.max(1);
 
                 cortex_m::asm::delay(up_x);
                 pin.set_pull(Pull::Down);
@@ -141,18 +148,24 @@ async fn run<'d, P: Pin>(pin: &mut Flex<'d, P>, syst: &mut SYST) -> Result<(), D
                 while pin.is_high() {}
                 let t2 = SYST::get_current();
                 down = t1 - t2;
-                down_x = down_x + T - up;
-                down_x = down_x.min(T * 4);
+
+                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;
             }
             pin.set_pull(Pull::None);
         });
 
         for i in 0..LOOP {
-            info!("{}  U {}  D {}", dels[i], ups[i], downs[i]);
+            info!("{}  U {} x {}  D {} x {}", dels[i], ups[i], upx[i], downs[i], downx[i]);
         }
         info!("===");
     }
-- 
GitLab