Skip to content

Commit

Permalink
Add quiet option
Browse files Browse the repository at this point in the history
  • Loading branch information
bofh69 committed May 20, 2022
1 parent c22c232 commit 4a6e78f
Showing 1 changed file with 37 additions and 22 deletions.
59 changes: 37 additions & 22 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@ enum Unit {
/// Measure current with Otii Arc measurement tool
#[derive(StructOpt)]
struct Cli {

/// Give up measurement after this many minutes
#[structopt(long, default_value = "20")]
timeout_min: u32,

/// Measure for this many seconds
#[structopt(name = "measurement-time", long, default_value = "1")]
measurement_time_sec: u32,

/// Wait this many seconds until measurement starts
#[structopt(name = "wait", long, default_value = "0")]
wait_to_start_sec: u32,
Expand Down Expand Up @@ -82,6 +81,10 @@ struct Cli {
/// GPI2 needs to be true before measurement
#[structopt(long = "wait-for-GPI2")]
wait_for_gpi2: Option<bool>,

/// Don't show a progress bar
#[structopt(long = "quiet")]
quiet: bool,
}

pub trait Tracer {
Expand Down Expand Up @@ -271,13 +274,18 @@ impl Otii {

// TODO Rewrite to use closure for the actual parsing to remove
// code duplication with wait_for_gpi
fn get_average_current(&mut self, max_samples: u32) -> f64 {
fn get_average_current(&mut self, max_samples: u32, quiet: bool) -> f64 {
let mut samples = 1;
let mut last_sample = 0;
let mut total_value = 0.0;
let mut nr_values = 0;
let pb = indicatif::ProgressBar::new(u64::from(max_samples));
pb.set_style(indicatif::ProgressStyle::default_bar().progress_chars("█▉▊▋▌▍▎▏ "));
let pb = if quiet {
None
} else {
let pb = indicatif::ProgressBar::new(u64::from(max_samples));
pb.set_style(indicatif::ProgressStyle::default_bar().progress_chars("█▉▊▋▌▍▎▏ "));
Some(pb)
};
loop {
if let Some(line) = self.read_line() {
let mut data = line.trim().split(':');
Expand Down Expand Up @@ -313,25 +321,29 @@ impl Otii {
};
nr_values += tmp_nrs;
samples += 1;
pb.inc(1);

pb.set_message(format!(
"{}/{}s",
indicatif::HumanDuration(std::time::Duration::from_secs(u64::from(
samples / SAMPLES_PER_SECOND
))),
indicatif::HumanDuration(std::time::Duration::from_secs(u64::from(
max_samples
)))
));
if let Some(ref pb) = &pb {
pb.inc(1);

pb.set_message(format!(
"{}/{}s",
indicatif::HumanDuration(std::time::Duration::from_secs(u64::from(
samples / SAMPLES_PER_SECOND
))),
indicatif::HumanDuration(std::time::Duration::from_secs(u64::from(
max_samples
)))
));
}

if samples > max_samples {
break;
}
}
}
}
pb.finish();
if let Some(pb) = pb {
pb.finish();
}
total_value / f64::from(nr_values)
}

Expand Down Expand Up @@ -419,11 +431,13 @@ fn main() {
// Handle timeout:
let timer = timer::Timer::new();
// If _guard is dropped, the timer is cancelled
let _guard =
timer.schedule_with_delay(chrono::Duration::minutes(i64::from(args.timeout_min)), || {
let _guard = timer.schedule_with_delay(
chrono::Duration::minutes(i64::from(args.timeout_min)),
|| {
write_to_user("Measurement took too long time, aborting...");
std::process::exit(1);
});
},
);

let tracer = get_tracer(args.debug_filename);

Expand All @@ -442,14 +456,15 @@ fn main() {
otii.wait_for_gpi2(value);
}
// Flush some measurement.
otii.get_average_current(500 + args.wait_to_start_sec * 1000);
otii.get_average_current(500 + args.wait_to_start_sec * 1000, true);

write_to_user(&format!(
"Starting measurement for {} seconds",
args.measurement_time_sec
));
let start_measurement_time = std::time::Instant::now();
let avg = otii.get_average_current(args.measurement_time_sec * SAMPLES_PER_SECOND) / mc_gain as f64;
let avg = otii.get_average_current(args.measurement_time_sec * SAMPLES_PER_SECOND, args.quiet)
/ mc_gain as f64;
write_to_user(&format!(
"Done. Measurement took {} seconds",
start_measurement_time.elapsed().as_secs()
Expand Down

0 comments on commit 4a6e78f

Please sign in to comment.