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