From eda5a884c64ef0646d90b8744dabbe43adc4c619 Mon Sep 17 00:00:00 2001 From: Victor Oliveira Date: Tue, 29 Dec 2020 01:07:11 -0300 Subject: [PATCH] LVGL G-code preview. Legacy MKS WiFi Cura plugin compatibility (#20589) --- Marlin/src/core/macros.h | 1 + Marlin/src/feature/e_parser.h | 1 - Marlin/src/gcode/queue.cpp | 2 -- Marlin/src/lcd/extui/lib/mks_ui/draw_print_file.cpp | 12 ++++++++++-- .../lcd/extui/lib/mks_ui/tft_lvgl_configuration.cpp | 12 +++++++++--- 5 files changed, 20 insertions(+), 8 deletions(-) diff --git a/Marlin/src/core/macros.h b/Marlin/src/core/macros.h index 058008646f..56e80b87dc 100644 --- a/Marlin/src/core/macros.h +++ b/Marlin/src/core/macros.h @@ -216,6 +216,7 @@ #define ANY_BUTTON(V...) DO(BTNEX,||,V) #define WITHIN(N,L,H) ((N) >= (L) && (N) <= (H)) +#define ISEOL(C) ((C) == '\n' || (C) == '\r') #define NUMERIC(a) WITHIN(a, '0', '9') #define DECIMAL(a) (NUMERIC(a) || a == '.') #define HEXCHR(a) (NUMERIC(a) ? (a) - '0' : WITHIN(a, 'a', 'f') ? ((a) - 'a' + 10) : WITHIN(a, 'A', 'F') ? ((a) - 'A' + 10) : -1) diff --git a/Marlin/src/feature/e_parser.h b/Marlin/src/feature/e_parser.h index 0bb0253149..a4c07de465 100644 --- a/Marlin/src/feature/e_parser.h +++ b/Marlin/src/feature/e_parser.h @@ -76,7 +76,6 @@ public: FORCE_INLINE static void disable() { enabled = false; } FORCE_INLINE static void update(State &state, const uint8_t c) { - #define ISEOL(C) ((C) == '\n' || (C) == '\r') switch (state) { case EP_RESET: switch (c) { diff --git a/Marlin/src/gcode/queue.cpp b/Marlin/src/gcode/queue.cpp index d23e9ee07f..98fe91db40 100644 --- a/Marlin/src/gcode/queue.cpp +++ b/Marlin/src/gcode/queue.cpp @@ -158,8 +158,6 @@ bool GCodeQueue::_enqueue(const char* cmd, bool say_ok/*=false*/ return true; } -#define ISEOL(C) ((C) == '\n' || (C) == '\r') - /** * Enqueue with Serial Echo * Return true if the command was consumed diff --git a/Marlin/src/lcd/extui/lib/mks_ui/draw_print_file.cpp b/Marlin/src/lcd/extui/lib/mks_ui/draw_print_file.cpp index 96e322bbec..38b62db3b4 100644 --- a/Marlin/src/lcd/extui/lib/mks_ui/draw_print_file.cpp +++ b/Marlin/src/lcd/extui/lib/mks_ui/draw_print_file.cpp @@ -417,12 +417,20 @@ void lv_gcode_file_read(uint8_t *data_buf) { } uint16_t c = card.get(); - // check if we have more data or finished the line (CR) - if (c == '\r') break; + // 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;) { diff --git a/Marlin/src/lcd/extui/lib/mks_ui/tft_lvgl_configuration.cpp b/Marlin/src/lcd/extui/lib/mks_ui/tft_lvgl_configuration.cpp index 8ce317c571..f13a4b36cf 100644 --- a/Marlin/src/lcd/extui/lib/mks_ui/tft_lvgl_configuration.cpp +++ b/Marlin/src/lcd/extui/lib/mks_ui/tft_lvgl_configuration.cpp @@ -369,8 +369,9 @@ lv_fs_res_t spi_flash_tell_cb(lv_fs_drv_t * drv, void * file_p, uint32_t * pos_p } //sd +extern uint8_t public_buf[512]; char *cur_namefff; -uint32_t sd_read_base_addr = 0,sd_read_addr_offset = 0; +uint32_t sd_read_base_addr = 0, sd_read_addr_offset = 0, small_image_size = 409; lv_fs_res_t sd_open_cb (lv_fs_drv_t * drv, void * file_p, const char * path, lv_fs_mode_t mode) { //cur_namefff = strrchr(path, '/'); char name_buf[100]; @@ -381,6 +382,11 @@ lv_fs_res_t sd_open_cb (lv_fs_drv_t * drv, void * file_p, const char * path, lv_ sd_read_base_addr = lv_open_gcode_file((char *)name_buf); sd_read_addr_offset = sd_read_base_addr; if (sd_read_addr_offset == UINT32_MAX) return LV_FS_RES_NOT_EX; + // find small image size + card.read(public_buf, 512); + public_buf[511] = '\0'; + char* eol = strpbrk((const char*)public_buf, "\n\r"); + small_image_size = (uintptr_t)eol - (uintptr_t)((uint32_t *)(&public_buf[0])) + 1; return LV_FS_RES_OK; } @@ -406,14 +412,14 @@ lv_fs_res_t sd_read_cb (lv_fs_drv_t * drv, void * file_p, void * buf, uint32_t b } lv_fs_res_t sd_seek_cb(lv_fs_drv_t * drv, void * file_p, uint32_t pos) { - sd_read_addr_offset = sd_read_base_addr + (pos - 4) / 200 * 409; + sd_read_addr_offset = sd_read_base_addr + (pos - 4) / 200 * small_image_size; lv_gcode_file_seek(sd_read_addr_offset); return LV_FS_RES_OK; } lv_fs_res_t sd_tell_cb(lv_fs_drv_t * drv, void * file_p, uint32_t * pos_p) { if (sd_read_addr_offset) *pos_p = 0; - else *pos_p = (sd_read_addr_offset - sd_read_base_addr) / 409 * 200 + 4; + else *pos_p = (sd_read_addr_offset - sd_read_base_addr) / small_image_size * 200 + 4; return LV_FS_RES_OK; }