added path and name parameters

This commit is contained in:
Данила Горнушко 2024-05-29 13:20:10 +03:00
parent b4c84d71e5
commit a90ad3fe05

View file

@ -6,6 +6,7 @@ use log::{debug, error, info, trace, LevelFilter};
use serialport::SerialPort;
use std::collections::HashMap;
use std::io::{BufRead, BufReader, Read, Write};
use std::path::Path;
use std::slice;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
@ -38,6 +39,12 @@ struct Args {
#[arg(short = 'f', long)]
first_sample: bool,
#[arg(long, default_value = "wave")]
name: String,
#[arg(long, default_value = "./out/")]
path: String,
#[arg(short, long, action = clap::ArgAction::Count)]
verbose: u8,
}
@ -82,7 +89,8 @@ fn main() {
}
get_set_threshold(&mut port, args.device_id, args.adcthresh).expect("Couldn't get threshold");
let winsize = get_set_winsize(&mut port, args.device_id, args.winsize).expect("Couldn't get winsize") as usize;
let winsize = get_set_winsize(&mut port, args.device_id, args.winsize)
.expect("Couldn't get winsize") as usize;
debug!("Expected buf size is: {}", exp_buf_size(winsize));
let spec = WavSpec {
@ -119,7 +127,9 @@ fn main() {
value
}
};
if (last_chunk_id != Some(cur_chunk_id) && last_chunk_id.is_some()) || (args.first_sample && last_chunk_id.is_none()) {
if (last_chunk_id != Some(cur_chunk_id) && last_chunk_id.is_some())
|| (args.first_sample && last_chunk_id.is_none())
{
debug!("New data available: {}", cur_chunk_id);
debug!("Getting new chunk!");
match get_chunk(&mut port, args.device_id, args.baud_rate, winsize) {
@ -127,11 +137,13 @@ fn main() {
debug!("Received new data with len: {}", data.len());
let timestamp_ms = current_timestamp_ms();
let filename = format!("./out/wave_{}.wav", timestamp_ms);
let path = Path::new(&args.path);
let filename = format!("{}_{}.wav", args.name, timestamp_ms);
let path = path.join(filename);
// Create a new WAV file
let mut writer =
WavWriter::create(&filename, spec).expect("Can't create new WAV file!");
WavWriter::create(&path, spec).expect("Can't create new WAV file!");
let mut i16_writer = writer.get_i16_writer(data.len() as u32);
for sample in data {
unsafe {
@ -141,7 +153,7 @@ fn main() {
i16_writer
.flush()
.expect("Something went wrong while writing WAV file");
info!("WAV file {} has been written.", filename);
info!("Sample: {cur_chunk_id}. WAV file {} has been written.", path.display());
err_counter = err_counter.saturating_sub(1);
last_chunk_id = Some(cur_chunk_id);
@ -179,7 +191,11 @@ fn get_stats(port: &mut Box<dyn SerialPort>, id: u32) -> Result<u32> {
Ok(num_since_startup)
}
fn get_set_winsize(port: &mut Box<dyn SerialPort>, id: u32, new_winsize: Option<u32>) -> Option<u32> {
fn get_set_winsize(
port: &mut Box<dyn SerialPort>,
id: u32,
new_winsize: Option<u32>,
) -> Option<u32> {
let command;
if let Some(i) = new_winsize {
debug!("Setting new winsize: {i}");