Added gridlocator calculation

This commit is contained in:
Sebastian 2021-04-16 20:35:33 +02:00
parent f005c5c178
commit 77b732824b
5 changed files with 49 additions and 6 deletions

View File

@ -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

View File

@ -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

40
src/loc.rs Normal file
View File

@ -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;
}

View File

@ -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

View File

@ -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")
}
}