Spawn a thread and send stdin data over a channel
This commit is contained in:
parent
207f2c8590
commit
054235b031
25
src/ble.rs
25
src/ble.rs
|
@ -1,5 +1,6 @@
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::str;
|
use std::str;
|
||||||
|
use std::sync::mpsc::Receiver;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use btleplug::api::{
|
use btleplug::api::{
|
||||||
|
@ -7,12 +8,11 @@ use btleplug::api::{
|
||||||
};
|
};
|
||||||
use btleplug::platform::{Adapter, Manager, Peripheral};
|
use btleplug::platform::{Adapter, Manager, Peripheral};
|
||||||
use console::Term;
|
use console::Term;
|
||||||
use dialoguer::{theme::ColorfulTheme, Input};
|
|
||||||
use futures::stream::StreamExt;
|
use futures::stream::StreamExt;
|
||||||
use tokio::time;
|
use tokio::time;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::utils::progress_bar;
|
use crate::utils::{get_stdin_line_channel, progress_bar};
|
||||||
|
|
||||||
const NORDIC_UART_SERVICE_UUID: Uuid = Uuid::from_u128(0x6e400001_b5a3_f393_e0a9_e50e24dcca9e);
|
const NORDIC_UART_SERVICE_UUID: Uuid = Uuid::from_u128(0x6e400001_b5a3_f393_e0a9_e50e24dcca9e);
|
||||||
const NORDIC_UART_TX_CHAR_UUID: Uuid = Uuid::from_u128(0x6e400002_b5a3_f393_e0a9_e50e24dcca9e);
|
const NORDIC_UART_TX_CHAR_UUID: Uuid = Uuid::from_u128(0x6e400002_b5a3_f393_e0a9_e50e24dcca9e);
|
||||||
|
@ -173,7 +173,11 @@ pub async fn repl(adapter_name: String, address: String) -> Result<(), Box<dyn E
|
||||||
device.connect().await?;
|
device.connect().await?;
|
||||||
if device.is_connected().await? {
|
if device.is_connected().await? {
|
||||||
device.discover_services().await?;
|
device.discover_services().await?;
|
||||||
tokio::spawn(get_input(device.clone(), term.clone()));
|
|
||||||
|
let line_channel = get_stdin_line_channel();
|
||||||
|
tokio::spawn(write_ble(device.clone(), line_channel));
|
||||||
|
|
||||||
|
// Receive data from the Bluetooth LE device.
|
||||||
let chars = device.characteristics();
|
let chars = device.characteristics();
|
||||||
let rx_char = chars
|
let rx_char = chars
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -186,12 +190,14 @@ pub async fn repl(adapter_name: String, address: String) -> Result<(), Box<dyn E
|
||||||
term.write_line(text.trim_end())?;
|
term.write_line(text.trim_end())?;
|
||||||
term.flush()?;
|
term.flush()?;
|
||||||
}
|
}
|
||||||
|
|
||||||
device.disconnect().await?;
|
device.disconnect().await?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_input(device: Peripheral, t: Term) {
|
/// Send data to Bluetooth LE device.
|
||||||
|
async fn write_ble(device: Peripheral, text_channel: Receiver<String>) {
|
||||||
let chars = device.characteristics();
|
let chars = device.characteristics();
|
||||||
let tx_char = chars
|
let tx_char = chars
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -199,12 +205,13 @@ async fn get_input(device: Peripheral, t: Term) {
|
||||||
.ok_or("Unable to find TX characteric")
|
.ok_or("Unable to find TX characteric")
|
||||||
.unwrap();
|
.unwrap();
|
||||||
loop {
|
loop {
|
||||||
let text: String = Input::with_theme(&ColorfulTheme::default())
|
let mut words = String::new();
|
||||||
.with_prompt("Φ]")
|
if let Ok(text) = text_channel.try_recv() {
|
||||||
.interact_on(&t)
|
words = text;
|
||||||
.unwrap();
|
}
|
||||||
|
|
||||||
device
|
device
|
||||||
.write(&tx_char, text.as_bytes(), WriteType::WithoutResponse)
|
.write(&tx_char, words.as_bytes(), WriteType::WithoutResponse)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
time::sleep(Duration::from_millis(100)).await;
|
time::sleep(Duration::from_millis(100)).await;
|
||||||
|
|
14
src/utils.rs
14
src/utils.rs
|
@ -1,3 +1,6 @@
|
||||||
|
use std::io;
|
||||||
|
use std::sync::mpsc;
|
||||||
|
use std::sync::mpsc::Receiver;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
@ -33,3 +36,14 @@ pub fn progress_bar(scan_time: Duration) {
|
||||||
rt.block_on(future);
|
rt.block_on(future);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Create a new thread that sends stdin data over a channel.
|
||||||
|
pub fn get_stdin_line_channel() -> Receiver<String> {
|
||||||
|
let (tx, rx) = mpsc::channel::<String>();
|
||||||
|
thread::spawn(move || loop {
|
||||||
|
let mut line = String::new();
|
||||||
|
io::stdin().read_line(&mut line).unwrap();
|
||||||
|
tx.send(line).unwrap();
|
||||||
|
});
|
||||||
|
rx
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue