My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

wifi_module.cpp 54KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../../../../inc/MarlinConfigPre.h"
  23. #if HAS_TFT_LVGL_UI
  24. #include "draw_ui.h"
  25. #include "wifi_module.h"
  26. #include "wifi_upload.h"
  27. #if ENABLED(USE_WIFI_FUNCTION)
  28. #include "../../../../MarlinCore.h"
  29. #include "../../../../module/temperature.h"
  30. #include "../../../../gcode/queue.h"
  31. #include "../../../../gcode/gcode.h"
  32. #include "../../../../lcd/marlinui.h"
  33. #include "../../../../sd/cardreader.h"
  34. #include "../../../../module/planner.h"
  35. #if ENABLED(POWER_LOSS_RECOVERY)
  36. #include "../../../../feature/powerloss.h"
  37. #endif
  38. #if ENABLED(PARK_HEAD_ON_PAUSE)
  39. #include "../../../../feature/pause.h"
  40. #endif
  41. #define WIFI_SET() WRITE(WIFI_RESET_PIN, HIGH);
  42. #define WIFI_RESET() WRITE(WIFI_RESET_PIN, LOW);
  43. #define WIFI_IO1_SET() WRITE(WIFI_IO1_PIN, HIGH);
  44. #define WIFI_IO1_RESET() WRITE(WIFI_IO1_PIN, LOW);
  45. extern uint8_t Explore_Disk (char* path , uint8_t recu_level);
  46. extern uint8_t commands_in_queue;
  47. extern uint8_t sel_id;
  48. int usartFifoAvailable(SZ_USART_FIFO *fifo);
  49. int readUsartFifo(SZ_USART_FIFO *fifo, int8_t *buf, int32_t len);
  50. int writeUsartFifo(SZ_USART_FIFO * fifo, int8_t * buf, int32_t len);
  51. extern unsigned int getTickDiff(unsigned int curTick, unsigned int lastTick);
  52. volatile SZ_USART_FIFO WifiRxFifo;
  53. #define WAIT_ESP_TRANS_TIMEOUT_TICK 10500
  54. int cfg_cloud_flag = 0;
  55. extern PRINT_TIME print_time;
  56. char wifi_firm_ver[20] = { 0 };
  57. WIFI_GCODE_BUFFER espGcodeFifo;
  58. extern uint8_t pause_resum;
  59. uint8_t wifi_connect_flg = 0;
  60. extern volatile uint8_t get_temp_flag;
  61. #define WIFI_MODE 2
  62. #define WIFI_AP_MODE 3
  63. int upload_result = 0;
  64. uint32_t upload_time = 0;
  65. uint32_t upload_size = 0;
  66. volatile WIFI_STATE wifi_link_state;
  67. WIFI_PARA wifiPara;
  68. IP_PARA ipPara;
  69. CLOUD_PARA cloud_para;
  70. char wifi_check_time = 0;
  71. extern uint8_t gCurDir[100];
  72. extern uint32_t wifi_loop_cycle;
  73. volatile TRANSFER_STATE esp_state;
  74. uint8_t left_to_send = 0;
  75. uint8_t left_to_save[96] = { 0 };
  76. volatile WIFI_DMA_RCV_FIFO wifiDmaRcvFifo;
  77. volatile WIFI_TRANS_ERROR wifiTransError;
  78. static bool need_ok_later = false;
  79. extern volatile WIFI_STATE wifi_link_state;
  80. extern WIFI_PARA wifiPara;
  81. extern IP_PARA ipPara;
  82. extern CLOUD_PARA cloud_para;
  83. extern bool once_flag, flash_preview_begin, default_preview_flg, gcode_preview_over;
  84. extern uint8_t bmp_public_buf[17 * 1024];
  85. uint32_t getWifiTick() {
  86. return millis();
  87. }
  88. uint32_t getWifiTickDiff(int32_t lastTick, int32_t curTick) {
  89. if (lastTick <= curTick)
  90. return (curTick - lastTick) * TICK_CYCLE;
  91. else
  92. return (0xFFFFFFFF - lastTick + curTick) * TICK_CYCLE;
  93. }
  94. void wifi_delay(int n) {
  95. uint32_t begin = getWifiTick();
  96. uint32_t end = begin;
  97. while (getWifiTickDiff(begin, end) < (uint32_t)n)
  98. end = getWifiTick();
  99. }
  100. void wifi_reset() {
  101. uint32_t start, now;
  102. start = getWifiTick();
  103. now = start;
  104. WIFI_RESET();
  105. while (getWifiTickDiff(start, now) < 500)
  106. now = getWifiTick();
  107. WIFI_SET();
  108. }
  109. void mount_file_sys(uint8_t disk_type) {
  110. if (disk_type == FILE_SYS_SD) {
  111. TERN_(SDSUPPORT, card.mount());
  112. }
  113. else if (disk_type == FILE_SYS_USB) {
  114. }
  115. }
  116. static void dma_init() {
  117. #if 0
  118. __HAL_RCC_DMA1_CLK_ENABLE();
  119. //HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_0);
  120. HAL_NVIC_SetPriority(DMA1_Channel5_IRQn, 4, 0);
  121. HAL_NVIC_EnableIRQ(DMA1_Channel5_IRQn);
  122. hdma_usart1_rx.Instance = DMA1_Channel5;
  123. //hdma_usart1_rx.Init.Channel = DMA_CHANNEL_4;
  124. hdma_usart1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  125. hdma_usart1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  126. hdma_usart1_rx.Init.MemInc = DMA_MINC_ENABLE;
  127. hdma_usart1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  128. hdma_usart1_rx.Init.MemDataAlignment = DMA_PDATAALIGN_BYTE;
  129. hdma_usart1_rx.Init.Mode = DMA_NORMAL;
  130. hdma_usart1_rx.Init.Priority = DMA_PRIORITY_VERY_HIGH;
  131. if (HAL_DMA_Init((DMA_HandleTypeDef *)&hdma_usart1_rx) != HAL_OK) {
  132. Error_Handler();
  133. }
  134. HAL_DMA_Start_IT((DMA_HandleTypeDef *)&hdma_usart1_rx,
  135. (uint32_t)&huart1.Instance->DR,
  136. (uint32_t)(&WifiRxFifo.uartTxBuffer[0]),
  137. UART_RX_BUFFER_SIZE);
  138. //HAL_UART_Receive_DMA(&huart1,(uint8_t*)&WifiRxFifo.uartTxBuffer[0], UART_RX_BUFFER_SIZE);
  139. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  140. in the UART CR3 register */
  141. SET_BIT(huart1.Instance->CR3, USART_CR3_DMAR);
  142. #endif
  143. for (uint8_t i = 0; i < TRANS_RCV_FIFO_BLOCK_NUM; i++) {
  144. wifiDmaRcvFifo.bufferAddr[i] = &bmp_public_buf[1024 * i];
  145. wifiDmaRcvFifo.state[i] = udisk_buf_empty;
  146. }
  147. memset(wifiDmaRcvFifo.bufferAddr[0], 0, 1024 * TRANS_RCV_FIFO_BLOCK_NUM);
  148. wifiDmaRcvFifo.read_cur = 0;
  149. wifiDmaRcvFifo.write_cur = 0;
  150. }
  151. static void wifi_deInit() {
  152. #if 0
  153. HAL_DMA_Abort((DMA_HandleTypeDef *)&hdma_usart1_rx);
  154. HAL_DMA_DeInit((DMA_HandleTypeDef *)&hdma_usart1_rx);
  155. __HAL_DMA_DISABLE((DMA_HandleTypeDef *)&hdma_usart1_rx);
  156. #endif
  157. }
  158. extern uint8_t mksUsart1Rx;
  159. void esp_port_begin(uint8_t interrupt) {
  160. WifiRxFifo.uart_read_point = 0;
  161. WifiRxFifo.uart_write_point = 0;
  162. #if 0
  163. NVIC_InitTypeDef NVIC_InitStructure;
  164. USART_InitTypeDef USART_InitStructure;
  165. GPIO_InitTypeDef GPIO_InitStruct;
  166. WifiRxFifo.uart_read_point = 0;
  167. WifiRxFifo.uart_write_point = 0;
  168. memset((uint8_t*)WifiRxFifo.uartTxBuffer, 0, sizeof(WifiRxFifo.uartTxBuffer));
  169. if (interrupt) {
  170. #if TAN
  171. wifi_deInit ();
  172. //SZ_STM32_COMInit(COM1, 115200);
  173. __HAL_UART_ENABLE_IT(USART1, USART_IT_RXNE);
  174. USART_InitStructure.USART_BaudRate = 115200; //���ڵIJ����ʣ�����115200 ��ߴ�4.5Mbits/s
  175. USART_InitStructure.USART_WordLength = USART_WordLength_8b; //�����ֳ���(8λ��9λ)
  176. USART_InitStructure.USART_StopBits = USART_StopBits_1; //�����õ�ֹͣλ-֧��1��2��ֹͣλ
  177. USART_InitStructure.USART_Parity = USART_Parity_No; //����żУ��
  178. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //��Ӳ��������
  179. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //˫��ģʽ��ʹ�ܷ��ͺͽ���
  180. __HAL_RCC_USART1_CLK_ENABLE();
  181. GPIO_InitStruct.Pin = TFT_WIFI_TX_Pin|TFT_WIFI_RX_Pin;
  182. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  183. GPIO_InitStruct.Pull = GPIO_PULLUP;
  184. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  185. GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
  186. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  187. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  188. GPIO_InitStruct.Pin = TFT_WIFI_RX_Pin;
  189. HAL_GPIO_Init(GPIOA,&GPIO_InitStruct);
  190. USART_Init(USART1, &USART_InitStructure);
  191. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  192. // NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  193. // NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  194. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
  195. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  196. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  197. NVIC_Init(&NVIC_InitStructure);
  198. #else
  199. HAL_UART_DeInit(&huart1);
  200. MX_USART1_UART_Init(3);
  201. //__HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE);
  202. HAL_UART_Receive_IT(&huart1,&mksUsart1Rx,1);
  203. #endif
  204. }
  205. else{
  206. #if 0
  207. NVIC_DisableIRQ(SZ_STM32_COM1_IRQn);
  208. USART_Cmd(SZ_STM32_COM1, DISABLE);
  209. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, DISABLE);
  210. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  211. SZ_STM32_COMInit(COM1, 1958400);
  212. USART_Cmd(SZ_STM32_COM1, ENABLE);
  213. wifi_delay(10);
  214. dma_init();
  215. #endif
  216. HAL_UART_DeInit(&huart1);
  217. MX_USART1_UART_Init(5);
  218. //dma1_5_IRQ_sel = 1;
  219. dma_init();
  220. }
  221. #endif
  222. if (interrupt) {
  223. #if ENABLED(USE_WIFI_FUNCTION)
  224. WIFISERIAL.end();
  225. for (uint16_t i = 0; i < 65535; i++);
  226. WIFISERIAL.begin(WIFI_BAUDRATE);
  227. uint32_t serial_connect_timeout = millis() + 1000UL;
  228. while (/*!WIFISERIAL && */PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  229. //for (uint8_t i=0;i<100;i++)WIFISERIAL.write(0x33);
  230. #endif
  231. }
  232. else {
  233. #if ENABLED(USE_WIFI_FUNCTION)
  234. WIFISERIAL.end();
  235. for (uint16_t i = 0; i < 65535; i++);
  236. WIFISERIAL.begin(WIFI_UPLOAD_BAUDRATE);
  237. uint32_t serial_connect_timeout = millis() + 1000UL;
  238. while (/*!WIFISERIAL && */PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  239. //for (uint16_t i=0;i<65535;i++);//WIFISERIAL.write(0x33);
  240. #endif
  241. dma_init();
  242. }
  243. }
  244. #if ENABLED(USE_WIFI_FUNCTION)
  245. int raw_send_to_wifi(char *buf, int len) {
  246. if (buf == 0 || len <= 0) return 0;
  247. for (int i = 0; i < len; i++)
  248. WIFISERIAL.write(*(buf + i));
  249. return len;
  250. }
  251. #endif
  252. void wifi_ret_ack() {}
  253. char buf_to_wifi[256];
  254. int index_to_wifi = 0;
  255. int package_to_wifi(WIFI_RET_TYPE type,char *buf, int len) {
  256. char wifi_ret_head = 0xA5;
  257. char wifi_ret_tail = 0xFC;
  258. if (type == WIFI_PARA_SET) {
  259. int data_offset = 4;
  260. int apLen = strlen((const char *)uiCfg.wifi_name);
  261. int keyLen = strlen((const char *)uiCfg.wifi_key);
  262. ZERO(buf_to_wifi);
  263. index_to_wifi = 0;
  264. buf_to_wifi[data_offset] = gCfgItems.wifi_mode_sel;
  265. buf_to_wifi[data_offset + 1] = apLen;
  266. strncpy(&buf_to_wifi[data_offset + 2], (const char *)uiCfg.wifi_name, apLen);
  267. buf_to_wifi[data_offset + apLen + 2] = keyLen;
  268. strncpy(&buf_to_wifi[data_offset + apLen + 3], (const char *)uiCfg.wifi_key, keyLen);
  269. buf_to_wifi[data_offset + apLen + keyLen + 3] = wifi_ret_tail;
  270. index_to_wifi = apLen + keyLen + 3;
  271. buf_to_wifi[0] = wifi_ret_head;
  272. buf_to_wifi[1] = type;
  273. buf_to_wifi[2] = index_to_wifi & 0xFF;
  274. buf_to_wifi[3] = (index_to_wifi >> 8) & 0xFF;
  275. raw_send_to_wifi(buf_to_wifi, 5 + index_to_wifi);
  276. ZERO(buf_to_wifi);
  277. index_to_wifi = 0;
  278. }
  279. else if (type == WIFI_TRANS_INF) {
  280. if (len > (int)(sizeof(buf_to_wifi) - index_to_wifi - 5)) {
  281. ZERO(buf_to_wifi);
  282. index_to_wifi = 0;
  283. return 0;
  284. }
  285. if (len > 0) {
  286. memcpy(&buf_to_wifi[4 + index_to_wifi], buf, len);
  287. index_to_wifi += len;
  288. if (index_to_wifi < 1)
  289. return 0;
  290. if (buf_to_wifi[index_to_wifi + 3] == '\n') {
  291. // mask "wait" "busy" "X:"
  292. if (((buf_to_wifi[4] == 'w') && (buf_to_wifi[5] == 'a') && (buf_to_wifi[6] == 'i') && (buf_to_wifi[7] == 't') )
  293. || ((buf_to_wifi[4] == 'b') && (buf_to_wifi[5] == 'u') && (buf_to_wifi[6] == 's') && (buf_to_wifi[7] == 'y') )
  294. || ((buf_to_wifi[4] == 'X') && (buf_to_wifi[5] == ':') )
  295. ) {
  296. ZERO(buf_to_wifi);
  297. index_to_wifi = 0;
  298. return 0;
  299. }
  300. buf_to_wifi[0] = wifi_ret_head;
  301. buf_to_wifi[1] = type;
  302. buf_to_wifi[2] = index_to_wifi & 0xFF;
  303. buf_to_wifi[3] = (index_to_wifi >> 8) & 0xFF;
  304. buf_to_wifi[4 + index_to_wifi] = wifi_ret_tail;
  305. raw_send_to_wifi(buf_to_wifi, 5 + index_to_wifi);
  306. ZERO(buf_to_wifi);
  307. index_to_wifi = 0;
  308. }
  309. }
  310. }
  311. else if (type == WIFI_EXCEP_INF) {
  312. ZERO(buf_to_wifi);
  313. buf_to_wifi[0] = wifi_ret_head;
  314. buf_to_wifi[1] = type;
  315. buf_to_wifi[2] = 1;
  316. buf_to_wifi[3] = 0;
  317. buf_to_wifi[4] = *buf;
  318. buf_to_wifi[5] = wifi_ret_tail;
  319. raw_send_to_wifi(buf_to_wifi, 6);
  320. ZERO(buf_to_wifi);
  321. index_to_wifi = 0;
  322. }
  323. else if (type == WIFI_CLOUD_CFG) {
  324. int data_offset = 4;
  325. int urlLen = strlen((const char *)uiCfg.cloud_hostUrl);
  326. ZERO(buf_to_wifi);
  327. index_to_wifi = 0;
  328. buf_to_wifi[data_offset] = gCfgItems.cloud_enable ? 0x0A : 0x05;
  329. buf_to_wifi[data_offset + 1] = urlLen;
  330. strncpy(&buf_to_wifi[data_offset + 2], (const char *)uiCfg.cloud_hostUrl, urlLen);
  331. buf_to_wifi[data_offset + urlLen + 2] = uiCfg.cloud_port & 0xFF;
  332. buf_to_wifi[data_offset + urlLen + 3] = (uiCfg.cloud_port >> 8) & 0xFF;
  333. buf_to_wifi[data_offset + urlLen + 4] = wifi_ret_tail;
  334. index_to_wifi = urlLen + 4;
  335. buf_to_wifi[0] = wifi_ret_head;
  336. buf_to_wifi[1] = type;
  337. buf_to_wifi[2] = index_to_wifi & 0xFF;
  338. buf_to_wifi[3] = (index_to_wifi >> 8) & 0xFF;
  339. raw_send_to_wifi(buf_to_wifi, 5 + index_to_wifi);
  340. ZERO(buf_to_wifi);
  341. index_to_wifi = 0;
  342. }
  343. else if (type == WIFI_CLOUD_UNBIND) {
  344. ZERO(buf_to_wifi);
  345. buf_to_wifi[0] = wifi_ret_head;
  346. buf_to_wifi[1] = type;
  347. buf_to_wifi[2] = 0;
  348. buf_to_wifi[3] = 0;
  349. buf_to_wifi[4] = wifi_ret_tail;
  350. raw_send_to_wifi(buf_to_wifi, 5);
  351. ZERO(buf_to_wifi);
  352. index_to_wifi = 0;
  353. }
  354. }
  355. int send_to_wifi(char *buf, int len) { return package_to_wifi(WIFI_TRANS_INF, buf, len); }
  356. void set_cur_file_sys(int fileType) { gCfgItems.fileSysType = fileType; }
  357. void get_file_list(char *path) {
  358. if (!path) return;
  359. if (gCfgItems.fileSysType == FILE_SYS_SD) {
  360. TERN_(SDSUPPORT, card.mount());
  361. }
  362. else if (gCfgItems.fileSysType == FILE_SYS_USB) {
  363. // udisk
  364. }
  365. Explore_Disk(path, 0);
  366. }
  367. char wait_ip_back_flag = 0;
  368. typedef struct {
  369. char write_buf[513];
  370. int write_index;
  371. uint8_t saveFileName[30];
  372. uint32_t fileLen;
  373. uint32_t tick_begin;
  374. uint32_t tick_end;
  375. } FILE_WRITER;
  376. FILE_WRITER file_writer;
  377. int32_t lastFragment = 0;
  378. char lastBinaryCmd[50] = { 0 };
  379. int total_write = 0;
  380. char binary_head[2] = { 0, 0 };
  381. unsigned char binary_data_len = 0;
  382. int write_to_file(char *buf, int len) {
  383. for (int i = 0; i < len; i++) {
  384. file_writer.write_buf[file_writer.write_index++] = buf[i];
  385. if (file_writer.write_index >= 512) {
  386. int res = card.write(file_writer.write_buf, file_writer.write_index);
  387. if (res == -1) return -1;
  388. ZERO(file_writer.write_buf);
  389. file_writer.write_index = 0;
  390. }
  391. }
  392. return 0;
  393. }
  394. #define ESP_PROTOC_HEAD (uint8_t)0xA5
  395. #define ESP_PROTOC_TAIL (uint8_t)0xFC
  396. #define ESP_TYPE_NET (uint8_t)0x0
  397. #define ESP_TYPE_GCODE (uint8_t)0x1
  398. #define ESP_TYPE_FILE_FIRST (uint8_t)0x2
  399. #define ESP_TYPE_FILE_FRAGMENT (uint8_t)0x3
  400. #define ESP_TYPE_WIFI_LIST (uint8_t)0x4
  401. uint8_t esp_msg_buf[UART_RX_BUFFER_SIZE] = { 0 };
  402. uint16_t esp_msg_index = 0;
  403. typedef struct {
  404. uint8_t head;
  405. uint8_t type;
  406. uint16_t dataLen;
  407. uint8_t *data;
  408. uint8_t tail;
  409. } ESP_PROTOC_FRAME;
  410. static int cut_msg_head(uint8_t *msg, uint16_t msgLen, uint16_t cutLen) {
  411. if (msgLen < cutLen) return 0;
  412. else if (msgLen == cutLen) {
  413. memset(msg, 0, msgLen);
  414. return 0;
  415. }
  416. for (int i = 0; i < (msgLen - cutLen); i++)
  417. msg[i] = msg[cutLen + i];
  418. memset(&msg[msgLen - cutLen], 0, cutLen);
  419. return msgLen - cutLen;
  420. }
  421. uint8_t Explore_Disk(char* path , uint8_t recu_level) {
  422. char tmp[200];
  423. char Fstream[200];
  424. if (!path) return 0;
  425. const uint8_t fileCnt = card.get_num_Files();
  426. for (uint8_t i = 0; i < fileCnt; i++) {
  427. card.getfilename_sorted(SD_ORDER(i, fileCnt));
  428. //if (card.longFilename[0] == 0)
  429. strcpy(tmp, card.filename);
  430. //else
  431. // strcpy(tmp, card.longFilename);
  432. ZERO(Fstream);
  433. strcpy(Fstream, tmp);
  434. if (card.flag.filenameIsDir && recu_level <= 10)
  435. strcat(Fstream, ".DIR");
  436. strcat(Fstream, "\r\n");
  437. send_to_wifi(Fstream, strlen(Fstream));
  438. }
  439. return fileCnt;
  440. }
  441. static void wifi_gcode_exec(uint8_t *cmd_line) {
  442. int8_t tempBuf[100] = { 0 };
  443. uint8_t *tmpStr = 0;
  444. int cmd_value;
  445. volatile int print_rate;
  446. if (strchr((char *)cmd_line, '\n') && (strchr((char *)cmd_line, 'G') || strchr((char *)cmd_line, 'M') || strchr((char *)cmd_line, 'T'))) {
  447. tmpStr = (uint8_t *)strchr((char *)cmd_line, '\n');
  448. if (tmpStr) *tmpStr = '\0';
  449. tmpStr = (uint8_t *)strchr((char *)cmd_line, '\r');
  450. if (tmpStr) *tmpStr = '\0';
  451. tmpStr = (uint8_t *)strchr((char *)cmd_line, '*');
  452. if (tmpStr) *tmpStr = '\0';
  453. tmpStr = (uint8_t *)strchr((char *)cmd_line, 'M');
  454. if (tmpStr) {
  455. cmd_value = atoi((char *)(tmpStr + 1));
  456. tmpStr = (uint8_t *)strchr((char *)tmpStr, ' ');
  457. switch (cmd_value) {
  458. case 20: // M20: Print SD / µdisk file
  459. if (uiCfg.print_state == IDLE) {
  460. int index = 0;
  461. if (tmpStr == 0) {
  462. gCfgItems.fileSysType = FILE_SYS_SD;
  463. send_to_wifi((char *)"Begin file list\r\n", strlen("Begin file list\r\n"));
  464. get_file_list((char *)"0:/");
  465. send_to_wifi((char *)"End file list\r\n", strlen("End file list\r\n"));
  466. send_to_wifi((char *)"ok\r\n", strlen("ok\r\n"));
  467. break;
  468. }
  469. while (tmpStr[index] == ' ') index++;
  470. if (gCfgItems.wifi_type == ESP_WIFI) {
  471. char *path = (char *)tempBuf;
  472. if (strlen((char *)&tmpStr[index]) < 80) {
  473. send_to_wifi((char *)"Begin file list\r\n", strlen("Begin file list\r\n"));
  474. if (strncmp((char *)&tmpStr[index], "1:", 2) == 0)
  475. gCfgItems.fileSysType = FILE_SYS_SD;
  476. else if (strncmp((char *)&tmpStr[index], "0:", 2) == 0)
  477. gCfgItems.fileSysType = FILE_SYS_USB;
  478. strcpy((char *)path, (char *)&tmpStr[index]);
  479. get_file_list(path);
  480. send_to_wifi((char *)"End file list\r\n", strlen("End file list\r\n"));
  481. }
  482. send_to_wifi((char *)"ok\r\n", strlen("ok\r\n"));
  483. }
  484. }
  485. break;
  486. case 21:
  487. /*init sd card*/
  488. send_to_wifi((char *)"ok\r\n", strlen("ok\r\n"));
  489. break;
  490. case 23:
  491. /*select the file*/
  492. if (uiCfg.print_state == IDLE) {
  493. int index = 0;
  494. while (tmpStr[index] == ' ') index++;
  495. if (strstr((char *)&tmpStr[index], ".g") || strstr((char *)&tmpStr[index], ".G")) {
  496. if (strlen((char *)&tmpStr[index]) < 80) {
  497. ZERO(list_file.file_name[sel_id]);
  498. if (gCfgItems.wifi_type == ESP_WIFI) {
  499. if (strncmp((char *)&tmpStr[index], "1:", 2) == 0)
  500. gCfgItems.fileSysType = FILE_SYS_SD;
  501. else if (strncmp((char *)&tmpStr[index], "0:", 2) == 0)
  502. gCfgItems.fileSysType = FILE_SYS_USB;
  503. else if (tmpStr[index] != '/')
  504. strcat((char *)list_file.file_name[0], "/");
  505. strcat((char *)list_file.file_name[sel_id], (char *)&tmpStr[index]);
  506. }
  507. else
  508. strcpy(list_file.file_name[sel_id], (char *)&tmpStr[index]);
  509. char *cur_name=strrchr(list_file.file_name[sel_id],'/');
  510. card.openFileRead(cur_name);
  511. if (card.isFileOpen())
  512. send_to_wifi((char *)"File selected\r\n", strlen("File selected\r\n"));
  513. else {
  514. send_to_wifi((char *)"file.open failed\r\n", strlen("file.open failed\r\n"));
  515. strcpy(list_file.file_name[sel_id], "notValid");
  516. }
  517. send_to_wifi((char *)"ok\r\n", strlen("ok\r\n"));
  518. }
  519. }
  520. }
  521. break;
  522. case 24:
  523. if (strcmp(list_file.file_name[sel_id], "notValid") != 0) {
  524. if (uiCfg.print_state == IDLE) {
  525. clear_cur_ui();
  526. reset_print_time();
  527. start_print_time();
  528. preview_gcode_prehandle(list_file.file_name[sel_id]);
  529. uiCfg.print_state = WORKING;
  530. lv_draw_printing();
  531. #if ENABLED(SDSUPPORT)
  532. if (!gcode_preview_over) {
  533. char *cur_name = strrchr(list_file.file_name[sel_id], '/');
  534. card.endFilePrint();
  535. SdFile file;
  536. SdFile *curDir;
  537. const char * const fname = card.diveToFile(true, curDir, cur_name);
  538. if (!fname) return;
  539. if (file.open(curDir, fname, O_READ)) {
  540. gCfgItems.curFilesize = file.fileSize();
  541. file.close();
  542. update_spi_flash();
  543. }
  544. card.openFileRead(cur_name);
  545. if (card.isFileOpen()) {
  546. //saved_feedrate_percentage = feedrate_percentage;
  547. feedrate_percentage = 100;
  548. planner.flow_percentage[0] = 100;
  549. planner.e_factor[0] = planner.flow_percentage[0] * 0.01f;
  550. #if EXTRUDERS == 2
  551. planner.flow_percentage[1] = 100;
  552. planner.e_factor[1] = planner.flow_percentage[1] * 0.01f;
  553. #endif
  554. card.startFileprint();
  555. TERN_(POWER_LOSS_RECOVERY, recovery.prepare());
  556. once_flag = false;
  557. }
  558. }
  559. #endif
  560. }
  561. else if (uiCfg.print_state == PAUSED) {
  562. uiCfg.print_state = RESUMING;
  563. clear_cur_ui();
  564. start_print_time();
  565. if (gCfgItems.from_flash_pic)
  566. flash_preview_begin = true;
  567. else
  568. default_preview_flg = true;
  569. lv_draw_printing();
  570. }
  571. else if (uiCfg.print_state == REPRINTING) {
  572. uiCfg.print_state = REPRINTED;
  573. clear_cur_ui();
  574. start_print_time();
  575. if (gCfgItems.from_flash_pic)
  576. flash_preview_begin = true;
  577. else
  578. default_preview_flg = true;
  579. lv_draw_printing();
  580. }
  581. }
  582. send_to_wifi((char *)"ok\r\n", strlen("ok\r\n"));
  583. break;
  584. case 25:
  585. /*pause print file*/
  586. if (uiCfg.print_state == WORKING) {
  587. stop_print_time();
  588. clear_cur_ui();
  589. #if ENABLED(SDSUPPORT)
  590. card.pauseSDPrint();
  591. uiCfg.print_state = PAUSING;
  592. #endif
  593. if (gCfgItems.from_flash_pic)
  594. flash_preview_begin = true;
  595. else
  596. default_preview_flg = true;
  597. lv_draw_printing();
  598. send_to_wifi((char *)"ok\r\n", strlen("ok\r\n"));
  599. }
  600. break;
  601. case 26:
  602. /*stop print file*/
  603. if ((uiCfg.print_state == WORKING) || (uiCfg.print_state == PAUSED) || (uiCfg.print_state == REPRINTING)) {
  604. stop_print_time();
  605. clear_cur_ui();
  606. #if ENABLED(SDSUPPORT)
  607. uiCfg.print_state = IDLE;
  608. card.flag.abort_sd_printing = true;
  609. #endif
  610. lv_draw_ready_print();
  611. send_to_wifi((char *)"ok\r\n", strlen("ok\r\n"));
  612. }
  613. break;
  614. case 27:
  615. /*report print rate*/
  616. if ((uiCfg.print_state == WORKING) || (uiCfg.print_state == PAUSED)|| (uiCfg.print_state == REPRINTING)) {
  617. print_rate = uiCfg.totalSend;
  618. ZERO(tempBuf);
  619. sprintf((char *)tempBuf, "M27 %d\r\n", print_rate);
  620. send_to_wifi((char *)tempBuf, strlen((char *)tempBuf));
  621. }
  622. break;
  623. case 28:
  624. /*begin to transfer file to filesys*/
  625. if (uiCfg.print_state == IDLE) {
  626. int index = 0;
  627. while (tmpStr[index] == ' ') index++;
  628. if (strstr((char *)&tmpStr[index], ".g") || strstr((char *)&tmpStr[index], ".G")) {
  629. strcpy((char *)file_writer.saveFileName, (char *)&tmpStr[index]);
  630. if (gCfgItems.fileSysType == FILE_SYS_SD) {
  631. ZERO(tempBuf);
  632. sprintf((char *)tempBuf, "%s", file_writer.saveFileName);
  633. }
  634. else if (gCfgItems.fileSysType == FILE_SYS_USB) {
  635. ZERO(tempBuf);
  636. sprintf((char *)tempBuf, "%s", (char *)file_writer.saveFileName);
  637. }
  638. mount_file_sys(gCfgItems.fileSysType);
  639. #if ENABLED(SDSUPPORT)
  640. char *cur_name = strrchr(list_file.file_name[sel_id], '/');
  641. card.openFileWrite(cur_name);
  642. if (card.isFileOpen()) {
  643. ZERO(file_writer.saveFileName);
  644. strcpy((char *)file_writer.saveFileName, (char *)&tmpStr[index]);
  645. ZERO(tempBuf);
  646. sprintf((char *)tempBuf, "Writing to file: %s\r\n", (char *)file_writer.saveFileName);
  647. wifi_ret_ack();
  648. send_to_wifi((char *)tempBuf, strlen((char *)tempBuf));
  649. total_write = 0;
  650. wifi_link_state = WIFI_WAIT_TRANS_START;
  651. }
  652. else {
  653. wifi_link_state = WIFI_CONNECTED;
  654. clear_cur_ui();
  655. lv_draw_dialog(DIALOG_TRANSFER_NO_DEVICE);
  656. }
  657. #endif
  658. }
  659. }
  660. break;
  661. case 105:
  662. case 991:
  663. ZERO(tempBuf);
  664. if (cmd_value == 105) {
  665. send_to_wifi((char *)"ok\r\n", strlen("ok\r\n"));
  666. sprintf((char *)tempBuf,"T:%.1f /%.1f B:%.1f /%.1f T0:%.1f /%.1f T1:%.1f /%.1f @:0 B@:0\r\n",
  667. (float)thermalManager.temp_hotend[0].celsius, (float)thermalManager.temp_hotend[0].target,
  668. #if HAS_HEATED_BED
  669. (float)thermalManager.temp_bed.celsius, (float)thermalManager.temp_bed.target,
  670. #else
  671. 0.0f, 0.0f,
  672. #endif
  673. (float)thermalManager.temp_hotend[0].celsius, (float)thermalManager.temp_hotend[0].target,
  674. #if !defined(SINGLENOZZLE) && HAS_MULTI_EXTRUDER
  675. (float)thermalManager.temp_hotend[1].celsius, (float)thermalManager.temp_hotend[1].target
  676. #else
  677. 0.0f, 0.0f
  678. #endif
  679. );
  680. }
  681. else {
  682. sprintf((char *)tempBuf,"T:%d /%d B:%d /%d T0:%d /%d T1:%d /%d @:0 B@:0\r\n",
  683. (int)thermalManager.temp_hotend[0].celsius, (int)thermalManager.temp_hotend[0].target,
  684. #if HAS_HEATED_BED
  685. (int)thermalManager.temp_bed.celsius, (int)thermalManager.temp_bed.target,
  686. #else
  687. 0, 0,
  688. #endif
  689. (int)thermalManager.temp_hotend[0].celsius, (int)thermalManager.temp_hotend[0].target,
  690. #if !defined(SINGLENOZZLE) && HAS_MULTI_EXTRUDER
  691. (int)thermalManager.temp_hotend[1].celsius, (int)thermalManager.temp_hotend[1].target
  692. #else
  693. 0, 0
  694. #endif
  695. );
  696. }
  697. send_to_wifi((char *)tempBuf, strlen((char *)tempBuf));
  698. queue.enqueue_one_P(PSTR("M105"));
  699. break;
  700. case 992:
  701. if ((uiCfg.print_state == WORKING) || (uiCfg.print_state == PAUSED)) {
  702. ZERO(tempBuf);
  703. sprintf((char *)tempBuf, "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);
  704. wifi_ret_ack();
  705. send_to_wifi((char *)tempBuf, strlen((char *)tempBuf));
  706. }
  707. break;
  708. case 994:
  709. if ((uiCfg.print_state == WORKING) || (uiCfg.print_state == PAUSED)) {
  710. ZERO(tempBuf);
  711. if (strlen((char *)list_file.file_name[sel_id]) > (100 - 1)) return;
  712. sprintf((char *)tempBuf, "M994 %s;%d\n", list_file.file_name[sel_id],(int)gCfgItems.curFilesize);
  713. wifi_ret_ack();
  714. send_to_wifi((char *)tempBuf, strlen((char *)tempBuf));
  715. }
  716. break;
  717. case 997:
  718. if (uiCfg.print_state == IDLE) {
  719. wifi_ret_ack();
  720. send_to_wifi((char *)"M997 IDLE\r\n", strlen("M997 IDLE\r\n"));
  721. }
  722. else if (uiCfg.print_state == WORKING) {
  723. wifi_ret_ack();
  724. send_to_wifi((char *)"M997 PRINTING\r\n", strlen("M997 PRINTING\r\n"));
  725. }
  726. else if (uiCfg.print_state == PAUSED) {
  727. wifi_ret_ack();
  728. send_to_wifi((char *)"M997 PAUSE\r\n", strlen("M997 PAUSE\r\n"));
  729. }
  730. else if (uiCfg.print_state == REPRINTING) {
  731. wifi_ret_ack();
  732. send_to_wifi((char *)"M997 PAUSE\r\n", strlen("M997 PAUSE\r\n"));
  733. }
  734. if (uiCfg.command_send == 0) get_wifi_list_command_send();
  735. break;
  736. case 998:
  737. if (uiCfg.print_state == IDLE) {
  738. int v = atoi((char *)tmpStr);
  739. if (v == 0)
  740. set_cur_file_sys(0);
  741. else if (v == 1)
  742. set_cur_file_sys(1);
  743. wifi_ret_ack();
  744. }
  745. break;
  746. case 115:
  747. ZERO(tempBuf);
  748. send_to_wifi((char *)"ok\r\n", strlen("ok\r\n"));
  749. send_to_wifi((char *)"FIRMWARE_NAME:Robin_nano\r\n", strlen("FIRMWARE_NAME:Robin_nano\r\n"));
  750. break;
  751. default:
  752. strcat((char *)cmd_line, "\n");
  753. if (espGcodeFifo.wait_tick > 5) {
  754. uint32_t left;
  755. if (espGcodeFifo.r > espGcodeFifo.w)
  756. left = espGcodeFifo.r - espGcodeFifo.w - 1;
  757. else
  758. left = WIFI_GCODE_BUFFER_SIZE + espGcodeFifo.r - espGcodeFifo.w - 1;
  759. if (left >= strlen((const char *)cmd_line)) {
  760. uint32_t index = 0;
  761. while (index < strlen((const char *)cmd_line)) {
  762. espGcodeFifo.Buffer[espGcodeFifo.w] = cmd_line[index] ;
  763. espGcodeFifo.w = (espGcodeFifo.w + 1) % WIFI_GCODE_BUFFER_SIZE;
  764. index++;
  765. }
  766. if (left - WIFI_GCODE_BUFFER_LEAST_SIZE >= strlen((const char *)cmd_line))
  767. send_to_wifi((char *)"ok\r\n", strlen("ok\r\n"));
  768. else
  769. need_ok_later = true;
  770. }
  771. }
  772. break;
  773. }
  774. }
  775. else {
  776. strcat((char *)cmd_line, "\n");
  777. if (espGcodeFifo.wait_tick > 5) {
  778. uint32_t left_g;
  779. if (espGcodeFifo.r > espGcodeFifo.w)
  780. left_g = espGcodeFifo.r - espGcodeFifo.w - 1;
  781. else
  782. left_g = WIFI_GCODE_BUFFER_SIZE + espGcodeFifo.r - espGcodeFifo.w - 1;
  783. if (left_g >= strlen((const char *)cmd_line)) {
  784. uint32_t index = 0;
  785. while (index < strlen((const char *)cmd_line)) {
  786. espGcodeFifo.Buffer[espGcodeFifo.w] = cmd_line[index] ;
  787. espGcodeFifo.w = (espGcodeFifo.w + 1) % WIFI_GCODE_BUFFER_SIZE;
  788. index++;
  789. }
  790. if (left_g - WIFI_GCODE_BUFFER_LEAST_SIZE >= strlen((const char *)cmd_line))
  791. send_to_wifi((char *)"ok\r\n", strlen("ok\r\n"));
  792. else
  793. need_ok_later = true;
  794. }
  795. }
  796. }
  797. }
  798. }
  799. static int32_t charAtArray(const uint8_t *_array, uint32_t _arrayLen, uint8_t _char) {
  800. for (uint32_t i = 0; i < _arrayLen; i++)
  801. if (*(_array + i) == _char) return i;
  802. return -1;
  803. }
  804. void get_wifi_list_command_send() {
  805. char buf[] = { 0xA5, 0x07, 0x00, 0x00, 0xFC };
  806. raw_send_to_wifi(buf, 5);
  807. }
  808. static void net_msg_handle(uint8_t * msg, uint16_t msgLen) {
  809. int wifiNameLen, wifiKeyLen, hostLen, id_len, ver_len;
  810. if (msgLen <= 0) return;
  811. // ip
  812. sprintf(ipPara.ip_addr, "%d.%d.%d.%d", msg[0], msg[1], msg[2], msg[3]);
  813. // port
  814. // connect state
  815. switch (msg[6]) {
  816. case 0x0A: wifi_link_state = WIFI_CONNECTED; break;
  817. case 0x0E: wifi_link_state = WIFI_EXCEPTION; break;
  818. default: wifi_link_state = WIFI_NOT_CONFIG; break;
  819. }
  820. // mode
  821. wifiPara.mode = msg[7];
  822. // wifi name
  823. wifiNameLen = msg[8];
  824. wifiKeyLen = msg[9 + wifiNameLen];
  825. if (wifiNameLen < 32) {
  826. ZERO(wifiPara.ap_name);
  827. memcpy(wifiPara.ap_name, &msg[9], wifiNameLen);
  828. memset(&wifi_list.wifiConnectedName, 0, sizeof(wifi_list.wifiConnectedName));
  829. memcpy(&wifi_list.wifiConnectedName, &msg[9], wifiNameLen);
  830. // wifi key
  831. if (wifiKeyLen < 64) {
  832. ZERO(wifiPara.keyCode);
  833. memcpy(wifiPara.keyCode, &msg[10 + wifiNameLen], wifiKeyLen);
  834. }
  835. }
  836. cloud_para.state =msg[10 + wifiNameLen + wifiKeyLen];
  837. hostLen = msg[11 + wifiNameLen + wifiKeyLen];
  838. if (cloud_para.state) {
  839. if (hostLen < 96) {
  840. ZERO(cloud_para.hostUrl);
  841. memcpy(cloud_para.hostUrl, &msg[12 + wifiNameLen + wifiKeyLen], hostLen);
  842. }
  843. cloud_para.port = msg[12 + wifiNameLen + wifiKeyLen + hostLen] + (msg[13 + wifiNameLen + wifiKeyLen + hostLen] << 8);
  844. }
  845. // id
  846. id_len = msg[14 + wifiNameLen + wifiKeyLen + hostLen];
  847. if (id_len == 20) {
  848. ZERO(cloud_para.id);
  849. memcpy(cloud_para.id, (const char *)&msg[15 + wifiNameLen + wifiKeyLen + hostLen], id_len);
  850. }
  851. ver_len = msg[15 + wifiNameLen + wifiKeyLen + hostLen + id_len];
  852. if (ver_len < 20) {
  853. ZERO(wifi_firm_ver);
  854. memcpy(wifi_firm_ver, (const char *)&msg[16 + wifiNameLen + wifiKeyLen + hostLen + id_len], ver_len);
  855. }
  856. if (uiCfg.configWifi == 1) {
  857. if ((wifiPara.mode != gCfgItems.wifi_mode_sel)
  858. || (strncmp(wifiPara.ap_name, (const char *)uiCfg.wifi_name, 32) != 0)
  859. || (strncmp(wifiPara.keyCode, (const char *)uiCfg.wifi_key, 64) != 0)) {
  860. package_to_wifi(WIFI_PARA_SET, (char *)0, 0);
  861. }
  862. else uiCfg.configWifi = 0;
  863. }
  864. if (cfg_cloud_flag == 1) {
  865. if (((cloud_para.state >> 4) != (char)gCfgItems.cloud_enable)
  866. || (strncmp(cloud_para.hostUrl, (const char *)uiCfg.cloud_hostUrl, 96) != 0)
  867. || (cloud_para.port != uiCfg.cloud_port)) {
  868. package_to_wifi(WIFI_CLOUD_CFG, (char *)0, 0);
  869. }
  870. else cfg_cloud_flag = 0;
  871. }
  872. }
  873. static void wifi_list_msg_handle(uint8_t * msg, uint16_t msgLen) {
  874. int wifiNameLen,wifiMsgIdex = 1;
  875. int8_t wifi_name_is_same = 0;
  876. int8_t i, j;
  877. int8_t wifi_name_num = 0;
  878. uint8_t *str = 0;
  879. int8_t valid_name_num;
  880. if (msgLen <= 0) return;
  881. if (disp_state == KEY_BOARD_UI) return;
  882. wifi_list.getNameNum = msg[0];
  883. if (wifi_list.getNameNum < 20) {
  884. uiCfg.command_send = 1;
  885. ZERO(wifi_list.wifiName);
  886. wifi_name_num = wifi_list.getNameNum;
  887. valid_name_num = 0;
  888. str = wifi_list.wifiName[0];
  889. if (wifi_list.getNameNum > 0) wifi_list.currentWifipage = 1;
  890. for (i = 0; i < wifi_list.getNameNum; i++) {
  891. wifiNameLen = msg[wifiMsgIdex++];
  892. if (wifiNameLen < 32) {
  893. memset(str, 0, WIFI_NAME_BUFFER_SIZE);
  894. memcpy(str, &msg[wifiMsgIdex], wifiNameLen);
  895. for (j = 0; j < valid_name_num; j++) {
  896. if (strcmp((const char *)str,(const char *)wifi_list.wifiName[j]) == 0) {
  897. wifi_name_is_same = 1;
  898. break;
  899. }
  900. }
  901. if (wifi_name_is_same != 1) {
  902. //for (j=0;j<wifiNameLen;j++) {
  903. if (str[0] > 0x80) {
  904. wifi_name_is_same = 1;
  905. //break;
  906. }
  907. //}
  908. }
  909. if (wifi_name_is_same == 1) {
  910. wifi_name_is_same = 0;
  911. wifiMsgIdex += wifiNameLen;
  912. //wifi_list.RSSI[i] = msg[wifiMsgIdex];
  913. wifiMsgIdex += 1;
  914. wifi_name_num--;
  915. //i--;
  916. continue;
  917. }
  918. if (i < WIFI_TOTAL_NUMBER - 1)
  919. str = wifi_list.wifiName[++valid_name_num];
  920. }
  921. wifiMsgIdex += wifiNameLen;
  922. wifi_list.RSSI[i] = msg[wifiMsgIdex++];
  923. }
  924. wifi_list.getNameNum = wifi_name_num;
  925. wifi_list.getPage = wifi_list.getNameNum / NUMBER_OF_PAGE + ((wifi_list.getNameNum % NUMBER_OF_PAGE) != 0);
  926. wifi_list.nameIndex = 0;
  927. if (disp_state == WIFI_LIST_UI) disp_wifi_list();
  928. }
  929. }
  930. static void gcode_msg_handle(uint8_t * msg, uint16_t msgLen) {
  931. uint8_t gcodeBuf[100] = { 0 };
  932. char *index_s, *index_e;
  933. if (msgLen <= 0) return;
  934. index_s = (char *)msg;
  935. index_e = (char *)strchr((char *)msg, '\n');
  936. if (*msg == 'N') {
  937. index_s = (char *)strchr((char *)msg, ' ');
  938. while (*index_s == ' ') index_s++;
  939. }
  940. while ((index_e != 0) && ((int)index_s < (int)index_e)) {
  941. if ((int)(index_e - index_s) < (int)sizeof(gcodeBuf)) {
  942. ZERO(gcodeBuf);
  943. memcpy(gcodeBuf, index_s, index_e - index_s + 1);
  944. wifi_gcode_exec(gcodeBuf);
  945. }
  946. while ((*index_e == '\r') || (*index_e == '\n')) index_e++;
  947. index_s = index_e;
  948. index_e = (char *)strchr(index_s, '\n');
  949. }
  950. }
  951. void utf8_2_unicode(uint8_t *source,uint8_t Len) {
  952. uint8_t i = 0, char_i = 0, char_byte_num = 0;
  953. uint16_t u16_h, u16_m, u16_l, u16_value;
  954. uint8_t FileName_unicode[30];
  955. ZERO(FileName_unicode);
  956. while (1) {
  957. char_byte_num = source[i] & 0xF0;
  958. if (source[i] < 0X80) {
  959. //ASCII --1byte
  960. FileName_unicode[char_i] = source[i];
  961. i += 1;
  962. char_i += 1;
  963. }
  964. else if (char_byte_num == 0XC0 || char_byte_num == 0XD0) {
  965. //--2byte
  966. u16_h = (((uint16_t)source[i] << 8) & 0x1F00) >> 2;
  967. u16_l = ((uint16_t)source[i + 1] & 0x003F);
  968. u16_value = (u16_h | u16_l);
  969. FileName_unicode[char_i] = (uint8_t)((u16_value & 0xFF00) >> 8);
  970. FileName_unicode[char_i + 1] = (uint8_t)(u16_value & 0x00FF);
  971. i += 2;
  972. char_i += 2;
  973. }
  974. else if (char_byte_num == 0XE0) {
  975. //--3byte
  976. u16_h = (((uint16_t)source[i] << 8) & 0x0F00) << 4;
  977. u16_m = (((uint16_t)source[i + 1] << 8) & 0x3F00) >> 2;
  978. u16_l = ((uint16_t)source[i + 2] & 0x003F);
  979. u16_value = (u16_h | u16_m | u16_l);
  980. FileName_unicode[char_i] = (uint8_t)((u16_value & 0xFF00) >> 8);
  981. FileName_unicode[char_i + 1] = (uint8_t)(u16_value & 0x00FF);
  982. i += 3;
  983. char_i += 2;
  984. }
  985. else if (char_byte_num == 0XF0) {
  986. //--4byte
  987. i += 4;
  988. //char_i += 3;
  989. }
  990. else {
  991. break;
  992. }
  993. if (i >= Len || i >= 255) break;
  994. }
  995. COPY(source, FileName_unicode);
  996. }
  997. char saveFilePath[50];
  998. static bool longName2DosName(const char* longName, uint8_t* dosName) {
  999. uint8_t i = 11;
  1000. while (i--) dosName[i] = '\0';
  1001. while (*longName) {
  1002. uint8_t c = *longName++;
  1003. if (c == '.') { // For a dot...
  1004. if (i == 0) return false;
  1005. else { strcat((char *)dosName,".GCO"); return dosName[0] != '\0'; }
  1006. }
  1007. else {
  1008. // Fail for illegal characters
  1009. PGM_P p = PSTR("|<>^+=?/[];,*\"\\");
  1010. while (uint8_t b = pgm_read_byte(p++)) if (b == c) return false;
  1011. if (c < 0x21 || c == 0x7F) return false; // Check size, non-printable characters
  1012. dosName[i++] = (c < 'a' || c > 'z') ? (c) : (c + ('A' - 'a')); // Uppercase required for 8.3 name
  1013. }
  1014. if (i >= 5) strcat((char *)dosName,"~1.GCO");
  1015. }
  1016. return dosName[0] != '\0'; // Return true if any name was set
  1017. }
  1018. static void file_first_msg_handle(uint8_t * msg, uint16_t msgLen) {
  1019. uint8_t fileNameLen = *msg;
  1020. if (msgLen != fileNameLen + 5) return;
  1021. file_writer.fileLen = *((uint32_t *)(msg + 1));
  1022. ZERO(file_writer.saveFileName);
  1023. memcpy(file_writer.saveFileName, msg + 5, fileNameLen);
  1024. utf8_2_unicode(file_writer.saveFileName,fileNameLen);
  1025. ZERO(file_writer.write_buf);
  1026. if (strlen((const char *)file_writer.saveFileName) > sizeof(saveFilePath))
  1027. return;
  1028. ZERO(saveFilePath);
  1029. if (gCfgItems.fileSysType == FILE_SYS_SD) {
  1030. //sprintf((char *)saveFilePath, "/%s", file_writer.saveFileName);
  1031. card.mount();
  1032. //ZERO(list_file.long_name[sel_id]);
  1033. //memcpy(list_file.long_name[sel_id],file_writer.saveFileName,sizeof(list_file.long_name[sel_id]));
  1034. }
  1035. else if (gCfgItems.fileSysType == FILE_SYS_USB) {
  1036. }
  1037. file_writer.write_index = 0;
  1038. lastFragment = -1;
  1039. wifiTransError.flag = 0;
  1040. wifiTransError.start_tick = 0;
  1041. wifiTransError.now_tick = 0;
  1042. TERN_(SDSUPPORT, card.closefile());
  1043. wifi_delay(1000);
  1044. #if ENABLED(SDSUPPORT)
  1045. uint8_t dosName[FILENAME_LENGTH];
  1046. if (!longName2DosName((const char *)file_writer.saveFileName,dosName)) {
  1047. clear_cur_ui();
  1048. upload_result = 2;
  1049. wifiTransError.flag = 1;
  1050. wifiTransError.start_tick = getWifiTick();
  1051. lv_draw_dialog(DIALOG_TYPE_UPLOAD_FILE);
  1052. return;
  1053. }
  1054. sprintf((char *)saveFilePath, "/%s", dosName);
  1055. ZERO(list_file.long_name[sel_id]);
  1056. memcpy(list_file.long_name[sel_id], dosName, sizeof(dosName));
  1057. char *cur_name=strrchr((const char *)saveFilePath,'/');
  1058. SdFile file;
  1059. SdFile *curDir;
  1060. card.endFilePrint();
  1061. const char * const fname = card.diveToFile(true, curDir, cur_name);
  1062. if (!fname) return;
  1063. if (file.open(curDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC)) {
  1064. gCfgItems.curFilesize = file.fileSize();
  1065. }
  1066. else {
  1067. clear_cur_ui();
  1068. upload_result = 2;
  1069. wifiTransError.flag = 1;
  1070. wifiTransError.start_tick = getWifiTick();
  1071. lv_draw_dialog(DIALOG_TYPE_UPLOAD_FILE);
  1072. return;
  1073. }
  1074. #endif // SDSUPPORT
  1075. wifi_link_state = WIFI_TRANS_FILE;
  1076. upload_result = 1;
  1077. clear_cur_ui();
  1078. lv_draw_dialog(DIALOG_TYPE_UPLOAD_FILE);
  1079. lv_task_handler();
  1080. file_writer.tick_begin = getWifiTick();
  1081. }
  1082. #define FRAG_MASK _BV32(31)
  1083. static void file_fragment_msg_handle(uint8_t * msg, uint16_t msgLen) {
  1084. uint32_t frag = *((uint32_t *)msg);
  1085. if ((frag & FRAG_MASK) != (uint32_t)(lastFragment + 1)) {
  1086. ZERO(file_writer.write_buf);
  1087. file_writer.write_index = 0;
  1088. wifi_link_state = WIFI_CONNECTED;
  1089. upload_result = 2;
  1090. }
  1091. else {
  1092. if (write_to_file((char *)msg + 4, msgLen - 4) < 0) {
  1093. ZERO(file_writer.write_buf);
  1094. file_writer.write_index = 0;
  1095. wifi_link_state = WIFI_CONNECTED;
  1096. upload_result = 2;
  1097. return;
  1098. }
  1099. lastFragment = frag;
  1100. if ((frag & (~FRAG_MASK))) {
  1101. int res = card.write(file_writer.write_buf, file_writer.write_index);
  1102. if (res == -1) {
  1103. ZERO(file_writer.write_buf);
  1104. file_writer.write_index = 0;
  1105. wifi_link_state = WIFI_CONNECTED;
  1106. upload_result = 2;
  1107. return;
  1108. }
  1109. ZERO(file_writer.write_buf);
  1110. file_writer.write_index = 0;
  1111. file_writer.tick_end = getWifiTick();
  1112. upload_time = getWifiTickDiff(file_writer.tick_begin, file_writer.tick_end) / 1000;
  1113. upload_size = gCfgItems.curFilesize;
  1114. wifi_link_state = WIFI_CONNECTED;
  1115. upload_result = 3;
  1116. }
  1117. }
  1118. }
  1119. void esp_data_parser(char *cmdRxBuf, int len) {
  1120. int32_t head_pos;
  1121. int32_t tail_pos;
  1122. uint16_t cpyLen;
  1123. int16_t leftLen = len;
  1124. bool loop_again = false;
  1125. ESP_PROTOC_FRAME esp_frame;
  1126. while (leftLen > 0 || loop_again) {
  1127. loop_again = false;
  1128. if (esp_msg_index != 0) {
  1129. head_pos = 0;
  1130. cpyLen = (leftLen < (int16_t)((sizeof(esp_msg_buf) - esp_msg_index)) ? leftLen : sizeof(esp_msg_buf) - esp_msg_index);
  1131. memcpy(&esp_msg_buf[esp_msg_index], cmdRxBuf + len - leftLen, cpyLen);
  1132. esp_msg_index += cpyLen;
  1133. leftLen = leftLen - cpyLen;
  1134. tail_pos = charAtArray(esp_msg_buf, esp_msg_index, ESP_PROTOC_TAIL);
  1135. if (tail_pos == -1) {
  1136. if (esp_msg_index >= sizeof(esp_msg_buf)) {
  1137. ZERO(esp_msg_buf);
  1138. esp_msg_index = 0;
  1139. }
  1140. return;
  1141. }
  1142. }
  1143. else {
  1144. head_pos = charAtArray((uint8_t const *)&cmdRxBuf[len - leftLen], leftLen, ESP_PROTOC_HEAD);
  1145. if (head_pos == -1) return;
  1146. ZERO(esp_msg_buf);
  1147. memcpy(esp_msg_buf, &cmdRxBuf[len - leftLen + head_pos], leftLen - head_pos);
  1148. esp_msg_index = leftLen - head_pos;
  1149. leftLen = 0;
  1150. head_pos = 0;
  1151. tail_pos = charAtArray(esp_msg_buf, esp_msg_index, ESP_PROTOC_TAIL);
  1152. if (tail_pos == -1) {
  1153. if (esp_msg_index >= sizeof(esp_msg_buf)) {
  1154. ZERO(esp_msg_buf);
  1155. esp_msg_index = 0;
  1156. }
  1157. return;
  1158. }
  1159. }
  1160. esp_frame.type = esp_msg_buf[1];
  1161. if ( esp_frame.type != ESP_TYPE_NET
  1162. && esp_frame.type != ESP_TYPE_GCODE
  1163. && esp_frame.type != ESP_TYPE_FILE_FIRST
  1164. && esp_frame.type != ESP_TYPE_FILE_FRAGMENT
  1165. && esp_frame.type != ESP_TYPE_WIFI_LIST
  1166. ) {
  1167. ZERO(esp_msg_buf);
  1168. esp_msg_index = 0;
  1169. return;
  1170. }
  1171. esp_frame.dataLen = esp_msg_buf[2] + (esp_msg_buf[3] << 8);
  1172. if ((int)(4 + esp_frame.dataLen) > (int)(sizeof(esp_msg_buf))) {
  1173. ZERO(esp_msg_buf);
  1174. esp_msg_index = 0;
  1175. return;
  1176. }
  1177. if (esp_msg_buf[4 + esp_frame.dataLen] != ESP_PROTOC_TAIL) {
  1178. if (esp_msg_index >= sizeof(esp_msg_buf)) {
  1179. ZERO(esp_msg_buf);
  1180. esp_msg_index = 0;
  1181. }
  1182. return;
  1183. }
  1184. esp_frame.data = &esp_msg_buf[4];
  1185. switch (esp_frame.type) {
  1186. case ESP_TYPE_NET:
  1187. net_msg_handle(esp_frame.data, esp_frame.dataLen);
  1188. break;
  1189. case ESP_TYPE_GCODE:
  1190. gcode_msg_handle(esp_frame.data, esp_frame.dataLen);
  1191. break;
  1192. case ESP_TYPE_FILE_FIRST:
  1193. file_first_msg_handle(esp_frame.data, esp_frame.dataLen);
  1194. break;
  1195. case ESP_TYPE_FILE_FRAGMENT:
  1196. file_fragment_msg_handle(esp_frame.data, esp_frame.dataLen);
  1197. break;
  1198. case ESP_TYPE_WIFI_LIST:
  1199. wifi_list_msg_handle(esp_frame.data, esp_frame.dataLen);
  1200. break;
  1201. default: break;
  1202. }
  1203. esp_msg_index = cut_msg_head(esp_msg_buf, esp_msg_index, esp_frame.dataLen + 5);
  1204. if (esp_msg_index > 0) {
  1205. if (charAtArray(esp_msg_buf, esp_msg_index, ESP_PROTOC_HEAD) == -1) {
  1206. ZERO(esp_msg_buf);
  1207. esp_msg_index = 0;
  1208. return;
  1209. }
  1210. if ((charAtArray(esp_msg_buf, esp_msg_index, ESP_PROTOC_HEAD) != -1) && (charAtArray(esp_msg_buf, esp_msg_index, ESP_PROTOC_TAIL) != -1))
  1211. loop_again = true;
  1212. }
  1213. }
  1214. }
  1215. int32_t tick_net_time1, tick_net_time2;
  1216. int storeRcvData(int32_t len) {
  1217. unsigned char tmpW = wifiDmaRcvFifo.write_cur;
  1218. if (len <= UDISKBUFLEN && wifiDmaRcvFifo.state[tmpW] == udisk_buf_empty) {
  1219. for (uint16_t i = 0; i < len; i++)
  1220. wifiDmaRcvFifo.bufferAddr[tmpW][i] = WIFISERIAL.read();
  1221. wifiDmaRcvFifo.state[tmpW] = udisk_buf_full;
  1222. wifiDmaRcvFifo.write_cur = (tmpW + 1) % TRANS_RCV_FIFO_BLOCK_NUM;
  1223. return 1;
  1224. }
  1225. return 0;
  1226. }
  1227. int32_t readWifiFifo(uint8_t *retBuf, uint32_t bufLen) {
  1228. unsigned char tmpR = wifiDmaRcvFifo.read_cur;
  1229. if (bufLen >= UDISKBUFLEN && wifiDmaRcvFifo.state[tmpR] == udisk_buf_full) {
  1230. memcpy(retBuf, (unsigned char *)wifiDmaRcvFifo.bufferAddr[tmpR], UDISKBUFLEN);
  1231. wifiDmaRcvFifo.state[tmpR] = udisk_buf_empty;
  1232. wifiDmaRcvFifo.read_cur = (tmpR + 1) % TRANS_RCV_FIFO_BLOCK_NUM;
  1233. return UDISKBUFLEN;
  1234. }
  1235. return 0;
  1236. }
  1237. void stopEspTransfer() {
  1238. if (wifi_link_state == WIFI_TRANS_FILE)
  1239. wifi_link_state = WIFI_CONNECTED;
  1240. TERN_(SDSUPPORT, card.closefile());
  1241. if (upload_result != 3) {
  1242. wifiTransError.flag = 1;
  1243. wifiTransError.start_tick = getWifiTick();
  1244. card.removeFile((const char *)saveFilePath);
  1245. }
  1246. else {
  1247. }
  1248. wifi_delay(200);
  1249. WIFI_IO1_SET();
  1250. //exchangeFlashMode(1); //change spi flash to use dma mode
  1251. esp_port_begin(1);
  1252. if (wifiTransError.flag != 0x1) WIFI_IO1_RESET();
  1253. }
  1254. void wifi_rcv_handle() {
  1255. int32_t len = 0;
  1256. uint8_t ucStr[(UART_RX_BUFFER_SIZE) + 1] = { 0 };
  1257. int8_t getDataF = 0;
  1258. if (wifi_link_state == WIFI_TRANS_FILE) {
  1259. #if 0
  1260. if (WIFISERIAL.available() == UART_RX_BUFFER_SIZE) {
  1261. for (uint16_t i=0;i<UART_RX_BUFFER_SIZE;i++) {
  1262. ucStr[i] = WIFISERIAL.read();
  1263. len++;
  1264. }
  1265. }
  1266. #else
  1267. len = readWifiFifo(ucStr, UART_RX_BUFFER_SIZE);
  1268. #endif
  1269. if (len > 0) {
  1270. esp_data_parser((char *)ucStr, len);
  1271. if (wifi_link_state == WIFI_CONNECTED) {
  1272. clear_cur_ui();
  1273. lv_draw_dialog(DIALOG_TYPE_UPLOAD_FILE);
  1274. stopEspTransfer();
  1275. }
  1276. getDataF = 1;
  1277. }
  1278. if (esp_state == TRANSFER_STORE) {
  1279. if (storeRcvData(UART_RX_BUFFER_SIZE)) {
  1280. esp_state = TRANSFERING;
  1281. //esp_dma_pre();
  1282. if (wifiTransError.flag != 0x1) WIFI_IO1_RESET();
  1283. }
  1284. else
  1285. WIFI_IO1_SET();
  1286. }
  1287. }
  1288. else {
  1289. //len = readUsartFifo((SZ_USART_FIFO *)&WifiRxFifo, (int8_t *)ucStr, UART_RX_BUFFER_SIZE);
  1290. len = readWifiBuf((int8_t *)ucStr, UART_RX_BUFFER_SIZE);
  1291. if (len > 0) {
  1292. esp_data_parser((char *)ucStr, len);
  1293. if (wifi_link_state == WIFI_TRANS_FILE) {
  1294. //exchangeFlashMode(0); //change spi flash not use dma mode
  1295. wifi_delay(10);
  1296. esp_port_begin(0);
  1297. wifi_delay(10);
  1298. tick_net_time1 = 0;
  1299. }
  1300. if (wifiTransError.flag != 0x1) WIFI_IO1_RESET();
  1301. getDataF = 1;
  1302. }
  1303. if (need_ok_later && (queue.length < BUFSIZE)) {
  1304. need_ok_later = false;
  1305. send_to_wifi((char *)"ok\r\n", strlen("ok\r\n"));
  1306. }
  1307. }
  1308. if (getDataF == 1) {
  1309. tick_net_time1 = getWifiTick();
  1310. }
  1311. else {
  1312. tick_net_time2 = getWifiTick();
  1313. if (wifi_link_state == WIFI_TRANS_FILE) {
  1314. if ((tick_net_time1 != 0) && (getWifiTickDiff(tick_net_time1, tick_net_time2) > 4500)) {
  1315. wifi_link_state = WIFI_CONNECTED;
  1316. upload_result = 2;
  1317. clear_cur_ui();
  1318. stopEspTransfer();
  1319. lv_draw_dialog(DIALOG_TYPE_UPLOAD_FILE);
  1320. }
  1321. }
  1322. if ((tick_net_time1 != 0) && (getWifiTickDiff(tick_net_time1, tick_net_time2) > 10000))
  1323. wifi_link_state = WIFI_NOT_CONFIG;
  1324. if ((tick_net_time1 != 0) && (getWifiTickDiff(tick_net_time1, tick_net_time2) > 120000)) {
  1325. wifi_link_state = WIFI_NOT_CONFIG;
  1326. wifi_reset();
  1327. tick_net_time1 = getWifiTick();
  1328. }
  1329. }
  1330. if (wifiTransError.flag == 0x1) {
  1331. wifiTransError.now_tick = getWifiTick();
  1332. if (getWifiTickDiff(wifiTransError.start_tick, wifiTransError.now_tick) > WAIT_ESP_TRANS_TIMEOUT_TICK) {
  1333. wifiTransError.flag = 0;
  1334. WIFI_IO1_RESET();
  1335. }
  1336. }
  1337. }
  1338. void wifi_looping() {
  1339. do { wifi_rcv_handle(); } while (wifi_link_state == WIFI_TRANS_FILE);
  1340. }
  1341. void mks_esp_wifi_init() {
  1342. wifi_link_state = WIFI_NOT_CONFIG;
  1343. SET_OUTPUT(WIFI_RESET_PIN);
  1344. WIFI_SET();
  1345. SET_OUTPUT(WIFI_IO1_PIN);
  1346. SET_INPUT_PULLUP(WIFI_IO0_PIN);
  1347. WIFI_IO1_SET();
  1348. esp_state = TRANSFER_IDLE;
  1349. esp_port_begin(1);
  1350. wifi_reset();
  1351. #if 0
  1352. res = f_open(&esp_upload.uploadFile, ESP_FIRMWARE_FILE, FA_OPEN_EXISTING | FA_READ);
  1353. if (res == FR_OK) {
  1354. f_close(&esp_upload.uploadFile);
  1355. wifi_delay(2000);
  1356. if (usartFifoAvailable((SZ_USART_FIFO *)&WifiRxFifo) < 20) {
  1357. return;
  1358. }
  1359. clear_cur_ui();
  1360. draw_dialog(DIALOG_TYPE_UPDATE_ESP_FIRMARE);
  1361. if (wifi_upload(0) >= 0) {
  1362. f_unlink("1:/MKS_WIFI_CUR");
  1363. f_rename(ESP_FIRMWARE_FILE,"/MKS_WIFI_CUR");
  1364. }
  1365. draw_return_ui();
  1366. update_flag = 1;
  1367. }
  1368. if (update_flag == 0) {
  1369. res = f_open(&esp_upload.uploadFile, ESP_WEB_FIRMWARE_FILE, FA_OPEN_EXISTING | FA_READ);
  1370. if (res == FR_OK) {
  1371. f_close(&esp_upload.uploadFile);
  1372. wifi_delay(2000);
  1373. if (usartFifoAvailable((SZ_USART_FIFO *)&WifiRxFifo) < 20) {
  1374. return;
  1375. }
  1376. clear_cur_ui();
  1377. draw_dialog(DIALOG_TYPE_UPDATE_ESP_FIRMARE);
  1378. if (wifi_upload(1) >= 0) {
  1379. f_unlink("1:/MKS_WIFI_CUR");
  1380. f_rename(ESP_WEB_FIRMWARE_FILE,"/MKS_WIFI_CUR");
  1381. }
  1382. draw_return_ui();
  1383. update_flag = 1;
  1384. }
  1385. }
  1386. if (update_flag == 0) {
  1387. res = f_open(&esp_upload.uploadFile, ESP_WEB_FILE, FA_OPEN_EXISTING | FA_READ);
  1388. if (res == FR_OK) {
  1389. f_close(&esp_upload.uploadFile);
  1390. wifi_delay(2000);
  1391. if (usartFifoAvailable((SZ_USART_FIFO *)&WifiRxFifo) < 20) {
  1392. return;
  1393. }
  1394. clear_cur_ui();
  1395. draw_dialog(DIALOG_TYPE_UPDATE_ESP_DATA);
  1396. if (wifi_upload(2) >= 0) {
  1397. f_unlink("1:/MKS_WEB_CONTROL_CUR");
  1398. f_rename(ESP_WEB_FILE,"/MKS_WEB_CONTROL_CUR");
  1399. }
  1400. draw_return_ui();
  1401. }
  1402. }
  1403. #endif
  1404. wifiPara.decodeType = WIFI_DECODE_TYPE;
  1405. wifiPara.baud = 115200;
  1406. wifi_link_state = WIFI_NOT_CONFIG;
  1407. }
  1408. #define BUF_INC_POINTER(p) ((p + 1 == UART_FIFO_BUFFER_SIZE) ? 0 : (p + 1))
  1409. int usartFifoAvailable(SZ_USART_FIFO *fifo) {
  1410. int diff = fifo->uart_write_point - fifo->uart_read_point;
  1411. if (diff < 0) diff += UART_FIFO_BUFFER_SIZE;
  1412. return diff;
  1413. }
  1414. int readUsartFifo(SZ_USART_FIFO *fifo, int8_t *buf, int32_t len) {
  1415. int i = 0 ;
  1416. while (i < len && fifo->uart_read_point != fifo->uart_write_point) {
  1417. buf[i++] = fifo->uartTxBuffer[fifo->uart_read_point];
  1418. fifo->uart_read_point = BUF_INC_POINTER(fifo->uart_read_point);
  1419. }
  1420. return i;
  1421. }
  1422. int writeUsartFifo(SZ_USART_FIFO *fifo, int8_t *buf, int32_t len) {
  1423. if (buf == 0 || len <= 0) return -1;
  1424. int i = 0 ;
  1425. while (i < len && fifo->uart_read_point != BUF_INC_POINTER(fifo->uart_write_point)) {
  1426. fifo->uartTxBuffer[fifo->uart_write_point] = buf[i++];
  1427. fifo->uart_write_point = BUF_INC_POINTER(fifo->uart_write_point);
  1428. }
  1429. return i;
  1430. }
  1431. void get_wifi_commands() {
  1432. static char wifi_line_buffer[MAX_CMD_SIZE];
  1433. static bool wifi_comment_mode = false;
  1434. static int wifi_read_count = 0;
  1435. if (espGcodeFifo.wait_tick > 5) {
  1436. while ((queue.length < BUFSIZE) && (espGcodeFifo.r != espGcodeFifo.w)) {
  1437. espGcodeFifo.wait_tick = 0;
  1438. char wifi_char = espGcodeFifo.Buffer[espGcodeFifo.r];
  1439. espGcodeFifo.r = (espGcodeFifo.r + 1) % WIFI_GCODE_BUFFER_SIZE;
  1440. /**
  1441. * If the character ends the line
  1442. */
  1443. if (wifi_char == '\n' || wifi_char == '\r') {
  1444. wifi_comment_mode = false; // end of line == end of comment
  1445. if (!wifi_read_count) continue; // skip empty lines
  1446. wifi_line_buffer[wifi_read_count] = 0; // terminate string
  1447. wifi_read_count = 0; //reset buffer
  1448. char* command = wifi_line_buffer;
  1449. while (*command == ' ') command++; // skip any leading spaces
  1450. // Movement commands alert when stopped
  1451. if (IsStopped()) {
  1452. char* gpos = strchr(command, 'G');
  1453. if (gpos) {
  1454. switch (strtol(gpos + 1, nullptr, 10)) {
  1455. case 0 ... 1:
  1456. #if ENABLED(ARC_SUPPORT)
  1457. case 2 ... 3:
  1458. #endif
  1459. #if ENABLED(BEZIER_CURVE_SUPPORT)
  1460. case 5:
  1461. #endif
  1462. SERIAL_ECHOLNPGM(STR_ERR_STOPPED);
  1463. LCD_MESSAGEPGM(MSG_STOPPED);
  1464. break;
  1465. }
  1466. }
  1467. }
  1468. #if DISABLED(EMERGENCY_PARSER)
  1469. // Process critical commands early
  1470. if (strcmp(command, "M108") == 0) {
  1471. wait_for_heatup = false;
  1472. TERN_(HAS_LCD_MENU, wait_for_user = false);
  1473. }
  1474. if (strcmp(command, "M112") == 0) kill(M112_KILL_STR, nullptr, true);
  1475. if (strcmp(command, "M410") == 0) quickstop_stepper();
  1476. #endif
  1477. // Add the command to the queue
  1478. queue.enqueue_one_P(wifi_line_buffer);
  1479. }
  1480. else if (wifi_read_count >= MAX_CMD_SIZE - 1) {
  1481. }
  1482. else { // it's not a newline, carriage return or escape char
  1483. if (wifi_char == ';') wifi_comment_mode = true;
  1484. if (!wifi_comment_mode) wifi_line_buffer[wifi_read_count++] = wifi_char;
  1485. }
  1486. }
  1487. }// queue has space, serial has data
  1488. else {
  1489. espGcodeFifo.wait_tick++;
  1490. }
  1491. }
  1492. int readWifiBuf(int8_t *buf, int32_t len) {
  1493. int i = 0;
  1494. while (i < len && WIFISERIAL.available())
  1495. buf[i++] = WIFISERIAL.read();
  1496. return i;
  1497. }
  1498. #endif // USE_WIFI_FUNCTION
  1499. #endif // HAS_TFT_LVGL_UI