From 917f3dac863b5556af0511bf286222114a0d67ba Mon Sep 17 00:00:00 2001 From: Nareshkumar Rao Date: Wed, 24 Apr 2024 22:22:22 +0200 Subject: [PATCH] wip --- src/actuators.rs | 2 +- src/cli_mode.rs | 104 ++++++++++++++++++++++++++++++++++++++ src/error.rs | 2 +- src/io.rs | 2 +- src/main.rs | 127 +++-------------------------------------------- src/sensors.rs | 2 +- 6 files changed, 116 insertions(+), 123 deletions(-) create mode 100644 src/cli_mode.rs diff --git a/src/actuators.rs b/src/actuators.rs index e10b90e..7361183 100644 --- a/src/actuators.rs +++ b/src/actuators.rs @@ -1,7 +1,7 @@ use crate::{ config::*, + error::GenericResult, io::{Relay, RelaySwitchState}, - GenericResult, }; pub fn switch_lights(relay: &mut Relay, state: RelaySwitchState) -> GenericResult<()> { diff --git a/src/cli_mode.rs b/src/cli_mode.rs new file mode 100644 index 0000000..6df3c97 --- /dev/null +++ b/src/cli_mode.rs @@ -0,0 +1,104 @@ +use crate::error::GenericResult; + +struct LoopFlags { + exit: bool, +} + +fn process_input(input: String) -> GenericResult { + todo!() +} + +fn cli_loop() -> GenericResult { + print!("Enter command: "); + let mut input_string = String::new(); + std::io::stdin().read_line(&mut input_string)?; + process_input(input_string) +} + +pub fn run_cli() { + 'cli_loop: loop { + match cli_loop() { + Ok(loop_flags) => { + if loop_flags.exit { + println!("Leaving CLI"); + break 'cli_loop; + } + } + Err(e) => println!("{}", e), + } + } +} + +// fn process_command(command: String, program_state: &mut ProgramState) -> GenericResult { +// let command = command +// .strip_suffix('\n') +// .ok_or("Couldn't strip whitespace")?; +// let args = command.split(' ').collect::>(); + +// let main_command = *args.first().ok_or("No command found")?; +// match main_command { +// "water" => toggle_water(&args, program_state)?, +// "fan" => toggle_fan(program_state)?, +// "light" => toggle_light(program_state)?, +// "ana" => analogue_command(&args, program_state)?, +// "exit" => return Ok(true), +// _ => return Err(format!("Main command '{}' invalid", main_command).into()), +// } + +// Ok(false) +// } + +// fn analogue_command(args: &[&str], program_state: &mut ProgramState) -> GenericResult<()> { +// let pin = args.get(1).ok_or("No pin given")?; +// let pin: u8 = pin.parse()?; + +// let adc_target = rppal::i2c::I2c::new()?; +// let mut adc_target = +// Ads1x1x::new_ads1115(adc_target, ads1x1x::SlaveAddr::Alternative(false, false)); +// let channel: ChannelSelection = match pin { +// 0 => ChannelSelection::SingleA0, +// 1 => ChannelSelection::SingleA1, +// 2 => ChannelSelection::SingleA2, +// 3 => ChannelSelection::SingleA3, +// _ => ChannelSelection::SingleA0, +// }; +// loop { +// let value = block!(adc_target.read(channel)).map_err(|e| format!("{:?}", e))?; +// let value = value as f32; +// let voltage = value / i16::MAX as f32 * 2.048; +// let resistance = (3.3 / voltage - 1.) * 9_700.; +// println!("Value: {}", value); +// println!("resistance: {}", resistance); +// let temp = 1. / ((1. / 298.15) + (1. / 3950. * f32::ln(resistance / 10_000.))) - 273.15; +// println!("Temp: {}", temp); +// //println!("Value of pin {} is: {}", pin, value); +// thread::sleep(Duration::from_millis(2000)); +// } +// Ok(()) +// } + +// fn toggle_water(args: &[&str], program_state: &mut ProgramState) -> GenericResult<()> { +// if let Some(duration) = args.get(1) { +// let duration: u64 = duration.parse()?; +// println!("Turning on water for {} milliseconds", duration); +// program_state.water_pin.set_low(); +// thread::sleep(Duration::from_millis(duration)); +// program_state.water_pin.set_high(); +// } else { +// program_state.water_pin.toggle(); +// println!("Toggling water output"); +// } +// Ok(()) +// } + +// fn toggle_light(program_state: &mut ProgramState) -> GenericResult<()> { +// program_state.light_pin.toggle(); +// println!("Toggling light output"); +// Ok(()) +// } + +// fn toggle_fan(program_state: &mut ProgramState) -> GenericResult<()> { +// program_state.fan_pin.toggle(); +// println!("Toggling fan output"); +// Ok(()) +// } diff --git a/src/error.rs b/src/error.rs index 7f7bc38..9fc7342 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,3 +1,3 @@ use std::error::Error; -type GenericResult = Result>; +pub type GenericResult = Result>; diff --git a/src/io.rs b/src/io.rs index b0da009..7f93adc 100644 --- a/src/io.rs +++ b/src/io.rs @@ -2,7 +2,7 @@ use ads1x1x::{Ads1x1x, ChannelSelection, DynamicOneShot}; use nb::block; use rppal::gpio::{Gpio, OutputPin}; -use crate::{config::*, GenericResult}; +use crate::{config::*, error::GenericResult}; pub fn get_input_voltage(pin: u8) -> GenericResult { const ADS1115_DEFAULT_RANGE: f32 = 2.048; diff --git a/src/main.rs b/src/main.rs index 3d2affe..49736fb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,131 +1,20 @@ #![allow(dead_code)] -use std::{error::Error, thread, time::Duration}; -use ads1x1x::{Ads1x1x, ChannelSelection, DynamicOneShot}; -use nb::block; -use rppal::gpio::{Gpio, OutputPin}; +use cli_mode::run_cli; mod actuators; +mod cli_mode; mod config; mod error; mod io; mod sensors; -type GenericResult = Result>; - -struct ProgramState { - water_pin: OutputPin, - light_pin: OutputPin, - fan_pin: OutputPin, -} - -fn setup_program_state() -> GenericResult { - let mut water_pin = Gpio::new()?.get(22)?.into_output(); - let mut light_pin = Gpio::new()?.get(17)?.into_output(); - let mut fan_pin = Gpio::new()?.get(27)?.into_output(); - - water_pin.set_high(); - light_pin.set_high(); - fan_pin.set_high(); - - Ok(ProgramState { - water_pin, - light_pin, - fan_pin, - }) -} - -fn main() -> GenericResult<()> { - let mut program_state: ProgramState = setup_program_state()?; - - loop { - println!("Enter command: "); - let mut input_string = String::new(); - std::io::stdin().read_line(&mut input_string)?; - - match process_command(input_string, &mut program_state) { - Ok(to_exit) => { - if to_exit { - break; - } - } - Err(err) => println!("Error: {}", err), +fn main() { + let args = std::env::args().collect::>(); + if let Some(mode) = args.first().map(|x| x.as_str()) { + match mode { + "cli" => run_cli(), + _ => run_cli(), } } - - Ok(()) -} - -fn process_command(command: String, program_state: &mut ProgramState) -> GenericResult { - let command = command - .strip_suffix('\n') - .ok_or("Couldn't strip whitespace")?; - let args = command.split(' ').collect::>(); - - let main_command = *args.first().ok_or("No command found")?; - match main_command { - "water" => toggle_water(&args, program_state)?, - "fan" => toggle_fan(program_state)?, - "light" => toggle_light(program_state)?, - "ana" => analogue_command(&args, program_state)?, - "exit" => return Ok(true), - _ => return Err(format!("Main command '{}' invalid", main_command).into()), - } - - Ok(false) -} - -fn analogue_command(args: &[&str], program_state: &mut ProgramState) -> GenericResult<()> { - let pin = args.get(1).ok_or("No pin given")?; - let pin: u8 = pin.parse()?; - - let adc_target = rppal::i2c::I2c::new()?; - let mut adc_target = - Ads1x1x::new_ads1115(adc_target, ads1x1x::SlaveAddr::Alternative(false, false)); - let channel: ChannelSelection = match pin { - 0 => ChannelSelection::SingleA0, - 1 => ChannelSelection::SingleA1, - 2 => ChannelSelection::SingleA2, - 3 => ChannelSelection::SingleA3, - _ => ChannelSelection::SingleA0, - }; - loop { - let value = block!(adc_target.read(channel)).map_err(|e| format!("{:?}", e))?; - let value = value as f32; - let voltage = value / i16::MAX as f32 * 2.048; - let resistance = (3.3 / voltage - 1.) * 9_700.; - println!("Value: {}", value); - println!("resistance: {}", resistance); - let temp = 1. / ((1. / 298.15) + (1. / 3950. * f32::ln(resistance / 10_000.))) - 273.15; - println!("Temp: {}", temp); - //println!("Value of pin {} is: {}", pin, value); - thread::sleep(Duration::from_millis(2000)); - } - Ok(()) -} - -fn toggle_water(args: &[&str], program_state: &mut ProgramState) -> GenericResult<()> { - if let Some(duration) = args.get(1) { - let duration: u64 = duration.parse()?; - println!("Turning on water for {} milliseconds", duration); - program_state.water_pin.set_low(); - thread::sleep(Duration::from_millis(duration)); - program_state.water_pin.set_high(); - } else { - program_state.water_pin.toggle(); - println!("Toggling water output"); - } - Ok(()) -} - -fn toggle_light(program_state: &mut ProgramState) -> GenericResult<()> { - program_state.light_pin.toggle(); - println!("Toggling light output"); - Ok(()) -} - -fn toggle_fan(program_state: &mut ProgramState) -> GenericResult<()> { - program_state.fan_pin.toggle(); - println!("Toggling fan output"); - Ok(()) } diff --git a/src/sensors.rs b/src/sensors.rs index 3ed6a8f..608f65b 100644 --- a/src/sensors.rs +++ b/src/sensors.rs @@ -1,4 +1,4 @@ -use crate::{config::*, io::get_input_voltage, GenericResult}; +use crate::{config::*, error::GenericResult, io::get_input_voltage}; pub fn get_temperature() -> GenericResult { const THERMISTOR_NOMINAL_RESISTANCE: f32 = 10_000.;