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