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.9KB

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