//! Calculator for hardware debouncing using an RC filter as described in
//! https://my.eng.utah.edu/~cs5780/debouncing.pdf.

use std::io::{self, Write};

fn main() {
    let supply = read_float("supply voltage [V]:");
    let high = read_float("high input threshold voltage [V]:");
    let low = read_float("low input threshold voltage [V]:");
    let leakage = read_float("max. leakage [uA]:") * 0.000001;
    let time = read_float("debounce time [ms]:") * 0.001;

    let c = 0.0000001; // 0.1uF
    let r_pullup = -time / (c * ((supply - low) / supply).ln());
    println!("pullup: {} Ohm", r_pullup);
    let r_pulldown = -time / (c * (high / supply).ln());
    println!("pulldown: {} Ohm", r_pulldown);
}

fn read_float(label: &str) -> f64 {
    println!("{}", label);
    io::stdout().flush().unwrap();

    let mut input = String::new();
    match io::stdin().read_line(&mut input) {
        Ok(_) => input.trim().parse().unwrap(),
        Err(error) => panic!("invalid input: {}", error),
    }
}
