-
Notifications
You must be signed in to change notification settings - Fork 37
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 (#281)
- Loading branch information
1 parent
faf212e
commit b05de65
Showing
7 changed files
with
71 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,43 @@ | ||
use clap::Parser; | ||
use color_eyre::{eyre::WrapErr as _, Result}; | ||
use humantime::parse_duration; | ||
use std::time::Duration; | ||
use tracing::info; | ||
|
||
use crate::boot::BUTTON_PIN; | ||
use crate::ftdi::{FtdiGpio, OutputState}; | ||
|
||
#[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