diff --git a/src/main.rs b/src/main.rs index 906410e..9b1d7db 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,14 +3,10 @@ #![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::{info, println, unwrap}; +use defmt::*; use defmt_rtt as _; use embassy_executor::Spawner; use embassy_stm32::{ @@ -65,75 +61,43 @@ async fn query_time(mut pon: POn, mut tco: Tco, interval: Duration) { loop { pon.set_low(); info!("Waiting"); - - // 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; + 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; } - } - 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 + "{}{}{}{}{}{}{}{}{}{} ({})", + (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() ); - Timer::after(Duration::from_millis(50)).await; } + pon.set_high(); - if !garbage { - Timer::after(interval).await; - } + Timer::after(interval).await; } } @@ -144,7 +108,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(120)))); + unwrap!(spawner.spawn(query_time(pon, tco, Duration::from_secs(300)))); LED.init(Output::new(p.PA5, Level::Low, Speed::Low)).await; let button = ExtiInput::new(Input::new(p.PC13, Pull::Up), p.EXTI13);