Add rate parameter (time). Part of #14.

This commit is contained in:
mattbk
2026-09-11 17:29:52 -05:00
parent 0fffecf5b9
commit c65ab32c06
3 changed files with 248 additions and 15 deletions
+42 -15
View File
@@ -8,6 +8,7 @@ use std::thread::sleep;
use std::time::Duration;
use std::{error::Error};
use clap::Parser;
use chrono::Local;
/// Command-line arguments using Clap.
#[derive(Parser, Debug)]
@@ -28,6 +29,10 @@ struct Cli {
/// Frequency
#[arg(short = 'f', long)]
freq: Option<u32>,
/// Seconds to wait between measurements
#[arg(short = 'w', long, default_value = "5")]
wait: Option<u16>,
}
// Read S-meter
@@ -112,7 +117,7 @@ fn set_freq(comport: &str, freq: u32) -> String {
}
// Write row to a file
fn write_row(str_arr: [String; 4], file: &String, fmt: &str) -> Result<(), Box<dyn Error>> {
fn write_row(str_arr: [String; 5], file: &String, fmt: &str) -> Result<(), Box<dyn Error>> {
// From https://docs.rs/csv/latest/csv/tutorial/index.html#writing-csv
if fmt == "csv" {
let file = OpenOptions::new()
@@ -139,8 +144,13 @@ fn write_row(str_arr: [String; 4], file: &String, fmt: &str) -> Result<(), Box<d
}
}
// Main funtion
fn main() {
// Arguments
let cli = Cli::parse();
@@ -172,11 +182,24 @@ fn main() {
} //todo!()
};
let wait: u16 = match cli.wait {
Some(wait) => wait,
None => {
return;
} //todo!()
};
// Set the requested frequency
set_freq(&com, frequency);
println!("Press Ctrl + C to quit.");
// Simple test loop to make sure we are getting values each time
for _i in 1..11 {
//for _i in 1..11 {
loop {
// Get datestamp
let date = Local::now();
// Get S-meter
let s_val = get_smeter(&com);
//println!("{a}");
match s_val.parse::<i32>() {
@@ -184,18 +207,22 @@ fn main() {
Ok(n) => println!("{} {}", s_val, "#".repeat(n.try_into().unwrap())),
Err(_e) => todo!(),
}
if let Err(err) = write_row(
[
"time".to_string(),
"lat".to_string(),
"lon".to_string(),
s_val.to_string(),
],
&file_out,
"csv"
) {
println!("{}", err);
}
// If they provided a file name
if file_out != "" {
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(),
s_val.to_string(),
],
&file_out,
"csv"
) {
println!("{}", err);
}
}
sleep(Duration::from_millis(wait as u64 * 1000));
}
}