// SPDX-License-Identifier: MIT OR Apache-2.0 // Copyright 2024 René Ladan use radio_datetime_utils::{DST_ANNOUNCED, DST_PROCESSED, DST_SUMMER}; use msf60_utils::MSFUtils; // quick and dirty parsing function fn parse_dst(dst: Option) -> String { if dst.is_none() { return String::from("*"); } let mut res = String::from(""); let s_dst = dst.unwrap(); if s_dst & DST_ANNOUNCED != 0 { res += "announced "; } if s_dst & DST_PROCESSED != 0 { res += "processed "; } if s_dst & DST_SUMMER != 0 { res += "summer"; } res } fn str_weekday(day: Option) -> String { String::from(match day { Some(0) => "Sunday", Some(1) => "Monday", Some(2) => "Tuesday", Some(3) => "Wednesday", Some(4) => "Thursday", Some(5) => "Friday", Some(6) => "Saturday", Some(7) => "?", None => "None", _ => "", }) } fn main() { // Pretend to do live decoding, e.g. by detecting the time differences in microseconds from // edges read from a GPIO pin. Samples need to be in absolute time since some point, not in // relative time to each other. Format is (time-in-us-since-some-point, edge-becomes-high). // The boolean value is the inverse of the radio signal. const SAMPLES: [(u32, bool); 135] = [ (480097898, false), (480909129, true), (480998372, false), (481905456, true), (482396098, false), (482906504, true), (482993883, false), (483908888, true), (483993519, false), (483993578, false), (483993623, false), (484905643, true), (484999472, false), (485909536, true), (485997179, false), (486908192, true), (486998019, false), (487906286, true), (487997465, false), (488900307, true), (488999571, false), (489904756, true), (489996274, false), (490905915, true), (490993902, false), (491904343, true), (491998425, false), (492905411, true), (492994929, false), (493907275, true), (494000510, false), (494909312, true), (494997767, false), (495907859, true), (495999521, false), (496903804, true), (496996022, false), (497904393, true), (497998389, false), (498902023, true), (498995599, false), (499906248, true), (499994605, false), (500911975, true), (501097918, false), (501906651, true), (502000289, false), (502907018, true), (502999103, false), (503905111, true), (503998501, false), (504903060, true), (505093147, false), (505905830, true), (505997306, false), (506908387, true), (507092576, false), (507909534, true), (507993459, false), (508903628, true), (509001819, false), (509902333, true), (509998524, false), (510906592, true), (511093691, false), (511903057, true), (511999019, false), (512905544, true), (512998127, false), (513904291, true), (513996323, false), (514909090, true), (515094873, false), (515906002, true), (515999869, false), (516905713, true), (517094858, false), (517905143, true), (518096078, false), (518902241, true), (519094998, false), (519903930, true), (519998402, false), (520905710, true), (520995714, false), (521907100, true), (522094065, false), (522905204, true), (523000056, false), (523000091, false), (523000103, true), (523000116, false), (523000129, false), (523904895, true), (523995464, false), (524906217, true), (525000486, false), (525904862, true), (526094321, false), (526905690, true), (527090263, false), (527911351, true), (527994518, false), (528903925, true), (529093867, false), (529903335, true), (530092889, false), (530902052, true), (530994385, false), (531904428, true), (531995603, false), (532904460, true), (532997952, false), (533908377, true), (533994459, false), (534908716, true), (535095720, false), (535906164, true), (536192900, false), (536903709, true), (537192272, false), (537907007, true), (538192799, false), (538910068, true), (539095963, false), (539906169, true), (540094776, false), (540905086, true), (540996129, false), (541903768, true), (542391315, false), (542903986, true), (542993789, false), (543902386, true), (543996392, false), ]; let mut msf = MSFUtils::default(); let mut old_time = SAMPLES[0].0; let mut old_second = 0xff; for s in SAMPLES { msf.handle_new_edge(s.1, s.0); if s.1 { println!( "{}{} {:?} {:?} {} {}", if msf.get_second() == old_second { "* " } else { "" }, msf.get_second(), msf.get_current_bit_a(), msf.get_current_bit_b(), msf.is_new_minute(), msf.is_past_new_minute(), ); old_second = msf.get_second(); if (s.0 - old_time > msf.get_spike_limit()) && !msf.increase_second() { println!( "Bad increase_second at second {}->{}", old_second, msf.get_second() ); } } else if msf.is_new_minute() { // This happens _before_ the above call to increase_second() in time, because // handle_new_edge() just found the 0111_1110 bit train at the high-to-low edge, // supposedly at second 59. println!("New minute! Time to decode!"); msf.decode_time(true, true); println!("DUT1={:?}", msf.get_dut1()); let rdt = msf.get_radio_datetime(); println!( "Parities={:?} {:?} {:?} {:?}", msf.get_parity_1(), msf.get_parity_2(), msf.get_parity_3(), msf.get_parity_4() ); println!( "Date/time={:?}-{:?}-{:?} {:?}:{:?} {} {}", rdt.get_year(), rdt.get_month(), rdt.get_day(), rdt.get_hour(), rdt.get_minute(), str_weekday(rdt.get_weekday()), parse_dst(rdt.get_dst()) ); } old_time = s.0; } }