Get Gcode abstraction running
This commit is contained in:
parent
498a81318e
commit
13781bd29e
|
@ -2,10 +2,12 @@
|
|||
|
||||
use red::printer::Printer;
|
||||
|
||||
const DEFAULT_TTY: &str = "/dev/ttyUSB0";
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> tokio_serial::Result<()> {
|
||||
let printer = Printer::connect("").await.unwrap();
|
||||
printer.printer_in.send("M114\n".to_string()).await;
|
||||
let printer = Printer::connect(DEFAULT_TTY).await.unwrap();
|
||||
printer.printer_in.send(Box::new(red::printer::M114Command::new())).await;
|
||||
tokio::time::sleep(std::time::Duration::from_secs(15)).await;
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -1,20 +1,22 @@
|
|||
use lazy_static::lazy_static;
|
||||
use regex::Regex;
|
||||
|
||||
pub trait GcodeCommand<'a, Response> {
|
||||
fn command(&self) -> &'a str;
|
||||
fn parse_reply(reply: &str) -> Response;
|
||||
pub trait GcodeCommand {
|
||||
fn command(&self) -> &str;
|
||||
fn parse_reply(&self, reply: &str) -> GcodeReply;
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct M114Command;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct M114Reply {
|
||||
x: Option<f64>,
|
||||
y: Option<f64>,
|
||||
z: Option<f64>,
|
||||
e: Option<f64>,
|
||||
pub enum GcodeReply {
|
||||
M114Reply {
|
||||
x: Option<f64>,
|
||||
y: Option<f64>,
|
||||
z: Option<f64>,
|
||||
e: Option<f64>,
|
||||
},
|
||||
}
|
||||
|
||||
impl M114Command {
|
||||
|
@ -23,11 +25,11 @@ impl M114Command {
|
|||
}
|
||||
}
|
||||
|
||||
impl GcodeCommand<'_, M114Reply> for M114Command {
|
||||
impl GcodeCommand for M114Command {
|
||||
fn command(&self) -> &'static str {
|
||||
"M114"
|
||||
}
|
||||
fn parse_reply(reply: &str) -> M114Reply {
|
||||
fn parse_reply(&self, reply: &str) -> GcodeReply {
|
||||
lazy_static! {
|
||||
static ref RE_SET: Vec<Regex> = vec![
|
||||
Regex::new(r"X:(\d+(?:\.\d+))").unwrap(),
|
||||
|
@ -46,7 +48,7 @@ impl GcodeCommand<'_, M114Reply> for M114Command {
|
|||
})
|
||||
.collect();
|
||||
|
||||
M114Reply {
|
||||
GcodeReply::M114Reply {
|
||||
x: fields[0],
|
||||
y: fields[1],
|
||||
z: fields[2],
|
||||
|
|
|
@ -6,9 +6,10 @@ use futures::stream::StreamExt;
|
|||
use std::{fmt::Write, io, rc::Rc, str};
|
||||
use tokio;
|
||||
use tokio_serial::SerialPortBuilderExt;
|
||||
use tokio::sync::mpsc::{Sender, channel};
|
||||
use tokio_util::codec::{Decoder, Encoder};
|
||||
|
||||
const DEFAULT_TTY: &str = "/dev/ttyUSB0";
|
||||
pub use gcode::{GcodeCommand, GcodeReply, M114Command};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum PrinterError {
|
||||
|
@ -16,17 +17,17 @@ pub enum PrinterError {
|
|||
}
|
||||
|
||||
pub struct State {
|
||||
position: (f64, f64),
|
||||
position: (f64, f64, f64, f64),
|
||||
}
|
||||
|
||||
pub struct Printer {
|
||||
pub printer_in: tokio::sync::mpsc::Sender<String>,
|
||||
pub printer_in: Sender<Box<dyn GcodeCommand + Send>>,
|
||||
pub state: Rc<State>,
|
||||
}
|
||||
|
||||
impl Printer {
|
||||
pub async fn connect(port_path: &str) -> Result<Self, PrinterError> {
|
||||
let mut port = tokio_serial::new(DEFAULT_TTY, 115200)
|
||||
let mut port = tokio_serial::new(port_path, 115200)
|
||||
.open_native_async()
|
||||
.expect("Unable to open serial port");
|
||||
|
||||
|
@ -34,7 +35,7 @@ impl Printer {
|
|||
.expect("Unable to set serial port exclusive to false");
|
||||
|
||||
let connection = LineCodec.framed(port);
|
||||
let (tx, mut rx) = tokio::sync::mpsc::channel(32);
|
||||
let (tx, mut rx) = channel::<Box<dyn GcodeCommand + Send>>(32);
|
||||
tokio::spawn(async move {
|
||||
let (mut printer_tx, mut printer_rx) = connection.split();
|
||||
while let Some(stuff) = printer_rx.next().await {
|
||||
|
@ -44,13 +45,15 @@ impl Printer {
|
|||
}
|
||||
|
||||
while let Some(command) = rx.recv().await {
|
||||
printer_tx.send(command).await.unwrap();
|
||||
let command_text = format!("{}\n", command.command());
|
||||
printer_tx.send(command_text).await.unwrap();
|
||||
while let Some(reply) = printer_rx.next().await {
|
||||
let reply = reply.unwrap();
|
||||
if reply.contains("Ok") {
|
||||
break;
|
||||
} else {
|
||||
println!("got reply: {}", reply);
|
||||
let reply = command.parse_reply(&reply);
|
||||
println!("got reply: {:?}", reply);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -59,7 +62,7 @@ impl Printer {
|
|||
Ok(Printer {
|
||||
printer_in: tx,
|
||||
state: Rc::new(State {
|
||||
position: (0.0, 0.0),
|
||||
position: (0.0, 0.0, 0.0, 0.0),
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue