My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

gcode_preview.cpp 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2022 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * DWIN G-code thumbnail preview
  24. * Author: Miguel A. Risco-Castillo
  25. * version: 2.1
  26. * Date: 2021/06/19
  27. *
  28. * Modded for JYERSUI by LCH-77
  29. */
  30. #include "../../../inc/MarlinConfigPre.h"
  31. #if ENABLED(DWIN_CREALITY_LCD_JYERSUI)
  32. #include "dwin_defines.h"
  33. #if HAS_GCODE_PREVIEW
  34. #include "../../../core/types.h"
  35. #include "../../marlinui.h"
  36. #include "../../../sd/cardreader.h"
  37. #include "../../../MarlinCore.h" // for wait_for_user
  38. #include "dwin_lcd.h"
  39. #include "dwinui.h"
  40. #include "dwin.h"
  41. #include "base64.hpp"
  42. #include "gcode_preview.h"
  43. typedef struct {
  44. char name[13] = ""; //8.3 + null
  45. uint32_t thumbstart = 0;
  46. int thumbsize = 0;
  47. int thumbheight = 0;
  48. int thumbwidth = 0;
  49. uint8_t *thumbdata = nullptr;
  50. float time = 0;
  51. float filament = 0;
  52. float layer = 0;
  53. float width = 0;
  54. float height = 0;
  55. float length = 0;
  56. void setname(const char * const fn);
  57. void clear();
  58. } fileprop_t;
  59. fileprop_t fileprop;
  60. void fileprop_t::setname(const char * const fn) {
  61. const uint8_t len = _MIN(sizeof(name) - 1, strlen(fn));
  62. memcpy(&name[0], fn, len);
  63. name[len] = '\0';
  64. }
  65. void fileprop_t::clear() {
  66. fileprop.name[0] = '\0';
  67. fileprop.thumbstart = 0;
  68. fileprop.thumbsize = 0;
  69. fileprop.thumbheight = 0;
  70. fileprop.thumbwidth = 0;
  71. fileprop.thumbdata = nullptr;
  72. fileprop.time = 0;
  73. fileprop.filament = 0;
  74. fileprop.layer = 0;
  75. fileprop.height = 0;
  76. fileprop.width = 0;
  77. fileprop.length = 0;
  78. }
  79. void Get_Value(char *buf, const char * const key, float &value) {
  80. char num[10] = "";
  81. char * posptr = 0;
  82. uint8_t i = 0;
  83. if (!!value) return;
  84. posptr = strstr(buf, key);
  85. if (posptr != nullptr) {
  86. while (i < sizeof(num)) {
  87. char c = posptr[0];
  88. if (!ISEOL(c) && (c != 0)) {
  89. if ((c > 47 && c < 58) || (c == '.')) num[i++] = c;
  90. posptr++;
  91. }
  92. else {
  93. num[i] = '\0';
  94. value = atof(num);
  95. return;
  96. }
  97. }
  98. }
  99. }
  100. bool Has_Preview() {
  101. const char * tbstart = "; thumbnail begin 230x180";
  102. char * posptr = 0;
  103. uint8_t nbyte = 1;
  104. uint32_t indx = 0;
  105. char buf[256];
  106. float tmp = 0;
  107. fileprop.clear();
  108. fileprop.setname(card.filename);
  109. card.openFileRead(fileprop.name);
  110. while ((nbyte > 0) && (indx < 4 * sizeof(buf)) && !fileprop.thumbstart) {
  111. nbyte = card.read(buf, sizeof(buf) - 1);
  112. if (nbyte > 0) {
  113. buf[nbyte] = '\0';
  114. Get_Value(buf, ";TIME:", fileprop.time);
  115. Get_Value(buf, ";Filament used:", fileprop.filament);
  116. Get_Value(buf, ";Layer height:", fileprop.layer);
  117. Get_Value(buf, ";MINX:", tmp);
  118. Get_Value(buf, ";MAXX:", fileprop.width);
  119. fileprop.width -= tmp;
  120. tmp = 0;
  121. Get_Value(buf, ";MINY:", tmp);
  122. Get_Value(buf, ";MAXY:", fileprop.length);
  123. fileprop.length -= tmp;
  124. tmp = 0;
  125. Get_Value(buf, ";MINZ:", tmp);
  126. Get_Value(buf, ";MAXZ:", fileprop.height);
  127. fileprop.height -= tmp;
  128. posptr = strstr(buf, tbstart);
  129. if (posptr != NULL) {
  130. fileprop.thumbstart = indx + (posptr - &buf[0]);
  131. }
  132. else {
  133. indx += _MAX(10, nbyte - (signed)strlen(tbstart));
  134. card.setIndex(indx);
  135. }
  136. }
  137. }
  138. if (!fileprop.thumbstart) {
  139. card.closefile();
  140. LCD_MESSAGE_F("Thumbnail not found");
  141. return 0;
  142. }
  143. // Get the size of the thumbnail
  144. card.setIndex(fileprop.thumbstart + strlen(tbstart));
  145. for (uint8_t i = 0; i < 16; i++) {
  146. char c = card.get();
  147. if (!ISEOL(c)) {
  148. buf[i] = c;
  149. }
  150. else {
  151. buf[i] = '\0';
  152. break;
  153. }
  154. }
  155. fileprop.thumbsize = atoi(buf);
  156. // Exit if there isn't a thumbnail
  157. if (!fileprop.thumbsize) {
  158. card.closefile();
  159. LCD_MESSAGE_F("Invalid Thumbnail Size");
  160. return 0;
  161. }
  162. uint16_t readed = 0;
  163. uint8_t buf64[fileprop.thumbsize];
  164. fileprop.thumbdata = new uint8_t[3 + 3 * (fileprop.thumbsize / 4)]; // Reserve space for the JPEG thumbnail
  165. while (readed < fileprop.thumbsize) {
  166. uint8_t c = card.get();
  167. if (!ISEOL(c) && (c != ';') && (c != ' ')) {
  168. buf64[readed] = c;
  169. readed++;
  170. }
  171. }
  172. card.closefile();
  173. buf64[readed] = 0;
  174. fileprop.thumbsize = decode_base64(buf64, fileprop.thumbdata); card.closefile();
  175. DWINUI::WriteToSRAM(0x00, fileprop.thumbsize, fileprop.thumbdata);
  176. delete[] fileprop.thumbdata;
  177. return true;
  178. }
  179. void Preview_DrawFromSD() {
  180. bool _has_preview = Has_Preview();
  181. CrealityDWIN.Popup_Handler(PrintConfirm, _has_preview);
  182. if (_has_preview) {
  183. char buf[46];
  184. char str_1[6] = "", str_2[6] = "", str_3[6] = "";
  185. // DWIN_Draw_Rectangle(1, Def_Background_Color, 0, 0, DWIN_WIDTH, STATUS_Y - 1);
  186. if (fileprop.time) {
  187. sprintf_P(buf, PSTR("Estimated time: %i:%02i"), (uint16_t)fileprop.time / 3600, ((uint16_t)fileprop.time % 3600) / 60);
  188. DWINUI::Draw_String(20, 10, buf);
  189. }
  190. if (fileprop.filament) {
  191. sprintf_P(buf, PSTR("Filament used: %s m"), dtostrf(fileprop.filament, 1, 2, str_1));
  192. DWINUI::Draw_String(20, 30, buf);
  193. }
  194. if (fileprop.layer) {
  195. sprintf_P(buf, PSTR("Layer height: %s mm"), dtostrf(fileprop.layer, 1, 2, str_1));
  196. DWINUI::Draw_String(20, 50, buf);
  197. }
  198. if (fileprop.width) {
  199. sprintf_P(buf, PSTR("Volume: %sx%sx%s mm"), dtostrf(fileprop.width, 1, 1, str_1), dtostrf(fileprop.length, 1, 1, str_2), dtostrf(fileprop.height, 1, 1, str_3));
  200. DWINUI::Draw_String(20, 70, buf);
  201. }
  202. // DWINUI::Draw_Button(BTN_Print, 26, 290);
  203. // DWINUI::Draw_Button(BTN_Cancel, 146, 290);
  204. DWIN_ICON_Show(0, 0, 1, 21, 90, 0x00);
  205. // Draw_Select_Highlight(true, 290);
  206. DWIN_UpdateLCD();
  207. }
  208. // else {
  209. // HMI_flag.select_flag = 1;
  210. // wait_for_user = false;
  211. // }
  212. }
  213. bool Preview_Valid() {
  214. return !!fileprop.thumbstart;
  215. }
  216. void Preview_Reset() {
  217. fileprop.thumbsize = 0;
  218. }
  219. #endif // HAS_GCODE_PREVIEW
  220. #endif // DWIN_LCD_PROUI