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