My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

wifi_module.cpp 57KB

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