-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hil): Add button control cmd and timeout for cmd
- Loading branch information
1 parent
a53f4cc
commit afa8a24
Showing
6 changed files
with
102 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use clap::Parser; | ||
use color_eyre::{eyre::WrapErr as _, Result}; | ||
use std::time::Duration; | ||
use tracing::info; | ||
|
||
use crate::boot::BUTTON_PIN; | ||
use crate::ftdi::{FtdiGpio, OutputState}; | ||
use crate::utils::parse_duration; | ||
|
||
#[derive(Debug, Parser)] | ||
pub struct ButtonCtrl { | ||
///Button press duration (e.g., "1s", "500ms") | ||
#[arg(long, default_value = "1s", value_parser = parse_duration)] | ||
press_duration: Duration, | ||
} | ||
|
||
impl ButtonCtrl { | ||
pub async fn run(self) -> Result<()> { | ||
fn make_ftdi() -> Result<FtdiGpio> { | ||
FtdiGpio::builder() | ||
.with_default_device() | ||
.and_then(|b| b.configure()) | ||
.wrap_err("failed to create ftdi device") | ||
} | ||
|
||
info!( | ||
"Holding button for {} seconds", | ||
self.press_duration.as_secs_f32() | ||
); | ||
tokio::task::spawn_blocking(move || -> Result<_, color_eyre::Report> { | ||
let mut ftdi = make_ftdi()?; | ||
ftdi.set_pin(BUTTON_PIN, OutputState::Low)?; | ||
std::thread::sleep(self.press_duration); | ||
ftdi.set_pin(BUTTON_PIN, OutputState::High)?; | ||
Ok(ftdi) | ||
}) | ||
.await | ||
.wrap_err("task panicked")??; | ||
info!("Button released"); | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use color_eyre::Result; | ||
use std::time::Duration; | ||
|
||
/// Custom duration parser to allow flexible time formats | ||
pub fn parse_duration(s: &str) -> Result<Duration> { | ||
let s = s.trim(); | ||
if s.ends_with("ms") { | ||
let value: u64 = s | ||
.trim_end_matches("ms") | ||
.parse() | ||
.map_err(|_| color_eyre::eyre::eyre!("Invalid duration format: {}", s))?; | ||
Ok(Duration::from_millis(value)) | ||
} else if s.ends_with('s') { | ||
let value: u64 = s | ||
.trim_end_matches('s') | ||
.parse() | ||
.map_err(|_| color_eyre::eyre::eyre!("Invalid duration format: {}", s))?; | ||
Ok(Duration::from_secs(value)) | ||
} else if s.ends_with('m') { | ||
let value: u64 = s | ||
.trim_end_matches('m') | ||
.parse() | ||
.map_err(|_| color_eyre::eyre::eyre!("Invalid duration format: {}", s))?; | ||
Ok(Duration::from_secs(value * 60)) | ||
} else { | ||
// Default to seconds if no unit is provided | ||
let value: u64 = s | ||
.parse() | ||
.map_err(|_| color_eyre::eyre::eyre!("Invalid duration format: {}", s))?; | ||
Ok(Duration::from_secs(value)) | ||
} | ||
} |