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 22KB

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