Clean up code

This commit is contained in:
Frederik Menke 2024-01-01 12:45:04 +01:00
parent a65f523636
commit 3b5892e7e0

View file

@ -22,6 +22,7 @@ use self::gcode::{G0Command, G28Command, G90Command, GcodeReplyError};
/// This should fit a simple "OK Pnn Bn" reply for GCODE commands that
/// do not return any data.
const RECV_BUFFER_CAPACITY: usize = 32;
const BAUD_RATE: u32 = 115200;
#[derive(Debug)]
pub enum PrinterError {
@ -32,8 +33,11 @@ pub enum PrinterError {
// If the "ok" line can't be parsed
ConfirmationError { parsed_string: String },
NoResponseFromPrinter(String),
// If the initial printer configuration fails
InitializationError(String),
}
#[derive(Debug, Clone, Copy)]
pub struct PrinterPosition {
x: f64,
y: f64,
@ -86,10 +90,7 @@ impl Printer {
}
pub async fn connect(port_path: &str) -> Result<Self, PrinterError> {
// TODO: implement auto position reporting.
// For this the whole reply-response control flow won't really work anymore, since the
// replies will just be interlaced with position reports.
let mut port = tokio_serial::new(port_path, 115200)
let mut port = tokio_serial::new(port_path, BAUD_RATE)
.open_native_async()
.expect("Unable to open serial port");
@ -141,13 +142,24 @@ impl Printer {
maximum_buffer_capacity: 0, // this is updated on the next call to `send_gcode()`
};
res.send_gcode(G91Command)
.await
.expect("Could not ask for current position!");
// This implicitly sets `res.last_buffer_capacity`
res.use_absolute_movements().await.map_err(|err| {
PrinterError::InitializationError(format!(
"Failed to set absolute movements mode: {:?}",
err
))
})?;
// since we never sent any positioning GCODE, we should be at max-capacity now.
res.maximum_buffer_capacity = res.last_buffer_capacity;
res.update_position().await.map_err(|err| {
PrinterError::InitializationError(format!(
"Failed to get the current position: {:?}",
err
))
})?;
Ok(res)
}
@ -180,6 +192,13 @@ impl Printer {
.map_err(|_| make_err())
}
/// Update the internal position by asking the printer for it
async fn update_position(&mut self) -> Result<PrinterPosition, PrinterError> {
let res = self.send_gcode(M114Command).await?;
self.state.position = res;
Ok(res)
}
/// Switch the printer to absolute movement mode.
///
/// This is generally necessary before sending consecutive `self.move_absolute()` commands.
@ -208,13 +227,13 @@ impl Printer {
/// * `x, y, z` - Whether the axis should be homed. Axis that are set to `false` will not be
/// homed.,
pub async fn auto_home(&mut self, x: bool, y: bool, z: bool) -> Result<(), PrinterError> {
let res = self.send_gcode(G28Command::new(x, y, z)).await?;
self.send_gcode(G28Command::new(x, y, z)).await?;
self.state.position = PrinterPosition {
x: 0.0,
y: 0.0,
z: 0.0,
};
Ok(res)
Ok(())
}
/// Move the printer by a specific distance
@ -241,7 +260,7 @@ impl Printer {
e: None, // Machine has no e
velocity,
};
let reply = if let MovementMode::AbsoluteMovements = self.state.movement_mode {
if let MovementMode::AbsoluteMovements = self.state.movement_mode {
self.use_relative_movements().await?;
let res = self.send_gcode(command).await;
self.use_absolute_movements().await?;
@ -254,7 +273,7 @@ impl Printer {
self.state.position.y += y;
self.state.position.z += z;
Ok(reply)
Ok(())
}
/// Move the printer to a specific position
///
@ -279,7 +298,7 @@ impl Printer {
e: None, // Machine has no e
velocity: Some(velocity),
};
let reply = if let MovementMode::RelativeMovements = self.state.movement_mode {
if let MovementMode::RelativeMovements = self.state.movement_mode {
self.use_absolute_movements().await?;
let res = self.send_gcode(command).await;
self.use_relative_movements().await?;
@ -292,7 +311,7 @@ impl Printer {
self.state.position.y = y;
self.state.position.z = z;
Ok(reply)
Ok(())
}
}