Skip to content

Commit 40805bc

Browse files
committed
Refactor engraving loop to calculate X and Y positions together, improving movement efficiency and clarity
1 parent 16c4739 commit 40805bc

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

Marlin/src/lcd/extui/knutwurst/anycubic_touchscreen.cpp

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -388,15 +388,12 @@ void AnycubicTouchscreenClass::Setup() {
388388

389389
// Main engraving loop: process image row by row
390390
for (i = 0; i < y_max; i++) {
391-
// Move to next Y position (row)
391+
// Calculate Y position for this row
392+
float y_pos;
392393
if (laser_printer_st.pic_y_mirror == 0)
393-
sprintf_P(cvalue, PSTR("G1 Y%sF%i"), ftostr42_52(y_start + i * laser_printer_st.pic_pixel_distance), ftemp);
394+
y_pos = y_start + i * laser_printer_st.pic_pixel_distance;
394395
else
395-
sprintf_P(cvalue, PSTR("G1 Y%sF%i"), ftostr42_52(y_start - i * laser_printer_st.pic_pixel_distance), ftemp);
396-
397-
// Execute Y movement before processing the row
398-
queue.enqueue_one_now(cvalue);
399-
while (commandsInQueue()) idle();
396+
y_pos = y_start - i * laser_printer_st.pic_pixel_distance;
400397

401398
// Even rows: scan left to right
402399
if (i % 2 == 0) {
@@ -405,11 +402,15 @@ void AnycubicTouchscreenClass::Setup() {
405402

406403
// Only engrave if pixel is dark enough (above threshold)
407404
if (Y > MIN_GRAY_VLAUE) {
408-
// Move to pixel position
405+
// Calculate X position
406+
float x_pos;
409407
if (laser_printer_st.pic_x_mirror == 1)
410-
sprintf_P(cvalue, PSTR("G1 X%sF%i"), ftostr42_52(x_start - j * laser_printer_st.pic_pixel_distance), ftemp);
408+
x_pos = x_start - j * laser_printer_st.pic_pixel_distance;
411409
else
412-
sprintf_P(cvalue, PSTR("G1 X%sF%i"), ftostr42_52(x_start + j * laser_printer_st.pic_pixel_distance), ftemp);
410+
x_pos = x_start + j * laser_printer_st.pic_pixel_distance;
411+
412+
// Move to pixel position (X and Y together)
413+
sprintf_P(cvalue, PSTR("G1 X%s Y%s F%i"), ftostr42_52(x_pos), ftostr42_52(y_pos), ftemp);
413414
queue.enqueue_one_now(cvalue);
414415
while (commandsInQueue()) idle();
415416

@@ -436,36 +437,51 @@ void AnycubicTouchscreenClass::Setup() {
436437
if (laser_status == 0) return;
437438
}
438439
}
440+
// Odd rows: scan right to left (bidirectional)
439441
else {
440442
for (j = 0; j < x_max; j++) {
441-
read_bmp(&Y, i, x_max - j - 1);
442-
if (Y > MIN_GRAY_VLAUE && j != 0) {
443+
// Read pixels in reverse order: x_max-1, x_max-2, ..., 0
444+
int pixel_x = x_max - 1 - j;
445+
read_bmp(&Y, i, pixel_x);
446+
447+
// Only engrave if pixel is dark enough (above threshold)
448+
if (Y > MIN_GRAY_VLAUE) {
449+
// Calculate X position (moving from right to left)
450+
float x_pos;
443451
if (laser_printer_st.pic_x_mirror == 1)
444-
sprintf_P(cvalue, PSTR("G1 X%sF%i"), ftostr42_52(x_end + j * laser_printer_st.pic_pixel_distance), ftemp);
452+
x_pos = x_end + j * laser_printer_st.pic_pixel_distance;
445453
else
446-
sprintf_P(cvalue, PSTR("G1 X%sF%i"), ftostr42_52(x_end - j * laser_printer_st.pic_pixel_distance), ftemp);
454+
x_pos = x_end - j * laser_printer_st.pic_pixel_distance;
455+
456+
// Move to pixel position (X and Y together)
457+
sprintf_P(cvalue, PSTR("G1 X%s Y%s F%i"), ftostr42_52(x_pos), ftostr42_52(y_pos), ftemp);
447458
queue.enqueue_one_now(cvalue);
448459
while (commandsInQueue()) idle();
460+
461+
// Fire laser
449462
WRITE(HEATER_0_PIN, 1);
463+
464+
// In raster mode, pulse duration is proportional to pixel darkness
450465
if (laser_printer_st.pic_vector == 0) {
451466
time = Y * laser_printer_st.pic_laser_time;
452467
while (time--) WRITE(HEATER_0_PIN, 1);
453468
WRITE(HEATER_0_PIN, 0);
454469
}
455470

471+
// Send progress update to TFT every 20 pixels
456472
if (laser_counter != 20) {
457473
laser_counter = 20;
458474
SENDLINE_PGM("J30");
459475
SENDLINE_PGM("J30");
460476
}
461477
}
462478
else {
463-
WRITE(HEATER_0_PIN, 0);
479+
WRITE(HEATER_0_PIN, 0); // Turn off laser for white pixels
464480
}
465481

482+
// Check for pause/stop commands from TFT
466483
TFTCommandScan();
467-
while (laser_print_pause)
468-
{
484+
while (laser_print_pause) {
469485
TFTCommandScan();
470486
if (laser_status == 0) return;
471487
}

0 commit comments

Comments
 (0)