Add more info for user.

This commit is contained in:
mattbk
2026-09-11 17:52:17 -05:00
parent 097da86536
commit 1a5eb11206
+45 -46
View File
@@ -1,29 +1,25 @@
use chrono::Local;
use clap::Parser;
use serialport::DataBits;
use serialport::Parity;
use serialport::StopBits;
use std::error::Error;
use std::fs::OpenOptions;
use std::io::ErrorKind;
use std::io::Write;
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)]
#[command(
author,
version,
about = "ASMR: Automatic S-Meter Reader"
)]
#[command(author, version, about = "ASMR: Automatic S-Meter Reader")]
struct Cli {
/// COM port
#[arg(short = 'c', long)]
com: Option<String>,
/// Format
#[arg(short = 'o', long, default_value = "result.csv")]
#[arg(short = 'o', long,)]
file: Option<String>,
/// Frequency
@@ -101,18 +97,17 @@ fn set_freq(comport: &str, freq: u32) -> String {
std::io::stdout().flush().unwrap();
}
Err(ref e) if e.kind() == ErrorKind::TimedOut => (),
Err(e) => eprintln!("{:?}", e)
Err(e) => eprintln!("{:?}", e),
}
let mut buf = [0u8; 1024];
match port.read(&mut buf) {
Ok(_) => {
return format!("Frequency set.");
}
Err(_) => {
return format!("Failed to set frequency.");
}
match port.read(&mut buf) {
Ok(_) => {
return format!("Frequency set.");
}
Err(_) => {
return format!("Failed to set frequency.");
}
}
}
@@ -120,37 +115,32 @@ fn set_freq(comport: &str, freq: u32) -> String {
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()
.write(true)
.create(true)
.append(true)
.open(file)
.unwrap();
let mut wtr = csv::Writer::from_writer(file);
let file = OpenOptions::new()
.write(true)
.create(true)
.append(true)
.open(file)
.unwrap();
let mut wtr = csv::Writer::from_writer(file);
// Since we're writing records manually, we must explicitly write our
// header record. A header record is written the same way that other
// records are written.
// wtr.write_record(["City", "State", "Population", "Latitude", "Longitude"])?;
let _ = wtr.write_record(str_arr);
// Since we're writing records manually, we must explicitly write our
// header record. A header record is written the same way that other
// records are written.
// wtr.write_record(["City", "State", "Population", "Latitude", "Longitude"])?;
let _ = wtr.write_record(str_arr);
// A CSV writer maintains an internal buffer, so it's important
// to flush the buffer when you're done.
wtr.flush()?;
Ok(())
// A CSV writer maintains an internal buffer, so it's important
// to flush the buffer when you're done.
wtr.flush()?;
Ok(())
} else {
println!("Only accepts csv format for now, not recording to file.");
Ok(())
}
}
// Main funtion
fn main() {
// Arguments
let cli = Cli::parse();
@@ -169,9 +159,13 @@ fn main() {
};
let file_out = match cli.file {
Some(file_out) => file_out,
Some(file_out) => {
println!("Saving results to {}", file_out);
file_out
},
None => {
return;
println!("Not saving results. Use `-o file.csv` to save to a file.");
"no file".to_string()
} //todo!()
};
@@ -204,11 +198,16 @@ fn main() {
//println!("{a}");
match s_val.parse::<i32>() {
// Simple graph as we go. Eventually this will be replaced.
Ok(n) => println!("{} {}", s_val, "#".repeat(n.try_into().unwrap())),
Ok(n) => println!(
"{} {} {}",
date.format("%Y-%m-%d %H:%M:%S").to_string(),
s_val,
"#".repeat(n.try_into().unwrap())
),
Err(_e) => todo!(),
}
// If they provided a file name
if file_out != "" {
if file_out != "no file" {
if let Err(err) = write_row(
[
date.format("%Y-%m-%d %H:%M:%S").to_string(),
@@ -218,11 +217,11 @@ fn main() {
s_val.to_string(),
],
&file_out,
"csv"
"csv",
) {
println!("{}", err);
}
}
sleep(Duration::from_millis(wait as u64 * 1000));
}
sleep(Duration::from_millis(wait as u64 * 1000));
}
}