diff --git a/Cargo.toml b/Cargo.toml index 99dc2cd..2f5b35b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,8 @@ stm32f1xx-hal = { version = "~0.6.1", features = ["stm32f103", "rt"] } embedded-hal = {version = "~0.2.3"} nb = "~1.0.0" nmea0183 = "~0.2.3" +arrayvec = {version = "~0.7.0", default-features = false} + [features] # set logging levels here diff --git a/src/lib.rs b/src/lib.rs index 2d2b88f..6c837d2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ use panic_probe as _; use stm32f1xx_hal as _; pub mod application; +pub mod loc; pub mod time; // same panicking *behavior* as `panic-probe` but doesn't print a panic message diff --git a/src/loc.rs b/src/loc.rs new file mode 100644 index 0000000..2cff861 --- /dev/null +++ b/src/loc.rs @@ -0,0 +1,40 @@ +use arrayvec::ArrayString; + +const FIELD_SYMBOLS: &str = "ABCDEFGHIJKLMNOPQR"; +const SUBSQUARE_SYMBOLS: &str = "abcdefghijklmnopqrstuvwx"; + +pub fn locator_from_coordinates(lat: f32, long: f32) -> ArrayString<6> { + let mut target_buf = ArrayString::<6>::new(); + + let false_east = if long < 180.0 { + long + 180.0 + } else { + long - 180.0 + }; + + let false_north = if lat < 90.0 { lat + 90.0 } else { lat - 90.0 }; + + let long_field = ((false_east / 20.0) as usize); + let mut long_rest = false_east % 20.0; + target_buf.push(FIELD_SYMBOLS.chars().nth(long_field).unwrap()); + + let lat_field = ((false_north / 10.0) as usize); + let mut lat_rest = false_north % 10.0; + target_buf.push(FIELD_SYMBOLS.chars().nth(lat_field).unwrap()); + + let long_square = (long_rest / 2.0) as u32; + long_rest = long_rest % 2.0; + target_buf.push(char::from_digit(long_square, 10).unwrap()); + + let lat_square = (lat_rest / 1.0) as u32; + lat_rest = lat_rest % 1.0; + target_buf.push(char::from_digit(lat_square, 10).unwrap()); + + let long_subsquare = (long_rest / 2.0 * 24.0) as usize; + target_buf.push(SUBSQUARE_SYMBOLS.chars().nth(long_subsquare).unwrap()); + + let lat_subsquare = (lat_rest / 1.0 * 24.0) as usize; + target_buf.push(SUBSQUARE_SYMBOLS.chars().nth(lat_subsquare).unwrap()); + + return target_buf; +} diff --git a/testsuite/Cargo.toml b/testsuite/Cargo.toml index cd069fd..c3255d7 100644 --- a/testsuite/Cargo.toml +++ b/testsuite/Cargo.toml @@ -7,7 +7,7 @@ edition = "2018" version = "0.1.0" [[test]] -name = "test" +name = "loc" harness = false [dependencies] @@ -18,6 +18,7 @@ defmt = "0.2.0" defmt-rtt = "0.2.0" defmt-test = "0.2.0" panic-probe = { version = "0.2.0", features = ["print-defmt"] } +arrayvec = {version = "~0.7.0", default-features = false} [features] # set logging levels here diff --git a/testsuite/tests/test.rs b/testsuite/tests/loc.rs similarity index 64% rename from testsuite/tests/test.rs rename to testsuite/tests/loc.rs index 3c35c4b..cea86de 100644 --- a/testsuite/tests/test.rs +++ b/testsuite/tests/loc.rs @@ -7,15 +7,14 @@ use wspr_beacon as _; // memory layout + panic handler // feature) #[defmt_test::tests] mod tests { + use arrayvec::ArrayString; use defmt::{assert, assert_eq}; + use wspr_beacon::loc; #[test] fn assert_true() { - assert!(true) - } + let res = loc::locator_from_coordinates(49.4395, 7.7635); - #[test] - fn assert_eq() { - assert_eq!(24, 42, "TODO: write actual tests") + assert_eq!(res.as_str(), "JN39vk") } }