diff --git a/src/main.rs b/src/main.rs index 9b1d7db..906410e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,10 +3,14 @@ #![feature(async_fn_track_caller, type_alias_impl_trait)] #![warn(rust_2018_idioms)] #![forbid(elided_lifetimes_in_paths, unsafe_code)] +// STOP HIGHLIGHTING MY ENTIRE CODE RED YOU MOTHERFUCKING PEACE OF GARBAGE SOFTWARE +// SOMETIMES I AM WRITING CODE BEFORE I INSERT A BREAK SOMEWHERE +// ARE YOU AWARE THAT CODING DOESN'T PRODUCE FINISHED CODE IN A MILLISECOND??????? +#![allow(clippy::never_loop, unreachable_code)] mod static_mutex; -use defmt::*; +use defmt::{info, println, unwrap}; use defmt_rtt as _; use embassy_executor::Spawner; use embassy_stm32::{ @@ -61,43 +65,75 @@ async fn query_time(mut pon: POn, mut tco: Tco, interval: Duration) { loop { pon.set_low(); info!("Waiting"); - tco.wait_for_falling_edge().await; - // for _ in 0 .. 10 { - // info!("Receiving"); - // let mut bytes = 0u64; - // for _ in 0 .. 60 { - // bytes = (bytes << 1) | (tco.is_low() as u64); - // Timer::after(Duration::from_millis(100)).await; - // } - // info!("DCF77: {:X}", bytes); - // } - let start = Instant::now(); - let mut next = start; - for _ in 0 .. 120 { - let mut bytes = 0u16; - for _ in 0 .. 10 { - bytes = (bytes << 1) | (tco.is_low() as u16); - next += Duration::from_millis(100); - Timer::at(next).await; - } - println!( - "{}{}{}{}{}{}{}{}{}{} ({})", - (bytes >> 9) & 1, - (bytes >> 8) & 1, - (bytes >> 7) & 1, - (bytes >> 6) & 1, - (bytes >> 5) & 1, - (bytes >> 4) & 1, - (bytes >> 3) & 1, - (bytes >> 2) & 1, - (bytes >> 1) & 1, - bytes & 1, - start.elapsed().as_millis() - ); - } - pon.set_high(); - Timer::after(interval).await; + // we need to find the minute marker + // that means we won't receive a transmission for an entire second + tco.wait_for_rising_edge().await; + loop { + tco.wait_for_falling_edge().await; + let start = Instant::now(); + tco.wait_for_rising_edge().await; + // our starting edge should have at least 800ms from the previous second, + // and then a full second. + if start.elapsed().as_millis() > 1500 { + break; + } + } + let mut start = Instant::now(); + + info!("Receiving"); + + // we always start on the rising edge, with `start` set already + const SECONDS: usize = 60; + let mut bytes = [0u64; SECONDS]; + let mut timing = [0u64; 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 = true; + "GARBAGE" + } + }; + println!( + "Bit {:02}: on for {:04} ms of {:04} ms total -- {:02}: {}", + i, bytes[i], timing[i], i, data + ); + Timer::after(Duration::from_millis(50)).await; + } + + if !garbage { + Timer::after(interval).await; + } } } @@ -108,7 +144,7 @@ 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);