diff --git a/Cargo.toml b/Cargo.toml index fcda0fb..42d46b8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,3 +14,7 @@ embassy-stm32 = { git = "https://github.com/embassy-rs/embassy", features = ["de embassy-sync = { version = "0.3", features = ["nightly"] } embassy-time = { version = "0.1.3", features = ["defmt", "nightly"] } panic-probe = { version = "0.3", features = ["print-defmt"] } + +[profile.release] +debug = true +opt-level = "z" diff --git a/src/main.rs b/src/main.rs index f36acb7..906410e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,19 +3,23 @@ #![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::{ exti::ExtiInput, gpio::{Input, Level, Output, Pull, Speed}, - peripherals::{PA5, PC13} + peripherals::{PA0, PA1, PA5, PC13} }; use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex; -use embassy_time::{Duration, Timer}; +use embassy_time::{Duration, Instant, Timer}; use panic_probe as _; use static_mutex::StaticMutex; @@ -24,6 +28,8 @@ type MutexGuard<'a, T> = embassy_sync::mutex::MutexGuard<'a, ThreadModeRawMutex, type NucleoLed = Output<'static, PA5>; type NucleoButton = ExtiInput<'static, PC13>; +type POn = Output<'static, PA0>; +type Tco = ExtiInput<'static, PA1>; static LED: StaticMutex = StaticMutex::new(); static BLINKING: Mutex = Mutex::new(true); @@ -54,10 +60,92 @@ async fn button_handler(mut button: NucleoButton) { } } +#[embassy_executor::task] +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; + } + } + 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; + } + } +} + #[embassy_executor::main] async fn main(spawner: Spawner) { let p = embassy_stm32::init(Default::default()); + 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)))); + LED.init(Output::new(p.PA5, Level::Low, Speed::Low)).await; let button = ExtiInput::new(Input::new(p.PC13, Pull::Up), p.EXTI13);