From dc30979dad4f344c70c4559e210614c4b77b53bd Mon Sep 17 00:00:00 2001 From: hodasemi Date: Wed, 20 Jul 2022 08:52:29 +0200 Subject: [PATCH] Add command pause time --- src/service.rs | 35 ++++++++++++++++++++++++++++++----- src/shared/config.rs | 4 ++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/service.rs b/src/service.rs index 9c52772..68a4ae6 100644 --- a/src/service.rs +++ b/src/service.rs @@ -1,13 +1,14 @@ mod service_specific; mod shared; -use std::process::Command; +use std::{process::Command, time::Instant}; use anyhow::Result; use service_specific::*; -use shared::config::Config; +use shared::config::{Config, COMMAND_COUNT, COMMAND_PAUSE}; fn main() -> Result<()> { + // create serial port let mut port = Port::open(SerialPortSettings { baud_rate: 57600, data_bits: DataBits::Eight, @@ -18,18 +19,26 @@ fn main() -> Result<()> { })?; let mut message_builder = MessageBuilder::default(); + let mut button_infos = [Duration::default(); COMMAND_COUNT]; + let start_time = Instant::now(); + // loop forever loop { + // check if port could be read if let Ok(r) = port.read() { + // handle incoming message match r { SerialReadResult::Message(msg) => { + // create message out of stream + // discard remaining for now if let Some(_remaining) = message_builder.check(msg) { debug_assert!(message_builder.is_complete()); println!("{}", message_builder.message()); + // if message can be parsed to a number, try to execute a command if let Ok(index) = message_builder.message().parse() { - execute_button_press(index)?; + execute_button_press(index, start_time.elapsed(), &mut button_infos)?; } message_builder = MessageBuilder::default(); @@ -41,13 +50,29 @@ fn main() -> Result<()> { } } -fn execute_button_press(button_id: usize) -> Result<()> { +fn execute_button_press( + button_id: usize, + time: Duration, + button_infos: &mut [Duration; COMMAND_COUNT], +) -> Result<()> { + // load config every button + // since it could be modified from the gui application let config = Config::load_config()?; - if button_id < 1 && button_id > 8 { + // check if ID is in a valid range + if button_id < 1 && button_id > COMMAND_COUNT { return Ok(()); } + // check that this commands last execution is longer than COMMAND_PAUSE ago + if (button_infos[button_id] + COMMAND_PAUSE) < time { + button_infos[button_id] = time; + } else { + return Ok(()); + } + + // check command at ID position if a command is set + // if so, simply execute this command as child process if let Some(cmd) = &config.commands[button_id - 1] { if !cmd.is_empty() { println!("cmd: {}", cmd); diff --git a/src/shared/config.rs b/src/shared/config.rs index 3f18c9f..3717fae 100644 --- a/src/shared/config.rs +++ b/src/shared/config.rs @@ -5,9 +5,12 @@ use std::{ fs::{self, File}, io::Write, path::Path, + time::Duration, }; pub const COMMAND_COUNT: usize = 8; +#[allow(unused)] +pub const COMMAND_PAUSE: Duration = Duration::from_millis(1500); #[derive(Debug, Serialize, Deserialize, Default)] pub struct Config { @@ -37,6 +40,7 @@ impl Config { } } + #[allow(unused)] pub fn save(&self, path: &str) -> Result<()> { let mut file = File::create(path)?; let s = serde_json::to_string(self)?;