|
|
@ -1,8 +1,11 @@ |
|
|
|
use std::{thread, time::Duration}; |
|
|
|
|
|
|
|
use rustyline::{config::Configurer, error::ReadlineError, history::FileHistory}; |
|
|
|
|
|
|
|
use crate::{ |
|
|
|
error::GenericResult, |
|
|
|
io::{self, get_input_voltage}, |
|
|
|
sensors, |
|
|
|
}; |
|
|
|
|
|
|
|
struct LoopFlags { |
|
|
@ -15,6 +18,8 @@ fn process_input(input: String, program_state: &mut ProgramState) -> GenericResu |
|
|
|
match main_command { |
|
|
|
"ana" => command_ana(&args)?, |
|
|
|
"rel" => command_rel(&args, program_state)?, |
|
|
|
"soil" => command_soil(&args)?, |
|
|
|
"temp" => command_temp(&args)?, |
|
|
|
"exit" => return Ok(LoopFlags { exit: true }), |
|
|
|
_ => return Err("Unknown main command".into()), |
|
|
|
}; |
|
|
@ -22,6 +27,40 @@ fn process_input(input: String, program_state: &mut ProgramState) -> GenericResu |
|
|
|
Ok(LoopFlags { exit: false }) |
|
|
|
} |
|
|
|
|
|
|
|
fn command_temp(args: &[&str]) -> GenericResult<()> { |
|
|
|
let show_loop = args |
|
|
|
.get(1) |
|
|
|
.map(|arg| matches!(*arg, "loop")) |
|
|
|
.unwrap_or(false); |
|
|
|
loop { |
|
|
|
let temperature = sensors::get_temperature()?; |
|
|
|
println!("Temperature: {}C", temperature); |
|
|
|
if show_loop { |
|
|
|
break; |
|
|
|
} |
|
|
|
thread::sleep(Duration::from_secs(1)); |
|
|
|
} |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
|
|
|
|
fn command_soil(args: &[&str]) -> GenericResult<()> { |
|
|
|
let show_loop = args |
|
|
|
.get(1) |
|
|
|
.map(|arg| matches!(*arg, "loop")) |
|
|
|
.unwrap_or(false); |
|
|
|
|
|
|
|
loop { |
|
|
|
let humidity = sensors::get_soil_moisture()?; |
|
|
|
println!("Soil humidity: {}", humidity); |
|
|
|
if show_loop { |
|
|
|
break; |
|
|
|
} |
|
|
|
thread::sleep(Duration::from_secs(1)); |
|
|
|
} |
|
|
|
|
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
|
|
|
|
fn command_rel(args: &[&str], program_state: &mut ProgramState) -> GenericResult<()> { |
|
|
|
let pin = args |
|
|
|
.get(1) |
|
|
@ -60,8 +99,19 @@ fn command_ana(args: &[&str]) -> GenericResult<()> { |
|
|
|
.parse::<u8>() |
|
|
|
.map_err(|_| "Not a valid pin number")?; |
|
|
|
|
|
|
|
let show_loop = args |
|
|
|
.get(2) |
|
|
|
.map(|arg| matches!(*arg, "loop")) |
|
|
|
.unwrap_or(false); |
|
|
|
|
|
|
|
loop { |
|
|
|
let voltage = get_input_voltage(pin)?; |
|
|
|
println!("Voltage read: {}", voltage); |
|
|
|
if show_loop { |
|
|
|
break; |
|
|
|
} |
|
|
|
thread::sleep(Duration::from_secs(1)); |
|
|
|
} |
|
|
|
|
|
|
|
Ok(()) |
|
|
|
} |
|
|
|