Bring in location! Closes #2.

This commit is contained in:
mattbk
2026-09-11 18:49:32 -05:00
parent 1a5eb11206
commit 52a829a9a8
3 changed files with 280 additions and 9 deletions
+98 -8
View File
@@ -1,5 +1,7 @@
use chrono::Local;
use clap::Parser;
use nmea::Nmea;
use nmea::SentenceType;
use serialport::DataBits;
use serialport::Parity;
use serialport::StopBits;
@@ -14,12 +16,16 @@ use std::time::Duration;
#[derive(Parser, Debug)]
#[command(author, version, about = "ASMR: Automatic S-Meter Reader")]
struct Cli {
/// COM port
/// Rig COM port
#[arg(short = 'c', long)]
com: Option<String>,
/// GPS COM port
#[arg(short = 'g', long)]
gps: Option<String>,
/// Format
#[arg(short = 'o', long,)]
#[arg(short = 'o', long)]
file: Option<String>,
/// Frequency
@@ -31,6 +37,73 @@ struct Cli {
wait: Option<u16>,
}
// Get location
// Mofified from https://amiok.net/gitea/W1CDN/gpsll
fn get_loc(port: String) -> [f64; 2] {
let mut nmea = Nmea::default();
// Harcode port for dev
//let port_name = "/dev/tty.usbmodem1301";
let port_name = port;
//fn read_loc(com: &str) -> &str {
//println!("Reading from {port_name}");
let port = serialport::new(port_name, 9600)
.timeout(Duration::from_millis(1000))
.open();
if let Ok(mut serial) = port {
let mut buf = [0u8; 1024];
loop {
match serial.read(&mut buf) {
Ok(n) => {
let data = String::from_utf8_lossy(&buf[..n]);
for line in data.lines() {
// Check sentence type
let sentence_type = match nmea.parse(line) {
Ok(sentence_type) => sentence_type,
Err(_) => {
break;
}
};
// Only keep the GLL sentences
if sentence_type == SentenceType::GLL {
let lat = match nmea.latitude {
Some(lat) => lat,
None => -9999.25,
};
let lon = match nmea.longitude {
Some(lon) => lon,
None => -9999.25,
};
//let date = Local::now();
//println!("{}, {lat}, {lon}", date.format("%Y-%m-%d %H:%M:%S"));
return [lat, lon];
//"{},{:.8},{:.8}",
//date.format("%Y-%m-%d %H:%M:%S"),
//"{:.8},{:.8}",
}
}
}
Err(_) => {} // lol that I can just not put a return in?
}
}
} else {
// https://github.com/serialport/serialport-rs#listing-available-ports
let ports = serialport::available_ports().expect("No ports found!");
println!("Available Ports:");
for p in ports {
println!("{}", p.port_name);
}
return [-9999.25, -9999.25]; //format!("\nCould not connect to GPS port. Is it listed above?");
}
}
// Read S-meter
fn get_smeter(comport: &str) -> String {
let mut port = serialport::new(comport, 4800)
@@ -153,7 +226,21 @@ fn main() {
for p in ports {
println!("{}", p.port_name);
}
println!("\nMissing `-c` port argument, use one listed above.");
println!("\nMissing `-c` rig port argument, use one listed above.");
return;
} //todo!()
};
let gps = match cli.gps {
Some(gps) => gps,
None => {
// https://github.com/serialport/serialport-rs#listing-available-ports
let ports = serialport::available_ports().expect("No ports found!");
println!("Available Ports:");
for p in ports {
println!("{}", p.port_name);
}
println!("\nMissing `-g` GPS port argument, use one listed above.");
return;
} //todo!()
};
@@ -162,7 +249,7 @@ fn main() {
Some(file_out) => {
println!("Saving results to {}", file_out);
file_out
},
}
None => {
println!("Not saving results. Use `-o file.csv` to save to a file.");
"no file".to_string()
@@ -191,6 +278,8 @@ fn main() {
// Simple test loop to make sure we are getting values each time
//for _i in 1..11 {
loop {
// Get location
let ll = get_loc(gps.clone());
// Get datestamp
let date = Local::now();
// Get S-meter
@@ -211,9 +300,10 @@ fn main() {
if let Err(err) = write_row(
[
date.format("%Y-%m-%d %H:%M:%S").to_string(),
frequency.to_string(),
"lat".to_string(),
"lon".to_string(),
//frequency.to_string(),
format!("{:09}", frequency),
format!("{:.8}", ll[0]),
format!("{:.8}", ll[1]),
s_val.to_string(),
],
&file_out,
@@ -222,6 +312,6 @@ fn main() {
println!("{}", err);
}
}
sleep(Duration::from_millis(wait as u64 * 1000));
sleep(Duration::from_millis((wait as u64 * 1000) - 1000));
}
}