Skip to content

Commit

Permalink
take a stab at spawning the print task
Browse files Browse the repository at this point in the history
  • Loading branch information
paultag committed Aug 23, 2024
1 parent cade9d7 commit 34578a1
Showing 1 changed file with 48 additions and 44 deletions.
92 changes: 48 additions & 44 deletions src/usb/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ use crate::{
MachineMakeModel, MachineType, Volume,
};
use anyhow::Result;
use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, ReadHalf, WriteHalf};
use std::sync::Arc;
use tokio::{
io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, ReadHalf, WriteHalf},
sync::Mutex,
task::JoinHandle,
};
use tokio_serial::SerialStream;

/// Handle to a USB based gcode 3D printer.
#[derive(Clone)]
pub struct Usb {
client: Client<WriteHalf<SerialStream>, ReadHalf<SerialStream>>,
client: Arc<Mutex<Client<WriteHalf<SerialStream>, ReadHalf<SerialStream>>>>,
machine_info: UsbMachineInfo,
}

Expand All @@ -19,15 +25,15 @@ impl Usb {
let (reader, writer) = tokio::io::split(stream);

Self {
client: Client::new(writer, reader),
client: Arc::new(Mutex::new(Client::new(writer, reader))),
machine_info,
}
}

Check warning on line 31 in src/usb/control.rs

View check run for this annotation

Codecov / codecov/patch

src/usb/control.rs#L24-L31

Added lines #L24 - L31 were not covered by tests

async fn wait_for_start(&mut self) -> Result<()> {
loop {
let mut line = String::new();
if let Err(e) = self.client.get_read().read_line(&mut line).await {
if let Err(e) = self.client.lock().await.get_read().read_line(&mut line).await {
println!("wait_for_start err: {}", e);
} else {

Check warning on line 38 in src/usb/control.rs

View check run for this annotation

Codecov / codecov/patch

src/usb/control.rs#L33-L38

Added lines #L33 - L38 were not covered by tests
// Use ends with because sometimes we may still have some data left on the buffer
Expand All @@ -41,7 +47,7 @@ impl Usb {
async fn wait_for_ok(&mut self) -> Result<()> {
loop {
let mut line = String::new();
if let Err(e) = self.client.get_read().read_line(&mut line).await {
if let Err(e) = self.client.lock().await.get_read().read_line(&mut line).await {
println!("wait_for_ok err: {}", e);
} else {
println!("RCVD: {}", line);
Expand All @@ -51,36 +57,6 @@ impl Usb {
}
}
}

Check warning on line 59 in src/usb/control.rs

View check run for this annotation

Codecov / codecov/patch

src/usb/control.rs#L59

Added line #L59 was not covered by tests

async fn send_gcode(&mut self, _job_name: &str, gcode: GcodeTemporaryFile) -> Result<()> {
let mut gcode = gcode.0;

let mut buf = String::new();
gcode.as_mut().read_to_string(&mut buf).await?;

let lines: Vec<String> = buf
.lines() // split the string into an iterator of string slices
.map(|s| {
let s = String::from(s);
match s.split_once(';') {
Some((command, _)) => command.trim().to_string(),
None => s.trim().to_string(),
}
})
.filter(|s| !s.is_empty()) // make each slice into a string
.collect();

self.wait_for_start().await?;

for line in lines.iter() {
let msg = format!("{}\r\n", line);
println!("writing: {}", line);
self.client.write_all(msg.as_bytes()).await?;
self.wait_for_ok().await?;
}

Ok(())
}
}

/// Information regarding a USB connected Machine.
Expand Down Expand Up @@ -124,11 +100,6 @@ impl UsbMachineInfo {
baud,
}
}

Check warning on line 102 in src/usb/control.rs

View check run for this annotation

Codecov / codecov/patch

src/usb/control.rs#L84-L102

Added lines #L84 - L102 were not covered by tests

// /// return the discovery key
// pub(crate) fn discovery_key(&self) -> (u16, u16, String) {
// (self.vendor_id, self.product_id, self.make_model.serial.clone().unwrap())
// }
}

impl MachineInfoTrait for UsbMachineInfo {
Expand All @@ -154,11 +125,11 @@ impl ControlTrait for Usb {
}

Check warning on line 125 in src/usb/control.rs

View check run for this annotation

Codecov / codecov/patch

src/usb/control.rs#L123-L125

Added lines #L123 - L125 were not covered by tests

async fn emergency_stop(&mut self) -> Result<()> {
self.client.emergency_stop().await
self.client.lock().await.emergency_stop().await
}

Check warning on line 129 in src/usb/control.rs

View check run for this annotation

Codecov / codecov/patch

src/usb/control.rs#L127-L129

Added lines #L127 - L129 were not covered by tests

async fn stop(&mut self) -> Result<()> {
self.client.stop().await
self.client.lock().await.stop().await
}

Check warning on line 133 in src/usb/control.rs

View check run for this annotation

Codecov / codecov/patch

src/usb/control.rs#L131-L133

Added lines #L131 - L133 were not covered by tests

async fn healthy(&self) -> bool {
Expand All @@ -168,7 +139,40 @@ impl ControlTrait for Usb {
}

impl GcodeControlTrait for Usb {
async fn build(&mut self, job_name: &str, gcode: GcodeTemporaryFile) -> Result<()> {
self.send_gcode(job_name, gcode).await
async fn build(&mut self, _job_name: &str, gcode: GcodeTemporaryFile) -> Result<()> {
let mut gcode = gcode.0;

let mut buf = String::new();
gcode.as_mut().read_to_string(&mut buf).await?;

Check warning on line 146 in src/usb/control.rs

View check run for this annotation

Codecov / codecov/patch

src/usb/control.rs#L142-L146

Added lines #L142 - L146 were not covered by tests

let lines: Vec<String> = buf
.lines() // split the string into an iterator of string slices
.map(|s| {
let s = String::from(s);
match s.split_once(';') {
Some((command, _)) => command.trim().to_string(),
None => s.trim().to_string(),

Check warning on line 154 in src/usb/control.rs

View check run for this annotation

Codecov / codecov/patch

src/usb/control.rs#L148-L154

Added lines #L148 - L154 were not covered by tests
}
})
.filter(|s| !s.is_empty()) // make each slice into a string
.collect();

self.wait_for_start().await?;

Check warning on line 160 in src/usb/control.rs

View check run for this annotation

Codecov / codecov/patch

src/usb/control.rs#L156-L160

Added lines #L156 - L160 were not covered by tests

let self1 = self.clone();
let _: JoinHandle<Result<()>> = tokio::spawn(async move {

Check failure on line 163 in src/usb/control.rs

View workflow job for this annotation

GitHub Actions / cargo clippy

non-binding `let` on a future
let mut usb = self1;
for line in lines.iter() {
let msg = format!("{}\r\n", line);
println!("writing: {}", line);
usb.client.lock().await.write_all(msg.as_bytes()).await?;
usb.wait_for_ok().await?;

Check warning on line 169 in src/usb/control.rs

View check run for this annotation

Codecov / codecov/patch

src/usb/control.rs#L162-L169

Added lines #L162 - L169 were not covered by tests
}
Ok(())
});

// store a handle to the joinhandle in an option in self or something?

Ok(())
}

Check warning on line 177 in src/usb/control.rs

View check run for this annotation

Codecov / codecov/patch

src/usb/control.rs#L171-L177

Added lines #L171 - L177 were not covered by tests
}

0 comments on commit 34578a1

Please sign in to comment.