From efbe1a54fdb2487e7ab1450738b2b954106f063f Mon Sep 17 00:00:00 2001 From: Nareshkumar Rao Date: Fri, 26 Apr 2024 21:10:00 +0200 Subject: [PATCH] wip --- growpi.toml | 21 +++++++++++++++++++++ src/cli_mode.rs | 5 ++++- src/config.rs | 29 +++++++++++++++++++---------- src/io.rs | 6 +++++- 4 files changed, 49 insertions(+), 12 deletions(-) create mode 100644 growpi.toml diff --git a/growpi.toml b/growpi.toml new file mode 100644 index 0000000..1c267ba --- /dev/null +++ b/growpi.toml @@ -0,0 +1,21 @@ +[board_settings] +logic_level = 3.3 + +[relay_settings] +light_pin = 0 +fan_pin = 1 +water_pump_pin = 2 +relay_gpio_pins = [17, 27, 22, -1] + +[soil_moisture_settings] +pin = 1 +voltage_100 = 1.417 +voltage_nominal = 2.823 +moisture_nominal = 0.41 + +[thermistor_settings] +pin = 0 +voltage_divider_resistance = 9700.0 +nominal_resistance = 10000.0 +nominal_temperature = 298.15 +thermal_constant = 3950.0 diff --git a/src/cli_mode.rs b/src/cli_mode.rs index 3bb9d73..47255c6 100644 --- a/src/cli_mode.rs +++ b/src/cli_mode.rs @@ -160,7 +160,10 @@ fn init_state(config: &Configuration) -> GenericResult { pub fn run_cli() { let mut rl = init_readline().unwrap(); - let config = Configuration::default(); + + let config = + Configuration::from_file(std::path::Path::new("./growpi.toml")).unwrap_or_default(); + let mut program_state = init_state(&config).unwrap(); 'cli_loop: loop { diff --git a/src/config.rs b/src/config.rs index 9821b54..f5853f3 100644 --- a/src/config.rs +++ b/src/config.rs @@ -7,7 +7,7 @@ pub struct RelaySettings { pub light_pin: u8, pub fan_pin: u8, pub water_pump_pin: u8, - pub relay_gpio_pins: Vec>, + pub relay_gpio_pins: Vec, } #[derive(Serialize, Deserialize)] @@ -41,22 +41,18 @@ pub struct Configuration { } impl Configuration { - fn from_file(path: &std::path::Path) -> GenericResult { + pub fn from_file(path: &std::path::Path) -> GenericResult { let text = std::fs::read_to_string(path)?; - let config: Configuration = toml::from_str(text.as_str())?; + let config = toml::from_str(text.as_str())?; Ok(config) } - fn save_to_file(path: &std::path::Path, config: &Configuration) -> GenericResult<()> { - let text = toml::to_string_pretty(config)?; + pub fn save_to_file(&self, path: &std::path::Path) -> GenericResult<()> { + let text = toml::to_string_pretty(self)?; std::fs::write(path, text)?; Ok(()) } } -const THERMISTOR_NOMINAL_RESISTANCE: f32 = 10_000.; -const THERMISTOR_NOMINAL_TEMPERATURE: f32 = 298.15; -const THERMISTOR_CONSTANT: f32 = 3950.; - impl Default for Configuration { fn default() -> Self { Self { @@ -65,7 +61,7 @@ impl Default for Configuration { light_pin: 0, fan_pin: 1, water_pump_pin: 2, - relay_gpio_pins: [Some(17), Some(27), Some(22), None].to_vec(), + relay_gpio_pins: [17, 27, 22, -1].to_vec(), }, soil_moisture_settings: SoilMoistureSettings { pin: 1, @@ -83,3 +79,16 @@ impl Default for Configuration { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_write_default() { + let config = Configuration::default(); + config + .save_to_file(std::path::Path::new("./growpi.toml")) + .unwrap(); + } +} diff --git a/src/io.rs b/src/io.rs index 73858af..482ec3e 100644 --- a/src/io.rs +++ b/src/io.rs @@ -39,7 +39,11 @@ impl Relay { .clone() .into_iter() .map(|pin| { - pin.and_then(|pin| { + match pin { + -1 => None, + _ => Some(pin as u8), + } + .and_then(|pin| { let result = (|| -> GenericResult { Ok(Gpio::new()?.get(pin)?.into_output()) })();