diff --git a/src/main.rs b/src/main.rs index ed01a08..906410e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -87,22 +87,42 @@ async fn query_time(mut pon: POn, mut tco: Tco, interval: Duration) { const SECONDS: usize = 60; let mut bytes = [0u64; SECONDS]; let mut timing = [0u64; SECONDS]; - for i in 0 .. SECONDS { + let mut i = 0; + while i < SECONDS { tco.wait_for_falling_edge().await; bytes[i] = start.elapsed().as_millis(); tco.wait_for_rising_edge().await; timing[i] = start.elapsed().as_millis(); start = Instant::now(); + + // verify that the transmission wasn't just noise of the previous one + if i > 0 && bytes[i] < 100 && timing[i - 1] - bytes[i - 1] < 100 { + bytes[i - 1] += timing[i - 1] + bytes[i]; + timing[i - 1] += timing[i]; + } + // and verify that the transmission wasn't a minute marker + else if timing[i] >= 1500 { + info!("Retrying"); + i = 0; + } + // otherwise advance the second + else { + i += 1; + } } pon.set_high(); info!("Done"); + let mut garbage = false; for i in 0 .. SECONDS { let data = match (bytes[i], timing[i]) { (75 ..= 174, 900 ..= 1100) => "0", (175 ..= 300, 900 ..= 1100) => "1", - _ => "GARBAGE" + _ => { + garbage = true; + "GARBAGE" + } }; println!( "Bit {:02}: on for {:04} ms of {:04} ms total -- {:02}: {}", @@ -111,7 +131,9 @@ async fn query_time(mut pon: POn, mut tco: Tco, interval: Duration) { Timer::after(Duration::from_millis(50)).await; } - Timer::after(interval).await; + if !garbage { + Timer::after(interval).await; + } } } @@ -122,11 +144,11 @@ async fn main(spawner: Spawner) { let pon = Output::new(p.PA0, Level::High, Speed::Low); let tco = ExtiInput::new(Input::new(p.PA1, Pull::None), p.EXTI1); - unwrap!(spawner.spawn(query_time(pon, tco, Duration::from_secs(300)))); + unwrap!(spawner.spawn(query_time(pon, tco, Duration::from_secs(120)))); LED.init(Output::new(p.PA5, Level::Low, Speed::Low)).await; let button = ExtiInput::new(Input::new(p.PC13, Pull::Up), p.EXTI13); - unwrap!(spawner.spawn(blinker(Duration::from_millis(120)))); + unwrap!(spawner.spawn(blinker(Duration::from_millis(500)))); unwrap!(spawner.spawn(button_handler(button))); }