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