mirror of
https://codeberg.org/icewind/hlk_ld6002.git
synced 2026-06-03 16:44:10 +02:00
async
This commit is contained in:
parent
fad21024fb
commit
6f80d51daf
6 changed files with 599 additions and 13 deletions
35
examples/async.rs
Normal file
35
examples/async.rs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
use embedded_io_adapters::tokio_1::FromTokio;
|
||||
use hlk_ld6002::{AsyncMessageStream, Data};
|
||||
use serialport::SerialPort;
|
||||
use std::env::args;
|
||||
use std::time::{Duration, Instant};
|
||||
use tokio_serial::{ClearBuffer, SerialPortBuilderExt};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let port = args().nth(1).expect("no port provided");
|
||||
let port = tokio_serial::new(&port, 1_382_400)
|
||||
.timeout(Duration::from_millis(50))
|
||||
.open_native_async()
|
||||
.expect("Failed to open port");
|
||||
port.clear(ClearBuffer::All).unwrap();
|
||||
|
||||
let mut messages = AsyncMessageStream::new(FromTokio::new(port));
|
||||
|
||||
let mut data = Data::default();
|
||||
|
||||
let mut last = Instant::now();
|
||||
|
||||
print!("{}", termion::cursor::Save);
|
||||
|
||||
loop {
|
||||
let message = messages.next().await;
|
||||
if let Ok(message) = message {
|
||||
data.update(message);
|
||||
if last.elapsed() > Duration::from_millis(100) {
|
||||
last = Instant::now();
|
||||
print!("{:?}{}", data, termion::cursor::Restore);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue