Skip to content

Commit

Permalink
Fixed [BUG] Crash and reboot after second time gcode preview.
Browse files Browse the repository at this point in the history
modified:   Marlin/Configuration.h
modified:   Marlin/src/lcd/e3v2/proui/custom_gcodes.cpp
modified:   Marlin/src/lcd/e3v2/proui/dwin.cpp
renamed:    Marlin/src/lcd/e3v2/proui/gcode_preview_nopro.cpp -> Marlin/src/lcd/e3v2/proui/gcode_preview.cpp
modified:   Marlin/src/lcd/e3v2/proui/gcode_preview.h
deleted:    Marlin/src/lcd/e3v2/proui/gcode_preview_nopro.h
modified:   configurations/Andrew427/Configuration.h
modified:   configurations/BTT-SKR_Mini_E3_V2/ConfigurationMM.h
modified:   configurations/BTT-SKR_Mini_E3_V2/ConfigurationUBL.h
modified:   configurations/BTT-SKR_Mini_E3_V3/ConfigurationMM.h
modified:   configurations/BTT-SKR_Mini_E3_V3/ConfigurationUBL.h
modified:   configurations/Creality Ender/BLT/Configuration.h
modified:   configurations/Creality Ender/Default-NP/Configuration.h
modified:   configurations/Creality Ender/MM/Configuration.h
modified:   configurations/Creality Ender/UBL/Configuration.h
modified:   configurations/Voxelab Aquila 427/BLT/Configuration.h
modified:   configurations/Voxelab Aquila 427/Default-NP/Configuration.h
modified:   configurations/Voxelab Aquila 427/MM/Configuration.h
modified:   configurations/Voxelab Aquila 427/UBL/Configuration.h
modified:   configurations/Voxelab Aquila/BLT/Configuration.h
modified:   configurations/Voxelab Aquila/BLT/ConfigurationPro.h
modified:   configurations/Voxelab Aquila/Default-NP/Configuration.h
modified:   configurations/Voxelab Aquila/MM/Configuration.h
modified:   configurations/Voxelab Aquila/UBL/Configuration.h
modified:   configurations/Voxelab Aquila/UBL/ConfigurationPro.h
modified:   configurations/Voxelab Aquila/X3/Configuration.h
  • Loading branch information
Nazar78 committed Oct 30, 2023
1 parent e456224 commit 7989e21
Show file tree
Hide file tree
Showing 26 changed files with 87 additions and 165 deletions.
2 changes: 0 additions & 2 deletions Marlin/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -3419,8 +3419,6 @@
#ifdef PROUI_EX
#define HAS_GCODE_PREVIEW 1
#define HAS_TOOLBAR 1
#else
#define HAS_GCODE_PREVIEW_NOPRO 1
#endif
#define DISABLE_TUNING_GRAPH 0// Temp plot graph - PID/MPC Tuning (1624 bytes of flash)
#define HAS_ESDIAG 1 // View End-stop switch continuity (560 bytes of flash)
Expand Down
4 changes: 2 additions & 2 deletions Marlin/src/lcd/e3v2/proui/custom_gcodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void C108() {
}

// Enable or disable preview screen
#if HAS_GCODE_PREVIEW || HAS_GCODE_PREVIEW_NOPRO
#if HAS_GCODE_PREVIEW
void C250() {
if (parser.seenval('P')) {
HMI_data.EnablePreview = !!parser.value_byte();
Expand Down Expand Up @@ -120,7 +120,7 @@ void custom_gcode(const int16_t codenum) {
case 35: C35(); break; // Launch bed tramming wizard
#endif
case 108: C108(); break; // Cancel a Wait for User without an Emergecy Parser
#if HAS_GCODE_PREVIEW || HAS_GCODE_PREVIEW_NOPRO
#if HAS_GCODE_PREVIEW
case 250: C250(); break; // Enable or disable preview screen
#endif
#if HAS_LOCKSCREEN
Expand Down
27 changes: 8 additions & 19 deletions Marlin/src/lcd/e3v2/proui/dwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@
#if HAS_GCODE_PREVIEW
#include "file_header.h"
#include "gcode_preview.h"
#elif HAS_GCODE_PREVIEW_NOPRO
#include "gcode_preview_nopro.h"
#endif

#if HAS_TOOLBAR
Expand Down Expand Up @@ -630,12 +628,6 @@ void Draw_PrintDone() {
DWINUI::ClearMainArea();
DWIN_Print_Header(nullptr);
#if HAS_GCODE_PREVIEW
const bool haspreview = Preview_Valid();
if (haspreview) {
Preview_Show();
DWINUI::Draw_Button(BTN_Continue, 86, 295, true);
}
#elif HAS_GCODE_PREVIEW_NOPRO
const bool haspreview = preview.valid();
if (haspreview) {
preview.show();
Expand Down Expand Up @@ -1843,14 +1835,13 @@ void DWIN_LevelingDone() {
// Started a Print Job
void DWIN_Print_Started() {
DEBUG_ECHOLNPGM("DWIN_Print_Started: ", SD_Printing());
TERN_(HAS_GCODE_PREVIEW, if (Host_Printing()) { Preview_Invalidate(); })
TERN_(HAS_GCODE_PREVIEW_NOPRO, if (Host_Printing()) { preview.invalidate(); })
TERN_(HAS_GCODE_PREVIEW, if (Host_Printing()) { preview.invalidate(); })
ui.progress_reset();
ui.reset_remaining_time();
HMI_flag.pause_flag = false;
HMI_flag.abort_flag = false;
select_print.reset();
#if PROUI_EX
#if PROUI_EX && HAS_GCODE_PREVIEW
if (!fileprop.isConfig) { Goto_PrintProcess(); }
#else
Goto_PrintProcess();
Expand All @@ -1876,7 +1867,7 @@ void DWIN_Print_Finished() {
HMI_flag.abort_flag = false;
HMI_flag.pause_flag = false;
wait_for_heatup = false;
#if PROUI_EX
#if PROUI_EX && HAS_GCODE_PREVIEW
if (!fileprop.isConfig) { Goto_PrintDone(); }
else { fileprop.isConfig = false; }
#else
Expand Down Expand Up @@ -2039,7 +2030,7 @@ void DWIN_SetDataDefaults() {
#endif
#endif
TERN_(ADAPTIVE_STEP_SMOOTHING, HMI_data.AdaptiveStepSmoothing = true;)
#if HAS_GCODE_PREVIEW || HAS_GCODE_PREVIEW_NOPRO
#if HAS_GCODE_PREVIEW
HMI_data.EnablePreview = true;
#endif
#if PROUI_EX
Expand Down Expand Up @@ -2279,7 +2270,7 @@ void DWIN_RedrawScreen() {

#endif // HAS_LOCKSCREEN

#if HAS_GCODE_PREVIEW || HAS_GCODE_PREVIEW_NOPRO
#if HAS_GCODE_PREVIEW

void SetPreview() { Toggle_Chkb_Line(HMI_data.EnablePreview); }

Expand All @@ -2297,7 +2288,7 @@ void DWIN_RedrawScreen() {
#endif

void Goto_ConfirmToPrint() {
#if PROUI_EX
#if PROUI_EX && HAS_GCODE_PREVIEW
fileprop.clear();
fileprop.setname(card.filename);
card.openFileRead(fileprop.name, 100);
Expand All @@ -2316,8 +2307,6 @@ void Goto_ConfirmToPrint() {
LaserOn(false); // If it is not laser file turn off laser mode
#endif
#if HAS_GCODE_PREVIEW
if (HMI_data.EnablePreview) return Goto_Popup(Preview_DrawFromSD, onClick_ConfirmToPrint);
#elif HAS_GCODE_PREVIEW_NOPRO
if (HMI_data.EnablePreview) return Goto_Popup(preview.drawFromSD, onClick_ConfirmToPrint);
#endif
card.openAndPrintFile(card.filename); // Direct print SD file
Expand Down Expand Up @@ -4520,7 +4509,7 @@ void Draw_AdvancedSettings_Menu() {
EDIT_ITEM(ICON_Sound, MSG_TICK, onDrawChkbMenu, SetEnableTick, &ui.tick_on);
EDIT_ITEM(ICON_Sound, MSG_SOUND, onDrawChkbMenu, SetEnableSound, &ui.sound_on);
#endif
#if HAS_GCODE_PREVIEW || HAS_GCODE_PREVIEW_NOPRO
#if HAS_GCODE_PREVIEW
EDIT_ITEM(ICON_File, MSG_HAS_PREVIEW, onDrawChkbMenu, SetPreview, &HMI_data.EnablePreview);
#endif
#if ENABLED(BAUD_RATE_GCODE)
Expand Down Expand Up @@ -4575,7 +4564,7 @@ void Draw_Advanced_Menu() { // From Control_Menu (Control) || Default-NP Advance
EDIT_ITEM(ICON_Sound, MSG_TICK, onDrawChkbMenu, SetEnableTick, &ui.tick_on);
EDIT_ITEM(ICON_Sound, MSG_SOUND, onDrawChkbMenu, SetEnableSound, &ui.sound_on);
#endif
#if HAS_GCODE_PREVIEW || HAS_GCODE_PREVIEW_NOPRO
#if HAS_GCODE_PREVIEW
EDIT_ITEM(ICON_File, MSG_HAS_PREVIEW, onDrawChkbMenu, SetPreview, &HMI_data.EnablePreview);
#endif
#if ENABLED(BAUD_RATE_GCODE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@

#include "../../../inc/MarlinConfigPre.h"

#if ALL(DWIN_LCD_PROUI, HAS_GCODE_PREVIEW_NOPRO)
#if ALL(DWIN_LCD_PROUI, HAS_GCODE_PREVIEW)

#include "gcode_preview_nopro.h"
#include "gcode_preview.h"

#include "../../../core/types.h"
#include "../../marlinui.h"
#include "../../../sd/cardreader.h"
#include "../../../MarlinCore.h" // for wait_for_user
#include "dwin.h"
#include "dwin_popup.h"
#include "base64.h"
Expand Down Expand Up @@ -75,7 +74,7 @@ typedef struct {

} fileprop_t;

fileprop_t fileprop;
fileprop_t fileprop_nopro;

void getValue(const char * const buf, PGM_P const key, float &value) {
if (value != 0.0f) return;
Expand All @@ -102,34 +101,34 @@ bool Preview::hasPreview() {
uint32_t indx = 0;
float tmp = 0;

fileprop.clear();
fileprop.setname(card.filename);
fileprop_nopro.clear();
fileprop_nopro.setname(card.filename);

card.openFileRead(fileprop.name);
card.openFileRead(fileprop_nopro.name);

char buf[256];
uint8_t nbyte = 1;
while (!fileprop.thumbstart && nbyte > 0 && indx < 4 * sizeof(buf)) {
while (!fileprop_nopro.thumbstart && nbyte > 0 && indx < 4 * sizeof(buf)) {
nbyte = card.read(buf, sizeof(buf) - 1);
if (nbyte > 0) {
buf[nbyte] = '\0';
getValue(buf, PSTR(";TIME:"), fileprop.time);
getValue(buf, PSTR(";Filament used:"), fileprop.filament);
getValue(buf, PSTR(";Layer height:"), fileprop.layer);
getValue(buf, PSTR(";TIME:"), fileprop_nopro.time);
getValue(buf, PSTR(";Filament used:"), fileprop_nopro.filament);
getValue(buf, PSTR(";Layer height:"), fileprop_nopro.layer);
getValue(buf, PSTR(";MINX:"), tmp);
getValue(buf, PSTR(";MAXX:"), fileprop.width);
fileprop.width -= tmp;
getValue(buf, PSTR(";MAXX:"), fileprop_nopro.width);
fileprop_nopro.width -= tmp;
tmp = 0;
getValue(buf, PSTR(";MINY:"), tmp);
getValue(buf, PSTR(";MAXY:"), fileprop.length);
fileprop.length -= tmp;
getValue(buf, PSTR(";MAXY:"), fileprop_nopro.length);
fileprop_nopro.length -= tmp;
tmp = 0;
getValue(buf, PSTR(";MINZ:"), tmp);
getValue(buf, PSTR(";MAXZ:"), fileprop.height);
fileprop.height -= tmp;
getValue(buf, PSTR(";MAXZ:"), fileprop_nopro.height);
fileprop_nopro.height -= tmp;
posptr = strstr_P(buf, tbstart);
if (posptr != nullptr) {
fileprop.thumbstart = indx + (posptr - &buf[0]);
fileprop_nopro.thumbstart = indx + (posptr - &buf[0]);
}
else {
indx += _MAX(10, nbyte - (signed)strlen_P(tbstart));
Expand All @@ -138,91 +137,95 @@ bool Preview::hasPreview() {
}
}

if (!fileprop.thumbstart) {
if (!fileprop_nopro.thumbstart) {
card.closefile();
LCD_MESSAGE_F("Thumbnail not found");
return false;
}

// Get the size of the thumbnail
card.setIndex(fileprop.thumbstart + strlen_P(tbstart));
card.setIndex(fileprop_nopro.thumbstart + strlen_P(tbstart));
for (uint8_t i = 0; i < 16; i++) {
const char c = card.get();
if (ISEOL(c)) { buf[i] = '\0'; break; }
buf[i] = c;
}
fileprop.thumbsize = atoi(buf);
fileprop_nopro.thumbsize = atoi(buf);

// Exit if there isn't a thumbnail
if (!fileprop.thumbsize) {
if (!fileprop_nopro.thumbsize) {
card.closefile();
LCD_MESSAGE_F("Invalid Thumbnail Size");
return false;
}

uint8_t buf64[fileprop.thumbsize + 1];
uint8_t buf64[fileprop_nopro.thumbsize + 1];
uint16_t nread = 0;
while (nread < fileprop.thumbsize) {
while (nread < fileprop_nopro.thumbsize) {
const uint8_t c = card.get();
if (!ISEOL(c) && c != ';' && c != ' ')
buf64[nread++] = c;
}
card.closefile();
buf64[nread] = '\0';

uint8_t thumbdata[3 + 3 * (fileprop.thumbsize / 4)]; // Reserve space for the JPEG thumbnail
fileprop.thumbsize = decode_base64(buf64, thumbdata);
DWINUI::WriteToSRAM(0x00, fileprop.thumbsize, thumbdata);
uint8_t thumbdata[3 + 3 * (fileprop_nopro.thumbsize / 4)]; // Reserve space for the JPEG thumbnail
fileprop_nopro.thumbsize = decode_base64(buf64, thumbdata);
DWINUI::WriteToSRAM(0x00, fileprop_nopro.thumbsize, thumbdata);

fileprop.thumbwidth = THUMBWIDTH;
fileprop.thumbheight = THUMBHEIGHT;
fileprop_nopro.thumbwidth = THUMBWIDTH;
fileprop_nopro.thumbheight = THUMBHEIGHT;

return true;
}

void Preview::drawFromSD() {
if (!hasPreview()) {
HMI_flag.select_flag = 1;
wait_for_user = false;
return;
}
hasPreview();

MString<45> buf;
DWIN_Draw_Rectangle(1, HMI_data.Background_Color, 0, 0, DWIN_WIDTH, STATUS_Y - 1);
if (fileprop.time) {
buf.setf(F("Estimated time: %i:%02i"), (uint16_t)fileprop.time / 3600, ((uint16_t)fileprop.time % 3600) / 60);
if (fileprop_nopro.time) {
buf.setf(F("Estimated time: %i:%02i"), (uint16_t)fileprop_nopro.time / 3600, ((uint16_t)fileprop_nopro.time % 3600) / 60);
DWINUI::Draw_String(20, 10, &buf);
}
if (fileprop.filament) {
buf.set(F("Filament used: "), p_float_t(fileprop.filament, 2), F(" m"));
if (fileprop_nopro.filament) {
buf.set(F("Filament used: "), p_float_t(fileprop_nopro.filament, 2), F(" m"));
DWINUI::Draw_String(20, 30, &buf);
}
if (fileprop.layer) {
buf.set(F("Layer height: "), p_float_t(fileprop.layer, 2), F(" mm"));
if (fileprop_nopro.layer) {
buf.set(F("Layer height: "), p_float_t(fileprop_nopro.layer, 2), F(" mm"));
DWINUI::Draw_String(20, 50, &buf);
}
if (fileprop.width) {
buf.set(F("Volume: "), p_float_t(fileprop.width, 1), 'x', p_float_t(fileprop.length, 1), 'x', p_float_t(fileprop.height, 1), F(" mm"));
if (fileprop_nopro.width) {
buf.set(F("Volume: "), p_float_t(fileprop_nopro.width, 1), 'x', p_float_t(fileprop_nopro.length, 1), 'x', p_float_t(fileprop_nopro.height, 1), F(" mm"));
DWINUI::Draw_String(20, 70, &buf);
}

if (!fileprop_nopro.thumbsize) {
const uint8_t xpos = ((DWIN_WIDTH) / 2) - 55, // 55 = iconW/2
ypos = ((DWIN_HEIGHT) / 2) - 125;
DWINUI::Draw_Icon(ICON_Info_0, xpos, ypos);
buf.set(PSTR("No " STRINGIFY(THUMBWIDTH) "x" STRINGIFY(THUMBHEIGHT) " Thumbnail"));
DWINUI::Draw_CenteredString(false, (DWINUI::fontid*3), DWINUI::textcolor, DWINUI::backcolor, 0, DWIN_WIDTH, (DWIN_HEIGHT / 2), &buf);
}
DWINUI::Draw_Button(BTN_Print, 26, 290);
DWINUI::Draw_Button(BTN_Cancel, 146, 290);
show();
Draw_Select_Highlight(true, 290);
if (fileprop_nopro.thumbsize) show();
Draw_Select_Highlight(false, 290);
DWIN_UpdateLCD();
}

void Preview::invalidate() {
fileprop.thumbsize = 0;
fileprop_nopro.thumbsize = 0;
}

bool Preview::valid() {
return !!fileprop.thumbsize;
return !!fileprop_nopro.thumbsize;
}

void Preview::show() {
const uint8_t xpos = ((DWIN_WIDTH) - fileprop.thumbwidth) / 2,
ypos = (205 - fileprop.thumbheight) / 2 + 87;
const uint8_t xpos = ((DWIN_WIDTH) - fileprop_nopro.thumbwidth) / 2,
ypos = (205 - fileprop_nopro.thumbheight) / 2 + 87;
DWIN_ICON_Show(xpos, ypos, 0x00);
}

Expand Down
39 changes: 26 additions & 13 deletions Marlin/src/lcd/e3v2/proui/gcode_preview.h
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
/**
* DWIN G-code thumbnail preview
* Author: Miguel A. Risco-Castillo
* version: 3.5.3
* Date: 2023/04/25
* Marlin 3D Printer Firmware
* Copyright (c) 2022 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the License, or
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* For commercial applications additional licenses can be requested
*/

#pragma once

void Preview_DrawFromSD();
void Preview_Invalidate();
bool Preview_Valid();
void Preview_Show();
/**
* DWIN G-code thumbnail preview
* Author: Miguel A. Risco-Castillo
* version: 3.1.2
* Date: 2022/09/03
*/

class Preview {
public:
static void drawFromSD();
static void invalidate();
static bool valid();
static void show();
private:
static bool hasPreview();
};

extern Preview preview;
Loading

0 comments on commit 7989e21

Please sign in to comment.