From f0f185f935efb3fac83e93253cfb659c34448e88 Mon Sep 17 00:00:00 2001 From: Stephen D <webmaster@scd31.com> Date: Tue, 11 Mar 2025 21:50:40 -0400 Subject: [PATCH] change constants and add math so that we center properly --- src/main.rs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 52914ba..c1dac89 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,11 +2,11 @@ use anyhow::bail; use image::{GenericImageView, ImageReader}; const TOAST_TIME_SECONDS: u64 = 60; -const CENTER_X: u32 = 100; -const CENTER_Y: u32 = 100; -const BREAD_HEIGHT: f64 = 15.5; +const CENTER_X: f64 = 100.0; +const CENTER_Y: f64 = 100.0; +const BREAD_HEIGHT: f64 = 15.3; const IDLE_HEIGHT: f64 = BREAD_HEIGHT + 10.0; -const PIXEL_SIZE_MM: f64 = 2.0; +const PIXEL_SIZE_MM: f64 = 4.0; fn main() -> anyhow::Result<()> { let img = ImageReader::open("/home/stephen/Downloads/qr_tiny.png")?.decode()?; @@ -22,7 +22,10 @@ fn main() -> anyhow::Result<()> { // nothing, for now } [255, 255, 255, 255] => { - toast(x, y); + toast( + x as f64 - (img.width() as f64 / 2.0), + y as f64 - (img.height() as f64 / 2.0), + ); } _ => bail!("Pixel was not entirely black nor entirely white: {:?}", p), } @@ -36,15 +39,15 @@ fn preamble() { println!( r#" G28 ; home -M109 S220 ; heat hotend to 220C and wait G90 ; absolute positioning G0 Z{IDLE_HEIGHT} G0 X{CENTER_X} Y{CENTER_Y} +M109 S220 ; heat hotend to 220C and wait "# ); } -fn toast(x: u32, y: u32) { +fn toast(x: f64, y: f64) { println!( r#" G0 X{:.2} Y{:.2}; @@ -52,7 +55,7 @@ G0 Z{BREAD_HEIGHT}; G4 S{TOAST_TIME_SECONDS} ; wait G0 Z{IDLE_HEIGHT}; "#, - x as f64 * PIXEL_SIZE_MM, - y as f64 * PIXEL_SIZE_MM + x * PIXEL_SIZE_MM + CENTER_X, + y * PIXEL_SIZE_MM + CENTER_Y ); } -- GitLab