-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
161 additions
and
117 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,76 @@ | ||
use std::sync::{ | ||
atomic::{AtomicBool, Ordering}, | ||
Arc, | ||
}; | ||
|
||
use ratatui::widgets::TableState; | ||
use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender}; | ||
|
||
use crate::{ | ||
scan::{bluetooth_scan, get_characteristics}, | ||
structs::{Characteristic, DeviceInfo}, | ||
}; | ||
|
||
pub enum DeviceData { | ||
DeviceInfo(DeviceInfo), | ||
#[allow(dead_code)] | ||
Characteristics(Vec<Characteristic>), | ||
Error(String), | ||
} | ||
|
||
pub struct App { | ||
pub rx: UnboundedReceiver<DeviceData>, | ||
pub tx: UnboundedSender<DeviceData>, | ||
pub loading_status: Arc<AtomicBool>, | ||
pub pause_status: Arc<AtomicBool>, | ||
pub table_state: TableState, | ||
pub devices: Vec<DeviceInfo>, | ||
pub inspect_view: bool, | ||
pub inspect_overlay_scroll: usize, | ||
pub selected_characteristics: Vec<Characteristic>, | ||
pub frame_count: usize, | ||
pub is_loading: bool, | ||
pub error_view: bool, | ||
pub error_message: String, | ||
} | ||
|
||
impl App { | ||
pub fn new() -> Self { | ||
let (tx, rx) = mpsc::unbounded_channel(); | ||
Self { | ||
tx, | ||
rx, | ||
loading_status: Arc::new(AtomicBool::default()), | ||
pause_status: Arc::new(AtomicBool::default()), | ||
table_state: TableState::default(), | ||
devices: Vec::new(), | ||
inspect_view: false, | ||
inspect_overlay_scroll: 0, | ||
selected_characteristics: Vec::new(), | ||
frame_count: 0, | ||
is_loading: false, | ||
error_view: false, | ||
error_message: String::new(), | ||
} | ||
} | ||
|
||
pub async fn scan(&mut self) { | ||
let pause_signal_clone = Arc::clone(&self.pause_status); | ||
let tx_clone = self.tx.clone(); | ||
tokio::spawn(async move { bluetooth_scan(tx_clone, pause_signal_clone).await }); | ||
} | ||
|
||
pub async fn connect(&mut self) { | ||
let selected_device = self | ||
.devices | ||
.get(self.table_state.selected().unwrap_or(0)) | ||
.unwrap(); | ||
|
||
self.pause_status.store(true, Ordering::SeqCst); | ||
|
||
let device = Arc::new(selected_device.clone()); | ||
let tx_clone = self.tx.clone(); | ||
|
||
tokio::spawn(async move { get_characteristics(tx_clone, device).await }); | ||
} | ||
} |
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
Oops, something went wrong.