Poll for position

This commit is contained in:
Frederik Menke 2022-10-16 17:09:30 +02:00
parent d944bac12f
commit c937432d25
2 changed files with 24 additions and 8 deletions

View file

@ -3,5 +3,5 @@ build:
rrun:
#scp -r ./src olimex@muele.local:red/
rsync -rvu --filter=':- .gitignore' ../red olimex@muele.local:
rsync -rv --filter=':- .gitignore' ../red olimex@muele.local:
ssh olimex@muele.local "cd red; /home/olimex/.cargo/bin/cargo run"

View file

@ -1,7 +1,8 @@
#![warn(rust_2018_idioms)]
use futures::stream::StreamExt;
use std::{env, io, str};
use futures::sink::SinkExt;
use std::{env, io, str, fmt::Write};
use tokio_util::codec::{Decoder, Encoder};
use bytes::BytesMut;
@ -34,7 +35,9 @@ impl Decoder for LineCodec {
impl Encoder<String> for LineCodec {
type Error = io::Error;
fn encode(&mut self, _item: String, _dst: &mut BytesMut) -> Result<(), Self::Error> {
fn encode(&mut self, item: String, dst: &mut BytesMut) -> Result<(), Self::Error> {
dst.write_str(&item)
.map_err(|e| {io::Error::new(io::ErrorKind::InvalidData, e.to_string())})?;
Ok(())
}
}
@ -51,11 +54,24 @@ async fn main() -> tokio_serial::Result<()> {
port.set_exclusive(false)
.expect("Unable to set serial port exclusive to false");
let mut reader = LineCodec.framed(port);
let (mut writer, mut reader) = LineCodec.framed(port).split();
println!("Starting read.");
while let Some(line_result) = reader.next().await {
let line = line_result.expect("Failed to read line");
println!("{}", line);
}
let reader_task = async move {
while let Some(line_result) = reader.next().await {
let line = line_result.expect("Failed to read line");
println!("{}", line);
}
};
let writer_task = async move {
loop {
if let Err(e) = writer.send("M114\n".into()).await {
println!("{:?}", e);
break;
}
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
}
};
tokio::join!(writer_task, reader_task);
Ok(())
}