diff --git a/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp b/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp index a5a8f7cbe41fd..2248ef334ffd8 100644 --- a/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp +++ b/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp @@ -298,10 +298,8 @@ void lv_fill_rect(lv_coord_t x1, lv_coord_t y1, lv_coord_t x2, lv_coord_t y2, lv W25QXX.init(SPI_QUARTER_SPEED); } -#define TICK_CYCLE 1 - -uint16_t getTickDiff(uint16_t curTick, uint16_t lastTick) { - return TICK_CYCLE * (lastTick <= curTick ? (curTick - lastTick) : (0xFFFFFFFF - lastTick + curTick)); +uint16_t getTickDiff(const uint16_t curTick, const uint16_t lastTick) { + return (TICK_CYCLE) * (lastTick <= curTick ? (curTick - lastTick) : (0xFFFFFFFF - lastTick + curTick)); } static bool get_point(int16_t *x, int16_t *y) { diff --git a/Marlin/src/lcd/extui/mks_ui/wifi_module.cpp b/Marlin/src/lcd/extui/mks_ui/wifi_module.cpp index 215281d450a47..b389109975830 100644 --- a/Marlin/src/lcd/extui/mks_ui/wifi_module.cpp +++ b/Marlin/src/lcd/extui/mks_ui/wifi_module.cpp @@ -55,11 +55,11 @@ #define WIFI_IO1_SET() WRITE(WIFI_IO1_PIN, HIGH); #define WIFI_IO1_RESET() WRITE(WIFI_IO1_PIN, LOW); -extern uint8_t Explore_Disk(char *path, uint8_t recu_level); +uint8_t Explore_Disk(const char * const path, const uint8_t recu_level, const bool with_longnames); extern uint8_t commands_in_queue; extern uint8_t sel_id; -extern uint16_t getTickDiff(uint16_t curTick, uint16_t lastTick); +uint16_t getTickDiff(const uint16_t curTick, const uint16_t lastTick); volatile SZ_USART_FIFO WifiRxFifo; @@ -114,33 +114,34 @@ extern CLOUD_PARA cloud_para; extern bool once_flag, flash_preview_begin, default_preview_flg, gcode_preview_over; extern bool flash_dma_mode; -uint32_t getWifiTick() { return millis(); } +millis_t getWifiTick() { return millis(); } -uint32_t getWifiTickDiff(int32_t lastTick, int32_t curTick) { - return (lastTick <= curTick ? curTick - lastTick : 0xFFFFFFFF - lastTick + curTick) * TICK_CYCLE; +millis_t getWifiTickDiff(const millis_t lastTick, const millis_t curTick) { + return (TICK_CYCLE) * (lastTick <= curTick ? curTick - lastTick : 0xFFFFFFFFUL - lastTick + curTick); } -void wifi_delay(int n) { - const uint32_t start = getWifiTick(); - while (getWifiTickDiff(start, getWifiTick()) < (uint32_t)n) +void wifi_delay(const uint16_t n) { + const millis_t start = getWifiTick(); + while (getWifiTickDiff(start, getWifiTick()) < millis_t(n)) hal.watchdog_refresh(); } void wifi_reset() { - uint32_t start = getWifiTick(); + const millis_t start = getWifiTick(); WIFI_RESET(); while (getWifiTickDiff(start, getWifiTick()) < 500) { /* nada */ } WIFI_SET(); } -void mount_file_sys(uint8_t disk_type) { - if (disk_type == FILE_SYS_SD) { - TERN_(SDSUPPORT, card.mount()); - } - else if (disk_type == FILE_SYS_USB) { +void mount_file_sys(const uint8_t disk_type) { + switch (disk_type) { + case FILE_SYS_SD: TERN_(SDSUPPORT, card.mount()); break; + case FILE_SYS_USB: break; } } +#define ILLEGAL_CHAR_REPLACE 0x5F // '_' + static bool longName2DosName(const char *longName, char *dosName) { uint8_t i = FILENAME_LENGTH; while (i) dosName[--i] = '\0'; @@ -152,18 +153,24 @@ static bool longName2DosName(const char *longName, char *dosName) { strcat_P(dosName, PSTR(".GCO")); return dosName[0] != '\0'; } + + // Fail for illegal characters + if (c < 0x21 || c == 0x7F) // Check size, non-printable characters + c = ILLEGAL_CHAR_REPLACE; // replace non-printable chars with underscore '_' else { - // Fail for illegal characters PGM_P p = PSTR("|<>^+=?/[];,*\"\\"); - while (uint8_t b = pgm_read_byte(p++)) if (b == c) return false; - if (c < 0x21 || c == 0x7F) return false; // Check size, non-printable characters - dosName[i++] = (c < 'a' || c > 'z') ? (c) : (c + ('A' - 'a')); // Uppercase required for 8.3 name + while (const uint8_t b = pgm_read_byte(p++)) + if (b == c) c = ILLEGAL_CHAR_REPLACE; // replace illegal chars with underscore '_' } + + dosName[i++] = (c < 'a' || c > 'z') ? (c) : (c + ('A' - 'a')); // Uppercase required for 8.3 name + if (i >= 5) { strcat_P(dosName, PSTR("~1.GCO")); return dosName[0] != '\0'; } } + return dosName[0] != '\0'; // Return true if any name was set } @@ -562,8 +569,8 @@ static bool longName2DosName(const char *longName, char *dosName) { #if ENABLED(MKS_WIFI_MODULE) - int raw_send_to_wifi(uint8_t *buf, int len) { - if (buf == 0 || len <= 0) return 0; + int raw_send_to_wifi(uint8_t * const buf, const int len) { + if (buf == nullptr || len <= 0) return 0; for (int i = 0; i < len; i++) WIFISERIAL.write(*(buf + i)); return len; } @@ -701,13 +708,13 @@ int package_to_wifi(WIFI_RET_TYPE type, uint8_t *buf, int len) { return 1; } +int send_to_wifi(uint8_t * const buf, const int len) { return package_to_wifi(WIFI_TRANS_INF, buf, len); } -#define SEND_OK_TO_WIFI send_to_wifi((uint8_t *)"ok\r\n", strlen("ok\r\n")) -int send_to_wifi(uint8_t *buf, int len) { return package_to_wifi(WIFI_TRANS_INF, buf, len); } +inline void send_ok_to_wifi() { send_to_wifi((uint8_t *)"ok\r\n", strlen("ok\r\n")); } void set_cur_file_sys(int fileType) { gCfgItems.fileSysType = fileType; } -void get_file_list(char *path) { +void get_file_list(const char * const path, const bool with_longnames) { if (!path) return; if (gCfgItems.fileSysType == FILE_SYS_SD) { @@ -716,7 +723,7 @@ void get_file_list(char *path) { else if (gCfgItems.fileSysType == FILE_SYS_USB) { // udisk } - Explore_Disk(path, 0); + Explore_Disk(path, 0, with_longnames); } char wait_ip_back_flag = 0; @@ -795,7 +802,7 @@ typedef struct { uint8_t tail; } ESP_PROTOC_FRAME; -static int cut_msg_head(uint8_t *msg, uint16_t msgLen, uint16_t cutLen) { +static int cut_msg_head(uint8_t * const msg, const uint16_t msgLen, uint16_t cutLen) { if (msgLen < cutLen) return 0; else if (msgLen == cutLen) { @@ -811,25 +818,31 @@ static int cut_msg_head(uint8_t *msg, uint16_t msgLen, uint16_t cutLen) { return msgLen - cutLen; } -uint8_t Explore_Disk(char *path , uint8_t recu_level) { - char tmp[200]; +uint8_t Explore_Disk(const char * const path, const uint8_t recu_level, const bool with_longnames) { char Fstream[200]; if (!path) return 0; const uint8_t fileCnt = card.get_num_Files(); + MediaFile file; + MediaFile *diveDir; for (uint8_t i = 0; i < fileCnt; i++) { card.getfilename_sorted(SD_ORDER(i, fileCnt)); - ZERO(tmp); - strcpy(tmp, card.filename); ZERO(Fstream); - strcpy(Fstream, tmp); + strcpy(Fstream, card.filename); if (card.flag.filenameIsDir && recu_level <= 10) strcat_P(Fstream, PSTR(".DIR")); + strcat_P(Fstream, PSTR(" 0")); // report 0 file size + + if (with_longnames) { + strcat_P(Fstream, PSTR(" ")); + strcat_P(Fstream, card.longest_filename()); + } + strcat_P(Fstream, PSTR("\r\n")); send_to_wifi((uint8_t*)Fstream, strlen(Fstream)); } @@ -837,434 +850,449 @@ uint8_t Explore_Disk(char *path , uint8_t recu_level) { return fileCnt; } -static void wifi_gcode_exec(uint8_t *cmd_line) { +static void wifi_gcode_exec(uint8_t * const cmd_line) { int8_t tempBuf[100] = { 0 }; - uint8_t *tmpStr = 0; int cmd_value; volatile int print_rate; - if (strchr((char *)cmd_line, '\n') && (strchr((char *)cmd_line, 'G') || strchr((char *)cmd_line, 'M') || strchr((char *)cmd_line, 'T'))) { - tmpStr = (uint8_t *)strchr((char *)cmd_line, '\n'); - if (tmpStr) *tmpStr = '\0'; - - tmpStr = (uint8_t *)strchr((char *)cmd_line, '\r'); - if (tmpStr) *tmpStr = '\0'; - tmpStr = (uint8_t *)strchr((char *)cmd_line, '*'); - if (tmpStr) *tmpStr = '\0'; + // Only accept commands with a linefeed + char * const lfStr = strchr((char *)cmd_line, '\n'); + if (!lfStr) return; + + // Only accept commands with G, M, or T somewhere + const char * const gStr = strchr((char *)cmd_line, 'G'); + const char * const mStr = strchr((char *)cmd_line, 'M'); + const char * const tStr = strchr((char *)cmd_line, 'T'); + if (!(gStr || mStr || tStr)) return; + + // Replace the linefeed with nul terminator + *lfStr = '\0'; + + // Terminate on the first cr, if any + char * const crStr = strchr((char *)cmd_line, '\r'); + if (crStr) *crStr = '\0'; + + // Terminate on the checksum marker, if any + char * const aStr = strchr((char *)cmd_line, '*'); + if (aStr) *aStr = '\0'; + + // Handle some M commands here + if (mStr) { + cmd_value = atoi(mStr + 1); + char * const spStr = strchr(mStr, ' '); + + switch (cmd_value) { + + case 20: // M20: Print SD / µdisk file + file_writer.fileTransfer = 0; + if (uiCfg.print_state == IDLE) { + int index = 0; + if (spStr == nullptr) { + gCfgItems.fileSysType = FILE_SYS_SD; + send_to_wifi((uint8_t *)(STR_BEGIN_FILE_LIST "\r\n"), strlen(STR_BEGIN_FILE_LIST "\r\n")); + get_file_list("0:/", false); + send_to_wifi((uint8_t *)(STR_END_FILE_LIST "\r\n"), strlen(STR_END_FILE_LIST "\r\n")); + send_ok_to_wifi(); + break; + } - tmpStr = (uint8_t *)strchr((char *)cmd_line, 'M'); - if (tmpStr) { - cmd_value = atoi((char *)(tmpStr + 1)); - tmpStr = (uint8_t *)strchr((char *)tmpStr, ' '); + while (mStr[index] == ' ') index++; - switch (cmd_value) { + if (gCfgItems.wifi_type == ESP_WIFI) { + char * const path = (char *)tempBuf; + if (strlen(&mStr[index]) < 80) { + send_to_wifi((uint8_t *)(STR_BEGIN_FILE_LIST "\r\n"), strlen(STR_BEGIN_FILE_LIST "\r\n")); - case 20: // M20: Print SD / µdisk file - file_writer.fileTransfer = 0; - if (uiCfg.print_state == IDLE) { - int index = 0; + if (strncmp(&mStr[index], "1:", 2) == 0) + gCfgItems.fileSysType = FILE_SYS_SD; + else if (strncmp(&mStr[index], "0:", 2) == 0) + gCfgItems.fileSysType = FILE_SYS_USB; - if (tmpStr == 0) { - gCfgItems.fileSysType = FILE_SYS_SD; - send_to_wifi((uint8_t *)(STR_BEGIN_FILE_LIST "\r\n"), strlen(STR_BEGIN_FILE_LIST "\r\n")); - get_file_list((char *)"0:/"); + strcpy(path, &mStr[index]); + const bool with_longnames = strchr(mStr, 'L') != nullptr; + get_file_list(path, with_longnames); send_to_wifi((uint8_t *)(STR_END_FILE_LIST "\r\n"), strlen(STR_END_FILE_LIST "\r\n")); - SEND_OK_TO_WIFI; - break; } + send_ok_to_wifi(); + } + } + break; - while (tmpStr[index] == ' ') index++; + case 21: send_ok_to_wifi(); break; // Init SD card - if (gCfgItems.wifi_type == ESP_WIFI) { - char *path = (char *)tempBuf; + case 23: + // Select the file + if (uiCfg.print_state == IDLE) { + int index = 0; + while (mStr[index] == ' ') index++; - if (strlen((char *)&tmpStr[index]) < 80) { - send_to_wifi((uint8_t *)(STR_BEGIN_FILE_LIST "\r\n"), strlen(STR_BEGIN_FILE_LIST "\r\n")); + if (strstr_P(&mStr[index], PSTR(".g")) || strstr_P(&mStr[index], PSTR(".G"))) { + if (strlen(&mStr[index]) < 80) { + ZERO(list_file.file_name[sel_id]); + ZERO(list_file.long_name[sel_id]); + uint8_t has_path_selected = 0; - if (strncmp((char *)&tmpStr[index], "1:", 2) == 0) + if (gCfgItems.wifi_type == ESP_WIFI) { + if (strncmp_P(&mStr[index], PSTR("1:"), 2) == 0) { gCfgItems.fileSysType = FILE_SYS_SD; - else if (strncmp((char *)&tmpStr[index], "0:", 2) == 0) + has_path_selected = 1; + } + else if (strncmp_P(&mStr[index], PSTR("0:"), 2) == 0) { gCfgItems.fileSysType = FILE_SYS_USB; + has_path_selected = 1; + } + else if (mStr[index] != '/') + strcat_P((char *)list_file.file_name[sel_id], PSTR("/")); + + if (file_writer.fileTransfer == 1) { + char dosName[FILENAME_LENGTH]; + uint8_t fileName[sizeof(list_file.file_name[sel_id])]; + fileName[0] = '\0'; + if (has_path_selected == 1) { + strcat((char *)fileName, &mStr[index + 3]); + strcat_P((char *)list_file.file_name[sel_id], PSTR("/")); + } + else strcat((char *)fileName, &mStr[index]); + if (!longName2DosName((const char *)fileName, dosName)) + strcpy_P(list_file.file_name[sel_id], PSTR("notValid")); + strcat((char *)list_file.file_name[sel_id], dosName); + strcat((char *)list_file.long_name[sel_id], (const char *)fileName); + } + else { + strcat((char *)list_file.file_name[sel_id], &mStr[index]); + strcat((char *)list_file.long_name[sel_id], &mStr[index]); + } + + } + else + strcpy(list_file.file_name[sel_id], &mStr[index]); + + char *cur_name = strrchr(list_file.file_name[sel_id],'/'); + + card.openFileRead(cur_name); - strcpy((char *)path, (char *)&tmpStr[index]); - get_file_list(path); - send_to_wifi((uint8_t *)(STR_END_FILE_LIST "\r\n"), strlen(STR_END_FILE_LIST "\r\n")); + if (card.isFileOpen()) + send_to_wifi((uint8_t *)"File selected\r\n", strlen("File selected\r\n")); + else { + send_to_wifi((uint8_t *)"file.open failed\r\n", strlen("file.open failed\r\n")); + strcpy_P(list_file.file_name[sel_id], PSTR("notValid")); } - SEND_OK_TO_WIFI; + send_ok_to_wifi(); } } - break; - - case 21: SEND_OK_TO_WIFI; break; // Init SD card + } + break; - case 23: - // Select the file + case 24: + if (strcmp_P(list_file.file_name[sel_id], PSTR("notValid")) != 0) { if (uiCfg.print_state == IDLE) { - int index = 0; - while (tmpStr[index] == ' ') index++; - - if (strstr_P((char *)&tmpStr[index], PSTR(".g")) || strstr_P((char *)&tmpStr[index], PSTR(".G"))) { - if (strlen((char *)&tmpStr[index]) < 80) { - ZERO(list_file.file_name[sel_id]); - ZERO(list_file.long_name[sel_id]); - uint8_t has_path_selected = 0; - - if (gCfgItems.wifi_type == ESP_WIFI) { - if (strncmp_P((char *)&tmpStr[index], PSTR("1:"), 2) == 0) { - gCfgItems.fileSysType = FILE_SYS_SD; - has_path_selected = 1; - } - else if (strncmp_P((char *)&tmpStr[index], PSTR("0:"), 2) == 0) { - gCfgItems.fileSysType = FILE_SYS_USB; - has_path_selected = 1; - } - else if (tmpStr[index] != '/') - strcat_P((char *)list_file.file_name[sel_id], PSTR("/")); + clear_cur_ui(); + reset_print_time(); + start_print_time(); + preview_gcode_prehandle(list_file.file_name[sel_id]); + uiCfg.print_state = WORKING; + lv_draw_printing(); - if (file_writer.fileTransfer == 1) { - char dosName[FILENAME_LENGTH]; - uint8_t fileName[sizeof(list_file.file_name[sel_id])]; - fileName[0] = '\0'; - if (has_path_selected == 1) { - strcat((char *)fileName, (char *)&tmpStr[index + 3]); - strcat_P((char *)list_file.file_name[sel_id], PSTR("/")); - } - else strcat((char *)fileName, (char *)&tmpStr[index]); - if (!longName2DosName((const char *)fileName, dosName)) - strcpy_P(list_file.file_name[sel_id], PSTR("notValid")); - strcat((char *)list_file.file_name[sel_id], dosName); - strcat((char *)list_file.long_name[sel_id], dosName); - } - else { - strcat((char *)list_file.file_name[sel_id], (char *)&tmpStr[index]); - strcat((char *)list_file.long_name[sel_id], (char *)&tmpStr[index]); - } + #if ENABLED(SDSUPPORT) + if (!gcode_preview_over) { + char *cur_name = strrchr(list_file.file_name[sel_id], '/'); + MediaFile file; + MediaFile *curDir; + card.abortFilePrintNow(); + const char * const fname = card.diveToFile(false, curDir, cur_name); + if (!fname) return; + if (file.open(curDir, fname, O_READ)) { + gCfgItems.curFilesize = file.fileSize(); + file.close(); + update_spi_flash(); } - else - strcpy(list_file.file_name[sel_id], (char *)&tmpStr[index]); - - char *cur_name=strrchr(list_file.file_name[sel_id],'/'); - card.openFileRead(cur_name); - - if (card.isFileOpen()) - send_to_wifi((uint8_t *)"File selected\r\n", strlen("File selected\r\n")); - else { - send_to_wifi((uint8_t *)"file.open failed\r\n", strlen("file.open failed\r\n")); - strcpy_P(list_file.file_name[sel_id], PSTR("notValid")); + if (card.isFileOpen()) { + //saved_feedrate_percentage = feedrate_percentage; + feedrate_percentage = 100; + #if HAS_EXTRUDERS + planner.flow_percentage[0] = 100; + planner.e_factor[0] = planner.flow_percentage[0] * 0.01f; + #endif + #if HAS_MULTI_EXTRUDER + planner.flow_percentage[1] = 100; + planner.e_factor[1] = planner.flow_percentage[1] * 0.01f; + #endif + card.startOrResumeFilePrinting(); + TERN_(POWER_LOSS_RECOVERY, recovery.prepare()); + once_flag = false; } - SEND_OK_TO_WIFI; } - } - } - break; - - case 24: - if (strcmp_P(list_file.file_name[sel_id], PSTR("notValid")) != 0) { - if (uiCfg.print_state == IDLE) { - clear_cur_ui(); - reset_print_time(); - start_print_time(); - preview_gcode_prehandle(list_file.file_name[sel_id]); - uiCfg.print_state = WORKING; - lv_draw_printing(); - - #if ENABLED(SDSUPPORT) - if (!gcode_preview_over) { - char *cur_name = strrchr(list_file.file_name[sel_id], '/'); - - MediaFile file; - MediaFile *curDir; - card.abortFilePrintNow(); - const char * const fname = card.diveToFile(false, curDir, cur_name); - if (!fname) return; - if (file.open(curDir, fname, O_READ)) { - gCfgItems.curFilesize = file.fileSize(); - file.close(); - update_spi_flash(); - } - card.openFileRead(cur_name); - if (card.isFileOpen()) { - //saved_feedrate_percentage = feedrate_percentage; - feedrate_percentage = 100; - #if HAS_EXTRUDERS - planner.flow_percentage[0] = 100; - planner.e_factor[0] = planner.flow_percentage[0] * 0.01f; - #endif - #if HAS_MULTI_EXTRUDER - planner.flow_percentage[1] = 100; - planner.e_factor[1] = planner.flow_percentage[1] * 0.01f; - #endif - card.startOrResumeFilePrinting(); - TERN_(POWER_LOSS_RECOVERY, recovery.prepare()); - once_flag = false; - } - } - #endif - } - else if (uiCfg.print_state == PAUSED) { - uiCfg.print_state = RESUMING; - clear_cur_ui(); - start_print_time(); - - if (gCfgItems.from_flash_pic) - flash_preview_begin = true; - else - default_preview_flg = true; - lv_draw_printing(); - } - else if (uiCfg.print_state == REPRINTING) { - uiCfg.print_state = REPRINTED; - clear_cur_ui(); - start_print_time(); - if (gCfgItems.from_flash_pic) - flash_preview_begin = true; - else - default_preview_flg = true; - lv_draw_printing(); - } + #endif } - SEND_OK_TO_WIFI; - break; - - case 25: - // Pause print file - if (uiCfg.print_state == WORKING) { - stop_print_time(); - + else if (uiCfg.print_state == PAUSED) { + uiCfg.print_state = RESUMING; clear_cur_ui(); + start_print_time(); - #if ENABLED(SDSUPPORT) - card.pauseSDPrint(); - uiCfg.print_state = PAUSING; - #endif if (gCfgItems.from_flash_pic) flash_preview_begin = true; else default_preview_flg = true; lv_draw_printing(); - SEND_OK_TO_WIFI; } - break; - - case 26: - // Stop print file - if ((uiCfg.print_state == WORKING) || (uiCfg.print_state == PAUSED) || (uiCfg.print_state == REPRINTING)) { - stop_print_time(); - + else if (uiCfg.print_state == REPRINTING) { + uiCfg.print_state = REPRINTED; clear_cur_ui(); - #if ENABLED(SDSUPPORT) - uiCfg.print_state = IDLE; - card.abortFilePrintSoon(); - #endif + start_print_time(); + if (gCfgItems.from_flash_pic) + flash_preview_begin = true; + else + default_preview_flg = true; + lv_draw_printing(); + } + } + send_ok_to_wifi(); + break; - lv_draw_ready_print(); + case 25: + // Pause print file + if (uiCfg.print_state == WORKING) { + stop_print_time(); - SEND_OK_TO_WIFI; - } - break; - - case 27: - // Report print rate - if ((uiCfg.print_state == WORKING) || (uiCfg.print_state == PAUSED)|| (uiCfg.print_state == REPRINTING)) { - print_rate = uiCfg.totalSend; - ZERO(tempBuf); - sprintf_P((char *)tempBuf, PSTR("M27 %d\r\n"), print_rate); - send_to_wifi((uint8_t *)tempBuf, strlen((char *)tempBuf)); - } - break; + clear_cur_ui(); - case 28: - // Begin to transfer file to filesys - if (uiCfg.print_state == IDLE) { + #if ENABLED(SDSUPPORT) + card.pauseSDPrint(); + uiCfg.print_state = PAUSING; + #endif + if (gCfgItems.from_flash_pic) + flash_preview_begin = true; + else + default_preview_flg = true; + lv_draw_printing(); + send_ok_to_wifi(); + } + break; - int index = 0; - while (tmpStr[index] == ' ') index++; + case 26: + // Stop print file + if (uiCfg.print_state == WORKING || uiCfg.print_state == PAUSED || uiCfg.print_state == REPRINTING) { + stop_print_time(); - if (strstr_P((char *)&tmpStr[index], PSTR(".g")) || strstr_P((char *)&tmpStr[index], PSTR(".G"))) { - strcpy((char *)file_writer.saveFileName, (char *)&tmpStr[index]); + clear_cur_ui(); + #if ENABLED(SDSUPPORT) + uiCfg.print_state = IDLE; + card.abortFilePrintSoon(); + #endif - if (gCfgItems.fileSysType == FILE_SYS_SD) { - ZERO(tempBuf); - sprintf_P((char *)tempBuf, PSTR("%s"), file_writer.saveFileName); - } - else if (gCfgItems.fileSysType == FILE_SYS_USB) { - ZERO(tempBuf); - sprintf_P((char *)tempBuf, PSTR("%s"), (char *)file_writer.saveFileName); - } - mount_file_sys(gCfgItems.fileSysType); + lv_draw_ready_print(); - #if ENABLED(SDSUPPORT) - char *cur_name = strrchr(list_file.file_name[sel_id], '/'); - card.openFileWrite(cur_name); - if (card.isFileOpen()) { - ZERO(file_writer.saveFileName); - strcpy((char *)file_writer.saveFileName, (char *)&tmpStr[index]); - ZERO(tempBuf); - sprintf_P((char *)tempBuf, PSTR("Writing to file: %s\r\n"), (char *)file_writer.saveFileName); - wifi_ret_ack(); - send_to_wifi((uint8_t *)tempBuf, strlen((char *)tempBuf)); - wifi_link_state = WIFI_WAIT_TRANS_START; - } - else { - wifi_link_state = WIFI_CONNECTED; - clear_cur_ui(); - lv_draw_dialog(DIALOG_TRANSFER_NO_DEVICE); - } - #endif - } - } - break; + send_ok_to_wifi(); + } + break; - case 105: - case 991: + case 27: + // Report print rate + if (uiCfg.print_state == WORKING || uiCfg.print_state == PAUSED|| uiCfg.print_state == REPRINTING) { + print_rate = uiCfg.totalSend; ZERO(tempBuf); - if (cmd_value == 105) { + sprintf_P((char *)tempBuf, PSTR("M27 %d\r\n"), print_rate); + send_to_wifi((uint8_t *)tempBuf, strlen((char *)tempBuf)); + } + break; - SEND_OK_TO_WIFI; + case 28: + // Begin to transfer file to filesys + if (uiCfg.print_state == IDLE) { - char *outBuf = (char *)tempBuf; - char tbuf[34]; + int index = 0; + while (mStr[index] == ' ') index++; - sprintf_P(tbuf, PSTR("%d /%d"), thermalManager.wholeDegHotend(0), thermalManager.degTargetHotend(0)); + if (strstr_P(&mStr[index], PSTR(".g")) || strstr_P(&mStr[index], PSTR(".G"))) { + strcpy((char *)file_writer.saveFileName, &mStr[index]); - const int tlen = strlen(tbuf); - sprintf_P(outBuf, PSTR("T:%s"), tbuf); - outBuf += 2 + tlen; + if (gCfgItems.fileSysType == FILE_SYS_SD) { + ZERO(tempBuf); + sprintf_P((char *)tempBuf, PSTR("%s"), file_writer.saveFileName); + } + else if (gCfgItems.fileSysType == FILE_SYS_USB) { + ZERO(tempBuf); + sprintf_P((char *)tempBuf, PSTR("%s"), (char *)file_writer.saveFileName); + } + mount_file_sys(gCfgItems.fileSysType); - strcpy_P(outBuf, PSTR(" B:")); - outBuf += 3; - #if HAS_HEATED_BED - sprintf_P(outBuf, PSTR("%d /%d"), thermalManager.wholeDegBed(), thermalManager.degTargetBed()); - #else - strcpy_P(outBuf, PSTR("0 /0")); - #endif - outBuf += 4; - - strcat_P(outBuf, PSTR(" T0:")); - strcat(outBuf, tbuf); - outBuf += 4 + tlen; - - strcat_P(outBuf, PSTR(" T1:")); - outBuf += 4; - #if HAS_MULTI_HOTEND - sprintf_P(outBuf, PSTR("%d /%d"), thermalManager.wholeDegHotend(1), thermalManager.degTargetHotend(1)); - #else - strcpy_P(outBuf, PSTR("0 /0")); + #if ENABLED(SDSUPPORT) + char *cur_name = strrchr(list_file.file_name[sel_id], '/'); + card.openFileWrite(cur_name); + if (card.isFileOpen()) { + ZERO(file_writer.saveFileName); + strcpy((char *)file_writer.saveFileName, &mStr[index]); + ZERO(tempBuf); + sprintf_P((char *)tempBuf, PSTR("Writing to file: %s\r\n"), (char *)file_writer.saveFileName); + wifi_ret_ack(); + send_to_wifi((uint8_t *)tempBuf, strlen((char *)tempBuf)); + wifi_link_state = WIFI_WAIT_TRANS_START; + } + else { + wifi_link_state = WIFI_CONNECTED; + clear_cur_ui(); + lv_draw_dialog(DIALOG_TRANSFER_NO_DEVICE); + } #endif - outBuf += 4; - - strcat_P(outBuf, PSTR(" @:0 B@:0\r\n")); - } - else { - sprintf_P((char *)tempBuf, PSTR("T:%d /%d B:%d /%d T0:%d /%d T1:%d /%d @:0 B@:0\r\n"), - thermalManager.wholeDegHotend(0), thermalManager.degTargetHotend(0), - TERN0(HAS_HEATED_BED, thermalManager.wholeDegBed()), - TERN0(HAS_HEATED_BED, thermalManager.degTargetBed()), - thermalManager.wholeDegHotend(0), thermalManager.degTargetHotend(0), - TERN0(HAS_MULTI_HOTEND, thermalManager.wholeDegHotend(1)), - TERN0(HAS_MULTI_HOTEND, thermalManager.degTargetHotend(1)) - ); } + } + break; - send_to_wifi((uint8_t *)tempBuf, strlen((char *)tempBuf)); - queue.enqueue_one(F("M105")); - break; + case 105: + case 991: + ZERO(tempBuf); + if (cmd_value == 105) { + + send_ok_to_wifi(); + + char *outBuf = (char *)tempBuf; + char tbuf[34]; + + sprintf_P(tbuf, PSTR("%d /%d"), thermalManager.wholeDegHotend(0), thermalManager.degTargetHotend(0)); + + const int tlen = strlen(tbuf); + sprintf_P(outBuf, PSTR("T:%s"), tbuf); + outBuf += 2 + tlen; + + strcpy_P(outBuf, PSTR(" B:")); + outBuf += 3; + #if HAS_HEATED_BED + sprintf_P(outBuf, PSTR("%d /%d"), thermalManager.wholeDegBed(), thermalManager.degTargetBed()); + #else + strcpy_P(outBuf, PSTR("0 /0")); + #endif + outBuf += 4; + + strcat_P(outBuf, PSTR(" T0:")); + strcat(outBuf, tbuf); + outBuf += 4 + tlen; + + strcat_P(outBuf, PSTR(" T1:")); + outBuf += 4; + #if HAS_MULTI_HOTEND + sprintf_P(outBuf, PSTR("%d /%d"), thermalManager.wholeDegHotend(1), thermalManager.degTargetHotend(1)); + #else + strcpy_P(outBuf, PSTR("0 /0")); + #endif + outBuf += 4; + + strcat_P(outBuf, PSTR(" @:0 B@:0\r\n")); + } + else { + sprintf_P((char *)tempBuf, PSTR("T:%d /%d B:%d /%d T0:%d /%d T1:%d /%d @:0 B@:0\r\n"), + thermalManager.wholeDegHotend(0), thermalManager.degTargetHotend(0), + TERN0(HAS_HEATED_BED, thermalManager.wholeDegBed()), + TERN0(HAS_HEATED_BED, thermalManager.degTargetBed()), + thermalManager.wholeDegHotend(0), thermalManager.degTargetHotend(0), + TERN0(HAS_MULTI_HOTEND, thermalManager.wholeDegHotend(1)), + TERN0(HAS_MULTI_HOTEND, thermalManager.degTargetHotend(1)) + ); + } - case 992: - if ((uiCfg.print_state == WORKING) || (uiCfg.print_state == PAUSED)) { - ZERO(tempBuf); - sprintf_P((char *)tempBuf, PSTR("M992 %d%d:%d%d:%d%d\r\n"), print_time.hours/10, print_time.hours%10, print_time.minutes/10, print_time.minutes%10, print_time.seconds/10, print_time.seconds%10); - wifi_ret_ack(); - send_to_wifi((uint8_t *)tempBuf, strlen((char *)tempBuf)); - } - break; + send_to_wifi((uint8_t *)tempBuf, strlen((char *)tempBuf)); + queue.enqueue_one(F("M105")); + break; - case 994: - if ((uiCfg.print_state == WORKING) || (uiCfg.print_state == PAUSED)) { - ZERO(tempBuf); - if (strlen((char *)list_file.file_name[sel_id]) > (100 - 1)) return; - sprintf_P((char *)tempBuf, PSTR("M994 %s;%d\n"), list_file.file_name[sel_id], (int)gCfgItems.curFilesize); - wifi_ret_ack(); - send_to_wifi((uint8_t *)tempBuf, strlen((char *)tempBuf)); - } - break; + case 992: + if (uiCfg.print_state == WORKING || uiCfg.print_state == PAUSED) { + ZERO(tempBuf); + sprintf_P((char *)tempBuf, PSTR("M992 %d%d:%d%d:%d%d\r\n"), print_time.hours/10, print_time.hours%10, print_time.minutes/10, print_time.minutes%10, print_time.seconds/10, print_time.seconds%10); + wifi_ret_ack(); + send_to_wifi((uint8_t *)tempBuf, strlen((char *)tempBuf)); + } + break; - case 997: - if (uiCfg.print_state == IDLE) { + case 994: + if (uiCfg.print_state == WORKING || uiCfg.print_state == PAUSED) { + ZERO(tempBuf); + if (strlen((char *)list_file.file_name[sel_id]) > (100 - 1)) return; + sprintf_P((char *)tempBuf, PSTR("M994 %s;%d\n"), list_file.file_name[sel_id], (int)gCfgItems.curFilesize); + wifi_ret_ack(); + send_to_wifi((uint8_t *)tempBuf, strlen((char *)tempBuf)); + } + break; + + case 997: + #define SENDIDLE "M997 IDLE\r\n" + #define SENDPRINTING "M997 PRINTING\r\n" + #define SENDPAUSE "M997 PAUSE\r\n" + switch (uiCfg.print_state) { + default: break; + case IDLE: wifi_ret_ack(); - send_to_wifi((uint8_t *)"M997 IDLE\r\n", strlen("M997 IDLE\r\n")); - } - else if (uiCfg.print_state == WORKING) { + send_to_wifi((uint8_t *)SENDIDLE, strlen(SENDIDLE)); + break; + case WORKING: wifi_ret_ack(); - send_to_wifi((uint8_t *)"M997 PRINTING\r\n", strlen("M997 PRINTING\r\n")); - } - else if (uiCfg.print_state == PAUSED) { + send_to_wifi((uint8_t *)SENDPRINTING, strlen(SENDPRINTING)); + break; + case PAUSED: wifi_ret_ack(); - send_to_wifi((uint8_t *)"M997 PAUSE\r\n", strlen("M997 PAUSE\r\n")); - } - else if (uiCfg.print_state == REPRINTING) { + send_to_wifi((uint8_t *)SENDPAUSE, strlen(SENDPAUSE)); + break; + case REPRINTING: wifi_ret_ack(); - send_to_wifi((uint8_t *)"M997 PAUSE\r\n", strlen("M997 PAUSE\r\n")); - } - if (!uiCfg.command_send) get_wifi_list_command_send(); - break; + send_to_wifi((uint8_t *)SENDPAUSE, strlen(SENDPAUSE)); + break; + } + if (!uiCfg.command_send) get_wifi_list_command_send(); + break; - case 998: - if (uiCfg.print_state == IDLE) { - const int v = atoi((char *)tmpStr); - if (v == 0 || v == 1) set_cur_file_sys(v); - wifi_ret_ack(); - } - break; + case 998: + if (uiCfg.print_state == IDLE) { + const int v = atoi(mStr); + if (v == 0 || v == 1) set_cur_file_sys(v); + wifi_ret_ack(); + } + break; - case 115: - ZERO(tempBuf); - SEND_OK_TO_WIFI; - send_to_wifi((uint8_t *)"FIRMWARE_NAME:Robin_nano\r\n", strlen("FIRMWARE_NAME:Robin_nano\r\n")); - break; - - default: - strcat_P((char *)cmd_line, PSTR("\n")); - - if (espGcodeFifo.wait_tick > 5) { - const uint32_t left = espGcodeFifo.r > espGcodeFifo.w - ? espGcodeFifo.r - espGcodeFifo.w - 1 - : WIFI_GCODE_BUFFER_SIZE + espGcodeFifo.r - espGcodeFifo.w - 1; - - if (left >= strlen((const char *)cmd_line)) { - for (uint32_t index = 0; index < strlen((const char *)cmd_line); index++) { - espGcodeFifo.Buffer[espGcodeFifo.w] = cmd_line[index] ; - espGcodeFifo.w = (espGcodeFifo.w + 1) % WIFI_GCODE_BUFFER_SIZE; - } - if (left - WIFI_GCODE_BUFFER_LEAST_SIZE >= strlen((const char *)cmd_line)) - SEND_OK_TO_WIFI; - else - need_ok_later = true; + case 115: + ZERO(tempBuf); + send_ok_to_wifi(); + #define SENDFW "FIRMWARE_NAME:Robin_nano\r\n" + send_to_wifi((uint8_t *)SENDFW, strlen(SENDFW)); + break; + + default: + strcat_P((char *)cmd_line, PSTR("\n")); + + if (espGcodeFifo.wait_tick > 5) { + uint32_t left = espGcodeFifo.r - espGcodeFifo.w - 1; + if (espGcodeFifo.r > espGcodeFifo.w) left += WIFI_GCODE_BUFFER_SIZE; + + if (left >= strlen((const char *)cmd_line)) { + for (uint32_t index = 0; index < strlen((const char *)cmd_line); index++) { + espGcodeFifo.Buffer[espGcodeFifo.w] = cmd_line[index]; + espGcodeFifo.w = (espGcodeFifo.w + 1) % (WIFI_GCODE_BUFFER_SIZE); } + if (left - (WIFI_GCODE_BUFFER_LEAST_SIZE) >= strlen((const char *)cmd_line)) + send_ok_to_wifi(); + else + need_ok_later = true; } - break; - } + } + break; } - else { - strcat_P((char *)cmd_line, PSTR("\n")); + } + else { + // Add another linefeed to the command, terminate with null + strcat_P((char *)cmd_line, PSTR("\n")); - if (espGcodeFifo.wait_tick > 5) { - const uint32_t left_g = espGcodeFifo.r > espGcodeFifo.w - ? espGcodeFifo.r - espGcodeFifo.w - 1 - : WIFI_GCODE_BUFFER_SIZE + espGcodeFifo.r - espGcodeFifo.w - 1; + if (espGcodeFifo.wait_tick > 5) { + uint32_t left_g = espGcodeFifo.r - espGcodeFifo.w - 1; + if (espGcodeFifo.r > espGcodeFifo.w) left_g += WIFI_GCODE_BUFFER_SIZE; - if (left_g >= strlen((const char *)cmd_line)) { - for (uint32_t index = 0; index < strlen((const char *)cmd_line); index++) { - espGcodeFifo.Buffer[espGcodeFifo.w] = cmd_line[index] ; - espGcodeFifo.w = (espGcodeFifo.w + 1) % WIFI_GCODE_BUFFER_SIZE; - } - if (left_g - WIFI_GCODE_BUFFER_LEAST_SIZE >= strlen((const char *)cmd_line)) - SEND_OK_TO_WIFI; - else - need_ok_later = true; + if (left_g >= strlen((char * const)cmd_line)) { + for (uint32_t index = 0; index < strlen((char * const)cmd_line); index++) { + espGcodeFifo.Buffer[espGcodeFifo.w] = cmd_line[index]; + espGcodeFifo.w = (espGcodeFifo.w + 1) % (WIFI_GCODE_BUFFER_SIZE); } + if (left_g - (WIFI_GCODE_BUFFER_LEAST_SIZE) >= strlen((char * const)cmd_line)) + send_ok_to_wifi(); + else + need_ok_later = true; } } } @@ -1281,7 +1309,7 @@ void get_wifi_list_command_send() { raw_send_to_wifi(cmd_wifi_list, COUNT(cmd_wifi_list)); } -static void net_msg_handle(uint8_t * msg, uint16_t msgLen) { +static void net_msg_handle(const uint8_t * const msg, const uint16_t msgLen) { int wifiNameLen, wifiKeyLen, hostLen, id_len, ver_len; if (msgLen <= 0) return; @@ -1316,7 +1344,7 @@ static void net_msg_handle(uint8_t * msg, uint16_t msgLen) { } } - cloud_para.state =msg[10 + wifiNameLen + wifiKeyLen]; + cloud_para.state = msg[10 + wifiNameLen + wifiKeyLen]; hostLen = msg[11 + wifiNameLen + wifiKeyLen]; if (cloud_para.state) { if (hostLen < 96) { @@ -1356,7 +1384,7 @@ static void net_msg_handle(uint8_t * msg, uint16_t msgLen) { } } -static void wifi_list_msg_handle(uint8_t * msg, uint16_t msgLen) { +static void wifi_list_msg_handle(const uint8_t * const msg, const uint16_t msgLen) { int wifiNameLen,wifiMsgIdex = 1; int8_t wifi_name_is_same = 0; int8_t i, j; @@ -1415,16 +1443,15 @@ static void wifi_list_msg_handle(uint8_t * msg, uint16_t msgLen) { } } -static void gcode_msg_handle(uint8_t * msg, uint16_t msgLen) { +static void gcode_msg_handle(const uint8_t * const msg, const uint16_t msgLen) { uint8_t gcodeBuf[100] = { 0 }; - char *index_s, *index_e; if (msgLen <= 0) return; - index_s = (char *)msg; - index_e = (char *)strchr((char *)msg, '\n'); + char *index_s = (char *)msg, + *index_e = strchr((char *)msg, '\n'); if (*msg == 'N') { - index_s = (char *)strchr((char *)msg, ' '); + index_s = strchr((char *)msg, ' '); while (*index_s == ' ') index_s++; } while ((index_e != 0) && ((int)index_s < (int)index_e)) { @@ -1435,7 +1462,7 @@ static void gcode_msg_handle(uint8_t * msg, uint16_t msgLen) { } while ((*index_e == '\r') || (*index_e == '\n')) index_e++; index_s = index_e; - index_e = (char *)strchr(index_s, '\n'); + index_e = strchr(index_s, '\n'); } } @@ -1482,8 +1509,8 @@ void utf8_2_unicode(uint8_t *source, uint8_t Len) { COPY(source, FileName_unicode); } -static void file_first_msg_handle(uint8_t * msg, uint16_t msgLen) { - uint8_t fileNameLen = *msg; +static void file_first_msg_handle(const uint8_t * const msg, const uint16_t msgLen) { + const uint8_t fileNameLen = *msg; if (msgLen != fileNameLen + 5) return; @@ -1565,8 +1592,8 @@ static void file_first_msg_handle(uint8_t * msg, uint16_t msgLen) { #define FRAG_MASK ~_BV32(31) -static void file_fragment_msg_handle(uint8_t * msg, uint16_t msgLen) { - uint32_t frag = *((uint32_t *)msg); +static void file_fragment_msg_handle(const uint8_t * const msg, const uint16_t msgLen) { + const uint32_t frag = *((uint32_t *)msg); if ((frag & FRAG_MASK) != (uint32_t)(lastFragment + 1)) { ZERO(public_buf); file_writer.write_index = 0; @@ -1842,7 +1869,7 @@ void wifi_rcv_handle() { } if (need_ok_later && !queue.ring_buffer.full()) { need_ok_later = false; - send_to_wifi((uint8_t *)"ok\r\n", strlen("ok\r\n")); + send_ok_to_wifi(); } } @@ -1872,7 +1899,7 @@ void wifi_rcv_handle() { if (wifiTransError.flag == 0x1) { wifiTransError.now_tick = getWifiTick(); - if (getWifiTickDiff(wifiTransError.start_tick, wifiTransError.now_tick) > WAIT_ESP_TRANS_TIMEOUT_TICK) { + if (getWifiTickDiff(wifiTransError.start_tick, wifiTransError.now_tick) > (WAIT_ESP_TRANS_TIMEOUT_TICK)) { wifiTransError.flag = 0; WIFI_IO1_RESET(); } @@ -1992,7 +2019,7 @@ void get_wifi_commands() { char wifi_char = espGcodeFifo.Buffer[espGcodeFifo.r]; - espGcodeFifo.r = (espGcodeFifo.r + 1) % WIFI_GCODE_BUFFER_SIZE; + espGcodeFifo.r = (espGcodeFifo.r + 1) % (WIFI_GCODE_BUFFER_SIZE); /** * If the character ends the line diff --git a/Marlin/src/lcd/extui/mks_ui/wifi_module.h b/Marlin/src/lcd/extui/mks_ui/wifi_module.h index 36998899b4836..851e85c512a0a 100644 --- a/Marlin/src/lcd/extui/mks_ui/wifi_module.h +++ b/Marlin/src/lcd/extui/mks_ui/wifi_module.h @@ -163,8 +163,8 @@ typedef struct { //uint8_t uartTxBuffer[UART_FIFO_BUFFER_SIZE]; } SZ_USART_FIFO; -#define WIFI_GCODE_BUFFER_LEAST_SIZE 96 -#define WIFI_GCODE_BUFFER_SIZE (WIFI_GCODE_BUFFER_LEAST_SIZE * 3) +#define WIFI_GCODE_BUFFER_LEAST_SIZE 96 +#define WIFI_GCODE_BUFFER_SIZE (WIFI_GCODE_BUFFER_LEAST_SIZE * 3) typedef struct { uint8_t wait_tick; uint8_t Buffer[WIFI_GCODE_BUFFER_SIZE]; @@ -179,14 +179,14 @@ extern CLOUD_PARA cloud_para; extern WIFI_GCODE_BUFFER espGcodeFifo; -uint32_t getWifiTick(); -uint32_t getWifiTickDiff(int32_t lastTick, int32_t curTick); +millis_t getWifiTick(); +millis_t getWifiTickDiff(const millis_t lastTick, const millis_t curTick); void mks_esp_wifi_init(); extern int cfg_cloud_flag; -int send_to_wifi(uint8_t *buf, int len); +int send_to_wifi(uint8_t * const buf, const int len); void wifi_looping(); -int raw_send_to_wifi(uint8_t *buf, int len); +int raw_send_to_wifi(uint8_t * const buf, const int len); int package_to_wifi(WIFI_RET_TYPE type, uint8_t *buf, int len); void get_wifi_list_command_send(); void get_wifi_commands(); diff --git a/Marlin/src/lcd/extui/mks_ui/wifi_upload.cpp b/Marlin/src/lcd/extui/mks_ui/wifi_upload.cpp index 44869d4770987..398d35fdc3f36 100644 --- a/Marlin/src/lcd/extui/mks_ui/wifi_upload.cpp +++ b/Marlin/src/lcd/extui/mks_ui/wifi_upload.cpp @@ -38,10 +38,8 @@ extern SZ_USART_FIFO WifiRxFifo; -extern int readUsartFifo(SZ_USART_FIFO *fifo, int8_t *buf, int32_t len); -extern int writeUsartFifo(SZ_USART_FIFO * fifo, int8_t * buf, int32_t len); void esp_port_begin(uint8_t interrupt); -void wifi_delay(int n); +void wifi_delay(const uint16_t n); #define ARRAY_SIZE(a) sizeof(a) / sizeof((a)[0]) @@ -246,7 +244,7 @@ EspUploadResult readPacket(uint8_t op, uint32_t *valp, size_t *bodyLen, uint32_t const size_t headerLength = 8; - uint32_t startTime = getWifiTick(); + const millis_t startTime = getWifiTick(); uint8_t hdr[headerLength]; uint16_t hdrIdx = 0; diff --git a/Marlin/src/lcd/extui/mks_ui/wifi_upload.h b/Marlin/src/lcd/extui/mks_ui/wifi_upload.h index 2daa505d9088f..524fb28f85fe9 100644 --- a/Marlin/src/lcd/extui/mks_ui/wifi_upload.h +++ b/Marlin/src/lcd/extui/mks_ui/wifi_upload.h @@ -59,8 +59,8 @@ typedef struct { UploadState state; uint32_t retriesPerBaudRate; uint32_t connectAttemptNumber; - uint32_t lastAttemptTime; - uint32_t lastResetTime; + millis_t lastAttemptTime; + millis_t lastResetTime; uint32_t uploadBlockNumber; uint32_t uploadNextPercentToReport; EspUploadResult uploadResult;