thermalcam_decoder/examples/cutoff.rs

112 lines
3.6 KiB
Rust
Raw Normal View History

2024-02-23 01:38:32 +02:00
use std::{collections::HashMap, io::stdout, time::Duration};
use futures::{future::FutureExt, select, StreamExt};
use futures_timer::Delay;
use crossterm::{
cursor::position,
2024-02-23 02:10:49 +02:00
event::{DisableMouseCapture, EnableMouseCapture, Event, EventStream, KeyCode, KeyEvent},
2024-02-23 01:38:32 +02:00
execute,
terminal::{disable_raw_mode, enable_raw_mode},
};
2024-02-23 02:10:49 +02:00
async fn set_cutoff(cutoff: f64, freq: f64) -> Result<(), reqwest::Error> {
2024-02-23 01:38:32 +02:00
// Some simple CLI args requirements...
let url = format!("http://localhost:3000/cutoff");
let mut map = HashMap::new();
2024-02-23 02:10:49 +02:00
map.insert("min_cutoff", cutoff);
map.insert("max_cutoff", cutoff + 10.0);
map.insert("freq_hz", freq);
2024-02-23 01:38:32 +02:00
let client = reqwest::Client::new();
let res = client.post(url).json(&map).send().await?;
// eprintln!("Response: {:?} {}", res.version(), res.status());
// eprintln!("Headers: {:#?}\n", res.headers());
// let body = res.text().await?;
// println!("{body}");
Ok(())
}
const HELP: &str = r#"EventStream based on futures_util::Stream with tokio
- Keyboard, mouse and terminal resize events enabled
- Prints "." every second if there's no event
- Hit "c" to print current cursor position
- Use Esc to quit
"#;
async fn print_events() {
let mut reader = EventStream::new();
let mut cutoff = 30.0;
let mut last_cutoff = cutoff;
2024-02-23 02:10:49 +02:00
let mut freq = 1.0;
2024-02-23 01:38:32 +02:00
loop {
let mut delay = Delay::new(Duration::from_millis(1_000)).fuse();
let mut event = reader.next().fuse();
2024-02-23 02:10:49 +02:00
let mut change = false;
2024-02-23 01:38:32 +02:00
select! {
_ = delay => {
},
maybe_event = event => {
match maybe_event {
Some(Ok(event)) => {
if event == Event::Key(KeyCode::Char('c').into()) {
println!("Cursor position: {:?}\r", position());
}
if event == Event::Key(KeyCode::Esc.into()) {
break;
}
2024-02-23 02:10:49 +02:00
if let Event::Key(k) = event {
if let KeyCode::Char(c) = k.code {
change = true;
match c {
'[' => {
cutoff -= 1.0;
}
']' => {
cutoff += 1.0;
}
'1' => {
freq *= 0.9;
}
'2' => {
freq *= 1.1;
}
_ => {
change = false;
}
}
}
2024-02-23 01:38:32 +02:00
}
2024-02-23 02:10:49 +02:00
if change {
set_cutoff(cutoff, freq).await.unwrap();
2024-02-23 01:38:32 +02:00
println!("cutoff = {}\r", cutoff);
}
}
Some(Err(e)) => println!("Error: {:?}\r", e),
None => break,
}
}
};
}
}
#[tokio::main]
async fn main() -> std::io::Result<()> {
println!("{}", HELP);
enable_raw_mode()?;
let mut stdout = stdout();
execute!(stdout, EnableMouseCapture)?;
print_events().await;
execute!(stdout, DisableMouseCapture)?;
disable_raw_mode()
}