2020-09-21 01:55:02 +00:00
|
|
|
/**
|
|
|
|
* 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/>.
|
|
|
|
*
|
|
|
|
*/
|
2021-09-16 09:36:26 +00:00
|
|
|
|
2021-05-06 09:17:59 +00:00
|
|
|
#include "../../../inc/MarlinConfigPre.h"
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-01-22 21:01:19 +00:00
|
|
|
#if BOTH(HAS_TFT_LVGL_UI, MKS_WIFI_MODULE)
|
2020-09-21 01:55:02 +00:00
|
|
|
|
|
|
|
#include "draw_ui.h"
|
|
|
|
#include "wifi_module.h"
|
|
|
|
#include "wifi_upload.h"
|
2021-01-14 04:41:09 +00:00
|
|
|
#include "SPI_TFT.h"
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-05-22 03:55:13 +00:00
|
|
|
#include "../../marlinui.h"
|
|
|
|
|
2021-05-06 09:17:59 +00:00
|
|
|
#include "../../../MarlinCore.h"
|
|
|
|
#include "../../../module/temperature.h"
|
|
|
|
#include "../../../gcode/queue.h"
|
|
|
|
#include "../../../gcode/gcode.h"
|
|
|
|
#include "../../../sd/cardreader.h"
|
|
|
|
#include "../../../module/planner.h"
|
|
|
|
#include "../../../module/servo.h"
|
|
|
|
#include "../../../module/probe.h"
|
2021-01-21 09:40:07 +00:00
|
|
|
|
|
|
|
#if DISABLED(EMERGENCY_PARSER)
|
2021-05-06 09:17:59 +00:00
|
|
|
#include "../../../module/motion.h"
|
2021-01-21 09:40:07 +00:00
|
|
|
#endif
|
2020-09-21 01:55:02 +00:00
|
|
|
#if ENABLED(POWER_LOSS_RECOVERY)
|
2021-05-06 09:17:59 +00:00
|
|
|
#include "../../../feature/powerloss.h"
|
2020-09-21 01:55:02 +00:00
|
|
|
#endif
|
|
|
|
#if ENABLED(PARK_HEAD_ON_PAUSE)
|
2021-05-06 09:17:59 +00:00
|
|
|
#include "../../../feature/pause.h"
|
2020-09-21 01:55:02 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define WIFI_SET() WRITE(WIFI_RESET_PIN, HIGH);
|
|
|
|
#define WIFI_RESET() WRITE(WIFI_RESET_PIN, LOW);
|
2021-07-13 00:17:28 +00:00
|
|
|
#define WIFI_IO1_SET() WRITE(WIFI_IO1_PIN, HIGH);
|
|
|
|
#define WIFI_IO1_RESET() WRITE(WIFI_IO1_PIN, LOW);
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-09-16 11:48:24 +00:00
|
|
|
extern uint8_t Explore_Disk(char *path, uint8_t recu_level);
|
2020-09-21 01:55:02 +00:00
|
|
|
|
|
|
|
extern uint8_t commands_in_queue;
|
|
|
|
extern uint8_t sel_id;
|
2021-09-16 11:48:24 +00:00
|
|
|
extern unsigned int getTickDiff(unsigned int curTick, unsigned int lastTick);
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-09-16 11:48:24 +00:00
|
|
|
volatile SZ_USART_FIFO WifiRxFifo;
|
2020-09-21 01:55:02 +00:00
|
|
|
|
|
|
|
#define WAIT_ESP_TRANS_TIMEOUT_TICK 10500
|
|
|
|
|
|
|
|
int cfg_cloud_flag = 0;
|
|
|
|
|
|
|
|
extern PRINT_TIME print_time;
|
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
char wifi_firm_ver[20] = { 0 };
|
2020-09-21 01:55:02 +00:00
|
|
|
WIFI_GCODE_BUFFER espGcodeFifo;
|
|
|
|
extern uint8_t pause_resum;
|
|
|
|
|
|
|
|
uint8_t wifi_connect_flg = 0;
|
|
|
|
extern volatile uint8_t get_temp_flag;
|
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
#define WIFI_MODE 2
|
2020-09-21 01:55:02 +00:00
|
|
|
#define WIFI_AP_MODE 3
|
|
|
|
|
|
|
|
int upload_result = 0;
|
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
uint32_t upload_time_sec = 0;
|
2020-09-21 01:55:02 +00:00
|
|
|
uint32_t upload_size = 0;
|
|
|
|
|
|
|
|
volatile WIFI_STATE wifi_link_state;
|
|
|
|
WIFI_PARA wifiPara;
|
|
|
|
IP_PARA ipPara;
|
|
|
|
CLOUD_PARA cloud_para;
|
|
|
|
|
|
|
|
char wifi_check_time = 0;
|
|
|
|
|
|
|
|
extern uint8_t gCurDir[100];
|
|
|
|
|
|
|
|
extern uint32_t wifi_loop_cycle;
|
|
|
|
|
|
|
|
volatile TRANSFER_STATE esp_state;
|
|
|
|
|
|
|
|
uint8_t left_to_send = 0;
|
2020-10-21 17:45:27 +00:00
|
|
|
uint8_t left_to_save[96] = { 0 };
|
2020-09-21 01:55:02 +00:00
|
|
|
|
|
|
|
volatile WIFI_DMA_RCV_FIFO wifiDmaRcvFifo;
|
|
|
|
|
|
|
|
volatile WIFI_TRANS_ERROR wifiTransError;
|
|
|
|
|
|
|
|
static bool need_ok_later = false;
|
|
|
|
|
|
|
|
extern volatile WIFI_STATE wifi_link_state;
|
|
|
|
extern WIFI_PARA wifiPara;
|
|
|
|
extern IP_PARA ipPara;
|
|
|
|
extern CLOUD_PARA cloud_para;
|
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
extern bool once_flag, flash_preview_begin, default_preview_flg, gcode_preview_over;
|
2021-01-14 04:41:09 +00:00
|
|
|
extern bool flash_dma_mode;
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-01-14 04:41:09 +00:00
|
|
|
uint32_t getWifiTick() { return millis(); }
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
uint32_t getWifiTickDiff(int32_t lastTick, int32_t curTick) {
|
2021-07-13 00:17:28 +00:00
|
|
|
return (lastTick <= curTick ? curTick - lastTick : 0xFFFFFFFF - lastTick + curTick) * TICK_CYCLE;
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void wifi_delay(int n) {
|
2021-07-13 00:17:28 +00:00
|
|
|
const uint32_t start = getWifiTick();
|
|
|
|
while (getWifiTickDiff(start, getWifiTick()) < (uint32_t)n)
|
2021-01-13 03:02:35 +00:00
|
|
|
watchdog_refresh();
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void wifi_reset() {
|
2021-07-13 00:17:28 +00:00
|
|
|
uint32_t start = getWifiTick();
|
2020-10-21 17:45:27 +00:00
|
|
|
WIFI_RESET();
|
2021-07-13 00:17:28 +00:00
|
|
|
while (getWifiTickDiff(start, getWifiTick()) < 500) { /* nada */ }
|
2020-10-21 17:45:27 +00:00
|
|
|
WIFI_SET();
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void mount_file_sys(uint8_t disk_type) {
|
|
|
|
if (disk_type == FILE_SYS_SD) {
|
2020-10-21 17:45:27 +00:00
|
|
|
TERN_(SDSUPPORT, card.mount());
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
else if (disk_type == FILE_SYS_USB) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-13 20:07:25 +00:00
|
|
|
static bool longName2DosName(const char *longName, char *dosName) {
|
2021-07-13 00:17:28 +00:00
|
|
|
uint8_t i = FILENAME_LENGTH;
|
|
|
|
while (i) dosName[--i] = '\0';
|
|
|
|
|
2021-01-14 04:41:09 +00:00
|
|
|
while (*longName) {
|
|
|
|
uint8_t c = *longName++;
|
|
|
|
if (c == '.') { // For a dot...
|
|
|
|
if (i == 0) return false;
|
2021-04-13 20:07:25 +00:00
|
|
|
strcat_P(dosName, PSTR(".GCO"));
|
2021-07-13 00:17:28 +00:00
|
|
|
return dosName[0] != '\0';
|
2021-01-14 04:41:09 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Fail for illegal characters
|
|
|
|
PGM_P p = PSTR("|<>^+=?/[];,*\"\\");
|
2021-07-13 00:17:28 +00:00
|
|
|
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
|
2021-01-14 04:41:09 +00:00
|
|
|
}
|
|
|
|
if (i >= 5) {
|
2021-04-13 20:07:25 +00:00
|
|
|
strcat_P(dosName, PSTR("~1.GCO"));
|
2021-07-13 00:17:28 +00:00
|
|
|
return dosName[0] != '\0';
|
2021-01-14 04:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return dosName[0] != '\0'; // Return true if any name was set
|
|
|
|
}
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
#ifdef __STM32F1__
|
|
|
|
|
|
|
|
#include <libmaple/timer.h>
|
|
|
|
#include <libmaple/util.h>
|
|
|
|
#include <libmaple/rcc.h>
|
|
|
|
|
|
|
|
#include <boards.h>
|
|
|
|
#include <wirish.h>
|
|
|
|
|
|
|
|
#include <libmaple/dma.h>
|
|
|
|
#include <libmaple/bitband.h>
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
#include <libmaple/libmaple.h>
|
|
|
|
#include <libmaple/gpio.h>
|
|
|
|
#include <libmaple/usart.h>
|
|
|
|
#include <libmaple/ring_buffer.h>
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
void changeFlashMode(const bool dmaMode) {
|
|
|
|
if (flash_dma_mode != dmaMode) {
|
|
|
|
flash_dma_mode = dmaMode;
|
|
|
|
if (!flash_dma_mode) {
|
|
|
|
dma_disable(DMA1, DMA_CH5);
|
|
|
|
dma_clear_isr_bits(DMA1, DMA_CH4);
|
|
|
|
}
|
|
|
|
}
|
2021-01-14 04:41:09 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
static int storeRcvData(volatile uint8_t *bufToCpy, int32_t len) {
|
|
|
|
unsigned char tmpW = wifiDmaRcvFifo.write_cur;
|
|
|
|
if (len > UDISKBUFLEN) return 0;
|
|
|
|
if (wifiDmaRcvFifo.state[tmpW] == udisk_buf_empty) {
|
|
|
|
memcpy((unsigned char *) wifiDmaRcvFifo.bufferAddr[tmpW], (uint8_t *)bufToCpy, len);
|
|
|
|
wifiDmaRcvFifo.state[tmpW] = udisk_buf_full;
|
|
|
|
wifiDmaRcvFifo.write_cur = (tmpW + 1) % TRANS_RCV_FIFO_BLOCK_NUM;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2021-01-14 04:41:09 +00:00
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
static void esp_dma_pre() {
|
|
|
|
dma_channel_reg_map *channel_regs = dma_tube_regs(DMA1, DMA_CH5);
|
|
|
|
|
|
|
|
CBI32(channel_regs->CCR, 0);
|
|
|
|
channel_regs->CMAR = (uint32_t)WIFISERIAL.usart_device->rb->buf;
|
|
|
|
channel_regs->CNDTR = 0x0000;
|
|
|
|
channel_regs->CNDTR = UART_RX_BUFFER_SIZE;
|
|
|
|
DMA1->regs->IFCR = 0xF0000;
|
|
|
|
SBI32(channel_regs->CCR, 0);
|
|
|
|
}
|
2021-01-14 04:41:09 +00:00
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
static void dma_ch5_irq_handle() {
|
2021-01-14 04:41:09 +00:00
|
|
|
uint8 status_bits = dma_get_isr_bits(DMA1, DMA_CH5);
|
|
|
|
dma_clear_isr_bits(DMA1, DMA_CH5);
|
|
|
|
if (status_bits & 0x8) {
|
|
|
|
// DMA transmit Error
|
|
|
|
}
|
|
|
|
else if (status_bits & 0x2) {
|
|
|
|
// DMA transmit complete
|
|
|
|
if (esp_state == TRANSFER_IDLE)
|
2021-08-08 07:26:54 +00:00
|
|
|
esp_state = TRANSFERRING;
|
2021-01-14 04:41:09 +00:00
|
|
|
|
|
|
|
if (storeRcvData(WIFISERIAL.usart_device->rb->buf, UART_RX_BUFFER_SIZE)) {
|
|
|
|
esp_dma_pre();
|
|
|
|
if (wifiTransError.flag != 0x1)
|
|
|
|
WIFI_IO1_RESET();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
WIFI_IO1_SET();
|
|
|
|
esp_state = TRANSFER_STORE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (status_bits & 0x4) {
|
|
|
|
// DMA transmit half
|
|
|
|
WIFI_IO1_SET();
|
|
|
|
}
|
2021-07-13 00:17:28 +00:00
|
|
|
}
|
2021-01-14 04:41:09 +00:00
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
static void wifi_usart_dma_init() {
|
|
|
|
dma_init(DMA1);
|
|
|
|
uint32_t flags = ( DMA_MINC_MODE | DMA_TRNS_CMPLT | DMA_HALF_TRNS | DMA_TRNS_ERR);
|
|
|
|
dma_xfer_size dma_bit_size = DMA_SIZE_8BITS;
|
|
|
|
dma_setup_transfer(DMA1, DMA_CH5, &USART1_BASE->DR, dma_bit_size,
|
|
|
|
(volatile void*)WIFISERIAL.usart_device->rb->buf, dma_bit_size, flags);// Transmit buffer DMA
|
|
|
|
dma_set_priority(DMA1, DMA_CH5, DMA_PRIORITY_LOW);
|
|
|
|
dma_attach_interrupt(DMA1, DMA_CH5, &dma_ch5_irq_handle);
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
dma_clear_isr_bits(DMA1, DMA_CH5);
|
|
|
|
dma_set_num_transfers(DMA1, DMA_CH5, UART_RX_BUFFER_SIZE);
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
bb_peri_set_bit(&USART1_BASE->CR3, USART_CR3_DMAR_BIT, 1);
|
|
|
|
dma_enable(DMA1, DMA_CH5); // enable transmit
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
for (uint8_t i = 0; i < TRANS_RCV_FIFO_BLOCK_NUM; i++) {
|
|
|
|
wifiDmaRcvFifo.bufferAddr[i] = &bmp_public_buf[1024 * i];
|
|
|
|
wifiDmaRcvFifo.state[i] = udisk_buf_empty;
|
|
|
|
}
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
memset(wifiDmaRcvFifo.bufferAddr[0], 0, 1024 * TRANS_RCV_FIFO_BLOCK_NUM);
|
|
|
|
wifiDmaRcvFifo.read_cur = 0;
|
|
|
|
wifiDmaRcvFifo.write_cur = 0;
|
|
|
|
}
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
void esp_port_begin(uint8_t interrupt) {
|
|
|
|
WifiRxFifo.uart_read_point = 0;
|
|
|
|
WifiRxFifo.uart_write_point = 0;
|
2021-01-14 04:41:09 +00:00
|
|
|
#if ENABLED(MKS_WIFI_MODULE)
|
|
|
|
if (interrupt) {
|
2021-07-13 00:17:28 +00:00
|
|
|
WIFISERIAL.end();
|
2021-01-14 04:41:09 +00:00
|
|
|
for (uint16_t i = 0; i < 65535; i++) { /*nada*/ }
|
|
|
|
WIFISERIAL.begin(WIFI_BAUDRATE);
|
2021-01-14 06:23:56 +00:00
|
|
|
millis_t serial_connect_timeout = millis() + 1000UL;
|
2021-01-14 04:41:09 +00:00
|
|
|
while (PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
|
|
|
|
}
|
|
|
|
else {
|
2021-07-13 00:17:28 +00:00
|
|
|
WIFISERIAL.end();
|
2021-01-14 04:41:09 +00:00
|
|
|
WIFISERIAL.usart_device->regs->CR1 &= ~USART_CR1_RXNEIE;
|
|
|
|
WIFISERIAL.begin(WIFI_UPLOAD_BAUDRATE);
|
|
|
|
wifi_usart_dma_init();
|
|
|
|
}
|
2020-09-21 01:55:02 +00:00
|
|
|
#endif
|
2021-07-13 00:17:28 +00:00
|
|
|
}
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
#else // !__STM32F1__
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
DMA_HandleTypeDef wifiUsartDMArx;
|
|
|
|
|
|
|
|
void changeFlashMode(const bool dmaMode) {
|
|
|
|
if (flash_dma_mode != dmaMode) {
|
|
|
|
flash_dma_mode = dmaMode;
|
|
|
|
if (flash_dma_mode == 1) {
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef STM32F1xx
|
2021-01-13 00:05:49 +00:00
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
HAL_StatusTypeDef HAL_DMA_PollForTransferCustomize(DMA_HandleTypeDef *hdma, uint32_t CompleteLevel, uint32_t Timeout) {
|
|
|
|
uint32_t temp;
|
|
|
|
uint32_t tickstart = 0U;
|
|
|
|
|
|
|
|
if (HAL_DMA_STATE_BUSY != hdma->State) { // No transfer ongoing
|
|
|
|
hdma->ErrorCode = HAL_DMA_ERROR_NO_XFER;
|
|
|
|
__HAL_UNLOCK(hdma);
|
|
|
|
return HAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Polling mode not supported in circular mode
|
|
|
|
if (RESET != (hdma->Instance->CCR & DMA_CCR_CIRC)) {
|
|
|
|
hdma->ErrorCode = HAL_DMA_ERROR_NOT_SUPPORTED;
|
|
|
|
return HAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the level transfer complete flag
|
|
|
|
temp = (CompleteLevel == HAL_DMA_FULL_TRANSFER
|
|
|
|
? __HAL_DMA_GET_TC_FLAG_INDEX(hdma) // Transfer Complete flag
|
|
|
|
: __HAL_DMA_GET_HT_FLAG_INDEX(hdma) // Half Transfer Complete flag
|
|
|
|
);
|
|
|
|
|
|
|
|
// Get tick
|
|
|
|
tickstart = HAL_GetTick();
|
|
|
|
|
|
|
|
while (__HAL_DMA_GET_FLAG(hdma, temp) == RESET) {
|
|
|
|
if ((__HAL_DMA_GET_FLAG(hdma, __HAL_DMA_GET_HT_FLAG_INDEX(hdma)) != RESET)) {
|
|
|
|
__HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_HT_FLAG_INDEX(hdma)); // Clear the half transfer complete flag
|
|
|
|
WIFI_IO1_SET();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((__HAL_DMA_GET_FLAG(hdma, __HAL_DMA_GET_TE_FLAG_INDEX(hdma)) != RESET)) {
|
|
|
|
/**
|
|
|
|
* When a DMA transfer error occurs
|
|
|
|
* A hardware clear of its EN bits is performed
|
|
|
|
* Clear all flags
|
|
|
|
*/
|
|
|
|
hdma->DmaBaseAddress->IFCR = (DMA_ISR_GIF1 << hdma->ChannelIndex);
|
|
|
|
|
|
|
|
SET_BIT(hdma->ErrorCode, HAL_DMA_ERROR_TE); // Update error code
|
|
|
|
hdma->State= HAL_DMA_STATE_READY; // Change the DMA state
|
|
|
|
__HAL_UNLOCK(hdma); // Process Unlocked
|
|
|
|
return HAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the Timeout
|
|
|
|
if (Timeout != HAL_MAX_DELAY && (!Timeout || (HAL_GetTick() - tickstart) > Timeout)) {
|
|
|
|
SET_BIT(hdma->ErrorCode, HAL_DMA_ERROR_TIMEOUT); // Update error code
|
|
|
|
hdma->State = HAL_DMA_STATE_READY; // Change the DMA state
|
|
|
|
__HAL_UNLOCK(hdma); // Process Unlocked
|
|
|
|
return HAL_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CompleteLevel == HAL_DMA_FULL_TRANSFER) {
|
|
|
|
// Clear the transfer complete flag
|
|
|
|
__HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_TC_FLAG_INDEX(hdma));
|
|
|
|
|
|
|
|
/* The selected Channelx EN bit is cleared (DMA is disabled and
|
|
|
|
all transfers are complete) */
|
|
|
|
hdma->State = HAL_DMA_STATE_READY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
__HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_HT_FLAG_INDEX(hdma)); // Clear the half transfer complete flag
|
|
|
|
|
|
|
|
__HAL_UNLOCK(hdma); // Process unlocked
|
|
|
|
|
|
|
|
return HAL_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else // !STM32F1xx
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
__IO uint32_t ISR; //!< DMA interrupt status register
|
|
|
|
__IO uint32_t Reserved0;
|
|
|
|
__IO uint32_t IFCR; //!< DMA interrupt flag clear register
|
|
|
|
} MYDMA_Base_Registers;
|
|
|
|
|
|
|
|
HAL_StatusTypeDef HAL_DMA_PollForTransferCustomize(DMA_HandleTypeDef *hdma, HAL_DMA_LevelCompleteTypeDef CompleteLevel, uint32_t Timeout) {
|
|
|
|
HAL_StatusTypeDef status = HAL_OK;
|
|
|
|
uint32_t mask_cpltlevel;
|
|
|
|
uint32_t tickstart = HAL_GetTick();
|
|
|
|
uint32_t tmpisr;
|
|
|
|
|
|
|
|
MYDMA_Base_Registers *regs; // Calculate DMA base and stream number
|
|
|
|
|
|
|
|
if (HAL_DMA_STATE_BUSY != hdma->State) { // No transfer ongoing
|
|
|
|
hdma->ErrorCode = HAL_DMA_ERROR_NO_XFER;
|
|
|
|
__HAL_UNLOCK(hdma);
|
|
|
|
return HAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Polling mode not supported in circular mode and double buffering mode
|
|
|
|
if ((hdma->Instance->CR & DMA_SxCR_CIRC) != RESET) {
|
|
|
|
hdma->ErrorCode = HAL_DMA_ERROR_NOT_SUPPORTED;
|
|
|
|
return HAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the level transfer complete flag
|
|
|
|
mask_cpltlevel = (CompleteLevel == HAL_DMA_FULL_TRANSFER
|
|
|
|
? DMA_FLAG_TCIF0_4 << hdma->StreamIndex // Transfer Complete flag
|
|
|
|
: DMA_FLAG_HTIF0_4 << hdma->StreamIndex // Half Transfer Complete flag
|
|
|
|
);
|
|
|
|
|
|
|
|
regs = (MYDMA_Base_Registers *)hdma->StreamBaseAddress;
|
|
|
|
tmpisr = regs->ISR;
|
|
|
|
|
|
|
|
while (((tmpisr & mask_cpltlevel) == RESET) && ((hdma->ErrorCode & HAL_DMA_ERROR_TE) == RESET)) {
|
|
|
|
// Check for the Timeout (Not applicable in circular mode)
|
|
|
|
if (Timeout != HAL_MAX_DELAY) {
|
|
|
|
if (!Timeout || (HAL_GetTick() - tickstart) > Timeout) {
|
|
|
|
hdma->ErrorCode = HAL_DMA_ERROR_TIMEOUT; // Update error code
|
|
|
|
__HAL_UNLOCK(hdma); // Process Unlocked
|
|
|
|
hdma->State = HAL_DMA_STATE_READY; // Change the DMA state
|
|
|
|
return HAL_TIMEOUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpisr = regs->ISR; // Get the ISR register value
|
|
|
|
|
|
|
|
if ((tmpisr & (DMA_FLAG_HTIF0_4 << hdma->StreamIndex)) != RESET) {
|
|
|
|
regs->IFCR = DMA_FLAG_HTIF0_4 << hdma->StreamIndex; // Clear the Direct Mode error flag
|
|
|
|
WIFI_IO1_SET();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((tmpisr & (DMA_FLAG_TEIF0_4 << hdma->StreamIndex)) != RESET) {
|
|
|
|
hdma->ErrorCode |= HAL_DMA_ERROR_TE; // Update error code
|
|
|
|
regs->IFCR = DMA_FLAG_TEIF0_4 << hdma->StreamIndex; // Clear the transfer error flag
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((tmpisr & (DMA_FLAG_FEIF0_4 << hdma->StreamIndex)) != RESET) {
|
|
|
|
hdma->ErrorCode |= HAL_DMA_ERROR_FE; // Update error code
|
|
|
|
regs->IFCR = DMA_FLAG_FEIF0_4 << hdma->StreamIndex; // Clear the FIFO error flag
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((tmpisr & (DMA_FLAG_DMEIF0_4 << hdma->StreamIndex)) != RESET) {
|
|
|
|
hdma->ErrorCode |= HAL_DMA_ERROR_DME; // Update error code
|
|
|
|
regs->IFCR = DMA_FLAG_DMEIF0_4 << hdma->StreamIndex; // Clear the Direct Mode error flag
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hdma->ErrorCode != HAL_DMA_ERROR_NONE && (hdma->ErrorCode & HAL_DMA_ERROR_TE) != RESET) {
|
|
|
|
HAL_DMA_Abort(hdma);
|
|
|
|
regs->IFCR = (DMA_FLAG_HTIF0_4 | DMA_FLAG_TCIF0_4) << hdma->StreamIndex; // Clear the half transfer and transfer complete flags
|
|
|
|
__HAL_UNLOCK(hdma); // Process Unlocked
|
|
|
|
hdma->State= HAL_DMA_STATE_READY; // Change the DMA state
|
|
|
|
return HAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the level transfer complete flag
|
|
|
|
if (CompleteLevel == HAL_DMA_FULL_TRANSFER) {
|
|
|
|
regs->IFCR = (DMA_FLAG_HTIF0_4 | DMA_FLAG_TCIF0_4) << hdma->StreamIndex; // Clear the half transfer and transfer complete flags
|
|
|
|
__HAL_UNLOCK(hdma); // Process Unlocked
|
|
|
|
hdma->State = HAL_DMA_STATE_READY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
regs->IFCR = (DMA_FLAG_HTIF0_4) << hdma->StreamIndex; // Clear the half transfer and transfer complete flags
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
2021-01-13 00:05:49 +00:00
|
|
|
#endif
|
2021-07-13 00:17:28 +00:00
|
|
|
|
|
|
|
static void dmaTransmitBegin() {
|
|
|
|
wifiUsartDMArx.Init.MemInc = DMA_MINC_ENABLE;
|
|
|
|
if (HAL_DMA_Init((DMA_HandleTypeDef *)&wifiUsartDMArx) != HAL_OK)
|
|
|
|
Error_Handler();
|
|
|
|
|
|
|
|
if (HAL_DMA_Start(&wifiUsartDMArx, (uint32_t)&(USART1->DR), (uint32_t)WIFISERIAL.wifiRxBuf, UART_RX_BUFFER_SIZE))
|
|
|
|
Error_Handler();
|
|
|
|
|
|
|
|
USART1->CR1 |= USART_CR1_UE;
|
|
|
|
|
|
|
|
SET_BIT(USART1->CR3, USART_CR3_DMAR);
|
|
|
|
WIFI_IO1_RESET();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int storeRcvData(volatile uint8_t *bufToCpy, int32_t len) {
|
|
|
|
unsigned char tmpW = wifiDmaRcvFifo.write_cur;
|
|
|
|
|
|
|
|
if (len > UDISKBUFLEN) return 0;
|
|
|
|
|
|
|
|
if (wifiDmaRcvFifo.state[tmpW] == udisk_buf_empty) {
|
2021-09-16 11:48:24 +00:00
|
|
|
const int timeOut = 2000; // millisecond
|
2021-07-13 00:17:28 +00:00
|
|
|
dmaTransmitBegin();
|
|
|
|
if (HAL_DMA_PollForTransferCustomize(&wifiUsartDMArx, HAL_DMA_FULL_TRANSFER, timeOut) == HAL_OK) {
|
|
|
|
memcpy((unsigned char *) wifiDmaRcvFifo.bufferAddr[tmpW], (uint8_t *)bufToCpy, len);
|
|
|
|
wifiDmaRcvFifo.state[tmpW] = udisk_buf_full;
|
|
|
|
wifiDmaRcvFifo.write_cur = (tmpW + 1) % TRANS_RCV_FIFO_BLOCK_NUM;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wifi_usart_dma_init() {
|
|
|
|
#ifdef STM32F1xx
|
|
|
|
__HAL_RCC_DMA1_CLK_ENABLE();
|
|
|
|
wifiUsartDMArx.Instance = DMA1_Channel5;
|
|
|
|
#else
|
|
|
|
__HAL_RCC_DMA2_CLK_ENABLE();
|
|
|
|
wifiUsartDMArx.Instance = DMA2_Stream2;
|
|
|
|
wifiUsartDMArx.Init.Channel = DMA_CHANNEL_4;
|
|
|
|
#endif
|
|
|
|
wifiUsartDMArx.Init.Direction = DMA_PERIPH_TO_MEMORY;
|
|
|
|
wifiUsartDMArx.Init.PeriphInc = DMA_PINC_DISABLE;
|
|
|
|
wifiUsartDMArx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
|
|
|
|
wifiUsartDMArx.Init.MemDataAlignment = DMA_PDATAALIGN_BYTE;
|
|
|
|
wifiUsartDMArx.Init.Mode = DMA_NORMAL;
|
|
|
|
#ifdef STM32F4xx
|
|
|
|
wifiUsartDMArx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
|
|
|
|
#endif
|
|
|
|
wifiUsartDMArx.Init.MemInc = DMA_MINC_ENABLE;
|
|
|
|
if (HAL_DMA_Init((DMA_HandleTypeDef *)&wifiUsartDMArx) != HAL_OK)
|
|
|
|
Error_Handler();
|
|
|
|
|
|
|
|
if (HAL_DMA_Start(&wifiUsartDMArx, (uint32_t)&(USART1->DR), (uint32_t)WIFISERIAL.wifiRxBuf, UART_RX_BUFFER_SIZE))
|
|
|
|
Error_Handler();
|
|
|
|
|
|
|
|
USART1->CR1 |= USART_CR1_UE;
|
|
|
|
|
|
|
|
SET_BIT(USART1->CR3, USART_CR3_DMAR); // Enable Rx DMA Request
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < TRANS_RCV_FIFO_BLOCK_NUM; i++) {
|
|
|
|
wifiDmaRcvFifo.bufferAddr[i] = &bmp_public_buf[1024 * i];
|
|
|
|
wifiDmaRcvFifo.state[i] = udisk_buf_empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(wifiDmaRcvFifo.bufferAddr[0], 0, 1024 * TRANS_RCV_FIFO_BLOCK_NUM);
|
|
|
|
wifiDmaRcvFifo.read_cur = 0;
|
|
|
|
wifiDmaRcvFifo.write_cur = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_port_begin(uint8_t interrupt) {
|
|
|
|
WifiRxFifo.uart_read_point = 0;
|
|
|
|
WifiRxFifo.uart_write_point = 0;
|
|
|
|
|
|
|
|
#if ENABLED(MKS_WIFI_MODULE)
|
|
|
|
if (interrupt) {
|
|
|
|
WIFISERIAL.end();
|
|
|
|
for (uint16_t i = 0; i < 65535; i++) { /*nada*/ }
|
|
|
|
WIFISERIAL.begin(WIFI_BAUDRATE);
|
|
|
|
uint32_t serial_connect_timeout = millis() + 1000UL;
|
|
|
|
while (PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
WIFISERIAL.end();
|
|
|
|
USART1->CR1 &= ~USART_CR1_RXNEIE;
|
|
|
|
WIFISERIAL.begin(WIFI_UPLOAD_BAUDRATE);
|
|
|
|
wifi_usart_dma_init();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !__STM32F1__
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2020-11-19 21:51:53 +00:00
|
|
|
#if ENABLED(MKS_WIFI_MODULE)
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-01-13 03:02:35 +00:00
|
|
|
int raw_send_to_wifi(uint8_t *buf, int len) {
|
2020-10-21 17:45:27 +00:00
|
|
|
if (buf == 0 || len <= 0) return 0;
|
2021-07-13 00:17:28 +00:00
|
|
|
for (int i = 0; i < len; i++) WIFISERIAL.write(*(buf + i));
|
2020-10-21 17:45:27 +00:00
|
|
|
return len;
|
|
|
|
}
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
#endif
|
2020-09-21 01:55:02 +00:00
|
|
|
|
|
|
|
void wifi_ret_ack() {}
|
|
|
|
|
2021-01-13 03:02:35 +00:00
|
|
|
uint8_t buf_to_wifi[256];
|
2020-09-21 01:55:02 +00:00
|
|
|
int index_to_wifi = 0;
|
2021-01-13 03:02:35 +00:00
|
|
|
int package_to_wifi(WIFI_RET_TYPE type, uint8_t *buf, int len) {
|
|
|
|
uint8_t wifi_ret_head = 0xA5;
|
|
|
|
uint8_t wifi_ret_tail = 0xFC;
|
2020-09-21 01:55:02 +00:00
|
|
|
|
|
|
|
if (type == WIFI_PARA_SET) {
|
2021-07-13 00:17:28 +00:00
|
|
|
int data_offset = 4,
|
|
|
|
apLen = strlen((const char *)uiCfg.wifi_name),
|
|
|
|
keyLen = strlen((const char *)uiCfg.wifi_key);
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(buf_to_wifi);
|
2020-09-21 01:55:02 +00:00
|
|
|
index_to_wifi = 0;
|
|
|
|
|
|
|
|
buf_to_wifi[data_offset] = gCfgItems.wifi_mode_sel;
|
|
|
|
buf_to_wifi[data_offset + 1] = apLen;
|
2021-01-13 03:02:35 +00:00
|
|
|
memcpy(&buf_to_wifi[data_offset + 2], (const char *)uiCfg.wifi_name, apLen);
|
2020-09-21 01:55:02 +00:00
|
|
|
buf_to_wifi[data_offset + apLen + 2] = keyLen;
|
2021-01-13 03:02:35 +00:00
|
|
|
memcpy(&buf_to_wifi[data_offset + apLen + 3], (const char *)uiCfg.wifi_key, keyLen);
|
2020-09-21 01:55:02 +00:00
|
|
|
buf_to_wifi[data_offset + apLen + keyLen + 3] = wifi_ret_tail;
|
|
|
|
|
|
|
|
index_to_wifi = apLen + keyLen + 3;
|
|
|
|
|
|
|
|
buf_to_wifi[0] = wifi_ret_head;
|
|
|
|
buf_to_wifi[1] = type;
|
2020-10-09 21:50:17 +00:00
|
|
|
buf_to_wifi[2] = index_to_wifi & 0xFF;
|
|
|
|
buf_to_wifi[3] = (index_to_wifi >> 8) & 0xFF;
|
2020-09-21 01:55:02 +00:00
|
|
|
|
|
|
|
raw_send_to_wifi(buf_to_wifi, 5 + index_to_wifi);
|
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(buf_to_wifi);
|
2020-09-21 01:55:02 +00:00
|
|
|
index_to_wifi = 0;
|
|
|
|
}
|
|
|
|
else if (type == WIFI_TRANS_INF) {
|
|
|
|
if (len > (int)(sizeof(buf_to_wifi) - index_to_wifi - 5)) {
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(buf_to_wifi);
|
2020-09-21 01:55:02 +00:00
|
|
|
index_to_wifi = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-09-16 11:48:24 +00:00
|
|
|
if (len > 0) {
|
2020-09-21 01:55:02 +00:00
|
|
|
memcpy(&buf_to_wifi[4 + index_to_wifi], buf, len);
|
|
|
|
index_to_wifi += len;
|
|
|
|
|
|
|
|
if (index_to_wifi < 1)
|
|
|
|
return 0;
|
|
|
|
|
2021-09-16 11:48:24 +00:00
|
|
|
if (buf_to_wifi[index_to_wifi + 3] == '\n') {
|
2020-10-21 17:45:27 +00:00
|
|
|
// mask "wait" "busy" "X:"
|
2021-07-13 00:17:28 +00:00
|
|
|
if ( ((buf_to_wifi[4] == 'w') && (buf_to_wifi[5] == 'a') && (buf_to_wifi[6] == 'i') && (buf_to_wifi[7] == 't'))
|
|
|
|
|| ((buf_to_wifi[4] == 'b') && (buf_to_wifi[5] == 'u') && (buf_to_wifi[6] == 's') && (buf_to_wifi[7] == 'y'))
|
|
|
|
|| ((buf_to_wifi[4] == 'X') && (buf_to_wifi[5] == ':'))
|
2020-10-21 17:45:27 +00:00
|
|
|
) {
|
|
|
|
ZERO(buf_to_wifi);
|
2020-09-21 01:55:02 +00:00
|
|
|
index_to_wifi = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf_to_wifi[0] = wifi_ret_head;
|
|
|
|
buf_to_wifi[1] = type;
|
2020-10-09 21:50:17 +00:00
|
|
|
buf_to_wifi[2] = index_to_wifi & 0xFF;
|
|
|
|
buf_to_wifi[3] = (index_to_wifi >> 8) & 0xFF;
|
2020-09-21 01:55:02 +00:00
|
|
|
buf_to_wifi[4 + index_to_wifi] = wifi_ret_tail;
|
|
|
|
|
|
|
|
raw_send_to_wifi(buf_to_wifi, 5 + index_to_wifi);
|
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(buf_to_wifi);
|
|
|
|
index_to_wifi = 0;
|
|
|
|
}
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (type == WIFI_EXCEP_INF) {
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(buf_to_wifi);
|
2020-09-21 01:55:02 +00:00
|
|
|
|
|
|
|
buf_to_wifi[0] = wifi_ret_head;
|
|
|
|
buf_to_wifi[1] = type;
|
|
|
|
buf_to_wifi[2] = 1;
|
|
|
|
buf_to_wifi[3] = 0;
|
|
|
|
buf_to_wifi[4] = *buf;
|
|
|
|
buf_to_wifi[5] = wifi_ret_tail;
|
|
|
|
|
|
|
|
raw_send_to_wifi(buf_to_wifi, 6);
|
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(buf_to_wifi);
|
2020-09-21 01:55:02 +00:00
|
|
|
index_to_wifi = 0;
|
|
|
|
}
|
|
|
|
else if (type == WIFI_CLOUD_CFG) {
|
|
|
|
int data_offset = 4;
|
|
|
|
int urlLen = strlen((const char *)uiCfg.cloud_hostUrl);
|
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(buf_to_wifi);
|
2020-09-21 01:55:02 +00:00
|
|
|
index_to_wifi = 0;
|
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
buf_to_wifi[data_offset] = gCfgItems.cloud_enable ? 0x0A : 0x05;
|
2020-09-21 01:55:02 +00:00
|
|
|
buf_to_wifi[data_offset + 1] = urlLen;
|
2021-01-13 03:02:35 +00:00
|
|
|
memcpy(&buf_to_wifi[data_offset + 2], (const char *)uiCfg.cloud_hostUrl, urlLen);
|
2020-10-21 17:45:27 +00:00
|
|
|
buf_to_wifi[data_offset + urlLen + 2] = uiCfg.cloud_port & 0xFF;
|
|
|
|
buf_to_wifi[data_offset + urlLen + 3] = (uiCfg.cloud_port >> 8) & 0xFF;
|
2020-09-21 01:55:02 +00:00
|
|
|
buf_to_wifi[data_offset + urlLen + 4] = wifi_ret_tail;
|
|
|
|
|
|
|
|
index_to_wifi = urlLen + 4;
|
|
|
|
|
|
|
|
buf_to_wifi[0] = wifi_ret_head;
|
|
|
|
buf_to_wifi[1] = type;
|
2020-10-09 21:50:17 +00:00
|
|
|
buf_to_wifi[2] = index_to_wifi & 0xFF;
|
|
|
|
buf_to_wifi[3] = (index_to_wifi >> 8) & 0xFF;
|
2020-09-21 01:55:02 +00:00
|
|
|
|
|
|
|
raw_send_to_wifi(buf_to_wifi, 5 + index_to_wifi);
|
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(buf_to_wifi);
|
2020-09-21 01:55:02 +00:00
|
|
|
index_to_wifi = 0;
|
|
|
|
}
|
|
|
|
else if (type == WIFI_CLOUD_UNBIND) {
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(buf_to_wifi);
|
2020-09-21 01:55:02 +00:00
|
|
|
|
|
|
|
buf_to_wifi[0] = wifi_ret_head;
|
|
|
|
buf_to_wifi[1] = type;
|
|
|
|
buf_to_wifi[2] = 0;
|
|
|
|
buf_to_wifi[3] = 0;
|
|
|
|
buf_to_wifi[4] = wifi_ret_tail;
|
|
|
|
|
|
|
|
raw_send_to_wifi(buf_to_wifi, 5);
|
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(buf_to_wifi);
|
2020-09-21 01:55:02 +00:00
|
|
|
index_to_wifi = 0;
|
|
|
|
}
|
2021-01-14 04:41:09 +00:00
|
|
|
return 1;
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
|
2021-01-14 04:41:09 +00:00
|
|
|
#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); }
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
void set_cur_file_sys(int fileType) { gCfgItems.fileSysType = fileType; }
|
2020-09-21 01:55:02 +00:00
|
|
|
|
|
|
|
void get_file_list(char *path) {
|
2020-10-21 17:45:27 +00:00
|
|
|
if (!path) return;
|
2020-09-21 01:55:02 +00:00
|
|
|
|
|
|
|
if (gCfgItems.fileSysType == FILE_SYS_SD) {
|
2020-10-21 17:45:27 +00:00
|
|
|
TERN_(SDSUPPORT, card.mount());
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
else if (gCfgItems.fileSysType == FILE_SYS_USB) {
|
2020-10-21 17:45:27 +00:00
|
|
|
// udisk
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
Explore_Disk(path, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
char wait_ip_back_flag = 0;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int write_index;
|
2021-04-13 20:07:25 +00:00
|
|
|
uint8_t saveFileName[30];
|
2021-01-14 04:41:09 +00:00
|
|
|
uint8_t fileTransfer;
|
2020-09-21 01:55:02 +00:00
|
|
|
uint32_t fileLen;
|
|
|
|
uint32_t tick_begin;
|
|
|
|
uint32_t tick_end;
|
|
|
|
} FILE_WRITER;
|
|
|
|
|
|
|
|
FILE_WRITER file_writer;
|
|
|
|
|
|
|
|
int32_t lastFragment = 0;
|
|
|
|
|
2021-01-14 04:41:09 +00:00
|
|
|
char saveFilePath[50];
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-01-14 04:41:09 +00:00
|
|
|
static SdFile upload_file, *upload_curDir;
|
|
|
|
static filepos_t pos;
|
2020-09-21 01:55:02 +00:00
|
|
|
|
|
|
|
int write_to_file(char *buf, int len) {
|
2021-01-14 04:41:09 +00:00
|
|
|
int i;
|
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
public_buf[file_writer.write_index++] = buf[i];
|
2020-09-21 01:55:02 +00:00
|
|
|
if (file_writer.write_index >= 512) {
|
2021-01-14 04:41:09 +00:00
|
|
|
res = upload_file.write(public_buf, file_writer.write_index);
|
|
|
|
|
|
|
|
if (res == -1) {
|
|
|
|
upload_file.close();
|
2021-05-08 09:35:35 +00:00
|
|
|
const char * const fname = card.diveToFile(false, upload_curDir, saveFilePath);
|
2021-01-14 04:41:09 +00:00
|
|
|
|
|
|
|
if (upload_file.open(upload_curDir, fname, O_WRITE)) {
|
|
|
|
upload_file.setpos(&pos);
|
|
|
|
res = upload_file.write(public_buf, file_writer.write_index);
|
|
|
|
}
|
|
|
|
}
|
2021-01-14 06:23:56 +00:00
|
|
|
|
|
|
|
if (res == -1) return -1;
|
|
|
|
|
2021-01-14 04:41:09 +00:00
|
|
|
upload_file.getpos(&pos);
|
2020-09-21 01:55:02 +00:00
|
|
|
file_writer.write_index = 0;
|
|
|
|
}
|
|
|
|
}
|
2021-01-14 04:41:09 +00:00
|
|
|
|
|
|
|
if (res == -1) {
|
2021-01-14 06:23:56 +00:00
|
|
|
ZERO(public_buf);
|
2021-01-14 04:41:09 +00:00
|
|
|
file_writer.write_index = 0;
|
2021-09-16 11:48:24 +00:00
|
|
|
return -1;
|
2021-01-14 04:41:09 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 01:55:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
#define ESP_PROTOC_HEAD (uint8_t)0xA5
|
|
|
|
#define ESP_PROTOC_TAIL (uint8_t)0xFC
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
#define ESP_TYPE_NET (uint8_t)0x0
|
|
|
|
#define ESP_TYPE_GCODE (uint8_t)0x1
|
2020-09-21 01:55:02 +00:00
|
|
|
#define ESP_TYPE_FILE_FIRST (uint8_t)0x2
|
2021-07-13 00:17:28 +00:00
|
|
|
#define ESP_TYPE_FILE_FRAGMENT (uint8_t)0x3
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
#define ESP_TYPE_WIFI_LIST (uint8_t)0x4
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
uint8_t esp_msg_buf[UART_RX_BUFFER_SIZE] = { 0 };
|
2020-09-21 01:55:02 +00:00
|
|
|
uint16_t esp_msg_index = 0;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint8_t head;
|
|
|
|
uint8_t type;
|
|
|
|
uint16_t dataLen;
|
|
|
|
uint8_t *data;
|
|
|
|
uint8_t tail;
|
|
|
|
} ESP_PROTOC_FRAME;
|
|
|
|
|
|
|
|
static int cut_msg_head(uint8_t *msg, uint16_t msgLen, uint16_t cutLen) {
|
|
|
|
if (msgLen < cutLen) return 0;
|
|
|
|
|
|
|
|
else if (msgLen == cutLen) {
|
|
|
|
memset(msg, 0, msgLen);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < (msgLen - cutLen); i++)
|
|
|
|
msg[i] = msg[cutLen + i];
|
|
|
|
|
|
|
|
memset(&msg[msgLen - cutLen], 0, cutLen);
|
|
|
|
|
|
|
|
return msgLen - cutLen;
|
|
|
|
}
|
|
|
|
|
2021-03-30 01:36:37 +00:00
|
|
|
uint8_t Explore_Disk(char *path , uint8_t recu_level) {
|
2020-10-21 17:45:27 +00:00
|
|
|
char tmp[200];
|
|
|
|
char Fstream[200];
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
if (!path) return 0;
|
2020-09-21 01:55:02 +00:00
|
|
|
|
|
|
|
const uint8_t fileCnt = card.get_num_Files();
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < fileCnt; i++) {
|
2020-10-21 17:45:27 +00:00
|
|
|
card.getfilename_sorted(SD_ORDER(i, fileCnt));
|
2021-01-14 06:23:56 +00:00
|
|
|
ZERO(tmp);
|
2021-01-14 04:41:09 +00:00
|
|
|
strcpy(tmp, card.filename);
|
2020-10-21 17:45:27 +00:00
|
|
|
|
|
|
|
ZERO(Fstream);
|
|
|
|
strcpy(Fstream, tmp);
|
|
|
|
|
|
|
|
if (card.flag.filenameIsDir && recu_level <= 10)
|
2021-01-14 06:23:56 +00:00
|
|
|
strcat_P(Fstream, PSTR(".DIR"));
|
2020-10-21 17:45:27 +00:00
|
|
|
|
2021-01-14 06:23:56 +00:00
|
|
|
strcat_P(Fstream, PSTR("\r\n"));
|
2021-01-13 03:02:35 +00:00
|
|
|
send_to_wifi((uint8_t*)Fstream, strlen(Fstream));
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fileCnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wifi_gcode_exec(uint8_t *cmd_line) {
|
2021-04-13 20:07:25 +00:00
|
|
|
int8_t tempBuf[100] = { 0 };
|
2020-09-21 01:55:02 +00:00
|
|
|
uint8_t *tmpStr = 0;
|
2021-09-16 11:48:24 +00:00
|
|
|
int cmd_value;
|
2020-09-21 01:55:02 +00:00
|
|
|
volatile int print_rate;
|
2020-10-21 17:45:27 +00:00
|
|
|
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';
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
tmpStr = (uint8_t *)strchr((char *)cmd_line, '\r');
|
|
|
|
if (tmpStr) *tmpStr = '\0';
|
|
|
|
|
|
|
|
tmpStr = (uint8_t *)strchr((char *)cmd_line, '*');
|
|
|
|
if (tmpStr) *tmpStr = '\0';
|
|
|
|
|
|
|
|
tmpStr = (uint8_t *)strchr((char *)cmd_line, 'M');
|
2020-09-21 01:55:02 +00:00
|
|
|
if (tmpStr) {
|
|
|
|
cmd_value = atoi((char *)(tmpStr + 1));
|
2020-10-21 17:45:27 +00:00
|
|
|
tmpStr = (uint8_t *)strchr((char *)tmpStr, ' ');
|
2020-09-21 01:55:02 +00:00
|
|
|
|
|
|
|
switch (cmd_value) {
|
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
case 20: // M20: Print SD / µdisk file
|
2021-01-14 04:41:09 +00:00
|
|
|
file_writer.fileTransfer = 0;
|
2020-09-21 01:55:02 +00:00
|
|
|
if (uiCfg.print_state == IDLE) {
|
|
|
|
int index = 0;
|
|
|
|
|
|
|
|
if (tmpStr == 0) {
|
|
|
|
gCfgItems.fileSysType = FILE_SYS_SD;
|
2021-05-18 02:17:22 +00:00
|
|
|
send_to_wifi((uint8_t *)(STR_BEGIN_FILE_LIST "\r\n"), strlen(STR_BEGIN_FILE_LIST "\r\n"));
|
2020-09-21 01:55:02 +00:00
|
|
|
get_file_list((char *)"0:/");
|
2021-05-18 02:17:22 +00:00
|
|
|
send_to_wifi((uint8_t *)(STR_END_FILE_LIST "\r\n"), strlen(STR_END_FILE_LIST "\r\n"));
|
2021-01-14 04:41:09 +00:00
|
|
|
SEND_OK_TO_WIFI;
|
2020-09-21 01:55:02 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
while (tmpStr[index] == ' ') index++;
|
2020-09-21 01:55:02 +00:00
|
|
|
|
|
|
|
if (gCfgItems.wifi_type == ESP_WIFI) {
|
2021-04-13 20:07:25 +00:00
|
|
|
char *path = (char *)tempBuf;
|
2020-09-21 01:55:02 +00:00
|
|
|
|
|
|
|
if (strlen((char *)&tmpStr[index]) < 80) {
|
2021-05-18 02:17:22 +00:00
|
|
|
send_to_wifi((uint8_t *)(STR_BEGIN_FILE_LIST "\r\n"), strlen(STR_BEGIN_FILE_LIST "\r\n"));
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
if (strncmp((char *)&tmpStr[index], "1:", 2) == 0)
|
2020-09-21 01:55:02 +00:00
|
|
|
gCfgItems.fileSysType = FILE_SYS_SD;
|
2020-10-21 17:45:27 +00:00
|
|
|
else if (strncmp((char *)&tmpStr[index], "0:", 2) == 0)
|
2020-09-21 01:55:02 +00:00
|
|
|
gCfgItems.fileSysType = FILE_SYS_USB;
|
2020-10-21 17:45:27 +00:00
|
|
|
|
2020-09-21 01:55:02 +00:00
|
|
|
strcpy((char *)path, (char *)&tmpStr[index]);
|
|
|
|
get_file_list(path);
|
2021-05-18 02:17:22 +00:00
|
|
|
send_to_wifi((uint8_t *)(STR_END_FILE_LIST "\r\n"), strlen(STR_END_FILE_LIST "\r\n"));
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
2021-01-14 04:41:09 +00:00
|
|
|
SEND_OK_TO_WIFI;
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
case 21: SEND_OK_TO_WIFI; break; // Init SD card
|
2020-09-21 01:55:02 +00:00
|
|
|
|
|
|
|
case 23:
|
2021-07-13 00:17:28 +00:00
|
|
|
// Select the file
|
2020-09-21 01:55:02 +00:00
|
|
|
if (uiCfg.print_state == IDLE) {
|
|
|
|
int index = 0;
|
2020-10-21 17:45:27 +00:00
|
|
|
while (tmpStr[index] == ' ') index++;
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-01-14 06:23:56 +00:00
|
|
|
if (strstr_P((char *)&tmpStr[index], PSTR(".g")) || strstr_P((char *)&tmpStr[index], PSTR(".G"))) {
|
2020-09-21 01:55:02 +00:00
|
|
|
if (strlen((char *)&tmpStr[index]) < 80) {
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(list_file.file_name[sel_id]);
|
2021-01-14 04:41:09 +00:00
|
|
|
ZERO(list_file.long_name[sel_id]);
|
|
|
|
uint8_t has_path_selected = 0;
|
2020-09-21 01:55:02 +00:00
|
|
|
|
|
|
|
if (gCfgItems.wifi_type == ESP_WIFI) {
|
2021-01-14 06:23:56 +00:00
|
|
|
if (strncmp_P((char *)&tmpStr[index], PSTR("1:"), 2) == 0) {
|
2020-09-21 01:55:02 +00:00
|
|
|
gCfgItems.fileSysType = FILE_SYS_SD;
|
2021-01-14 04:41:09 +00:00
|
|
|
has_path_selected = 1;
|
|
|
|
}
|
2021-01-14 06:23:56 +00:00
|
|
|
else if (strncmp_P((char *)&tmpStr[index], PSTR("0:"), 2) == 0) {
|
2020-09-21 01:55:02 +00:00
|
|
|
gCfgItems.fileSysType = FILE_SYS_USB;
|
2021-01-14 04:41:09 +00:00
|
|
|
has_path_selected = 1;
|
|
|
|
}
|
2020-10-21 17:45:27 +00:00
|
|
|
else if (tmpStr[index] != '/')
|
2021-01-14 06:23:56 +00:00
|
|
|
strcat_P((char *)list_file.file_name[sel_id], PSTR("/"));
|
2021-01-14 04:41:09 +00:00
|
|
|
|
|
|
|
if (file_writer.fileTransfer == 1) {
|
2021-04-13 20:07:25 +00:00
|
|
|
char dosName[FILENAME_LENGTH];
|
2021-01-14 04:41:09 +00:00
|
|
|
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]);
|
2021-01-14 06:23:56 +00:00
|
|
|
strcat_P((char *)list_file.file_name[sel_id], PSTR("/"));
|
2021-01-14 04:41:09 +00:00
|
|
|
}
|
|
|
|
else strcat((char *)fileName, (char *)&tmpStr[index]);
|
2021-01-14 06:23:56 +00:00
|
|
|
if (!longName2DosName((const char *)fileName, dosName))
|
|
|
|
strcpy_P(list_file.file_name[sel_id], PSTR("notValid"));
|
2021-04-13 20:07:25 +00:00
|
|
|
strcat((char *)list_file.file_name[sel_id], dosName);
|
|
|
|
strcat((char *)list_file.long_name[sel_id], dosName);
|
2021-01-14 04:41:09 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
strcat((char *)list_file.file_name[sel_id], (char *)&tmpStr[index]);
|
|
|
|
strcat((char *)list_file.long_name[sel_id], (char *)&tmpStr[index]);
|
|
|
|
}
|
2020-09-21 01:55:02 +00:00
|
|
|
|
|
|
|
}
|
2020-10-21 17:45:27 +00:00
|
|
|
else
|
2020-09-21 01:55:02 +00:00
|
|
|
strcpy(list_file.file_name[sel_id], (char *)&tmpStr[index]);
|
|
|
|
|
|
|
|
char *cur_name=strrchr(list_file.file_name[sel_id],'/');
|
|
|
|
|
|
|
|
card.openFileRead(cur_name);
|
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
if (card.isFileOpen())
|
2021-01-13 03:02:35 +00:00
|
|
|
send_to_wifi((uint8_t *)"File selected\r\n", strlen("File selected\r\n"));
|
2020-09-21 01:55:02 +00:00
|
|
|
else {
|
2021-01-13 03:02:35 +00:00
|
|
|
send_to_wifi((uint8_t *)"file.open failed\r\n", strlen("file.open failed\r\n"));
|
2021-01-14 06:23:56 +00:00
|
|
|
strcpy_P(list_file.file_name[sel_id], PSTR("notValid"));
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
2021-01-14 04:41:09 +00:00
|
|
|
SEND_OK_TO_WIFI;
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 24:
|
2021-01-14 06:23:56 +00:00
|
|
|
if (strcmp_P(list_file.file_name[sel_id], PSTR("notValid")) != 0) {
|
2020-09-21 01:55:02 +00:00
|
|
|
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();
|
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
#if ENABLED(SDSUPPORT)
|
|
|
|
if (!gcode_preview_over) {
|
|
|
|
char *cur_name = strrchr(list_file.file_name[sel_id], '/');
|
|
|
|
|
|
|
|
SdFile file;
|
|
|
|
SdFile *curDir;
|
2021-05-08 09:35:35 +00:00
|
|
|
card.abortFilePrintNow();
|
|
|
|
const char * const fname = card.diveToFile(false, curDir, cur_name);
|
2020-10-21 17:45:27 +00:00
|
|
|
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;
|
2021-09-21 11:25:13 +00:00
|
|
|
#if HAS_EXTRUDERS
|
2021-07-13 00:17:28 +00:00
|
|
|
planner.flow_percentage[0] = 100;
|
|
|
|
planner.e_factor[0] = planner.flow_percentage[0] * 0.01f;
|
|
|
|
#endif
|
2021-09-21 11:25:13 +00:00
|
|
|
#if HAS_MULTI_EXTRUDER
|
2020-10-21 17:45:27 +00:00
|
|
|
planner.flow_percentage[1] = 100;
|
|
|
|
planner.e_factor[1] = planner.flow_percentage[1] * 0.01f;
|
|
|
|
#endif
|
2021-05-08 09:35:35 +00:00
|
|
|
card.startOrResumeFilePrinting();
|
2020-10-21 17:45:27 +00:00
|
|
|
TERN_(POWER_LOSS_RECOVERY, recovery.prepare());
|
|
|
|
once_flag = false;
|
|
|
|
}
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
2020-10-21 17:45:27 +00:00
|
|
|
#endif
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
else if (uiCfg.print_state == PAUSED) {
|
|
|
|
uiCfg.print_state = RESUMING;
|
|
|
|
clear_cur_ui();
|
|
|
|
start_print_time();
|
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
if (gCfgItems.from_flash_pic)
|
|
|
|
flash_preview_begin = true;
|
2020-09-21 01:55:02 +00:00
|
|
|
else
|
2020-10-21 17:45:27 +00:00
|
|
|
default_preview_flg = true;
|
|
|
|
lv_draw_printing();
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
else if (uiCfg.print_state == REPRINTING) {
|
|
|
|
uiCfg.print_state = REPRINTED;
|
|
|
|
clear_cur_ui();
|
|
|
|
start_print_time();
|
2020-10-21 17:45:27 +00:00
|
|
|
if (gCfgItems.from_flash_pic)
|
|
|
|
flash_preview_begin = true;
|
2020-09-21 01:55:02 +00:00
|
|
|
else
|
2020-10-21 17:45:27 +00:00
|
|
|
default_preview_flg = true;
|
2020-09-21 01:55:02 +00:00
|
|
|
lv_draw_printing();
|
|
|
|
}
|
|
|
|
}
|
2021-01-14 04:41:09 +00:00
|
|
|
SEND_OK_TO_WIFI;
|
2020-09-21 01:55:02 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 25:
|
2021-07-13 00:17:28 +00:00
|
|
|
// Pause print file
|
2020-09-21 01:55:02 +00:00
|
|
|
if (uiCfg.print_state == WORKING) {
|
|
|
|
stop_print_time();
|
|
|
|
|
|
|
|
clear_cur_ui();
|
|
|
|
|
|
|
|
#if ENABLED(SDSUPPORT)
|
2020-10-21 17:45:27 +00:00
|
|
|
card.pauseSDPrint();
|
|
|
|
uiCfg.print_state = PAUSING;
|
|
|
|
#endif
|
|
|
|
if (gCfgItems.from_flash_pic)
|
|
|
|
flash_preview_begin = true;
|
2020-09-21 01:55:02 +00:00
|
|
|
else
|
2020-10-21 17:45:27 +00:00
|
|
|
default_preview_flg = true;
|
2020-09-21 01:55:02 +00:00
|
|
|
lv_draw_printing();
|
2021-01-14 04:41:09 +00:00
|
|
|
SEND_OK_TO_WIFI;
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 26:
|
2021-07-13 00:17:28 +00:00
|
|
|
// Stop print file
|
2020-09-21 01:55:02 +00:00
|
|
|
if ((uiCfg.print_state == WORKING) || (uiCfg.print_state == PAUSED) || (uiCfg.print_state == REPRINTING)) {
|
|
|
|
stop_print_time();
|
|
|
|
|
|
|
|
clear_cur_ui();
|
2020-10-21 17:45:27 +00:00
|
|
|
#if ENABLED(SDSUPPORT)
|
|
|
|
uiCfg.print_state = IDLE;
|
2021-05-08 09:35:35 +00:00
|
|
|
card.abortFilePrintSoon();
|
2020-09-21 01:55:02 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
lv_draw_ready_print();
|
|
|
|
|
2021-01-14 04:41:09 +00:00
|
|
|
SEND_OK_TO_WIFI;
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 27:
|
2021-07-13 00:17:28 +00:00
|
|
|
// Report print rate
|
2020-09-21 01:55:02 +00:00
|
|
|
if ((uiCfg.print_state == WORKING) || (uiCfg.print_state == PAUSED)|| (uiCfg.print_state == REPRINTING)) {
|
|
|
|
print_rate = uiCfg.totalSend;
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(tempBuf);
|
2021-04-13 20:07:25 +00:00
|
|
|
sprintf_P((char *)tempBuf, PSTR("M27 %d\r\n"), print_rate);
|
|
|
|
send_to_wifi((uint8_t *)tempBuf, strlen((char *)tempBuf));
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 28:
|
2021-07-13 00:17:28 +00:00
|
|
|
// Begin to transfer file to filesys
|
2020-09-21 01:55:02 +00:00
|
|
|
if (uiCfg.print_state == IDLE) {
|
|
|
|
|
|
|
|
int index = 0;
|
2020-10-21 17:45:27 +00:00
|
|
|
while (tmpStr[index] == ' ') index++;
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-01-14 06:23:56 +00:00
|
|
|
if (strstr_P((char *)&tmpStr[index], PSTR(".g")) || strstr_P((char *)&tmpStr[index], PSTR(".G"))) {
|
2021-04-13 20:07:25 +00:00
|
|
|
strcpy((char *)file_writer.saveFileName, (char *)&tmpStr[index]);
|
2020-09-21 01:55:02 +00:00
|
|
|
|
|
|
|
if (gCfgItems.fileSysType == FILE_SYS_SD) {
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(tempBuf);
|
2021-04-13 20:07:25 +00:00
|
|
|
sprintf_P((char *)tempBuf, PSTR("%s"), file_writer.saveFileName);
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
else if (gCfgItems.fileSysType == FILE_SYS_USB) {
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(tempBuf);
|
2021-04-13 20:07:25 +00:00
|
|
|
sprintf_P((char *)tempBuf, PSTR("%s"), (char *)file_writer.saveFileName);
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
mount_file_sys(gCfgItems.fileSysType);
|
|
|
|
|
|
|
|
#if ENABLED(SDSUPPORT)
|
2020-10-21 17:45:27 +00:00
|
|
|
char *cur_name = strrchr(list_file.file_name[sel_id], '/');
|
|
|
|
card.openFileWrite(cur_name);
|
|
|
|
if (card.isFileOpen()) {
|
|
|
|
ZERO(file_writer.saveFileName);
|
2021-04-13 20:07:25 +00:00
|
|
|
strcpy((char *)file_writer.saveFileName, (char *)&tmpStr[index]);
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(tempBuf);
|
2021-04-13 20:07:25 +00:00
|
|
|
sprintf_P((char *)tempBuf, PSTR("Writing to file: %s\r\n"), (char *)file_writer.saveFileName);
|
2020-10-21 17:45:27 +00:00
|
|
|
wifi_ret_ack();
|
2021-04-13 20:07:25 +00:00
|
|
|
send_to_wifi((uint8_t *)tempBuf, strlen((char *)tempBuf));
|
2020-10-21 17:45:27 +00:00
|
|
|
wifi_link_state = WIFI_WAIT_TRANS_START;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
wifi_link_state = WIFI_CONNECTED;
|
|
|
|
clear_cur_ui();
|
|
|
|
lv_draw_dialog(DIALOG_TRANSFER_NO_DEVICE);
|
|
|
|
}
|
2020-09-21 01:55:02 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2021-02-08 02:36:57 +00:00
|
|
|
|
2020-09-21 01:55:02 +00:00
|
|
|
case 105:
|
|
|
|
case 991:
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(tempBuf);
|
2020-09-21 01:55:02 +00:00
|
|
|
if (cmd_value == 105) {
|
2021-02-08 02:36:57 +00:00
|
|
|
|
2021-01-14 04:41:09 +00:00
|
|
|
SEND_OK_TO_WIFI;
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-04-13 20:07:25 +00:00
|
|
|
char *outBuf = (char *)tempBuf;
|
2021-04-13 04:40:32 +00:00
|
|
|
char tbuf[34];
|
2021-02-08 02:36:57 +00:00
|
|
|
|
2021-04-24 01:19:23 +00:00
|
|
|
sprintf_P(tbuf, PSTR("%d /%d"), thermalManager.wholeDegHotend(0), thermalManager.degTargetHotend(0));
|
2021-02-08 02:36:57 +00:00
|
|
|
|
|
|
|
const int tlen = strlen(tbuf);
|
|
|
|
sprintf_P(outBuf, PSTR("T:%s"), tbuf);
|
|
|
|
outBuf += 2 + tlen;
|
|
|
|
|
|
|
|
strcpy_P(outBuf, PSTR(" B:"));
|
|
|
|
outBuf += 3;
|
2020-09-21 01:55:02 +00:00
|
|
|
#if HAS_HEATED_BED
|
2021-04-24 01:19:23 +00:00
|
|
|
sprintf_P(outBuf, PSTR("%d /%d"), thermalManager.wholeDegBed(), thermalManager.degTargetBed());
|
2020-09-21 01:55:02 +00:00
|
|
|
#else
|
2021-02-08 02:36:57 +00:00
|
|
|
strcpy_P(outBuf, PSTR("0 /0"));
|
2020-09-21 01:55:02 +00:00
|
|
|
#endif
|
2021-02-08 02:36:57 +00:00
|
|
|
outBuf += 4;
|
|
|
|
|
|
|
|
strcat_P(outBuf, PSTR(" T0:"));
|
|
|
|
strcat(outBuf, tbuf);
|
|
|
|
outBuf += 4 + tlen;
|
|
|
|
|
|
|
|
strcat_P(outBuf, PSTR(" T1:"));
|
|
|
|
outBuf += 4;
|
|
|
|
#if HAS_MULTI_HOTEND
|
2021-04-24 01:19:23 +00:00
|
|
|
sprintf_P(outBuf, PSTR("%d /%d"), thermalManager.wholeDegHotend(1), thermalManager.degTargetHotend(1));
|
2020-09-21 01:55:02 +00:00
|
|
|
#else
|
2021-07-13 00:17:28 +00:00
|
|
|
strcpy_P(outBuf, PSTR("0 /0"));
|
2020-09-21 01:55:02 +00:00
|
|
|
#endif
|
2021-02-08 02:36:57 +00:00
|
|
|
outBuf += 4;
|
|
|
|
|
|
|
|
strcat_P(outBuf, PSTR(" @:0 B@:0\r\n"));
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
else {
|
2021-04-13 20:07:25 +00:00
|
|
|
sprintf_P((char *)tempBuf, PSTR("T:%d /%d B:%d /%d T0:%d /%d T1:%d /%d @:0 B@:0\r\n"),
|
2021-04-24 01:19:23 +00:00
|
|
|
thermalManager.wholeDegHotend(0), thermalManager.degTargetHotend(0),
|
2021-07-13 00:17:28 +00:00
|
|
|
TERN0(HAS_HEATED_BED, thermalManager.wholeDegBed()),
|
|
|
|
TERN0(HAS_HEATED_BED, thermalManager.degTargetBed()),
|
2021-04-24 01:19:23 +00:00
|
|
|
thermalManager.wholeDegHotend(0), thermalManager.degTargetHotend(0),
|
2021-07-13 00:17:28 +00:00
|
|
|
TERN0(HAS_MULTI_HOTEND, thermalManager.wholeDegHotend(1)),
|
|
|
|
TERN0(HAS_MULTI_HOTEND, thermalManager.degTargetHotend(1))
|
2020-09-21 01:55:02 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-04-13 20:07:25 +00:00
|
|
|
send_to_wifi((uint8_t *)tempBuf, strlen((char *)tempBuf));
|
2021-09-26 02:11:31 +00:00
|
|
|
queue.enqueue_one(F("M105"));
|
2020-09-21 01:55:02 +00:00
|
|
|
break;
|
2020-10-21 17:45:27 +00:00
|
|
|
|
2020-09-21 01:55:02 +00:00
|
|
|
case 992:
|
|
|
|
if ((uiCfg.print_state == WORKING) || (uiCfg.print_state == PAUSED)) {
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(tempBuf);
|
2021-04-13 20:07:25 +00:00
|
|
|
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);
|
2020-09-21 01:55:02 +00:00
|
|
|
wifi_ret_ack();
|
2021-04-13 20:07:25 +00:00
|
|
|
send_to_wifi((uint8_t *)tempBuf, strlen((char *)tempBuf));
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
break;
|
2020-10-21 17:45:27 +00:00
|
|
|
|
2020-09-21 01:55:02 +00:00
|
|
|
case 994:
|
|
|
|
if ((uiCfg.print_state == WORKING) || (uiCfg.print_state == PAUSED)) {
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(tempBuf);
|
|
|
|
if (strlen((char *)list_file.file_name[sel_id]) > (100 - 1)) return;
|
2021-04-13 20:07:25 +00:00
|
|
|
sprintf_P((char *)tempBuf, PSTR("M994 %s;%d\n"), list_file.file_name[sel_id], (int)gCfgItems.curFilesize);
|
2020-09-21 01:55:02 +00:00
|
|
|
wifi_ret_ack();
|
2021-04-13 20:07:25 +00:00
|
|
|
send_to_wifi((uint8_t *)tempBuf, strlen((char *)tempBuf));
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
break;
|
2020-10-21 17:45:27 +00:00
|
|
|
|
2020-09-21 01:55:02 +00:00
|
|
|
case 997:
|
|
|
|
if (uiCfg.print_state == IDLE) {
|
|
|
|
wifi_ret_ack();
|
2021-01-13 03:02:35 +00:00
|
|
|
send_to_wifi((uint8_t *)"M997 IDLE\r\n", strlen("M997 IDLE\r\n"));
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
else if (uiCfg.print_state == WORKING) {
|
|
|
|
wifi_ret_ack();
|
2021-01-13 03:02:35 +00:00
|
|
|
send_to_wifi((uint8_t *)"M997 PRINTING\r\n", strlen("M997 PRINTING\r\n"));
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
else if (uiCfg.print_state == PAUSED) {
|
|
|
|
wifi_ret_ack();
|
2021-01-14 04:41:09 +00:00
|
|
|
send_to_wifi((uint8_t *)"M997 PAUSE\r\n", strlen("M997 PAUSE\r\n"));
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
else if (uiCfg.print_state == REPRINTING) {
|
|
|
|
wifi_ret_ack();
|
2021-01-13 03:02:35 +00:00
|
|
|
send_to_wifi((uint8_t *)"M997 PAUSE\r\n", strlen("M997 PAUSE\r\n"));
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
2021-01-17 02:43:46 +00:00
|
|
|
if (!uiCfg.command_send) get_wifi_list_command_send();
|
2020-09-21 01:55:02 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 998:
|
|
|
|
if (uiCfg.print_state == IDLE) {
|
2021-07-13 00:17:28 +00:00
|
|
|
const int v = atoi((char *)tmpStr);
|
|
|
|
if (v == 0 || v == 1) set_cur_file_sys(v);
|
2020-09-21 01:55:02 +00:00
|
|
|
wifi_ret_ack();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 115:
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(tempBuf);
|
2021-01-14 04:41:09 +00:00
|
|
|
SEND_OK_TO_WIFI;
|
|
|
|
send_to_wifi((uint8_t *)"FIRMWARE_NAME:Robin_nano\r\n", strlen("FIRMWARE_NAME:Robin_nano\r\n"));
|
2020-09-21 01:55:02 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2021-01-14 06:23:56 +00:00
|
|
|
strcat_P((char *)cmd_line, PSTR("\n"));
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
if (espGcodeFifo.wait_tick > 5) {
|
2021-07-13 00:17:28 +00:00
|
|
|
const uint32_t left = espGcodeFifo.r > espGcodeFifo.w
|
|
|
|
? espGcodeFifo.r - espGcodeFifo.w - 1
|
|
|
|
: WIFI_GCODE_BUFFER_SIZE + espGcodeFifo.r - espGcodeFifo.w - 1;
|
2020-10-21 17:45:27 +00:00
|
|
|
|
|
|
|
if (left >= strlen((const char *)cmd_line)) {
|
2021-07-13 00:17:28 +00:00
|
|
|
for (uint32_t index = 0; index < strlen((const char *)cmd_line); index++) {
|
2020-10-21 17:45:27 +00:00
|
|
|
espGcodeFifo.Buffer[espGcodeFifo.w] = cmd_line[index] ;
|
|
|
|
espGcodeFifo.w = (espGcodeFifo.w + 1) % WIFI_GCODE_BUFFER_SIZE;
|
|
|
|
}
|
2020-09-21 01:55:02 +00:00
|
|
|
if (left - WIFI_GCODE_BUFFER_LEAST_SIZE >= strlen((const char *)cmd_line))
|
2021-01-14 04:41:09 +00:00
|
|
|
SEND_OK_TO_WIFI;
|
2020-09-21 01:55:02 +00:00
|
|
|
else
|
|
|
|
need_ok_later = true;
|
2020-10-21 17:45:27 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-21 01:55:02 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-10-21 17:45:27 +00:00
|
|
|
else {
|
2021-01-14 06:23:56 +00:00
|
|
|
strcat_P((char *)cmd_line, PSTR("\n"));
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
if (espGcodeFifo.wait_tick > 5) {
|
2021-07-13 00:17:28 +00:00
|
|
|
const uint32_t left_g = espGcodeFifo.r > espGcodeFifo.w
|
|
|
|
? espGcodeFifo.r - espGcodeFifo.w - 1
|
|
|
|
: WIFI_GCODE_BUFFER_SIZE + espGcodeFifo.r - espGcodeFifo.w - 1;
|
2020-10-21 17:45:27 +00:00
|
|
|
|
2020-09-21 01:55:02 +00:00
|
|
|
if (left_g >= strlen((const char *)cmd_line)) {
|
2021-07-13 00:17:28 +00:00
|
|
|
for (uint32_t index = 0; index < strlen((const char *)cmd_line); index++) {
|
2020-09-21 01:55:02 +00:00
|
|
|
espGcodeFifo.Buffer[espGcodeFifo.w] = cmd_line[index] ;
|
2020-10-21 17:45:27 +00:00
|
|
|
espGcodeFifo.w = (espGcodeFifo.w + 1) % WIFI_GCODE_BUFFER_SIZE;
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
2020-10-21 17:45:27 +00:00
|
|
|
if (left_g - WIFI_GCODE_BUFFER_LEAST_SIZE >= strlen((const char *)cmd_line))
|
2021-01-14 04:41:09 +00:00
|
|
|
SEND_OK_TO_WIFI;
|
2020-10-21 17:45:27 +00:00
|
|
|
else
|
|
|
|
need_ok_later = true;
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t charAtArray(const uint8_t *_array, uint32_t _arrayLen, uint8_t _char) {
|
|
|
|
for (uint32_t i = 0; i < _arrayLen; i++)
|
|
|
|
if (*(_array + i) == _char) return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void get_wifi_list_command_send() {
|
2021-01-13 03:02:35 +00:00
|
|
|
uint8_t cmd_wifi_list[] = { 0xA5, 0x07, 0x00, 0x00, 0xFC };
|
|
|
|
raw_send_to_wifi(cmd_wifi_list, COUNT(cmd_wifi_list));
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void net_msg_handle(uint8_t * msg, uint16_t msgLen) {
|
|
|
|
int wifiNameLen, wifiKeyLen, hostLen, id_len, ver_len;
|
|
|
|
|
|
|
|
if (msgLen <= 0) return;
|
2020-10-21 17:45:27 +00:00
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
// IP address
|
2021-01-14 06:23:56 +00:00
|
|
|
sprintf_P(ipPara.ip_addr, PSTR("%d.%d.%d.%d"), msg[0], msg[1], msg[2], msg[3]);
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
// port connect state
|
2020-10-21 17:45:27 +00:00
|
|
|
switch (msg[6]) {
|
|
|
|
case 0x0A: wifi_link_state = WIFI_CONNECTED; break;
|
|
|
|
case 0x0E: wifi_link_state = WIFI_EXCEPTION; break;
|
|
|
|
default: wifi_link_state = WIFI_NOT_CONFIG; break;
|
|
|
|
}
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
// mode
|
2020-09-21 01:55:02 +00:00
|
|
|
wifiPara.mode = msg[7];
|
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
// WiFi name
|
2020-09-21 01:55:02 +00:00
|
|
|
wifiNameLen = msg[8];
|
|
|
|
wifiKeyLen = msg[9 + wifiNameLen];
|
|
|
|
if (wifiNameLen < 32) {
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(wifiPara.ap_name);
|
2020-09-21 01:55:02 +00:00
|
|
|
memcpy(wifiPara.ap_name, &msg[9], wifiNameLen);
|
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
memset(&wifi_list.wifiConnectedName, 0, sizeof(wifi_list.wifiConnectedName));
|
|
|
|
memcpy(&wifi_list.wifiConnectedName, &msg[9], wifiNameLen);
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
// WiFi key
|
2020-09-21 01:55:02 +00:00
|
|
|
if (wifiKeyLen < 64) {
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(wifiPara.keyCode);
|
2020-09-21 01:55:02 +00:00
|
|
|
memcpy(wifiPara.keyCode, &msg[10 + wifiNameLen], wifiKeyLen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cloud_para.state =msg[10 + wifiNameLen + wifiKeyLen];
|
|
|
|
hostLen = msg[11 + wifiNameLen + wifiKeyLen];
|
|
|
|
if (cloud_para.state) {
|
|
|
|
if (hostLen < 96) {
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(cloud_para.hostUrl);
|
2020-09-21 01:55:02 +00:00
|
|
|
memcpy(cloud_para.hostUrl, &msg[12 + wifiNameLen + wifiKeyLen], hostLen);
|
|
|
|
}
|
|
|
|
cloud_para.port = msg[12 + wifiNameLen + wifiKeyLen + hostLen] + (msg[13 + wifiNameLen + wifiKeyLen + hostLen] << 8);
|
|
|
|
}
|
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
// ID
|
2020-09-21 01:55:02 +00:00
|
|
|
id_len = msg[14 + wifiNameLen + wifiKeyLen + hostLen];
|
|
|
|
if (id_len == 20) {
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(cloud_para.id);
|
2020-09-21 01:55:02 +00:00
|
|
|
memcpy(cloud_para.id, (const char *)&msg[15 + wifiNameLen + wifiKeyLen + hostLen], id_len);
|
|
|
|
}
|
|
|
|
ver_len = msg[15 + wifiNameLen + wifiKeyLen + hostLen + id_len];
|
|
|
|
if (ver_len < 20) {
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(wifi_firm_ver);
|
2020-09-21 01:55:02 +00:00
|
|
|
memcpy(wifi_firm_ver, (const char *)&msg[16 + wifiNameLen + wifiKeyLen + hostLen + id_len], ver_len);
|
|
|
|
}
|
|
|
|
|
2021-01-17 02:43:46 +00:00
|
|
|
if (uiCfg.configWifi) {
|
2020-09-21 01:55:02 +00:00
|
|
|
if ((wifiPara.mode != gCfgItems.wifi_mode_sel)
|
|
|
|
|| (strncmp(wifiPara.ap_name, (const char *)uiCfg.wifi_name, 32) != 0)
|
|
|
|
|| (strncmp(wifiPara.keyCode, (const char *)uiCfg.wifi_key, 64) != 0)) {
|
2021-01-13 03:02:35 +00:00
|
|
|
package_to_wifi(WIFI_PARA_SET, (uint8_t *)0, 0);
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
2021-01-17 02:43:46 +00:00
|
|
|
else uiCfg.configWifi = false;
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
if (cfg_cloud_flag == 1) {
|
|
|
|
if (((cloud_para.state >> 4) != (char)gCfgItems.cloud_enable)
|
|
|
|
|| (strncmp(cloud_para.hostUrl, (const char *)uiCfg.cloud_hostUrl, 96) != 0)
|
2021-07-13 00:17:28 +00:00
|
|
|
|| (cloud_para.port != uiCfg.cloud_port)
|
|
|
|
) package_to_wifi(WIFI_CLOUD_CFG, (uint8_t *)0, 0);
|
|
|
|
else
|
|
|
|
cfg_cloud_flag = 0;
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wifi_list_msg_handle(uint8_t * msg, uint16_t msgLen) {
|
2020-10-21 17:45:27 +00:00
|
|
|
int wifiNameLen,wifiMsgIdex = 1;
|
|
|
|
int8_t wifi_name_is_same = 0;
|
|
|
|
int8_t i, j;
|
|
|
|
int8_t wifi_name_num = 0;
|
|
|
|
uint8_t *str = 0;
|
2020-09-21 01:55:02 +00:00
|
|
|
int8_t valid_name_num;
|
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
if (msgLen <= 0) return;
|
2021-04-13 22:34:19 +00:00
|
|
|
if (disp_state == KEYBOARD_UI) return;
|
2020-09-21 01:55:02 +00:00
|
|
|
|
|
|
|
wifi_list.getNameNum = msg[0];
|
|
|
|
|
|
|
|
if (wifi_list.getNameNum < 20) {
|
2021-01-17 02:43:46 +00:00
|
|
|
uiCfg.command_send = true;
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(wifi_list.wifiName);
|
2020-09-21 01:55:02 +00:00
|
|
|
wifi_name_num = wifi_list.getNameNum;
|
2020-10-21 17:45:27 +00:00
|
|
|
valid_name_num = 0;
|
|
|
|
str = wifi_list.wifiName[0];
|
2020-09-21 01:55:02 +00:00
|
|
|
|
|
|
|
if (wifi_list.getNameNum > 0) wifi_list.currentWifipage = 1;
|
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
for (i = 0; i < wifi_list.getNameNum; i++) {
|
|
|
|
wifiNameLen = msg[wifiMsgIdex++];
|
2020-09-21 01:55:02 +00:00
|
|
|
if (wifiNameLen < 32) {
|
|
|
|
memset(str, 0, WIFI_NAME_BUFFER_SIZE);
|
|
|
|
memcpy(str, &msg[wifiMsgIdex], wifiNameLen);
|
|
|
|
for (j = 0; j < valid_name_num; j++) {
|
2021-01-14 06:23:56 +00:00
|
|
|
if (strcmp((const char *)str, (const char *)wifi_list.wifiName[j]) == 0) {
|
2020-09-21 01:55:02 +00:00
|
|
|
wifi_name_is_same = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-01-14 06:23:56 +00:00
|
|
|
|
|
|
|
if (wifi_name_is_same != 1 && str[0] > 0x80)
|
|
|
|
wifi_name_is_same = 1;
|
|
|
|
|
2020-09-21 01:55:02 +00:00
|
|
|
if (wifi_name_is_same == 1) {
|
|
|
|
wifi_name_is_same = 0;
|
|
|
|
wifiMsgIdex += wifiNameLen;
|
|
|
|
wifiMsgIdex += 1;
|
|
|
|
wifi_name_num--;
|
|
|
|
//i--;
|
|
|
|
continue;
|
|
|
|
}
|
2020-10-21 17:45:27 +00:00
|
|
|
if (i < WIFI_TOTAL_NUMBER - 1)
|
2020-09-21 01:55:02 +00:00
|
|
|
str = wifi_list.wifiName[++valid_name_num];
|
|
|
|
}
|
2020-10-21 17:45:27 +00:00
|
|
|
wifiMsgIdex += wifiNameLen;
|
|
|
|
wifi_list.RSSI[i] = msg[wifiMsgIdex++];
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
wifi_list.getNameNum = wifi_name_num;
|
2020-10-21 17:45:27 +00:00
|
|
|
wifi_list.getPage = wifi_list.getNameNum / NUMBER_OF_PAGE + ((wifi_list.getNameNum % NUMBER_OF_PAGE) != 0);
|
2020-09-21 01:55:02 +00:00
|
|
|
wifi_list.nameIndex = 0;
|
2020-10-21 17:45:27 +00:00
|
|
|
|
|
|
|
if (disp_state == WIFI_LIST_UI) disp_wifi_list();
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gcode_msg_handle(uint8_t * msg, uint16_t msgLen) {
|
2020-10-21 17:45:27 +00:00
|
|
|
uint8_t gcodeBuf[100] = { 0 };
|
|
|
|
char *index_s, *index_e;
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
if (msgLen <= 0) return;
|
2020-09-21 01:55:02 +00:00
|
|
|
|
|
|
|
index_s = (char *)msg;
|
2020-10-21 17:45:27 +00:00
|
|
|
index_e = (char *)strchr((char *)msg, '\n');
|
2020-09-21 01:55:02 +00:00
|
|
|
if (*msg == 'N') {
|
2020-10-21 17:45:27 +00:00
|
|
|
index_s = (char *)strchr((char *)msg, ' ');
|
|
|
|
while (*index_s == ' ') index_s++;
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
while ((index_e != 0) && ((int)index_s < (int)index_e)) {
|
|
|
|
if ((int)(index_e - index_s) < (int)sizeof(gcodeBuf)) {
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(gcodeBuf);
|
2020-09-21 01:55:02 +00:00
|
|
|
memcpy(gcodeBuf, index_s, index_e - index_s + 1);
|
|
|
|
wifi_gcode_exec(gcodeBuf);
|
|
|
|
}
|
2020-10-21 17:45:27 +00:00
|
|
|
while ((*index_e == '\r') || (*index_e == '\n')) index_e++;
|
2020-09-21 01:55:02 +00:00
|
|
|
index_s = index_e;
|
2020-10-21 17:45:27 +00:00
|
|
|
index_e = (char *)strchr(index_s, '\n');
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 06:23:56 +00:00
|
|
|
void utf8_2_unicode(uint8_t *source, uint8_t Len) {
|
2020-10-21 17:45:27 +00:00
|
|
|
uint8_t i = 0, char_i = 0, char_byte_num = 0;
|
|
|
|
uint16_t u16_h, u16_m, u16_l, u16_value;
|
2020-09-21 01:55:02 +00:00
|
|
|
uint8_t FileName_unicode[30];
|
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(FileName_unicode);
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-09-02 00:29:20 +00:00
|
|
|
for (;;) {
|
2020-09-21 01:55:02 +00:00
|
|
|
char_byte_num = source[i] & 0xF0;
|
2021-09-02 00:29:20 +00:00
|
|
|
if (source[i] < 0x80) { // ASCII -- 1 byte
|
|
|
|
FileName_unicode[char_i++] = source[i++];
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
2021-09-02 00:29:20 +00:00
|
|
|
else if (char_byte_num == 0xC0 || char_byte_num == 0xD0) { // -- 2 byte
|
2020-10-21 17:45:27 +00:00
|
|
|
u16_h = (((uint16_t)source[i] << 8) & 0x1F00) >> 2;
|
|
|
|
u16_l = ((uint16_t)source[i + 1] & 0x003F);
|
2020-09-21 01:55:02 +00:00
|
|
|
u16_value = (u16_h | u16_l);
|
2020-10-09 21:50:17 +00:00
|
|
|
FileName_unicode[char_i] = (uint8_t)((u16_value & 0xFF00) >> 8);
|
|
|
|
FileName_unicode[char_i + 1] = (uint8_t)(u16_value & 0x00FF);
|
2020-09-21 01:55:02 +00:00
|
|
|
i += 2;
|
|
|
|
char_i += 2;
|
|
|
|
}
|
2021-09-02 00:29:20 +00:00
|
|
|
else if (char_byte_num == 0xE0) { // -- 3 byte
|
2020-10-21 17:45:27 +00:00
|
|
|
u16_h = (((uint16_t)source[i] << 8) & 0x0F00) << 4;
|
|
|
|
u16_m = (((uint16_t)source[i + 1] << 8) & 0x3F00) >> 2;
|
|
|
|
u16_l = ((uint16_t)source[i + 2] & 0x003F);
|
2020-09-21 01:55:02 +00:00
|
|
|
u16_value = (u16_h | u16_m | u16_l);
|
2020-10-09 21:50:17 +00:00
|
|
|
FileName_unicode[char_i] = (uint8_t)((u16_value & 0xFF00) >> 8);
|
|
|
|
FileName_unicode[char_i + 1] = (uint8_t)(u16_value & 0x00FF);
|
2020-09-21 01:55:02 +00:00
|
|
|
i += 3;
|
|
|
|
char_i += 2;
|
|
|
|
}
|
2021-09-02 00:29:20 +00:00
|
|
|
else if (char_byte_num == 0xF0) { // -- 4 byte
|
2020-09-21 01:55:02 +00:00
|
|
|
i += 4;
|
|
|
|
//char_i += 3;
|
|
|
|
}
|
2021-01-14 06:23:56 +00:00
|
|
|
else
|
2020-09-21 01:55:02 +00:00
|
|
|
break;
|
2021-01-14 06:23:56 +00:00
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
if (i >= Len || i >= 255) break;
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
2020-10-21 17:45:27 +00:00
|
|
|
COPY(source, FileName_unicode);
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void file_first_msg_handle(uint8_t * msg, uint16_t msgLen) {
|
|
|
|
uint8_t fileNameLen = *msg;
|
|
|
|
|
|
|
|
if (msgLen != fileNameLen + 5) return;
|
|
|
|
|
|
|
|
file_writer.fileLen = *((uint32_t *)(msg + 1));
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(file_writer.saveFileName);
|
2020-09-21 01:55:02 +00:00
|
|
|
|
|
|
|
memcpy(file_writer.saveFileName, msg + 5, fileNameLen);
|
|
|
|
|
2021-09-16 11:48:24 +00:00
|
|
|
utf8_2_unicode(file_writer.saveFileName, fileNameLen);
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-01-14 04:41:09 +00:00
|
|
|
ZERO(public_buf);
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-04-13 20:07:25 +00:00
|
|
|
if (strlen((const char *)file_writer.saveFileName) > sizeof(saveFilePath))
|
2020-09-21 01:55:02 +00:00
|
|
|
return;
|
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(saveFilePath);
|
2020-09-21 01:55:02 +00:00
|
|
|
|
|
|
|
if (gCfgItems.fileSysType == FILE_SYS_SD) {
|
2021-01-14 04:41:09 +00:00
|
|
|
TERN_(SDSUPPORT, card.mount());
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
else if (gCfgItems.fileSysType == FILE_SYS_USB) {
|
2021-09-02 00:29:20 +00:00
|
|
|
// nothing
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
file_writer.write_index = 0;
|
|
|
|
lastFragment = -1;
|
|
|
|
|
|
|
|
wifiTransError.flag = 0;
|
|
|
|
wifiTransError.start_tick = 0;
|
|
|
|
wifiTransError.now_tick = 0;
|
|
|
|
|
|
|
|
TERN_(SDSUPPORT, card.closefile());
|
|
|
|
|
|
|
|
wifi_delay(1000);
|
|
|
|
|
|
|
|
#if ENABLED(SDSUPPORT)
|
|
|
|
|
2021-04-13 08:49:20 +00:00
|
|
|
char dosName[FILENAME_LENGTH];
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-04-13 20:07:25 +00:00
|
|
|
if (!longName2DosName((const char *)file_writer.saveFileName, dosName)) {
|
2020-09-21 01:55:02 +00:00
|
|
|
clear_cur_ui();
|
|
|
|
upload_result = 2;
|
|
|
|
wifiTransError.flag = 1;
|
|
|
|
wifiTransError.start_tick = getWifiTick();
|
|
|
|
lv_draw_dialog(DIALOG_TYPE_UPLOAD_FILE);
|
|
|
|
return;
|
|
|
|
}
|
2021-04-13 20:07:25 +00:00
|
|
|
strcpy((char *)saveFilePath, dosName);
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-01-14 04:41:09 +00:00
|
|
|
card.cdroot();
|
|
|
|
upload_file.close();
|
2021-05-08 09:35:35 +00:00
|
|
|
const char * const fname = card.diveToFile(false, upload_curDir, saveFilePath);
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-01-14 04:41:09 +00:00
|
|
|
if (!upload_file.open(upload_curDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC)) {
|
2020-09-21 01:55:02 +00:00
|
|
|
clear_cur_ui();
|
|
|
|
upload_result = 2;
|
2021-01-14 04:41:09 +00:00
|
|
|
|
2020-09-21 01:55:02 +00:00
|
|
|
wifiTransError.flag = 1;
|
|
|
|
wifiTransError.start_tick = getWifiTick();
|
2021-01-14 04:41:09 +00:00
|
|
|
|
2020-09-21 01:55:02 +00:00
|
|
|
lv_draw_dialog(DIALOG_TYPE_UPLOAD_FILE);
|
|
|
|
return;
|
|
|
|
}
|
2020-10-21 17:45:27 +00:00
|
|
|
|
|
|
|
#endif // SDSUPPORT
|
2020-09-21 01:55:02 +00:00
|
|
|
|
|
|
|
wifi_link_state = WIFI_TRANS_FILE;
|
|
|
|
|
|
|
|
upload_result = 1;
|
|
|
|
|
|
|
|
clear_cur_ui();
|
|
|
|
lv_draw_dialog(DIALOG_TYPE_UPLOAD_FILE);
|
|
|
|
|
|
|
|
lv_task_handler();
|
|
|
|
|
|
|
|
file_writer.tick_begin = getWifiTick();
|
2021-01-14 04:41:09 +00:00
|
|
|
|
|
|
|
file_writer.fileTransfer = 1;
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 04:41:09 +00:00
|
|
|
#define FRAG_MASK ~_BV32(31)
|
2020-09-21 01:55:02 +00:00
|
|
|
|
|
|
|
static void file_fragment_msg_handle(uint8_t * msg, uint16_t msgLen) {
|
|
|
|
uint32_t frag = *((uint32_t *)msg);
|
|
|
|
if ((frag & FRAG_MASK) != (uint32_t)(lastFragment + 1)) {
|
2021-01-14 04:41:09 +00:00
|
|
|
ZERO(public_buf);
|
2020-09-21 01:55:02 +00:00
|
|
|
file_writer.write_index = 0;
|
|
|
|
wifi_link_state = WIFI_CONNECTED;
|
|
|
|
upload_result = 2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (write_to_file((char *)msg + 4, msgLen - 4) < 0) {
|
2021-01-14 04:41:09 +00:00
|
|
|
ZERO(public_buf);
|
2020-09-21 01:55:02 +00:00
|
|
|
file_writer.write_index = 0;
|
|
|
|
wifi_link_state = WIFI_CONNECTED;
|
|
|
|
upload_result = 2;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
lastFragment = frag;
|
|
|
|
|
2021-01-14 04:41:09 +00:00
|
|
|
if ((frag & (~FRAG_MASK)) != 0) {
|
2021-07-13 00:17:28 +00:00
|
|
|
wifiDmaRcvFifo.receiveEspData = false;
|
2021-01-14 04:41:09 +00:00
|
|
|
int res = upload_file.write(public_buf, file_writer.write_index);
|
2020-09-21 01:55:02 +00:00
|
|
|
if (res == -1) {
|
2021-01-14 04:41:09 +00:00
|
|
|
upload_file.close();
|
2021-05-08 09:35:35 +00:00
|
|
|
const char * const fname = card.diveToFile(false, upload_curDir, saveFilePath);
|
2021-01-14 04:41:09 +00:00
|
|
|
if (upload_file.open(upload_curDir, fname, O_WRITE)) {
|
|
|
|
upload_file.setpos(&pos);
|
|
|
|
res = upload_file.write(public_buf, file_writer.write_index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
upload_file.close();
|
|
|
|
SdFile file, *curDir;
|
2021-05-08 09:35:35 +00:00
|
|
|
const char * const fname = card.diveToFile(false, curDir, saveFilePath);
|
2021-01-14 04:41:09 +00:00
|
|
|
if (file.open(curDir, fname, O_RDWR)) {
|
|
|
|
gCfgItems.curFilesize = file.fileSize();
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ZERO(public_buf);
|
2020-09-21 01:55:02 +00:00
|
|
|
file_writer.write_index = 0;
|
|
|
|
wifi_link_state = WIFI_CONNECTED;
|
|
|
|
upload_result = 2;
|
|
|
|
return;
|
|
|
|
}
|
2021-01-14 04:41:09 +00:00
|
|
|
ZERO(public_buf);
|
2020-09-21 01:55:02 +00:00
|
|
|
file_writer.write_index = 0;
|
|
|
|
file_writer.tick_end = getWifiTick();
|
2021-07-13 00:17:28 +00:00
|
|
|
upload_time_sec = getWifiTickDiff(file_writer.tick_begin, file_writer.tick_end) / 1000;
|
2020-09-21 01:55:02 +00:00
|
|
|
upload_size = gCfgItems.curFilesize;
|
|
|
|
wifi_link_state = WIFI_CONNECTED;
|
|
|
|
upload_result = 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_data_parser(char *cmdRxBuf, int len) {
|
2021-01-14 06:23:56 +00:00
|
|
|
int32_t head_pos, tail_pos;
|
2020-09-21 01:55:02 +00:00
|
|
|
uint16_t cpyLen;
|
|
|
|
int16_t leftLen = len;
|
|
|
|
bool loop_again = false;
|
|
|
|
|
|
|
|
ESP_PROTOC_FRAME esp_frame;
|
|
|
|
|
|
|
|
while (leftLen > 0 || loop_again) {
|
|
|
|
loop_again = false;
|
|
|
|
|
|
|
|
if (esp_msg_index != 0) {
|
|
|
|
head_pos = 0;
|
|
|
|
cpyLen = (leftLen < (int16_t)((sizeof(esp_msg_buf) - esp_msg_index)) ? leftLen : sizeof(esp_msg_buf) - esp_msg_index);
|
|
|
|
|
|
|
|
memcpy(&esp_msg_buf[esp_msg_index], cmdRxBuf + len - leftLen, cpyLen);
|
|
|
|
|
|
|
|
esp_msg_index += cpyLen;
|
|
|
|
|
|
|
|
leftLen = leftLen - cpyLen;
|
|
|
|
tail_pos = charAtArray(esp_msg_buf, esp_msg_index, ESP_PROTOC_TAIL);
|
|
|
|
|
|
|
|
if (tail_pos == -1) {
|
|
|
|
if (esp_msg_index >= sizeof(esp_msg_buf)) {
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(esp_msg_buf);
|
2020-09-21 01:55:02 +00:00
|
|
|
esp_msg_index = 0;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
head_pos = charAtArray((uint8_t const *)&cmdRxBuf[len - leftLen], leftLen, ESP_PROTOC_HEAD);
|
|
|
|
if (head_pos == -1) return;
|
|
|
|
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(esp_msg_buf);
|
2020-09-21 01:55:02 +00:00
|
|
|
memcpy(esp_msg_buf, &cmdRxBuf[len - leftLen + head_pos], leftLen - head_pos);
|
|
|
|
|
|
|
|
esp_msg_index = leftLen - head_pos;
|
|
|
|
|
|
|
|
leftLen = 0;
|
|
|
|
head_pos = 0;
|
|
|
|
tail_pos = charAtArray(esp_msg_buf, esp_msg_index, ESP_PROTOC_TAIL);
|
|
|
|
if (tail_pos == -1) {
|
|
|
|
if (esp_msg_index >= sizeof(esp_msg_buf)) {
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(esp_msg_buf);
|
2020-09-21 01:55:02 +00:00
|
|
|
esp_msg_index = 0;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_frame.type = esp_msg_buf[1];
|
2020-10-21 17:45:27 +00:00
|
|
|
if ( esp_frame.type != ESP_TYPE_NET
|
|
|
|
&& esp_frame.type != ESP_TYPE_GCODE
|
|
|
|
&& esp_frame.type != ESP_TYPE_FILE_FIRST
|
|
|
|
&& esp_frame.type != ESP_TYPE_FILE_FRAGMENT
|
|
|
|
&& esp_frame.type != ESP_TYPE_WIFI_LIST
|
|
|
|
) {
|
|
|
|
ZERO(esp_msg_buf);
|
2020-09-21 01:55:02 +00:00
|
|
|
esp_msg_index = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_frame.dataLen = esp_msg_buf[2] + (esp_msg_buf[3] << 8);
|
|
|
|
|
|
|
|
if ((int)(4 + esp_frame.dataLen) > (int)(sizeof(esp_msg_buf))) {
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(esp_msg_buf);
|
2020-09-21 01:55:02 +00:00
|
|
|
esp_msg_index = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (esp_msg_buf[4 + esp_frame.dataLen] != ESP_PROTOC_TAIL) {
|
|
|
|
if (esp_msg_index >= sizeof(esp_msg_buf)) {
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(esp_msg_buf);
|
2020-09-21 01:55:02 +00:00
|
|
|
esp_msg_index = 0;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_frame.data = &esp_msg_buf[4];
|
|
|
|
switch (esp_frame.type) {
|
|
|
|
case ESP_TYPE_NET:
|
|
|
|
net_msg_handle(esp_frame.data, esp_frame.dataLen);
|
|
|
|
break;
|
|
|
|
case ESP_TYPE_GCODE:
|
|
|
|
gcode_msg_handle(esp_frame.data, esp_frame.dataLen);
|
|
|
|
break;
|
|
|
|
case ESP_TYPE_FILE_FIRST:
|
|
|
|
file_first_msg_handle(esp_frame.data, esp_frame.dataLen);
|
|
|
|
break;
|
|
|
|
case ESP_TYPE_FILE_FRAGMENT:
|
|
|
|
file_fragment_msg_handle(esp_frame.data, esp_frame.dataLen);
|
|
|
|
break;
|
|
|
|
case ESP_TYPE_WIFI_LIST:
|
|
|
|
wifi_list_msg_handle(esp_frame.data, esp_frame.dataLen);
|
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_msg_index = cut_msg_head(esp_msg_buf, esp_msg_index, esp_frame.dataLen + 5);
|
|
|
|
if (esp_msg_index > 0) {
|
|
|
|
if (charAtArray(esp_msg_buf, esp_msg_index, ESP_PROTOC_HEAD) == -1) {
|
2020-10-21 17:45:27 +00:00
|
|
|
ZERO(esp_msg_buf);
|
2020-09-21 01:55:02 +00:00
|
|
|
esp_msg_index = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((charAtArray(esp_msg_buf, esp_msg_index, ESP_PROTOC_HEAD) != -1) && (charAtArray(esp_msg_buf, esp_msg_index, ESP_PROTOC_TAIL) != -1))
|
|
|
|
loop_again = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t tick_net_time1, tick_net_time2;
|
|
|
|
int32_t readWifiFifo(uint8_t *retBuf, uint32_t bufLen) {
|
|
|
|
unsigned char tmpR = wifiDmaRcvFifo.read_cur;
|
|
|
|
if (bufLen >= UDISKBUFLEN && wifiDmaRcvFifo.state[tmpR] == udisk_buf_full) {
|
|
|
|
memcpy(retBuf, (unsigned char *)wifiDmaRcvFifo.bufferAddr[tmpR], UDISKBUFLEN);
|
|
|
|
wifiDmaRcvFifo.state[tmpR] = udisk_buf_empty;
|
|
|
|
wifiDmaRcvFifo.read_cur = (tmpR + 1) % TRANS_RCV_FIFO_BLOCK_NUM;
|
|
|
|
return UDISKBUFLEN;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void stopEspTransfer() {
|
|
|
|
if (wifi_link_state == WIFI_TRANS_FILE)
|
2021-09-16 11:48:24 +00:00
|
|
|
wifi_link_state = WIFI_CONNECTED;
|
2020-09-21 01:55:02 +00:00
|
|
|
|
|
|
|
TERN_(SDSUPPORT, card.closefile());
|
|
|
|
|
|
|
|
if (upload_result != 3) {
|
|
|
|
wifiTransError.flag = 1;
|
|
|
|
wifiTransError.start_tick = getWifiTick();
|
|
|
|
card.removeFile((const char *)saveFilePath);
|
|
|
|
}
|
2021-01-14 04:41:09 +00:00
|
|
|
|
2020-09-21 01:55:02 +00:00
|
|
|
wifi_delay(200);
|
|
|
|
WIFI_IO1_SET();
|
2021-01-14 04:41:09 +00:00
|
|
|
|
|
|
|
// disable dma
|
2021-07-13 00:17:28 +00:00
|
|
|
#ifdef __STM32F1__
|
|
|
|
dma_clear_isr_bits(DMA1, DMA_CH5);
|
|
|
|
bb_peri_set_bit(&USART1_BASE->CR3, USART_CR3_DMAR_BIT, 0);
|
|
|
|
dma_disable(DMA1, DMA_CH5);
|
|
|
|
#else
|
|
|
|
// First, abort any running dma
|
|
|
|
HAL_DMA_Abort(&wifiUsartDMArx);
|
|
|
|
// DeInit objects
|
|
|
|
HAL_DMA_DeInit(&wifiUsartDMArx);
|
|
|
|
#endif
|
2021-01-14 04:41:09 +00:00
|
|
|
|
|
|
|
wifi_delay(200);
|
|
|
|
changeFlashMode(true); // Set SPI flash to use DMA mode
|
2020-09-21 01:55:02 +00:00
|
|
|
esp_port_begin(1);
|
2021-01-14 04:41:09 +00:00
|
|
|
wifi_delay(200);
|
|
|
|
|
|
|
|
W25QXX.init(SPI_QUARTER_SPEED);
|
|
|
|
|
|
|
|
TERN_(HAS_TFT_LVGL_UI_SPI, SPI_TFT.spi_init(SPI_FULL_SPEED));
|
|
|
|
TERN_(HAS_SERVOS, servo_init());
|
|
|
|
TERN_(HAS_Z_SERVO_PROBE, probe.servo_probe_init());
|
|
|
|
|
2020-09-21 01:55:02 +00:00
|
|
|
if (wifiTransError.flag != 0x1) WIFI_IO1_RESET();
|
|
|
|
}
|
|
|
|
|
|
|
|
void wifi_rcv_handle() {
|
|
|
|
int32_t len = 0;
|
2021-01-14 04:41:09 +00:00
|
|
|
uint8_t ucStr[(UART_RX_BUFFER_SIZE) + 1] = {0};
|
2020-09-21 01:55:02 +00:00
|
|
|
int8_t getDataF = 0;
|
|
|
|
if (wifi_link_state == WIFI_TRANS_FILE) {
|
|
|
|
#if 0
|
|
|
|
if (WIFISERIAL.available() == UART_RX_BUFFER_SIZE) {
|
|
|
|
for (uint16_t i=0;i<UART_RX_BUFFER_SIZE;i++) {
|
|
|
|
ucStr[i] = WIFISERIAL.read();
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2021-07-13 00:17:28 +00:00
|
|
|
#ifndef __STM32F1__
|
|
|
|
if (wifiDmaRcvFifo.receiveEspData) storeRcvData(WIFISERIAL.wifiRxBuf, UART_RX_BUFFER_SIZE);
|
|
|
|
#endif
|
2020-09-21 01:55:02 +00:00
|
|
|
len = readWifiFifo(ucStr, UART_RX_BUFFER_SIZE);
|
|
|
|
#endif
|
|
|
|
if (len > 0) {
|
|
|
|
esp_data_parser((char *)ucStr, len);
|
|
|
|
if (wifi_link_state == WIFI_CONNECTED) {
|
|
|
|
clear_cur_ui();
|
|
|
|
lv_draw_dialog(DIALOG_TYPE_UPLOAD_FILE);
|
|
|
|
stopEspTransfer();
|
|
|
|
}
|
|
|
|
getDataF = 1;
|
|
|
|
}
|
2021-07-13 00:17:28 +00:00
|
|
|
#ifdef __STM32F1__
|
|
|
|
if (esp_state == TRANSFER_STORE) {
|
|
|
|
if (storeRcvData(WIFISERIAL.wifiRxBuf, UART_RX_BUFFER_SIZE)) {
|
2021-08-08 07:26:54 +00:00
|
|
|
esp_state = TRANSFERRING;
|
2021-07-13 00:17:28 +00:00
|
|
|
esp_dma_pre();
|
|
|
|
if (wifiTransError.flag != 0x1) WIFI_IO1_RESET();
|
|
|
|
}
|
|
|
|
else WIFI_IO1_SET();
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
2021-07-13 00:17:28 +00:00
|
|
|
#endif
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
len = readWifiBuf((int8_t *)ucStr, UART_RX_BUFFER_SIZE);
|
|
|
|
if (len > 0) {
|
|
|
|
esp_data_parser((char *)ucStr, len);
|
|
|
|
if (wifi_link_state == WIFI_TRANS_FILE) {
|
2021-01-14 04:41:09 +00:00
|
|
|
changeFlashMode(false); // Set SPI flash to use non-DMA mode
|
2020-09-21 01:55:02 +00:00
|
|
|
wifi_delay(10);
|
|
|
|
esp_port_begin(0);
|
|
|
|
wifi_delay(10);
|
|
|
|
tick_net_time1 = 0;
|
2021-07-13 00:17:28 +00:00
|
|
|
#ifndef __STM32F1__
|
|
|
|
wifiDmaRcvFifo.receiveEspData = true;
|
|
|
|
return;
|
|
|
|
#endif
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
if (wifiTransError.flag != 0x1) WIFI_IO1_RESET();
|
|
|
|
getDataF = 1;
|
|
|
|
}
|
2021-02-26 22:54:46 +00:00
|
|
|
if (need_ok_later && !queue.ring_buffer.full()) {
|
2020-09-21 01:55:02 +00:00
|
|
|
need_ok_later = false;
|
2021-01-14 04:41:09 +00:00
|
|
|
send_to_wifi((uint8_t *)"ok\r\n", strlen("ok\r\n"));
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
if (getDataF == 1)
|
2020-09-21 01:55:02 +00:00
|
|
|
tick_net_time1 = getWifiTick();
|
|
|
|
else {
|
|
|
|
tick_net_time2 = getWifiTick();
|
|
|
|
|
|
|
|
if (wifi_link_state == WIFI_TRANS_FILE) {
|
2021-01-14 04:41:09 +00:00
|
|
|
if (tick_net_time1 && getWifiTickDiff(tick_net_time1, tick_net_time2) > 8000) {
|
2020-09-21 01:55:02 +00:00
|
|
|
wifi_link_state = WIFI_CONNECTED;
|
|
|
|
upload_result = 2;
|
|
|
|
clear_cur_ui();
|
|
|
|
stopEspTransfer();
|
|
|
|
lv_draw_dialog(DIALOG_TYPE_UPLOAD_FILE);
|
|
|
|
}
|
|
|
|
}
|
2021-01-14 04:41:09 +00:00
|
|
|
if (tick_net_time1 && getWifiTickDiff(tick_net_time1, tick_net_time2) > 10000)
|
2020-09-21 01:55:02 +00:00
|
|
|
wifi_link_state = WIFI_NOT_CONFIG;
|
|
|
|
|
2021-01-14 04:41:09 +00:00
|
|
|
if (tick_net_time1 && getWifiTickDiff(tick_net_time1, tick_net_time2) > 120000) {
|
2020-09-21 01:55:02 +00:00
|
|
|
wifi_link_state = WIFI_NOT_CONFIG;
|
|
|
|
wifi_reset();
|
|
|
|
tick_net_time1 = getWifiTick();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wifiTransError.flag == 0x1) {
|
|
|
|
wifiTransError.now_tick = getWifiTick();
|
|
|
|
if (getWifiTickDiff(wifiTransError.start_tick, wifiTransError.now_tick) > WAIT_ESP_TRANS_TIMEOUT_TICK) {
|
|
|
|
wifiTransError.flag = 0;
|
|
|
|
WIFI_IO1_RESET();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void wifi_looping() {
|
2021-01-14 04:41:09 +00:00
|
|
|
do {
|
|
|
|
wifi_rcv_handle();
|
|
|
|
watchdog_refresh();
|
|
|
|
} while (wifi_link_state == WIFI_TRANS_FILE);
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void mks_esp_wifi_init() {
|
|
|
|
wifi_link_state = WIFI_NOT_CONFIG;
|
|
|
|
|
|
|
|
SET_OUTPUT(WIFI_RESET_PIN);
|
|
|
|
WIFI_SET();
|
|
|
|
SET_OUTPUT(WIFI_IO1_PIN);
|
|
|
|
SET_INPUT_PULLUP(WIFI_IO0_PIN);
|
|
|
|
WIFI_IO1_SET();
|
|
|
|
|
|
|
|
esp_state = TRANSFER_IDLE;
|
|
|
|
esp_port_begin(1);
|
2021-07-13 00:17:28 +00:00
|
|
|
watchdog_refresh();
|
2020-09-21 01:55:02 +00:00
|
|
|
wifi_reset();
|
|
|
|
|
|
|
|
#if 0
|
2021-01-14 04:41:09 +00:00
|
|
|
if (update_flag == 0) {
|
|
|
|
res = f_open(&esp_upload.uploadFile, ESP_WEB_FIRMWARE_FILE, FA_OPEN_EXISTING | FA_READ);
|
2021-07-13 00:17:28 +00:00
|
|
|
if (res == FR_OK) {
|
|
|
|
f_close(&esp_upload.uploadFile);
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
wifi_delay(2000);
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
if (usartFifoAvailable((SZ_USART_FIFO *)&WifiRxFifo) < 20) return;
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
clear_cur_ui();
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
draw_dialog(DIALOG_TYPE_UPDATE_ESP_FIRMWARE);
|
|
|
|
if (wifi_upload(1) >= 0) {
|
|
|
|
f_unlink("1:/MKS_WIFI_CUR");
|
|
|
|
f_rename(ESP_WEB_FIRMWARE_FILE,"/MKS_WIFI_CUR");
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
2021-07-13 00:17:28 +00:00
|
|
|
draw_return_ui();
|
|
|
|
update_flag = 1;
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
2021-07-13 00:17:28 +00:00
|
|
|
}
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
if (update_flag == 0) {
|
|
|
|
res = f_open(&esp_upload.uploadFile, ESP_WEB_FILE, FA_OPEN_EXISTING | FA_READ);
|
|
|
|
if (res == FR_OK) {
|
|
|
|
f_close(&esp_upload.uploadFile);
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
wifi_delay(2000);
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
if (usartFifoAvailable((SZ_USART_FIFO *)&WifiRxFifo) < 20) return;
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
clear_cur_ui();
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
draw_dialog(DIALOG_TYPE_UPDATE_ESP_DATA);
|
2020-09-21 01:55:02 +00:00
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
if (wifi_upload(2) >= 0) {
|
|
|
|
f_unlink("1:/MKS_WEB_CONTROL_CUR");
|
|
|
|
f_rename(ESP_WEB_FILE,"/MKS_WEB_CONTROL_CUR");
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
2021-07-13 00:17:28 +00:00
|
|
|
draw_return_ui();
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
2021-07-13 00:17:28 +00:00
|
|
|
}
|
2020-09-21 01:55:02 +00:00
|
|
|
#endif
|
2021-07-13 00:17:28 +00:00
|
|
|
|
2020-09-21 01:55:02 +00:00
|
|
|
wifiPara.decodeType = WIFI_DECODE_TYPE;
|
|
|
|
wifiPara.baud = 115200;
|
|
|
|
wifi_link_state = WIFI_NOT_CONFIG;
|
|
|
|
}
|
|
|
|
|
2021-01-14 04:41:09 +00:00
|
|
|
void mks_wifi_firmware_update() {
|
2021-07-13 00:17:28 +00:00
|
|
|
watchdog_refresh();
|
2021-01-14 04:41:09 +00:00
|
|
|
card.openFileRead((char *)ESP_FIRMWARE_FILE);
|
|
|
|
|
|
|
|
if (card.isFileOpen()) {
|
|
|
|
card.closefile();
|
|
|
|
|
|
|
|
wifi_delay(2000);
|
2021-07-13 00:17:28 +00:00
|
|
|
watchdog_refresh();
|
|
|
|
if (usartFifoAvailable((SZ_USART_FIFO *)&WifiRxFifo) < 20) return;
|
2021-01-14 04:41:09 +00:00
|
|
|
|
|
|
|
clear_cur_ui();
|
|
|
|
|
2021-01-19 22:26:25 +00:00
|
|
|
lv_draw_dialog(DIALOG_TYPE_UPDATE_ESP_FIRMWARE);
|
2021-01-14 04:41:09 +00:00
|
|
|
|
|
|
|
lv_task_handler();
|
|
|
|
watchdog_refresh();
|
|
|
|
|
|
|
|
if (wifi_upload(0) >= 0) {
|
|
|
|
card.removeFile((char *)ESP_FIRMWARE_FILE_RENAME);
|
|
|
|
SdFile file, *curDir;
|
2021-05-08 09:35:35 +00:00
|
|
|
const char * const fname = card.diveToFile(false, curDir, ESP_FIRMWARE_FILE);
|
2021-01-14 04:41:09 +00:00
|
|
|
if (file.open(curDir, fname, O_READ)) {
|
|
|
|
file.rename(curDir, (char *)ESP_FIRMWARE_FILE_RENAME);
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
clear_cur_ui();
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void get_wifi_commands() {
|
|
|
|
static char wifi_line_buffer[MAX_CMD_SIZE];
|
|
|
|
static bool wifi_comment_mode = false;
|
|
|
|
static int wifi_read_count = 0;
|
|
|
|
|
|
|
|
if (espGcodeFifo.wait_tick > 5) {
|
2021-02-26 22:54:46 +00:00
|
|
|
while (!queue.ring_buffer.full() && (espGcodeFifo.r != espGcodeFifo.w)) {
|
2020-09-21 01:55:02 +00:00
|
|
|
|
|
|
|
espGcodeFifo.wait_tick = 0;
|
|
|
|
|
|
|
|
char wifi_char = espGcodeFifo.Buffer[espGcodeFifo.r];
|
|
|
|
|
|
|
|
espGcodeFifo.r = (espGcodeFifo.r + 1) % WIFI_GCODE_BUFFER_SIZE;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the character ends the line
|
|
|
|
*/
|
|
|
|
if (wifi_char == '\n' || wifi_char == '\r') {
|
|
|
|
|
|
|
|
wifi_comment_mode = false; // end of line == end of comment
|
|
|
|
|
|
|
|
if (!wifi_read_count) continue; // skip empty lines
|
|
|
|
|
|
|
|
wifi_line_buffer[wifi_read_count] = 0; // terminate string
|
|
|
|
wifi_read_count = 0; //reset buffer
|
|
|
|
|
|
|
|
char* command = wifi_line_buffer;
|
|
|
|
while (*command == ' ') command++; // skip any leading spaces
|
|
|
|
|
2021-05-08 03:39:34 +00:00
|
|
|
// Movement commands alert when stopped
|
|
|
|
if (IsStopped()) {
|
2020-09-21 01:55:02 +00:00
|
|
|
char* gpos = strchr(command, 'G');
|
|
|
|
if (gpos) {
|
2021-04-04 22:35:58 +00:00
|
|
|
switch (strtol(gpos + 1, nullptr, 10)) {
|
2020-09-21 01:55:02 +00:00
|
|
|
case 0 ... 1:
|
2021-09-16 11:48:24 +00:00
|
|
|
TERN_(ARC_SUPPORT, case 2 ... 3:)
|
|
|
|
TERN_(BEZIER_CURVE_SUPPORT, case 5:)
|
2020-09-21 01:55:02 +00:00
|
|
|
SERIAL_ECHOLNPGM(STR_ERR_STOPPED);
|
2021-09-25 22:05:11 +00:00
|
|
|
LCD_MESSAGE(MSG_STOPPED);
|
2020-09-21 01:55:02 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DISABLED(EMERGENCY_PARSER)
|
|
|
|
// Process critical commands early
|
|
|
|
if (strcmp(command, "M108") == 0) {
|
|
|
|
wait_for_heatup = false;
|
|
|
|
TERN_(HAS_LCD_MENU, wait_for_user = false);
|
|
|
|
}
|
|
|
|
if (strcmp(command, "M112") == 0) kill(M112_KILL_STR, nullptr, true);
|
|
|
|
if (strcmp(command, "M410") == 0) quickstop_stepper();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Add the command to the queue
|
2021-09-26 02:11:31 +00:00
|
|
|
queue.enqueue_one(wifi_line_buffer);
|
2020-09-21 01:55:02 +00:00
|
|
|
}
|
|
|
|
else if (wifi_read_count >= MAX_CMD_SIZE - 1) {
|
|
|
|
|
|
|
|
}
|
|
|
|
else { // it's not a newline, carriage return or escape char
|
|
|
|
if (wifi_char == ';') wifi_comment_mode = true;
|
|
|
|
if (!wifi_comment_mode) wifi_line_buffer[wifi_read_count++] = wifi_char;
|
|
|
|
}
|
|
|
|
}
|
2021-01-14 04:41:09 +00:00
|
|
|
} // queue has space, serial has data
|
|
|
|
else
|
2020-09-21 01:55:02 +00:00
|
|
|
espGcodeFifo.wait_tick++;
|
|
|
|
}
|
|
|
|
|
|
|
|
int readWifiBuf(int8_t *buf, int32_t len) {
|
|
|
|
int i = 0;
|
|
|
|
while (i < len && WIFISERIAL.available())
|
|
|
|
buf[i++] = WIFISERIAL.read();
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2021-07-13 00:17:28 +00:00
|
|
|
int usartFifoAvailable(SZ_USART_FIFO *fifo) { return WIFISERIAL.available(); }
|
|
|
|
|
2021-01-22 21:01:19 +00:00
|
|
|
#endif // HAS_TFT_LVGL_UI && MKS_WIFI_MODULE
|