Move printer code to lib
This commit is contained in:
parent
1bc575d06c
commit
cf8c624099
3 changed files with 76 additions and 68 deletions
1
red/src/lib.rs
Normal file
1
red/src/lib.rs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
pub mod printer;
|
|
@ -1,76 +1,10 @@
|
||||||
#![warn(rust_2018_idioms)]
|
#![warn(rust_2018_idioms)]
|
||||||
|
|
||||||
use futures::stream::StreamExt;
|
use red::printer;
|
||||||
use futures::sink::SinkExt;
|
|
||||||
use std::{env, io, str, fmt::Write};
|
|
||||||
use tokio_util::codec::{Decoder, Encoder};
|
|
||||||
|
|
||||||
use bytes::BytesMut;
|
|
||||||
use tokio_serial::SerialPortBuilderExt;
|
|
||||||
|
|
||||||
#[cfg(unix)]
|
|
||||||
const DEFAULT_TTY: &str = "/dev/ttyUSB0";
|
|
||||||
#[cfg(windows)]
|
|
||||||
const DEFAULT_TTY: &str = "COM1";
|
|
||||||
|
|
||||||
struct LineCodec;
|
|
||||||
|
|
||||||
impl Decoder for LineCodec {
|
|
||||||
type Item = String;
|
|
||||||
type Error = io::Error;
|
|
||||||
|
|
||||||
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
|
|
||||||
let newline = src.as_ref().iter().position(|b| *b == b'\n');
|
|
||||||
if let Some(n) = newline {
|
|
||||||
let line = src.split_to(n + 1);
|
|
||||||
return match str::from_utf8(line.as_ref()) {
|
|
||||||
Ok(s) => Ok(Some(s.to_string())),
|
|
||||||
Err(_) => Err(io::Error::new(io::ErrorKind::Other, "Invalid String")),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Encoder<String> for LineCodec {
|
|
||||||
type Error = io::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(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> tokio_serial::Result<()> {
|
async fn main() -> tokio_serial::Result<()> {
|
||||||
let mut args = env::args();
|
printer::communicate().await?;
|
||||||
let tty_path = args.nth(1).unwrap_or_else(|| DEFAULT_TTY.into());
|
|
||||||
|
|
||||||
let mut port = tokio_serial::new(tty_path, 115200).open_native_async()
|
|
||||||
.expect("Unable to open serial port");
|
|
||||||
|
|
||||||
#[cfg(unix)]
|
|
||||||
port.set_exclusive(false)
|
|
||||||
.expect("Unable to set serial port exclusive to false");
|
|
||||||
|
|
||||||
let (mut writer, mut reader) = LineCodec.framed(port).split();
|
|
||||||
println!("Starting read.");
|
|
||||||
let reader_task = async move {
|
|
||||||
let mut counter = 0;
|
|
||||||
while let Some(line_result) = reader.next().await {
|
|
||||||
let line = line_result.expect("Failed to read line");
|
|
||||||
if line.contains("ok") || line.contains("Loaded") {
|
|
||||||
if let Err(e) = writer.send("M114\n".into()).await {
|
|
||||||
println!("{:?}", e);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
counter += 1;
|
|
||||||
println!("updates received: {}", counter);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
reader_task.await;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
73
red/src/printer/mod.rs
Normal file
73
red/src/printer/mod.rs
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
use futures::stream::StreamExt;
|
||||||
|
use futures::sink::SinkExt;
|
||||||
|
use std::{env, io, str, fmt::Write};
|
||||||
|
use tokio_util::codec::{Decoder, Encoder};
|
||||||
|
|
||||||
|
use bytes::BytesMut;
|
||||||
|
use tokio_serial::SerialPortBuilderExt;
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
|
const DEFAULT_TTY: &str = "/dev/ttyUSB0";
|
||||||
|
#[cfg(windows)]
|
||||||
|
const DEFAULT_TTY: &str = "COM1";
|
||||||
|
|
||||||
|
struct LineCodec;
|
||||||
|
|
||||||
|
impl Decoder for LineCodec {
|
||||||
|
type Item = String;
|
||||||
|
type Error = io::Error;
|
||||||
|
|
||||||
|
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
|
||||||
|
let newline = src.as_ref().iter().position(|b| *b == b'\n');
|
||||||
|
if let Some(n) = newline {
|
||||||
|
let line = src.split_to(n + 1);
|
||||||
|
return match str::from_utf8(line.as_ref()) {
|
||||||
|
Ok(s) => Ok(Some(s.to_string())),
|
||||||
|
Err(_) => Err(io::Error::new(io::ErrorKind::Other, "Invalid String")),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encoder<String> for LineCodec {
|
||||||
|
type Error = io::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(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn communicate() -> tokio_serial::Result<()> {
|
||||||
|
let mut args = env::args();
|
||||||
|
let tty_path = args.nth(1).unwrap_or_else(|| DEFAULT_TTY.into());
|
||||||
|
|
||||||
|
let mut port = tokio_serial::new(tty_path, 115200).open_native_async()
|
||||||
|
.expect("Unable to open serial port");
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
|
port.set_exclusive(false)
|
||||||
|
.expect("Unable to set serial port exclusive to false");
|
||||||
|
|
||||||
|
let (mut writer, mut reader) = LineCodec.framed(port).split();
|
||||||
|
println!("Starting read.");
|
||||||
|
let reader_task = async move {
|
||||||
|
let mut counter = 0;
|
||||||
|
while let Some(line_result) = reader.next().await {
|
||||||
|
let line = line_result.expect("Failed to read line");
|
||||||
|
if line.contains("ok") || line.contains("Loaded") {
|
||||||
|
if let Err(e) = writer.send("M114\n".into()).await {
|
||||||
|
println!("{:?}", e);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
counter += 1;
|
||||||
|
println!("updates received: {}", counter);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
reader_task.await;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue