My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

draw_print_file.cpp 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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. #include "../../../../inc/MarlinConfigPre.h"
  23. #if HAS_TFT_LVGL_UI
  24. #include "../../../../MarlinCore.h"
  25. #include "lv_conf.h"
  26. //#include "../lvgl/src/lv_objx/lv_imgbtn.h"
  27. //#include "../lvgl/src/lv_objx/lv_img.h"
  28. //#include "../lvgl/src/lv_core/lv_disp.h"
  29. //#include "../lvgl/src/lv_core/lv_refr.h"
  30. #include "draw_ui.h"
  31. #include "../../../../sd/cardreader.h"
  32. static lv_obj_t * scr;
  33. static lv_obj_t *buttonPageUp, *buttonPageDown, *buttonBack,
  34. *buttonGcode[FILE_BTN_CNT], *labelPageUp[FILE_BTN_CNT], *buttonText[FILE_BTN_CNT];
  35. #define ID_P_UP 7
  36. #define ID_P_DOWN 8
  37. #define ID_P_RETURN 9
  38. int8_t curDirLever = 0;
  39. LIST_FILE list_file;
  40. DIR_OFFSET dir_offset[10];
  41. extern uint8_t public_buf[512];
  42. extern char public_buf_m[100];
  43. uint8_t sel_id = 0;
  44. #if ENABLED(SDSUPPORT)
  45. static uint8_t search_file() {
  46. int valid_name_cnt = 0;
  47. //char tmp[SHORT_NEME_LEN*MAX_DIR_LEVEL+1];
  48. list_file.Sd_file_cnt = 0;
  49. //list_file.Sd_file_offset = dir_offset[curDirLever].cur_page_first_offset;
  50. //root2.rewind();
  51. //SERIAL_ECHOLN(list_file.curDirPath);
  52. if (curDirLever != 0) card.cd(list_file.curDirPath);
  53. else card.cdroot(); // while(card.cdup());
  54. const uint16_t fileCnt = card.get_num_Files();
  55. for (uint16_t i = 0; i < fileCnt; i++) {
  56. if (list_file.Sd_file_cnt == list_file.Sd_file_offset) {
  57. const uint16_t nr = SD_ORDER(i, fileCnt);
  58. card.getfilename_sorted(nr);
  59. if (card.flag.filenameIsDir) {
  60. //SERIAL_ECHOLN(card.longest_filename);
  61. list_file.IsFolder[valid_name_cnt] = 1;
  62. }
  63. else {
  64. //SERIAL_ECHOLN(card.longFilename);
  65. list_file.IsFolder[valid_name_cnt] = 0;
  66. }
  67. #if 1
  68. //
  69. memset(list_file.file_name[valid_name_cnt], 0, strlen(list_file.file_name[valid_name_cnt]));
  70. strcpy(list_file.file_name[valid_name_cnt], list_file.curDirPath);
  71. strcat_P(list_file.file_name[valid_name_cnt], PSTR("/"));
  72. strcat(list_file.file_name[valid_name_cnt], card.filename);
  73. //
  74. memset(list_file.long_name[valid_name_cnt], 0, strlen(list_file.long_name[valid_name_cnt]));
  75. if (card.longFilename[0] == 0)
  76. strncpy(list_file.long_name[valid_name_cnt], card.filename, strlen(card.filename));
  77. else
  78. strncpy(list_file.long_name[valid_name_cnt], card.longFilename, strlen(card.longFilename));
  79. valid_name_cnt++;
  80. if (valid_name_cnt == 1)
  81. dir_offset[curDirLever].cur_page_first_offset = list_file.Sd_file_offset;
  82. if (valid_name_cnt >= FILE_NUM) {
  83. dir_offset[curDirLever].cur_page_last_offset = list_file.Sd_file_offset;
  84. list_file.Sd_file_offset++;
  85. break;
  86. }
  87. list_file.Sd_file_offset++;
  88. #endif
  89. }
  90. list_file.Sd_file_cnt++;
  91. }
  92. //card.closefile(false);
  93. return valid_name_cnt;
  94. }
  95. #endif // SDSUPPORT
  96. uint8_t have_pre_pic(char *path) {
  97. #if ENABLED(SDSUPPORT)
  98. char *ps1, *ps2, *cur_name = strrchr(path, '/');
  99. card.openFileRead(cur_name);
  100. card.read(public_buf, 512);
  101. ps1 = strstr((char *)public_buf, ";simage:");
  102. card.read(public_buf, 512);
  103. ps2 = strstr((char *)public_buf, ";simage:");
  104. if (ps1 || ps2) {
  105. card.closefile();
  106. return 1;
  107. }
  108. card.closefile();
  109. #endif
  110. return 0;
  111. }
  112. LV_IMG_DECLARE(bmp_pic_117x92);
  113. LV_IMG_DECLARE(bmp_pic_100x100);
  114. LV_IMG_DECLARE(bmp_pic);
  115. LV_IMG_DECLARE(bmp_pic_100x40);
  116. static void event_handler(lv_obj_t * obj, lv_event_t event) {
  117. uint8_t i, file_count = 0;
  118. //switch (obj->mks_obj_id)
  119. //{
  120. if (obj->mks_obj_id == ID_P_UP) {
  121. if (event == LV_EVENT_CLICKED) {
  122. }
  123. else if (event == LV_EVENT_RELEASED) {
  124. if (dir_offset[curDirLever].curPage > 0) {
  125. // 2015.05.19
  126. list_file.Sd_file_cnt = 0;
  127. if (dir_offset[curDirLever].cur_page_first_offset >= FILE_NUM)
  128. list_file.Sd_file_offset = dir_offset[curDirLever].cur_page_first_offset - FILE_NUM;
  129. #if ENABLED(SDSUPPORT)
  130. file_count = search_file();
  131. #endif
  132. if (file_count != 0) {
  133. dir_offset[curDirLever].curPage--;
  134. lv_obj_del(scr);
  135. disp_gcode_icon(file_count);
  136. }
  137. }
  138. }
  139. }
  140. else if (obj->mks_obj_id == ID_P_DOWN) {
  141. if (event == LV_EVENT_CLICKED) {
  142. }
  143. else if (event == LV_EVENT_RELEASED) {
  144. if (dir_offset[curDirLever].cur_page_last_offset > 0) {
  145. list_file.Sd_file_cnt = 0;
  146. list_file.Sd_file_offset = dir_offset[curDirLever].cur_page_last_offset + 1;
  147. #if ENABLED(SDSUPPORT)
  148. file_count = search_file();
  149. #endif
  150. if (file_count != 0) {
  151. dir_offset[curDirLever].curPage++;
  152. lv_obj_del(scr);
  153. disp_gcode_icon(file_count);
  154. }
  155. if (file_count < FILE_NUM)
  156. dir_offset[curDirLever].cur_page_last_offset = 0;
  157. }
  158. }
  159. }
  160. else if (obj->mks_obj_id == ID_P_RETURN) {
  161. if (event == LV_EVENT_CLICKED) {
  162. }
  163. else if (event == LV_EVENT_RELEASED) {
  164. if (curDirLever > 0) {
  165. int8_t *ch = (int8_t *)strrchr(list_file.curDirPath, '/');
  166. if (ch) {
  167. *ch = 0;
  168. #if ENABLED(SDSUPPORT)
  169. card.cdup();
  170. #endif
  171. dir_offset[curDirLever].curPage = 0;
  172. dir_offset[curDirLever].cur_page_first_offset = 0;
  173. dir_offset[curDirLever].cur_page_last_offset = 0;
  174. curDirLever--;
  175. list_file.Sd_file_offset = dir_offset[curDirLever].cur_page_first_offset;
  176. #if ENABLED(SDSUPPORT)
  177. file_count = search_file();
  178. #endif
  179. lv_obj_del(scr);
  180. disp_gcode_icon(file_count);
  181. }
  182. }
  183. else {
  184. lv_obj_del(scr);
  185. lv_draw_ready_print();
  186. }
  187. }
  188. }
  189. else {
  190. for (i = 0; i < FILE_BTN_CNT; i++) {
  191. if (obj->mks_obj_id == (i + 1)) {
  192. if (event == LV_EVENT_CLICKED) {
  193. }
  194. else if (event == LV_EVENT_RELEASED) {
  195. if (list_file.file_name[i][0] != 0) {
  196. if (list_file.IsFolder[i] == 1) {
  197. ZERO(list_file.curDirPath);
  198. strcpy(list_file.curDirPath, list_file.file_name[i]);
  199. curDirLever++;
  200. list_file.Sd_file_offset = dir_offset[curDirLever].cur_page_first_offset;
  201. #if ENABLED(SDSUPPORT)
  202. file_count = search_file();
  203. #endif
  204. lv_obj_del(scr);
  205. disp_gcode_icon(file_count);
  206. }
  207. else {
  208. sel_id = i;
  209. lv_obj_del(scr);
  210. lv_draw_dialog(DIALOG_TYPE_PRINT_FILE);
  211. }
  212. break;
  213. }
  214. }
  215. }
  216. }
  217. }
  218. }
  219. void lv_draw_print_file(void) {
  220. //uint8_t i;
  221. uint8_t file_count;
  222. if (disp_state_stack._disp_state[disp_state_stack._disp_index] != PRINT_FILE_UI) {
  223. disp_state_stack._disp_index++;
  224. disp_state_stack._disp_state[disp_state_stack._disp_index] = PRINT_FILE_UI;
  225. }
  226. disp_state = PRINT_FILE_UI;
  227. curDirLever = 0;
  228. dir_offset[curDirLever].curPage = 0;
  229. list_file.Sd_file_offset = 0;
  230. list_file.Sd_file_cnt = 0;
  231. ZERO(dir_offset);
  232. ZERO(list_file.IsFolder);
  233. ZERO(list_file.curDirPath);
  234. list_file.Sd_file_offset = dir_offset[curDirLever].cur_page_first_offset;
  235. #if ENABLED(SDSUPPORT)
  236. card.mount();
  237. file_count = search_file();
  238. #endif
  239. disp_gcode_icon(file_count);
  240. //lv_obj_t * labelPageUp = lv_label_create(buttonPageUp, NULL);
  241. //lv_obj_t * labelPageDown = lv_label_create(buttonPageDown, NULL);
  242. //lv_obj_t * label_Back = lv_label_create(buttonBack, NULL);
  243. /*
  244. if (gCfgItems.multiple_language != 0) {
  245. lv_label_set_text(labelPageUp, tool_menu.preheat);
  246. lv_obj_align(labelPageUp, buttonPageUp, LV_ALIGN_IN_BOTTOM_MID,0, BUTTON_TEXT_Y_OFFSET);
  247. lv_label_set_text(labelPageDown, tool_menu.extrude);
  248. lv_obj_align(labelPageDown, buttonPageDown, LV_ALIGN_IN_BOTTOM_MID,0, BUTTON_TEXT_Y_OFFSET);
  249. lv_label_set_text(label_Back, common_menu.text_back);
  250. lv_obj_align(label_Back, buttonBack, LV_ALIGN_IN_BOTTOM_MID,0, BUTTON_TEXT_Y_OFFSET);
  251. }
  252. */
  253. }
  254. void disp_gcode_icon(uint8_t file_num) {
  255. uint8_t i;
  256. scr = lv_obj_create(NULL, NULL);
  257. //static lv_style_t tool_style;
  258. lv_obj_set_style(scr, &tft_style_scr);
  259. lv_scr_load(scr);
  260. lv_obj_clean(scr);
  261. lv_obj_t * title = lv_label_create(scr, NULL);
  262. lv_obj_set_style(title, &tft_style_label_rel);
  263. lv_obj_set_pos(title, TITLE_XPOS, TITLE_YPOS);
  264. lv_label_set_text(title, creat_title_text());
  265. lv_refr_now(lv_refr_get_disp_refreshing());
  266. buttonPageUp = lv_imgbtn_create(scr, NULL);
  267. buttonPageDown = lv_imgbtn_create(scr, NULL);
  268. buttonBack = lv_imgbtn_create(scr, NULL);
  269. lv_obj_set_event_cb_mks(buttonPageUp, event_handler, ID_P_UP, "bmp_pageUp.bin", 0);
  270. lv_imgbtn_set_src(buttonPageUp, LV_BTN_STATE_REL, &bmp_pic_117x92);
  271. lv_imgbtn_set_src(buttonPageUp, LV_BTN_STATE_PR, &bmp_pic_117x92);
  272. lv_imgbtn_set_style(buttonPageUp, LV_BTN_STATE_PR, &tft_style_label_pre);
  273. lv_imgbtn_set_style(buttonPageUp, LV_BTN_STATE_REL, &tft_style_label_rel);
  274. lv_obj_clear_protect(buttonPageUp, LV_PROTECT_FOLLOW);
  275. #if 1
  276. lv_obj_set_event_cb_mks(buttonPageDown, event_handler, ID_P_DOWN, "bmp_pageDown.bin", 0);
  277. lv_imgbtn_set_src(buttonPageDown, LV_BTN_STATE_REL, &bmp_pic_117x92);
  278. lv_imgbtn_set_src(buttonPageDown, LV_BTN_STATE_PR, &bmp_pic_117x92);
  279. lv_imgbtn_set_style(buttonPageDown, LV_BTN_STATE_PR, &tft_style_label_pre);
  280. lv_imgbtn_set_style(buttonPageDown, LV_BTN_STATE_REL, &tft_style_label_rel);
  281. lv_obj_set_event_cb_mks(buttonBack, event_handler, ID_P_RETURN, "bmp_back.bin", 0);
  282. lv_imgbtn_set_src(buttonBack, LV_BTN_STATE_REL, &bmp_pic_117x92);
  283. lv_imgbtn_set_src(buttonBack, LV_BTN_STATE_PR, &bmp_pic_117x92);
  284. lv_imgbtn_set_style(buttonBack, LV_BTN_STATE_PR, &tft_style_label_pre);
  285. lv_imgbtn_set_style(buttonBack, LV_BTN_STATE_REL, &tft_style_label_rel);
  286. #endif
  287. lv_obj_set_pos(buttonPageUp, OTHER_BTN_XPIEL * 3 + INTERVAL_V * 4, titleHeight);
  288. lv_obj_set_pos(buttonPageDown, OTHER_BTN_XPIEL * 3 + INTERVAL_V * 4, titleHeight + OTHER_BTN_YPIEL + INTERVAL_H);
  289. lv_obj_set_pos(buttonBack, OTHER_BTN_XPIEL * 3 + INTERVAL_V * 4, titleHeight + OTHER_BTN_YPIEL * 2 + INTERVAL_H * 2);
  290. /*Create a label on the Image button*/
  291. lv_btn_set_layout(buttonPageUp, LV_LAYOUT_OFF);
  292. lv_btn_set_layout(buttonPageDown, LV_LAYOUT_OFF);
  293. lv_btn_set_layout(buttonBack, LV_LAYOUT_OFF);
  294. for (i = 0; i < FILE_BTN_CNT; i++) {
  295. /*
  296. if (seq) {
  297. j = (FILE_BTN_CNT-1) - i;
  298. back_flg = 1;
  299. }
  300. else {
  301. j = i;
  302. back_flg = 0;
  303. }
  304. */
  305. if (i >= file_num) break;
  306. #ifdef TFT35
  307. buttonGcode[i] = lv_imgbtn_create(scr, NULL);
  308. lv_imgbtn_set_style(buttonGcode[i], LV_BTN_STATE_PR, &tft_style_label_pre);
  309. lv_imgbtn_set_style(buttonGcode[i], LV_BTN_STATE_REL, &tft_style_label_rel);
  310. lv_obj_clear_protect(buttonGcode[i], LV_PROTECT_FOLLOW);
  311. lv_btn_set_layout(buttonGcode[i], LV_LAYOUT_OFF);
  312. ZERO(public_buf_m);
  313. cutFileName((char *)list_file.long_name[i], 16, 8, (char *)public_buf_m);
  314. if (list_file.IsFolder[i] == 1) {
  315. lv_obj_set_event_cb_mks(buttonGcode[i], event_handler, (i + 1), "bmp_dir.bin", 0);
  316. lv_imgbtn_set_src(buttonGcode[i], LV_BTN_STATE_REL, &bmp_pic);
  317. lv_imgbtn_set_src(buttonGcode[i], LV_BTN_STATE_PR, &bmp_pic);
  318. if (i < 3)
  319. lv_obj_set_pos(buttonGcode[i], BTN_X_PIXEL * i + INTERVAL_V * (i + 1), titleHeight);
  320. else
  321. lv_obj_set_pos(buttonGcode[i], BTN_X_PIXEL * (i - 3) + INTERVAL_V * ((i - 3) + 1), BTN_Y_PIXEL + INTERVAL_H + titleHeight);
  322. labelPageUp[i] = lv_label_create(buttonGcode[i], NULL);
  323. lv_obj_set_style(labelPageUp[i], &tft_style_label_rel);
  324. lv_label_set_text(labelPageUp[i], public_buf_m);
  325. lv_obj_align(labelPageUp[i], buttonGcode[i], LV_ALIGN_IN_BOTTOM_MID, 0, -5);
  326. }
  327. else {
  328. if (have_pre_pic((char *)list_file.file_name[i])) {
  329. lv_obj_set_event_cb_mks(buttonGcode[i], event_handler, (i + 1), list_file.file_name[i], 1);
  330. lv_imgbtn_set_src(buttonGcode[i], LV_BTN_STATE_REL, &bmp_pic_100x100);
  331. lv_imgbtn_set_src(buttonGcode[i], LV_BTN_STATE_PR, &bmp_pic_100x100);
  332. if (i < 3) {
  333. lv_obj_set_pos(buttonGcode[i], BTN_X_PIXEL * i + INTERVAL_V * (i + 1) + FILE_PRE_PIC_X_OFFSET, titleHeight + FILE_PRE_PIC_Y_OFFSET);
  334. buttonText[i] = lv_btn_create(scr, NULL);
  335. //lv_obj_set_event_cb(buttonText[i], event_handler);
  336. lv_btn_set_style(buttonText[i], LV_BTN_STATE_PR, &tft_style_label_pre);
  337. lv_btn_set_style(buttonText[i], LV_BTN_STATE_REL, &tft_style_label_rel);
  338. //lv_obj_set_style(buttonText[i], &tft_style_label_pre);
  339. //lv_obj_set_style(buttonText[i], &tft_style_label_rel);
  340. lv_obj_clear_protect(buttonText[i], LV_PROTECT_FOLLOW);
  341. lv_btn_set_layout(buttonText[i], LV_LAYOUT_OFF);
  342. //lv_obj_set_event_cb_mks(buttonText[i], event_handler,(i+10),NULL,0);
  343. //lv_imgbtn_set_src(buttonText[i], LV_BTN_STATE_REL, &bmp_pic_100x40);
  344. //lv_imgbtn_set_src(buttonText[i], LV_BTN_STATE_PR, &bmp_pic_100x40);
  345. lv_obj_set_pos(buttonText[i], BTN_X_PIXEL * i + INTERVAL_V * (i + 1) + FILE_PRE_PIC_X_OFFSET, titleHeight + FILE_PRE_PIC_Y_OFFSET + 100);
  346. lv_obj_set_size(buttonText[i], 100, 40);
  347. }
  348. else {
  349. lv_obj_set_pos(buttonGcode[i], BTN_X_PIXEL * (i - 3) + INTERVAL_V * ((i - 3) + 1) + FILE_PRE_PIC_X_OFFSET, BTN_Y_PIXEL + INTERVAL_H + titleHeight + FILE_PRE_PIC_Y_OFFSET);
  350. buttonText[i] = lv_btn_create(scr, NULL);
  351. //lv_obj_set_event_cb(buttonText[i], event_handler);
  352. lv_btn_set_style(buttonText[i], LV_BTN_STATE_PR, &tft_style_label_pre);
  353. lv_btn_set_style(buttonText[i], LV_BTN_STATE_REL, &tft_style_label_rel);
  354. //lv_imgbtn_set_style(buttonText[i], LV_BTN_STATE_REL, &tft_style_label_rel);
  355. lv_obj_clear_protect(buttonText[i], LV_PROTECT_FOLLOW);
  356. lv_btn_set_layout(buttonText[i], LV_LAYOUT_OFF);
  357. //lv_obj_set_event_cb_mks(buttonText[i], event_handler,(i+10),NULL,0);
  358. //lv_imgbtn_set_src(buttonText[i], LV_BTN_STATE_REL, &bmp_pic_100x40);
  359. //lv_imgbtn_set_src(buttonText[i], LV_BTN_STATE_PR, &bmp_pic_100x40);
  360. lv_obj_set_pos(buttonText[i], BTN_X_PIXEL * (i - 3) + INTERVAL_V * ((i - 3) + 1) + FILE_PRE_PIC_X_OFFSET, BTN_Y_PIXEL + INTERVAL_H + titleHeight + FILE_PRE_PIC_Y_OFFSET + 100);
  361. lv_obj_set_size(buttonText[i], 100, 40);
  362. }
  363. labelPageUp[i] = lv_label_create(buttonText[i], NULL);
  364. lv_obj_set_style(labelPageUp[i], &tft_style_label_rel);
  365. lv_label_set_text(labelPageUp[i], public_buf_m);
  366. lv_obj_align(labelPageUp[i], buttonText[i], LV_ALIGN_IN_BOTTOM_MID, 0, 0);
  367. }
  368. else {
  369. lv_obj_set_event_cb_mks(buttonGcode[i], event_handler, (i + 1), "bmp_file.bin", 0);
  370. lv_imgbtn_set_src(buttonGcode[i], LV_BTN_STATE_REL, &bmp_pic);
  371. lv_imgbtn_set_src(buttonGcode[i], LV_BTN_STATE_PR, &bmp_pic);
  372. if (i < 3)
  373. lv_obj_set_pos(buttonGcode[i], BTN_X_PIXEL * i + INTERVAL_V * (i + 1), titleHeight);
  374. else
  375. lv_obj_set_pos(buttonGcode[i], BTN_X_PIXEL * (i - 3) + INTERVAL_V * ((i - 3) + 1), BTN_Y_PIXEL + INTERVAL_H + titleHeight);
  376. labelPageUp[i] = lv_label_create(buttonGcode[i], NULL);
  377. lv_obj_set_style(labelPageUp[i], &tft_style_label_rel);
  378. lv_label_set_text(labelPageUp[i], public_buf_m);
  379. lv_obj_align(labelPageUp[i], buttonGcode[i], LV_ALIGN_IN_BOTTOM_MID, 0, -5);
  380. }
  381. }
  382. #else // !TFT35
  383. #endif // !TFT35
  384. }
  385. }
  386. void lv_open_gcode_file(char *path) {
  387. #if ENABLED(SDSUPPORT)
  388. uint32_t *ps4;
  389. int pre_sread_cnt;
  390. char *cur_name;
  391. cur_name = strrchr(path, '/');
  392. card.openFileRead(cur_name);
  393. card.read(public_buf, 512);
  394. ps4 = (uint32_t *)strstr((char *)public_buf, ";simage:");
  395. // Ignore the beginning message of gcode file
  396. if (ps4) {
  397. pre_sread_cnt = (uint32_t)ps4 - (uint32_t)((uint32_t *)(&public_buf[0]));
  398. card.setIndex(pre_sread_cnt);
  399. }
  400. #endif // SDSUPPORT
  401. }
  402. int ascii2dec_test(char *ascii) {
  403. int result = 0;
  404. if (ascii == 0) return 0;
  405. if (*(ascii) >= '0' && *(ascii) <= '9')
  406. result = *(ascii) - '0';
  407. else if (*(ascii) >= 'a' && *(ascii) <= 'f')
  408. result = *(ascii) - 'a' + 0x0a;
  409. else if (*(ascii) >= 'A' && *(ascii) <= 'F')
  410. result = *(ascii) - 'A' + 0x0a;
  411. else
  412. return 0;
  413. return result;
  414. }
  415. void lv_gcode_file_read(uint8_t *data_buf) {
  416. #if ENABLED(SDSUPPORT)
  417. uint16_t i = 0, j = 0, k = 0;
  418. uint16_t row_1 = 0;
  419. bool ignore_start = true;
  420. char temp_test[200];
  421. volatile uint16_t *p_index;
  422. memset(public_buf, 0, 200);
  423. while (card.isFileOpen()) {
  424. if (ignore_start) card.read(temp_test, 8); // line start -> ignore
  425. card.read(temp_test, 200); // data
  426. // \r;;gimage: we got the bit img, so stop here
  427. if (temp_test[1] == ';') {
  428. card.closefile();
  429. break;
  430. }
  431. for (i = 0; i < 200;) {
  432. public_buf[row_1 * 200 + 100 * k + j] = (char)(ascii2dec_test(&temp_test[i]) << 4 | ascii2dec_test(&temp_test[i + 1]));
  433. j++;
  434. i += 2;
  435. }
  436. uint16_t c = card.get();
  437. // check if we have more data or finished the line (CR)
  438. if (c == '\r') break;
  439. card.setIndex(card.getIndex());
  440. k++;
  441. j = 0;
  442. ignore_start = false;
  443. }
  444. #if ENABLED(TFT_LVGL_UI_SPI)
  445. for (i = 0; i < 200;) {
  446. p_index = (uint16_t *)(&public_buf[i]);
  447. //Color = (*p_index >> 8);
  448. //*p_index = Color | ((*p_index & 0xff) << 8);
  449. i += 2;
  450. if (*p_index == 0x0000) *p_index = LV_COLOR_BACKGROUND.full;
  451. }
  452. #else
  453. for (i = 0; i < 200;) {
  454. p_index = (uint16_t *)(&public_buf[i]);
  455. //Color = (*p_index >> 8);
  456. //*p_index = Color | ((*p_index & 0xff) << 8);
  457. i += 2;
  458. if (*p_index == 0x0000) *p_index = LV_COLOR_BACKGROUND.full; // 0x18C3;
  459. }
  460. #endif // TFT_LVGL_UI_SPI
  461. memcpy(data_buf, public_buf, 200);
  462. #endif // SDSUPPORT
  463. }
  464. void lv_close_gcode_file() {TERN_(SDSUPPORT, card.closefile());}
  465. void cutFileName(char *path, int len, int bytePerLine, char *outStr) {
  466. #if _LFN_UNICODE
  467. TCHAR *tmpFile;
  468. TCHAR *strIndex1 = 0, *strIndex2 = 0, *beginIndex;
  469. TCHAR secSeg[10] = {0};
  470. TCHAR gFileTail[4] = {'~', '.', 'g', '\0'};
  471. #else
  472. char *tmpFile;
  473. char *strIndex1 = 0, *strIndex2 = 0, *beginIndex;
  474. char secSeg[10] = {0};
  475. #endif
  476. if (path == 0 || len <= 3 || outStr == 0) return;
  477. tmpFile = path;
  478. #if _LFN_UNICODE
  479. strIndex1 = (WCHAR *)wcsstr((const WCHAR *)tmpFile, (const WCHAR *)'/');
  480. strIndex2 = (WCHAR *)wcsstr((const WCHAR *)tmpFile, (const WCHAR *)'.');
  481. #else
  482. strIndex1 = (char *)strrchr(tmpFile, '/');
  483. strIndex2 = (char *)strrchr(tmpFile, '.');
  484. #endif
  485. beginIndex = (strIndex1 != 0
  486. //&& (strIndex2 != 0) && (strIndex1 < strIndex2)
  487. ) ? strIndex1 + 1 : tmpFile;
  488. if (strIndex2 == 0 || (strIndex1 > strIndex2)) { // not gcode file
  489. #if _LFN_UNICODE
  490. if (wcslen(beginIndex) > len)
  491. wcsncpy(outStr, beginIndex, len);
  492. else
  493. wcscpy(outStr, beginIndex);
  494. #else
  495. if ((int)strlen(beginIndex) > len)
  496. strncpy(outStr, beginIndex, len);
  497. else
  498. strcpy(outStr, beginIndex);
  499. #endif
  500. }
  501. else { // gcode file
  502. if (strIndex2 - beginIndex > (len - 2)) {
  503. #if _LFN_UNICODE
  504. wcsncpy(outStr, (const WCHAR *)beginIndex, len - 3);
  505. wcscat(outStr, (const WCHAR *)gFileTail);
  506. #else
  507. //strncpy(outStr, beginIndex, len - 3);
  508. strncpy(outStr, beginIndex, len - 4);
  509. strcat_P(outStr, PSTR("~.g"));
  510. #endif
  511. }
  512. else {
  513. #if _LFN_UNICODE
  514. wcsncpy(outStr, (const WCHAR *)beginIndex, strIndex2 - beginIndex + 1);
  515. wcscat(outStr, (const WCHAR *)&gFileTail[3]);
  516. #else
  517. strncpy(outStr, beginIndex, strIndex2 - beginIndex + 1);
  518. strcat_P(outStr, PSTR("g"));
  519. #endif
  520. }
  521. }
  522. #if _LFN_UNICODE
  523. if (wcslen(outStr) > bytePerLine) {
  524. wcscpy(secSeg, (const WCHAR *)&outStr[bytePerLine]);
  525. outStr[bytePerLine] = '\n';
  526. outStr[bytePerLine + 1] = '\0';
  527. wcscat(outStr, (const WCHAR *)secSeg);
  528. }
  529. #else
  530. if ((int)strlen(outStr) > bytePerLine) {
  531. strcpy(secSeg, &outStr[bytePerLine]);
  532. outStr[bytePerLine] = '\n';
  533. outStr[bytePerLine + 1] = '\0';
  534. strcat(outStr, secSeg);
  535. }
  536. else {
  537. strcat_P(outStr, PSTR("\n"));
  538. }
  539. #endif
  540. }
  541. void lv_clear_print_file() { lv_obj_del(scr); }
  542. #endif // HAS_TFT_LVGL_UI