hmi.c 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090
  1. /*
  2. * Copyright (c) 2006-2020, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-05-26 Administrator the first version
  9. */
  10. #include <rtthread.h>
  11. #include "hmi.h"
  12. #include <board.h>
  13. #include <control.h>
  14. #include <rtdevice.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "stdarg.h"
  19. #include "string.h"
  20. #include "sht30_collect.h"
  21. #include "beep.h"
  22. #include "easyflash.h"
  23. #include "sound.h"
  24. #include "ymodem.h"
  25. #include <shell.h>
  26. #include "dtu_uart2.h"
  27. #include <fal.h>
  28. #include "selfcheck.h"
  29. #include <float.h>
  30. #include <math.h>
  31. #define DBG_ENABLE
  32. #define DBG_COLOR
  33. #define DBG_TAG "hmi"
  34. #define DBG_LEVEL DBG_LOG
  35. #include <rtdbg.h> //must after of DBG_LVL, DBG_TAG or other options
  36. #define HMI_THREAD_PRIORITY 5 //线程优先级
  37. #define HMI_THREAD_STACK_SIZE 2048 //线程栈空间大小
  38. #define HMI_THREAD_TIMESLICE 10 //时间片
  39. #define CONTROL_MOTOR_PRIORITY 7
  40. #define CONTROL_MOTOR_STACK_SIZE 2048
  41. #define CONTROL_MOTOR_TIMESLICE 5
  42. static char msg_pool[512];
  43. static char rx_buffer[HMI_UART_RXBUFFER_SIZE + 1];
  44. static rt_bool_t flash_flag = RT_TRUE;
  45. void list_mem(void);
  46. int mb_master_samlpe();
  47. /**/
  48. Device_Status_typedef device_status;
  49. Device_slide_typedef slide_para;
  50. time_t time(time_t *t);
  51. /* 串口接收消息结构*/
  52. struct rx_msg
  53. {
  54. rt_device_t dev;
  55. rt_size_t size;
  56. };
  57. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; /*初始化配置参数*/
  58. ////
  59. static rt_thread_t hmi1 = RT_NULL;
  60. static rt_thread_t ctr_motor = RT_NULL;
  61. rt_device_t serial; /*串口设备句柄*/
  62. rt_timer_t timer_uart2 = RT_NULL;
  63. /* 消息队列控制块 */
  64. static struct rt_messagequeue rx_mq;//接收数据消息队列
  65. static void HMI_Decode(void);
  66. static rt_err_t uart_input(rt_device_t dev, rt_size_t size);
  67. static int HMI_uart4_init(void)
  68. {
  69. serial = rt_device_find(UART_NAME);
  70. /*修改串口配置参数*/
  71. config.baud_rate = BAUD_RATE_115200; //修改波特率9600
  72. config.data_bits = DATA_BITS_8; //数据位8
  73. config.stop_bits = STOP_BITS_1; //停止位1
  74. #ifdef RT_USING_SERIAL_V2
  75. config.rx_bufsz = 512;
  76. config.tx_bufsz = 128;
  77. #else
  78. config.bufsz = 512; //缓冲区128字节
  79. #endif
  80. config.parity = PARITY_NONE; //无奇偶校验
  81. /* step3:控制串口设备。通过控制接口传入命令控制字,与控制参数 */
  82. rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
  83. // /* step4:打开串口设备。以中断接收及轮询发送模式打开串口设备 */
  84. //rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
  85. /* 以 DMA 接收及轮询发送模式打开串口设备 */
  86. #ifdef RT_USING_SERIAL_V2
  87. rt_err_t uart_res = rt_device_open(serial, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING); // 串口设备使用模式为 (发送阻塞 接收非阻塞) 模式
  88. #else
  89. rt_err_t uart_res = rt_device_open(serial, RT_DEVICE_FLAG_DMA_RX); //RT_DEVICE_FLAG_DMA_RX
  90. #endif
  91. if(uart_res!=RT_EOK)
  92. {
  93. LOG_E("%s打开失败!",UART_NAME);
  94. }else{
  95. LOG_I("%s Open Success!",UART_NAME);
  96. }
  97. return RT_EOK;
  98. }
  99. /*串口屏写数据*/
  100. void HMI_WRITE(char *fmt, ...)
  101. {
  102. va_list ap;
  103. //末尾需要拼接的3个ff
  104. const char hmi_hex[3]={0xff,0xff,0xff};
  105. static char str[MAX_SIZE];
  106. memset(str,'\0',sizeof(str));
  107. rt_size_t str_length;
  108. va_start(ap,fmt);
  109. str_length = rt_vsnprintf(str, sizeof(str) - 1, fmt, ap);
  110. if (str_length > MAX_SIZE - 1)
  111. str_length = MAX_SIZE - 1;
  112. rt_device_write(serial, 0, str, str_length);
  113. va_end(ap);
  114. rt_device_write(serial, 0,hmi_hex,3);
  115. }
  116. /* 接收数据回调函数 */
  117. static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
  118. {
  119. struct rx_msg msg;
  120. rt_err_t result;
  121. msg.dev = dev;
  122. msg.size = size;
  123. result = rt_mq_send(&rx_mq, &msg, sizeof(msg));
  124. if ( result == -RT_EFULL)
  125. {
  126. /* 消息队列满 */
  127. LOG_I("hmi message queue full!\n");
  128. }
  129. return result;
  130. }
  131. /*入口函数*/
  132. static void Hmi_thread_entry(void *parameter)
  133. {
  134. struct rx_msg msg;
  135. rt_err_t result;
  136. rt_uint32_t rx_length;
  137. if(device_status.dtype==3){
  138. device_status.collt_box_sta = 0;
  139. device_status.run_sta = 0;
  140. }
  141. device_status.camsta = 1;
  142. while(1)
  143. {
  144. rt_memset(&msg, 0, sizeof(msg));
  145. /* 从消息队列中读取消息*/
  146. result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), 50);
  147. if (result == RT_EOK)
  148. {
  149. rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size);
  150. rx_buffer[rx_length] = '\0';
  151. //LOG_I("%s",rx_buffer);
  152. //\r 对应的hex为0x0d,0x0a,占两个字节,屏幕发送的数据都以\r结尾
  153. HMI_Decode();
  154. }
  155. /*********************************************************************************************************************/
  156. //rt_kprintf("%04d-%02d-%02d %02d:%02d:%02d\n",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  157. //rt_kprintf("workSta.vbat.txt=%c%d.%02d%c\n",hmi_str,voltage/100,voltage%100,hmi_str);
  158. }
  159. }
  160. /*******************************************************************************************/
  161. static void t_uart2_callback(void *parameter)
  162. {
  163. HMI_WRITE("sysSet.va0.txt=\"指令超时,请重试...\"");
  164. LOG_E("t_uart2_callback");
  165. }
  166. void Start_timer_Uart2(void)
  167. {
  168. if(timer_uart2!=RT_NULL)
  169. {
  170. rt_timer_start(timer_uart2);
  171. }
  172. else
  173. {
  174. LOG_E("timer_uart2 start Failed!");
  175. }
  176. }
  177. /***************************** 解析从屏幕收到的数据 begin **********************************/
  178. static void HMI_Decode(void)
  179. {
  180. time_t now = time(RT_NULL);
  181. static struct tm tm;
  182. gmtime_r(&now, &tm);
  183. char *json_temp;
  184. char *var=NULL;
  185. rt_free(var);
  186. if(device_status.refresh_num==0) device_status.refresh_num = 10;
  187. if(strstr(rx_buffer,"getFullData") != NULL){
  188. char *readinfo = makeJson_ReadInfo();
  189. if(device_status.refresh_num==5)
  190. {
  191. device_status.refresh_num = 15;
  192. DTU_Write("%s",readinfo);
  193. //list_mem();
  194. device_status.hmi_fresh = 1;
  195. rt_thread_delay(100);
  196. }
  197. //在解析getinfo中清除超时
  198. if(device_status.refresh_timeout==30)
  199. {
  200. rt_sprintf(device_status.sim,"1");
  201. rt_sprintf(device_status.net,"1");
  202. rt_sprintf(device_status.csq,"0");
  203. device_status.camsta = 1;
  204. device_status.link_sta=0;
  205. device_status.refresh_timeout=0;
  206. }
  207. rt_free(readinfo);
  208. device_status.refresh_num--;
  209. device_status.refresh_timeout++;
  210. if(device_status.version_type!=6)
  211. {
  212. HMI_WRITE("uploadSet.iccid.txt=\"%s\"",device_status.iccid);
  213. if(strstr(device_status.mqttcfg1,"8.136.98.49")!=NULL)
  214. {
  215. HMI_WRITE("uploadSet.plt.val=%d",RESET);
  216. }
  217. else if (strstr(device_status.mqttcfg1,"120.27.222.26")!=NULL) {
  218. HMI_WRITE("uploadSet.plt.val=%d",SET);
  219. }
  220. //HMI_WRITE("mqttcfg.mqtt1Message.txt=\"%s\"",device_status.mqttcfg1);
  221. HMI_WRITE("uploadSet.t0.txt=\"%s\"",device_status.mqttcfg1);
  222. //更新固件版本号
  223. HMI_WRITE("debug.t3.txt=\"%s\"",device_status.driver);
  224. //更新固件编译日期
  225. HMI_WRITE("debug.t4.txt=\"%s\"",device_status.driver_compile);
  226. //
  227. HMI_WRITE("debug.t5.txt=\"%s\"",device_status.driver_bl);
  228. HMI_WRITE("debug.t7.txt=\"%s\"",device_status.driver_compile);
  229. //更新dtu/rtu版本
  230. HMI_WRITE("debug.t9.txt=\"%s\"",device_status.driver_4g);
  231. //更新时间
  232. HMI_WRITE("menu.rtc.txt=\"%04d-%02d-%02d %02d:%02d:%02d\"",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  233. HMI_WRITE("workSta.rtc.txt=\"%04d-%02d-%02d %02d:%02d:%02d\"",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  234. HMI_WRITE("set.t0.txt=\"%d\"",tm.tm_hour);
  235. HMI_WRITE("set.t1.txt=\"%d\"",tm.tm_min);
  236. HMI_WRITE("set.t2.txt=\"%d\"",tm.tm_sec);
  237. //更新电压
  238. HMI_WRITE("workSta.vbat.txt=\"%s\"",device_status.voltage);
  239. //更新电流
  240. HMI_WRITE("workSta.power.txt=\"%s\"",device_status.power);
  241. //更新温湿度数据
  242. HMI_WRITE("workSta.envHum.val=%d",atoi(device_status.humidity));
  243. HMI_WRITE("workSta.envTem.val=%d",atoi(device_status.tempture));
  244. //更新电池状态
  245. HMI_WRITE("workSta.batStatus.val=%d",device_status.bat_status);
  246. //更新上仓门状态
  247. HMI_WRITE("workSta.upStatus.val=%d",device_status.up_status);
  248. //更新下仓门状态
  249. HMI_WRITE("workSta.downStatus.val=%d",device_status.down_status);
  250. //更新工作状态
  251. HMI_WRITE("workSta.workStatus.val=%d",device_status.work_status);
  252. //更新温控状态
  253. HMI_WRITE("workSta.tempStatus.val=%d",device_status.temp_status);
  254. //更新雨控状态
  255. HMI_WRITE("workSta.rainStatus.val=%d",device_status.rain_status);
  256. //更新光控状态
  257. HMI_WRITE("workSta.sunStatus.val=%d",device_status.sun_status);
  258. //更新有线连接状态
  259. HMI_WRITE("menu.va_net.val=%d",device_status.link_sta);
  260. //更新CSQ
  261. HMI_WRITE("menu.csq_value.txt=\"%s\"",device_status.csq);
  262. HMI_WRITE("menu.va_csq.val=%d",atoi(device_status.csq));
  263. HMI_WRITE("workSta.csq_value.txt=\"%s\"",device_status.csq);
  264. //更新收集时间
  265. HMI_WRITE("sysSet.stime.val=%d",device_status.clt_time);
  266. //更新加热时间
  267. HMI_WRITE("workSta.heatTime.val=%d",device_status.heattime);
  268. HMI_WRITE("sysSet.jtime.val=%d",device_status.heattime);
  269. //
  270. HMI_WRITE("sysSet.vol.val=%d",device_status.vol);
  271. //更新加热时间和温度
  272. HMI_WRITE("workSta.heatTem.val=%d",device_status.heattem);
  273. HMI_WRITE("sysSet.jheat.val=%d",device_status.heatvalue);
  274. //更新加热状态
  275. HMI_WRITE("workSta.heatStatus.val=%d",device_status.heat_status);
  276. //更新密码
  277. HMI_WRITE("inputPasswd.pwdData.txt=\"8888\"");
  278. //更新时间设置中的参数
  279. //更新定时模式
  280. HMI_WRITE("clockSet.timeSwitch.val=%d",device_status.timeSwitch);
  281. //更新光控定时时长
  282. HMI_WRITE("clockSet.sunBar.val=%d",device_status.sunBar);
  283. //更新时控开始
  284. HMI_WRITE("clockSet.startBar.val=%d",device_status.startBar);
  285. //更新时控结束
  286. HMI_WRITE("clockSet.stopBar.val=%d",device_status.stopBar);
  287. //SIM卡状态
  288. HMI_WRITE("workSta.bt0.val=%d",atoi(device_status.sim));
  289. //ID
  290. HMI_WRITE("menu.equipid.txt=\"%s\"",device_status.imei);
  291. HMI_WRITE("menu.deviceId.txt=\"%s\"",device_status.deviceId);
  292. //HMI_WRITE("workSta.equipid.txt=\"%s\"",device_status.imei);
  293. //数据上传间隔
  294. HMI_WRITE("uploadSet.datt.val=%d",device_status.dattim);
  295. //语音音量
  296. HMI_WRITE("sysSet.vol.val=%d",device_status.vol);
  297. //更新熄屏时间
  298. if(device_status.thsp<60) device_status.thsp = device_status.thsp * 60;
  299. HMI_WRITE("thsp=%d",device_status.thsp);
  300. }
  301. HMI_WRITE("menu.cam_sta.val=%d",device_status.camsta);
  302. #if BOOT_ERR == 1
  303. if(atoi(device_status.gps)==1){
  304. HMI_WRITE("menu.gps_state.txt=\"定位成功:(%s)%d/%d\"",device_status.gps_state,device_status.gps_inuse,device_status.gps_view);
  305. }else if(atoi(device_status.gps)==2){
  306. HMI_WRITE("menu.gps_state.txt=\"定位成功:(基站)\"");
  307. }
  308. #endif
  309. //更新联网状态
  310. if(atoi(device_status.net)==1)
  311. {
  312. HMI_WRITE("menu.net.val=1");
  313. HMI_WRITE("workSta.net.val=1");
  314. }
  315. else {
  316. HMI_WRITE("workSta.net.val=0");
  317. HMI_WRITE("menu.net.val=0");
  318. }
  319. if(atoi(device_status.csq)==0&&atoi(device_status.net)==1)
  320. {
  321. HMI_WRITE("menu.net.val=1");
  322. HMI_WRITE("workSta.net.val=2");
  323. }
  324. if(device_status.dtype==7)
  325. {
  326. //更新温湿度数据
  327. HMI_WRITE("workSta.env_hum.txt=\"%s\"",device_status.humidity);
  328. HMI_WRITE("workSta.env_tem.txt=\"%s\"",device_status.tempture);
  329. HMI_WRITE("workSta.on_off.val=%d",device_status.ds);
  330. HMI_WRITE("workSta.work_sta.val=%d",device_status.work_status);
  331. HMI_WRITE("workSta.collect_fan.val=%d",device_status.collect_fan_status);
  332. //更新电池状态
  333. HMI_WRITE("workSta.bat_sta.val=%d",device_status.bat_status);
  334. //更新散热风机状态
  335. HMI_WRITE("workSta.cool_fan.val=%d",device_status.cool_fan_status);
  336. //更新开关状态
  337. HMI_WRITE("workSta.on_off.val=%d",device_status.ds);
  338. //更新工作状态
  339. HMI_WRITE("workSta.work_sta.val=%d",device_status.work_status);
  340. //箱内温度-外壳
  341. HMI_WRITE("workSta.box_tmp.txt=\"%d\"",device_status.box_temp);
  342. HMI_WRITE("temHum.box_tmp.txt=\"%d\"",device_status.box_temp);
  343. //制冷开关
  344. HMI_WRITE("workSta.cold_sw.val=%d",device_status.cold_sw);
  345. HMI_WRITE("sysSet.cold_sw.val=%d",device_status.cold_sw);
  346. //制冷状态
  347. HMI_WRITE("workSta.cold_sta.val=%d",device_status.cold_status);
  348. HMI_WRITE("sysSet.cold_sw.val=%d",device_status.cold_sw);
  349. //培养温度
  350. HMI_WRITE("workSta.set_temp.val=%d",device_status.heatvalue);
  351. HMI_WRITE("sysSet.keep.val=%d",device_status.heatvalue);
  352. //数据上传间隔
  353. HMI_WRITE("sysSet.datt.val=%d",device_status.dattim);
  354. HMI_WRITE("sysSet.lcd_time.val=%d",device_status.thsp*60);
  355. //当前温度-培养仓
  356. HMI_WRITE("workSta.pre_temp.val=%d",device_status.pre_temp);
  357. //设定培养时间
  358. HMI_WRITE("workSta.cul_time.val=%d",device_status.cul_time);
  359. HMI_WRITE("clockSet.stay.val=%d",device_status.cul_time);
  360. //已培养时间
  361. if(device_status.develop_flag==0)
  362. HMI_WRITE("workSta.staytime.txt=\"0\"");
  363. //滴液时间
  364. HMI_WRITE("clockSet.drop.val=%d",device_status.shake_sec);
  365. //采集开始时间1
  366. HMI_WRITE("clockSet.start1.val=%d",device_status.collecting_sh1);
  367. //采集开始时间2
  368. HMI_WRITE("clockSet.start2.val=%d",device_status.collecting_sh2);
  369. //采集结束时间1
  370. HMI_WRITE("clockSet.end1.val=%d",device_status.collecting_eh1);
  371. //采集结束时间2
  372. HMI_WRITE("clockSet.end2.val=%d",device_status.collecting_eh2);
  373. if(device_status.autotakepic==0) HMI_WRITE("sysTest.auto_flag.txt=\"\"");
  374. }
  375. /**********************************吸虫塔*********************************/
  376. if(device_status.version_type==6)
  377. {
  378. HMI_WRITE("menu.equipid.txt=\"%s\"",device_status.imei);
  379. //更新电压
  380. HMI_WRITE("menu.vbat.txt=\"%sV\"",device_status.voltage);
  381. //工作状态
  382. HMI_WRITE("menu.worksta.val=%d",device_status.work_status);
  383. //网卡0状态
  384. HMI_WRITE("menu.eth0_sta.val=%d",device_status.link_sta);
  385. //更新电流
  386. HMI_WRITE("menu.power.txt=\"%smA\"",device_status.power);
  387. //更新温湿度数据
  388. HMI_WRITE("menu.envHum.txt=\"%s%RH\"",device_status.humidity);
  389. HMI_WRITE("menu.envTem.txt=\"%s℃\"",device_status.tempture);
  390. HMI_WRITE("uploadSet.st.val=%d",device_status.startBar);
  391. HMI_WRITE("uploadSet.et.val=%d",device_status.stopBar);
  392. HMI_WRITE("menu.board_temp.txt=\"%d.%d℃\"",Get_Temprate()/100,Get_Temprate()%100);
  393. //数据上传间隔
  394. HMI_WRITE("menu.datt.val=%d",device_status.dattim);
  395. //SIM卡状态
  396. HMI_WRITE("menu.bt0.val=%d",atoi(device_status.sim));
  397. //更新震动时间
  398. HMI_WRITE("menu.shake_sec.val=%d",device_status.shake_sec);
  399. //校准屏幕的RTC
  400. HMI_WRITE("rtc0=%d",tm.tm_year+1900);
  401. HMI_WRITE("rtc1=%d",tm.tm_mon+1);
  402. HMI_WRITE("rtc2=%d",tm.tm_mday);
  403. HMI_WRITE("rtc3=%d",tm.tm_hour);
  404. HMI_WRITE("rtc4=%d",tm.tm_min);
  405. HMI_WRITE("rtc5=%d",tm.tm_sec);
  406. //更新熄屏时间
  407. HMI_WRITE("thsp=%d",device_status.thsp);
  408. }
  409. /**********************************************************************/
  410. }
  411. else if (strstr(rx_buffer,"getClockData")!=NULL) {
  412. //更新定时模式
  413. HMI_WRITE("clockSet.timeSwitch.val=%d",device_status.timeSwitch);
  414. //更新光控定时时长
  415. HMI_WRITE("clockSet.sunBar.val=%d",device_status.sunBar);
  416. //更新时控开始
  417. HMI_WRITE("clockSet.startBar.val=%d",device_status.startBar);
  418. //更新时控结束
  419. HMI_WRITE("clockSet.stopBar.val=%d",device_status.stopBar);
  420. }
  421. else if (strstr(rx_buffer,"button")!=NULL) {
  422. LOG_D("button!");
  423. DTU_Write("button");
  424. }
  425. //拍照
  426. else if(strstr(rx_buffer,"putTest.switchPhoto=1")!=NULL) {
  427. device_status.photo_ws = 1;
  428. }
  429. else if (strstr(rx_buffer,"putTest.switchPic=1")!=NULL) {
  430. device_status.photo_ws = 1;
  431. }
  432. //打开电磁阀
  433. else if (strstr(rx_buffer,"putTest.switchDrop=1")!=NULL) {
  434. LOG_I("开始滴液!");
  435. #if Color_EN == 1
  436. device_status.run_sta = 5;
  437. #else
  438. ctr_shake(ON, device_status.shake_sec);
  439. #endif
  440. }
  441. //关闭电磁阀
  442. else if (strstr(rx_buffer,"putTest.switchDrop=0")!=NULL) {
  443. LOG_I("停止滴液!");
  444. ctr_shake(OFF, 0);
  445. }
  446. //打开补光
  447. else if (strstr(rx_buffer,"putTest.switchLed=1")!=NULL) {
  448. LOG_I("打开补光!!");
  449. ctr_led(ON);
  450. }
  451. //关闭补光
  452. else if (strstr(rx_buffer,"putTest.switchLed=0")!=NULL) {
  453. LOG_I("关闭补光!!!");
  454. ctr_led(OFF);
  455. }
  456. //关闭加热
  457. else if(strstr(rx_buffer,"putTest.switchHeat=0")!=NULL) {
  458. device_status.heat_status = 0;
  459. ctr_Heat(OFF);
  460. LOG_I("Close Heat!");
  461. }
  462. //打开加热
  463. else if(strstr(rx_buffer,"putTest.switchHeat=1")!=NULL) {
  464. device_status.heat_status = 1;
  465. ctr_Heat(ON);
  466. LOG_I("Open Heat!");
  467. }
  468. //打开制冷
  469. else if(strstr(rx_buffer,"putTest.switchCold=1")!=NULL) {
  470. ctr_Cold(ON);
  471. LOG_I("Open Cold!");
  472. }
  473. //关闭制冷
  474. else if(strstr(rx_buffer,"putTest.switchCold=0")!=NULL) {
  475. ctr_Cold(OFF);
  476. LOG_I("Close Cold!");
  477. }
  478. //打开采集
  479. else if(strstr(rx_buffer,"putTest.switchCollect=1")!=NULL){
  480. ctr_Collect_Fan(ON);
  481. LOG_I("采集风机已打开!");
  482. }
  483. //关闭采集
  484. else if(strstr(rx_buffer,"putTest.switchCollect=0")!=NULL){
  485. ctr_Collect_Fan(OFF);
  486. LOG_I("采集风机已关闭!");
  487. }
  488. //打开散热
  489. else if(strstr(rx_buffer,"putTest.switchCool=1")!=NULL){
  490. ctr_Cool_Fan(ON);
  491. LOG_I("散热风机已打开!");
  492. }
  493. //关闭散热
  494. else if(strstr(rx_buffer,"putTest.switchCool=0")!=NULL){
  495. ctr_Cool_Fan(OFF);
  496. LOG_I("散热风机已关闭!");
  497. }
  498. //开始对焦拍照
  499. else if(strstr(rx_buffer,"putTest.switchAutopic=1")!=NULL){
  500. device_status.autotakepic=1;
  501. rt_kprintf("开始对焦拍照!\n");
  502. }
  503. //停止对焦拍照
  504. else if(strstr(rx_buffer,"putTest.switchAutopic=0")!=NULL){
  505. device_status.autotakepic=0;
  506. }
  507. //粘附测试-开
  508. else if(strstr(rx_buffer,"putTest.switchFsl=1")!=NULL){
  509. rt_kprintf("粘附测试开!\n");
  510. device_status.fsl_sw = 1;
  511. }
  512. //粘附测试-关
  513. else if(strstr(rx_buffer,"putTest.switchFsl=0")!=NULL){
  514. rt_kprintf("粘附测试关!\n");
  515. pwm_off(2, 0);
  516. pwm_off(3,0);
  517. }
  518. else if (strstr(rx_buffer,"putTest.switchAutotest=1")!=NULL)
  519. {
  520. LOG_I("开始一键测试!!!");
  521. device_status.auto_test_sw=1;
  522. }
  523. else if (strstr(rx_buffer,"putTest.switchAutotest=1")!=NULL)
  524. {
  525. LOG_I("停止一键测试!!!");
  526. device_status.auto_test_sw=0;
  527. }
  528. //震动
  529. else if (strstr(rx_buffer,"putTest.switchShake=1")!=NULL) {
  530. LOG_I("震动!");
  531. if(device_status.version_type==6)
  532. {
  533. Motor_shake(ON, device_status.shake_sec);
  534. }
  535. else{
  536. ctr_shake(ON,device_status.shake_sec);
  537. }
  538. }
  539. //打开补光
  540. else if (strstr(rx_buffer,"putTest.switchFill=1")!=NULL) {
  541. rt_kprintf("\n打开补光!!!\n");
  542. ctr_led(ON);
  543. }
  544. //关闭补光
  545. else if (strstr(rx_buffer,"putTest.switchFill=0")!=NULL) {
  546. rt_kprintf("\n关闭补光!!!\n");
  547. ctr_led(OFF);
  548. }
  549. //打开灯管
  550. else if (strstr(rx_buffer,"putTest.switchLamp=1")!=NULL)
  551. {
  552. device_status.test_flag=1;
  553. rt_kprintf("Open Lamp!\n");
  554. ctr_lamp(ON,1);
  555. #if LAMP_EN == 1
  556. ctr_lamp(ON,2);
  557. ctr_lamp(ON,3);
  558. #endif
  559. }
  560. //关闭灯管
  561. else if (strstr(rx_buffer,"putTest.switchLamp=0")!=NULL)
  562. {
  563. device_status.test_flag=0;
  564. rt_kprintf("Close Lamp1!\n");
  565. ctr_lamp(OFF,1);
  566. #if LAMP_EN == 1
  567. ctr_lamp(OFF,2);
  568. ctr_lamp(OFF,3);
  569. #endif
  570. }
  571. //关闭加热
  572. else if(strstr(rx_buffer,"putTest.switchHeat=0")!=NULL) {
  573. device_status.heat_status = 0;
  574. ctr_Heat(OFF);
  575. rt_kprintf("Close Heat!\n");
  576. }
  577. //打开加热
  578. else if(strstr(rx_buffer,"putTest.switchHeat=1")!=NULL) {
  579. device_status.heat_status = 1;
  580. ctr_Heat(ON);
  581. rt_kprintf("Open Heat!\n");
  582. }
  583. else if(strstr(rx_buffer,"putTest.Testpaichong")!=NULL){
  584. if(device_status.collt_box_sta==0){
  585. device_status.collt_box_sta = 111;
  586. }else{
  587. HMI_WRITE("sysTest.result.txt=\"请等待当前执行完成再进行操作\"");
  588. }
  589. LOG_I("排虫测试!");
  590. }
  591. //排虫
  592. else if(strstr(rx_buffer,"putTest.paichong")!=NULL){
  593. if(device_status.run_sta == 0){
  594. device_status.run_sta = 111;
  595. }else{
  596. HMI_WRITE("sysTest.result.txt=\"请等待当前执行完成再进行操作\"");
  597. }
  598. LOG_I("开始排虫!");
  599. device_status.handle_bugger = 0;
  600. ef_set_env_blob("handle_bugger", &device_status.handle_bugger, sizeof(device_status.handle_bugger));
  601. }
  602. else if(strstr(rx_buffer,"putTest.shouchong")!=NULL){
  603. if(device_status.run_sta == 0){
  604. device_status.run_sta = 112;
  605. }else{
  606. HMI_WRITE("sysTest.result.txt=\"请等待当前执行完成再进行操作\"");
  607. }
  608. LOG_I("开始收虫!");
  609. device_status.handle_bugger = 1;
  610. ef_set_env_blob("handle_bugger", &device_status.handle_bugger, sizeof(device_status.handle_bugger));
  611. }
  612. //设置设备时间
  613. else if (strstr(rx_buffer,"stamp=")!=NULL)
  614. {
  615. var = strstr(rx_buffer,"=");
  616. var ++;
  617. LOG_I("设置时间:%s",var);
  618. static char year_s[5],month_s[3],day_s[3],hour_s[3],min_s[3],sec_s[3];
  619. strncpy(year_s, var, 4);
  620. strncpy(month_s,var+4,2);
  621. strncpy(day_s,var+6,2);
  622. strncpy(hour_s,var+8,2);
  623. strncpy(min_s,var+10,2);
  624. strncpy(sec_s,var+12,2);
  625. if(atoi(year_s)>=2024&&atoi(year_s)<3000&&atoi(month_s)>0&&atoi(month_s)<13&&atoi(day_s)>0&&atoi(day_s)<32)
  626. {
  627. if(set_date(atoi(year_s),atoi(month_s),atoi(day_s))==RT_EOK)
  628. {
  629. LOG_I("设置日期:%d年%d月%d日",atoi(year_s),atoi(month_s),atoi(day_s));
  630. if(atoi(hour_s)>0&&atoi(hour_s)<=24&&atoi(min_s)>=0&&atoi(min_s)<=59&&atoi(sec_s)>=0&&atoi(sec_s)<=59)
  631. {
  632. LOG_I("设置时间:%d时%d分%d秒",atoi(hour_s),atoi(min_s),atoi(sec_s));
  633. set_time(atoi(hour_s),atoi(min_s),atoi(sec_s));
  634. MA_save();
  635. }
  636. //上传最新数据
  637. device_status.num_updata = 2; //2秒后上报数据更新RTU的时间
  638. }
  639. else {
  640. LOG_E("set_date failed!");
  641. }
  642. }
  643. }
  644. //设备编码设置
  645. else if (strstr(rx_buffer,"deviceId=")!=NULL)
  646. {
  647. char value_str[20];
  648. memset(value_str,0,sizeof(value_str));
  649. LOG_I("设备编码 长度:%d",strlen(rx_buffer));
  650. if(strlen(rx_buffer)!=19){
  651. LOG_E("编码长度错误!");
  652. return;
  653. }
  654. var = strstr(rx_buffer,"=");
  655. var ++;
  656. strncpy(value_str,var,8); //设备编码为16位
  657. if(strstr(value_str,"00000000")!=RT_NULL){
  658. char *config_reset = makeJson_Config_Reset();
  659. //发送json到RTU
  660. DTU_Write("%s",config_reset);
  661. rt_free(config_reset);
  662. //恢复RTU的默认配置参数
  663. }else{
  664. rt_sprintf(device_status.deviceId,"10300101%s",value_str); //设备大类编码103-虫情 设备小类编码 001 生产商代码01
  665. ef_set_env_blob("deviceId", &device_status.deviceId, sizeof(device_status.deviceId));
  666. LOG_I("解析到设备编码:%s",device_status.deviceId);
  667. char *device_id = makeJson_Config_BDH(device_status.deviceId);
  668. //发送json到RTU
  669. DTU_Write("%s",device_id);
  670. rt_free(device_id);
  671. }
  672. }
  673. //定时设置
  674. else if (strstr(rx_buffer,"saveClockSet=")!=NULL) {
  675. char value_str[10];
  676. memset(value_str,0,sizeof(value_str));
  677. if(device_status.dtype==7)
  678. {
  679. var = strstr(rx_buffer,"=");
  680. var ++;
  681. strncpy(value_str,var,2);
  682. if(atoi(value_str)>=0&&atoi(value_str)<=23)
  683. {
  684. device_status.collecting_sh1 = atoi(value_str);
  685. ef_set_env_blob("collecting_sh1", &device_status.collecting_sh1, sizeof(device_status.collecting_sh1));
  686. }
  687. else {
  688. LOG_E("Set collecting_sh1 Failed!");
  689. }
  690. memset(value_str,0,sizeof(value_str));
  691. strncpy(value_str,var+2,2);
  692. if(atoi(value_str)>=0&&atoi(value_str)<=23)
  693. {
  694. device_status.collecting_eh1 = atoi(value_str);
  695. ef_set_env_blob("collecting_eh1", &device_status.collecting_eh1, sizeof(device_status.collecting_eh1));
  696. }
  697. else {
  698. LOG_E("Set collecting_eh1 Failed!");
  699. }
  700. memset(value_str,0,sizeof(value_str));
  701. strncpy(value_str,var+4,2);
  702. if(atoi(value_str)>=0&&atoi(value_str)<=23)
  703. {
  704. device_status.collecting_sh2 = atoi(value_str);
  705. ef_set_env_blob("collecting_sh2", &device_status.collecting_sh2, sizeof(device_status.collecting_sh2));
  706. }
  707. else {
  708. LOG_E("Set collecting_sh2 Failed!");
  709. }
  710. memset(value_str,0,sizeof(value_str));
  711. strncpy(value_str,var+6,2);
  712. if(atoi(value_str)>=0&&atoi(value_str)<=23)
  713. {
  714. device_status.collecting_eh2 = atoi(value_str);
  715. ef_set_env_blob("collecting_eh2", &device_status.collecting_eh2, sizeof(device_status.collecting_eh2));
  716. }
  717. else {
  718. LOG_E("Set collecting_eh2 Failed!");
  719. }
  720. memset(value_str,0,sizeof(value_str));
  721. strncpy(value_str,var+8,2);
  722. if(atoi(value_str)>=0&&atoi(value_str)<=23)
  723. {
  724. device_status.shake_sec = atoi(value_str);
  725. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec));
  726. }
  727. else {
  728. LOG_E("Set shake_sec Failed!");
  729. }
  730. memset(value_str,0,sizeof(value_str));
  731. strncpy(value_str,var+10,2);
  732. if(atoi(value_str)>0&&atoi(value_str)<=24)
  733. {
  734. device_status.cul_time = atoi(value_str);
  735. ef_set_env_blob("cul_time", &device_status.cul_time, sizeof(device_status.cul_time));
  736. }
  737. else {
  738. LOG_E("Set cul_time Failed!");
  739. }
  740. MA_save();
  741. LOG_I("参数已保存:%d-%d-%d-%d-%d-%d",device_status.collecting_sh1,device_status.collecting_eh1,device_status.collecting_sh2,device_status.collecting_eh2,device_status.shake_sec,device_status.cul_time);
  742. return;
  743. }
  744. LOG_I("时间设置!");
  745. var=strstr(rx_buffer,"=");
  746. var++;
  747. strncpy(value_str,var,2);
  748. if(atoi(value_str) > 0 && atoi(value_str) <60)
  749. {
  750. if(device_status.version_type!=6)
  751. {
  752. #if LAMP_EN == 1
  753. if(device_status.sunBar%3==0)
  754. #endif
  755. device_status.sunBar=atoi(value_str);
  756. ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar));
  757. }
  758. else {
  759. device_status.shake_sec = atoi(value_str);
  760. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec));
  761. }
  762. // time_t now;
  763. // time(&now);
  764. // static struct tm nowstamp;
  765. // nowstamp =*localtime(&now);
  766. // nowstamp.tm_hour = nowstamp.tm_hour - 8;
  767. // static rt_uint32_t nowstamp_num = 0;
  768. // nowstamp_num = mktime(&nowstamp);
  769. // device_status.light_over_tim = device_status.sunBar * 3600 + nowstamp_num;
  770. }
  771. else {
  772. LOG_E("参数设置异常(光控时长):%d!!!",atoi(value_str));
  773. }
  774. memset(value_str,0,sizeof(value_str));
  775. //时控开始时间
  776. strncpy(value_str,var+2,2);
  777. if(atoi(value_str) >= 0 && atoi(value_str) < 24)
  778. {
  779. #if 1
  780. device_status.startBar=atoi(value_str);
  781. ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar));
  782. #endif
  783. }
  784. else {
  785. LOG_E("参数设置异常(开始):%d!!!",atoi(value_str));
  786. }
  787. memset(value_str,0,sizeof(value_str));
  788. //
  789. strncpy(value_str,var+4,2);
  790. if(atoi(value_str) >= 0&& atoi(value_str) < 24)
  791. {
  792. #if 1
  793. device_status.stopBar=atoi(value_str);
  794. ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar));
  795. #endif
  796. }
  797. else {
  798. LOG_E("参数设置异常(结束):%d!!!",atoi(value_str));
  799. }
  800. memset(value_str,0,sizeof(value_str));
  801. //
  802. strncpy(value_str,var+6,2);
  803. if(atoi(value_str) >= 0 && atoi(value_str) <= 1)
  804. {
  805. device_status.timeSwitch=atoi(value_str);
  806. ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
  807. }
  808. else {
  809. if(device_status.version_type==6)
  810. {
  811. device_status.timeSwitch=1;
  812. device_status.dattim=atoi(value_str);
  813. ef_set_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim));
  814. ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
  815. }
  816. else {
  817. LOG_E("参数设置异常(模式):%d!!!",atoi(value_str));
  818. }
  819. }
  820. //2022/06/23,add:
  821. rt_sprintf(device_status.json_w_clock,"屏幕操作:修改工作时间:开始-%d,结束-%d,模式-%d(0-光控,1-时控)操作时间:%s",device_status.startBar,device_status.stopBar,device_status.timeSwitch,device_status.stamp);
  822. json_temp = makeJson_Warn(device_status.json_w_clock, "修改工作时间");
  823. ef_set_env_blob("json_w_clock", &device_status.json_w_clock, sizeof(device_status.json_w_clock)); //将修改屏幕参数操作记录保存到Flash中
  824. if(atoi(device_status.net)==0)
  825. DTU_Write("%s",json_temp);
  826. rt_free(json_temp);
  827. rt_thread_delay(2000);
  828. MA_save();
  829. rt_free(var);
  830. rt_kprintf("工作时间参数已保存:模式-%d,光控时间-%d,时控开始时间-%d,时控结束时间-%d\n",device_status.timeSwitch,device_status.sunBar,device_status.startBar,device_status.stopBar);
  831. }
  832. else if (strstr(rx_buffer,"saveSystemSet")!=NULL) {
  833. static char clt_time_str[3],heattime_str[3],heatvale_str[4],vol_str[4],thsp_str[5];
  834. static char value_temp[5];
  835. LOG_I("系统设置!");
  836. if(device_status.dtype==7)
  837. {
  838. LOG_I("系统设置,孢子仪!");
  839. var = strstr(rx_buffer,"=");
  840. var ++;
  841. memset(value_temp,0,sizeof(value_temp));
  842. strncpy(value_temp,var,2);
  843. if(atoi(value_temp)==0 || atoi(value_temp) == 1)
  844. {
  845. device_status.cold_sw = atoi(value_temp); //制冷开关
  846. ef_set_env_blob("cold_sw", &device_status.cold_sw, sizeof(device_status.cold_sw));
  847. }
  848. else {
  849. LOG_E("制冷开关参数设置错误!");
  850. }
  851. memset(value_temp,0,sizeof(value_temp));
  852. strncpy(value_temp,var+2,2);
  853. if(atoi(value_temp)>=0 && atoi(value_temp) <= 7)
  854. {
  855. device_status.vol = atoi(value_temp); //音量
  856. }
  857. else {
  858. LOG_E("音量参数设置错误!");
  859. }
  860. memset(value_temp,0,sizeof(value_temp));
  861. strncpy(value_temp,var+4,2);
  862. if(atoi(value_temp)>=10 && atoi(value_temp) <= 50)
  863. {
  864. device_status.heatvalue = atoi(value_temp); //设定的培养温度
  865. ef_set_env_blob("heatvalue", &device_status.heatvalue, sizeof(device_status.heatvalue));
  866. }
  867. else {
  868. LOG_E("培养温度参数设置失败!");
  869. }
  870. memset(value_temp,0,sizeof(value_temp));
  871. strncpy(value_temp,var+6,2);
  872. if(atoi(value_temp)>=1 && atoi(value_temp) <= 60)
  873. {
  874. device_status.dattim = atoi(value_temp); //设定的数据上传间隔,单位:分钟
  875. ef_set_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim));
  876. }
  877. else {
  878. LOG_E("数据上传间隔参数设置失败!");
  879. }
  880. memset(value_temp,0,sizeof(value_temp));
  881. strncpy(value_temp,var+8,2);
  882. if(atoi(value_temp)>=1 && atoi(value_temp) <= 30)
  883. {
  884. device_status.thsp = atoi(value_temp); //设定的息屏间隔,单位:分钟
  885. ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
  886. }
  887. else {
  888. LOG_E("数据息屏间隔参数设置失败!");
  889. }
  890. LOG_I("制冷开关:%d",device_status.cold_sw);
  891. LOG_I("系统设置已保存:\n自动熄屏-%d\n音量-%d\n培养温度-%d\n数据上传间隔-%d",device_status.thsp,device_status.vol,device_status.heatvalue,device_status.dattim);
  892. MA_save();
  893. return;
  894. }
  895. var = strstr(rx_buffer,"=");
  896. var ++;
  897. strncpy(clt_time_str,var,2);
  898. if(atoi(clt_time_str)>0 && atoi(clt_time_str) <= 30)
  899. {
  900. device_status.clt_time = atoi(clt_time_str); //收集时间
  901. ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time));
  902. }
  903. strncpy(heattime_str,var+2,2);
  904. if(atoi(heattime_str)>0 && atoi(heattime_str) <= 30)
  905. {
  906. rt_kprintf("加热时间:%d\n",atoi(heattime_str));
  907. device_status.heattime = atoi(heattime_str); //加热时间
  908. ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime));
  909. }
  910. strncpy(heatvale_str,var+4,3);
  911. if(atoi(heatvale_str) > 0 && atoi(heatvale_str) <= 130)
  912. {
  913. device_status.heatvalue=atoi(heatvale_str);
  914. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
  915. }
  916. strncpy(vol_str,var+7,3);
  917. if(atoi(vol_str)>0&&atoi(vol_str)<8)
  918. {
  919. device_status.vol = atoi(vol_str); //音量
  920. DTU_Write("volume=");
  921. }
  922. strncpy(thsp_str,var+10,4);
  923. if(atoi(thsp_str)>0&&atoi(thsp_str)<3600)
  924. {
  925. device_status.thsp = atoi(thsp_str); //熄屏时间
  926. }
  927. else {
  928. rt_kprintf("参数设置失败!!!\n");
  929. }
  930. rt_thread_delay(1000);
  931. MA_save();
  932. rt_free(var);
  933. rt_kprintf("系统设置已保存:收集时间-%d,加热时间-%d,加热温度-%d,音量-%d,自动关屏-%d\n",device_status.clt_time,device_status.heattime,device_status.heatvalue,device_status.vol,device_status.thsp);
  934. }
  935. else if (strstr(rx_buffer,"rtc_h=")!=NULL) {
  936. char rtc_h[2];
  937. var = strstr(rx_buffer,"=");
  938. var++;
  939. strncpy(rtc_h,var,2);
  940. if(atoi(rtc_h)>=0 && atoi(rtc_h) <= 24)
  941. {
  942. set_time(atoi(rtc_h), 99, 99);
  943. }
  944. }
  945. else if (strstr(rx_buffer,"rtc_m=")!=NULL) {
  946. char rtc_m[2];
  947. var = strstr(rx_buffer,"=");
  948. var++;
  949. strncpy(rtc_m,var,2);
  950. if(atoi(rtc_m)>=0 && atoi(rtc_m) <= 24)
  951. {
  952. set_time(99, atoi(rtc_m), 99);
  953. }
  954. }
  955. else if (strstr(rx_buffer,"rtc_s=")!=NULL) {
  956. char rtc_s[2];
  957. var = strstr(rx_buffer,"=");
  958. var++;
  959. strncpy(rtc_s,var,2);
  960. if(atoi(rtc_s)>=0 && atoi(rtc_s) <= 24)
  961. {
  962. set_time(99, 99, atoi(rtc_s));
  963. }
  964. }
  965. else if(strstr(rx_buffer,"saveUploadSet=")!=NULL)
  966. {
  967. static char dattim_str[3];
  968. rt_kprintf("上传间隔设置!\n");
  969. var = strstr(rx_buffer,"=");
  970. var ++;
  971. strncpy(dattim_str,var,2);
  972. if(atoi(dattim_str)>=1&&atoi(dattim_str)<=120)
  973. {
  974. device_status.dattim =atoi(dattim_str); //数据上传间隔
  975. ef_set_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim));
  976. MA_save();
  977. }
  978. else {
  979. rt_kprintf("设置失败!!!\n");
  980. }
  981. rt_free(var);
  982. rt_kprintf("数据上传设置:%d\n",device_status.dattim);
  983. }
  984. //光控测试
  985. else if(strstr(rx_buffer,"putTest.switchSun=1")!=NULL)
  986. {
  987. rt_kprintf("光控测试!!!");
  988. ctr_lamp(ON,1);
  989. device_status.sun_test = 1;
  990. }
  991. else if (strstr(rx_buffer,"setshake_sec=")!=NULL)
  992. {
  993. static char shakesec_str[4];
  994. LOG_I("震动时间设置!");
  995. var = strstr(rx_buffer,"=");
  996. var ++;
  997. strncpy(shakesec_str,var,2);
  998. if(atoi(shakesec_str)>=0&&atoi(shakesec_str)<=10)
  999. {
  1000. device_status.shake_sec =atoi(shakesec_str)*10; //
  1001. }
  1002. else {
  1003. LOG_E("设置失败!!!");
  1004. }
  1005. rt_free(var);
  1006. LOG_D("震动时间设置:%d",device_status.shake_sec);
  1007. }
  1008. //恢复出厂设置电池
  1009. else if (strstr(rx_buffer,"resetdcDefault")!=NULL)
  1010. {
  1011. ResetParamDefault_DC();
  1012. rt_thread_delay(1000);
  1013. //2022/06/23,add:
  1014. rt_sprintf(device_status.json_w_reset,"屏幕操作:系统重置:电池供电,操作时间:%04d-%02d-%02d %02d:%02d:%02d",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  1015. json_temp = makeJson_Warn(device_status.json_w_reset, "系统重置");
  1016. ef_set_env_blob("json_w_reset", &device_status.json_w_reset, sizeof(device_status.json_w_reset)); //将修改屏幕参数操作记录保存到Flash中
  1017. if(atoi(device_status.net)==0)
  1018. DTU_Write("%s",json_temp);
  1019. rt_free(json_temp);
  1020. }
  1021. //恢复出厂设置-交流
  1022. else if (strstr(rx_buffer,"resetDefault")!=NULL)
  1023. {
  1024. ResetParamDefault();
  1025. rt_thread_delay(1000);
  1026. //2022/06/23,add:
  1027. rt_sprintf(device_status.json_w_reset,"屏幕操作:系统重置:交流供电,操作时间:%04d-%02d-%02d %02d:%02d:%02d",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  1028. json_temp = makeJson_Warn(device_status.json_w_reset, "系统重置");
  1029. ef_set_env_blob("json_w_reset", &device_status.json_w_reset, sizeof(device_status.json_w_reset)); //将修改屏幕参数操作记录保存到Flash中
  1030. if(atoi(device_status.net)==0)
  1031. DTU_Write("%s",json_temp);
  1032. rt_free(json_temp);
  1033. }
  1034. //获取SIM卡信息
  1035. else if(strstr(rx_buffer,"getSim")!=NULL)
  1036. {
  1037. //http://sim.openluat.com/api/wechat/openapidoc
  1038. DTU_Write("getsiminfo");
  1039. }
  1040. else if (strstr(rx_buffer,"DebugEnable")!=NULL) {
  1041. Debug_log_ON();
  1042. }
  1043. else if (strstr(rx_buffer,"DebugDisable")!=NULL) {
  1044. Debug_log_OFF();
  1045. }
  1046. else if((strstr(rx_buffer,"SwversionV7")!=NULL)) {
  1047. if(device_status.dtype!=7)
  1048. {
  1049. LOG_D("设置版本类型:7");
  1050. device_status.dtype = 7;
  1051. device_status.version_type = 7;
  1052. ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //设备类型
  1053. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1054. DTU_Write("SwversionV7");
  1055. MA_save();
  1056. }
  1057. }
  1058. else if(strstr(rx_buffer,"DisplayPower")!=RT_NULL)
  1059. {
  1060. HMI_WRITE("sysTest.sysTest_power.txt=\"%s\"",device_status.power);
  1061. HMI_WRITE("sysTest.heat_txt.txt=\"%s\"",device_status.heat_power);
  1062. // HMI_WRITE("sysTest.switchHeat.val=%d",device_status.heat_status);
  1063. }
  1064. //立即上传
  1065. else if(strstr(rx_buffer,"uploaddata")!=RT_NULL)
  1066. {
  1067. UpLoad_Data();
  1068. }
  1069. else if (strstr(rx_buffer,"checkupdate")!=NULL)
  1070. {
  1071. time_t now = time(RT_NULL);
  1072. static struct tm tm;
  1073. gmtime_r(&now, &tm);
  1074. HMI_WRITE("hardcheck.tip_rtc.txt=\"%04d-%02d-%02d %02d:%02d:%02d\"",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  1075. HMI_WRITE("hardcheck.p0.pic=%d",70);
  1076. //RTC时间判断,如果当前年份小于2020,提示需要设置时间
  1077. if(tm.tm_year+1900<2020)
  1078. {
  1079. rt_kprintf("时间未设置:%04d-%02d-%02d %02d:%02d:%02d\n",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  1080. HMI_WRITE("hardcheck.tip_rtc.txt=%04d-%02d-%02d %02d:%02d:%02d",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  1081. }
  1082. }
  1083. //进入bootloader模式
  1084. else if (strstr(rx_buffer,"bootloader")!=NULL) {
  1085. device_status.stop_in_bootloader = 1;
  1086. ef_set_env_blob("stop_in_bootloader", &device_status.stop_in_bootloader, sizeof(device_status.stop_in_bootloader));
  1087. rt_hw_cpu_reset();
  1088. }
  1089. //软件重启
  1090. else if (strstr(rx_buffer,"reboot")!=NULL) {
  1091. rt_hw_cpu_reset();
  1092. }
  1093. //屏幕上触发远程升级
  1094. else if (strstr(rx_buffer,"hmi_update")!=NULL) {
  1095. DTU_Write("hmi_update");
  1096. device_status.update_flag = 1;
  1097. ef_set_env_blob("update_flag", &device_status.update_flag, sizeof(device_status.update_flag));
  1098. rt_thread_delay(2000);
  1099. rt_hw_cpu_reset();
  1100. }else if (strstr(rx_buffer,"checkcamera")!=NULL){
  1101. DTU_Write("{\"ctr_cam\":\"checkcamera\"}");
  1102. //校准屏幕的RTC
  1103. HMI_WRITE("rtc0=%d",tm.tm_year+1900);
  1104. HMI_WRITE("rtc1=%d",tm.tm_mon+1);
  1105. HMI_WRITE("rtc2=%d",tm.tm_mday);
  1106. HMI_WRITE("rtc3=%d",tm.tm_hour);
  1107. HMI_WRITE("rtc4=%d",tm.tm_min);
  1108. HMI_WRITE("rtc5=%d",tm.tm_sec);
  1109. //开启超时定时器
  1110. Start_timer_Uart2();
  1111. }else if (strstr(rx_buffer,"ReadCamParam")!=NULL){
  1112. DTU_Write("{\"ctr_cam\":\"ReadParam\"}");
  1113. //开启超时定时器
  1114. Start_timer_Uart2();
  1115. }//SaveCamParam:2070.00000-0.7-144-30-0-0-5472*5472
  1116. else if (strstr(rx_buffer,"SaveCamParam")!=NULL){
  1117. const char *prefix ="SaveCamParam:";
  1118. size_t prefix_len = strlen(prefix);
  1119. const char *start_pos = strstr(rx_buffer, prefix);
  1120. if(start_pos!=NULL)
  1121. {
  1122. // 移动到前缀之后的位置
  1123. start_pos += prefix_len;
  1124. DTU_Write("%s",makeJson_Cam("SaveParam",start_pos));
  1125. }
  1126. //开启超时定时器
  1127. Start_timer_Uart2();
  1128. }else if (strstr(rx_buffer,"GrabOn")!=NULL){
  1129. DTU_Write("{\"ctr_cam\":\"GrabOn\"}");
  1130. //开启超时定时器
  1131. Start_timer_Uart2();
  1132. }else if (strstr(rx_buffer,"GrabOff")!=NULL){
  1133. DTU_Write("{\"ctr_cam\":\"GrabOff\"}");
  1134. //开启超时定时器
  1135. Start_timer_Uart2();
  1136. }
  1137. else if(strstr(rx_buffer,"SelfCheck")!=NULL)
  1138. {
  1139. //校准屏幕的RTC
  1140. HMI_WRITE("rtc0=%d",tm.tm_year+1900);
  1141. HMI_WRITE("rtc1=%d",tm.tm_mon+1);
  1142. HMI_WRITE("rtc2=%d",tm.tm_mday);
  1143. HMI_WRITE("rtc3=%d",tm.tm_hour);
  1144. HMI_WRITE("rtc4=%d",tm.tm_min);
  1145. HMI_WRITE("rtc5=%d",tm.tm_sec);
  1146. device_status.selfcheck_ws = 2;
  1147. LOG_W("开始自检:%d",device_status.selfcheck_ws);
  1148. }
  1149. }
  1150. #define EPS 1e-3
  1151. // 找出一维数组中最大数的索引
  1152. int findMaxIndex(double arr[], int size) {
  1153. int maxIndex = 0; // 假设最大值的索引为0
  1154. for (int i = 1; i < size; i++) {
  1155. if ((arr[i] - arr[maxIndex])>EPS) {
  1156. maxIndex = i; // 更新最大值的索引
  1157. }
  1158. }
  1159. return maxIndex;
  1160. }
  1161. void send_signal(int sig)
  1162. {
  1163. rt_thread_kill(ctr_motor, sig);
  1164. }
  1165. // 全局状态变量(需在合适位置初始化)
  1166. static float g_best_sharpness = -FLT_MAX; // 全局最佳清晰度
  1167. static int g_best_pos = 0; // 全局最佳位置
  1168. static float g_last_sharpness = 0.0; // 上一次移动后的清晰度
  1169. static bool g_initial_move = true; // 初始方向判断标志
  1170. static bool focus_done = false;
  1171. int maxIterations = 100;
  1172. uint8_t dir = 1;
  1173. uint8_t stepSize = 20; // 初始步长(单位:步进电机脉冲数)
  1174. int current_pos = 0; // 根据实际类型修改(int/float等)
  1175. static int g_peak_counter = 0; // 连续上升后下降的计数器
  1176. static bool g_last_rising = false; // 记录上次是否上升
  1177. static int counter_sharpness_reduce = 0; //清晰度降低累计
  1178. static int counter_sharpness_diff = 0; //清晰度累计插值 次数变化
  1179. #define MAX(a, b) ((a) > (b) ? (a) : (b)) // 手动定义宏
  1180. void thread1_signal_handler(int sig)
  1181. {
  1182. rt_timer_stop(timer_takephoto);
  1183. LOG_I("[对焦] 清晰度值更新:%.2f", device_status.focus_value);
  1184. slide_para.pulse_period = mapSpeed(1); // 更新电机速度参数
  1185. maxIterations--;
  1186. if (device_status.autotakepic == 2 && maxIterations >= 0) {
  1187. float current_sharpness = device_status.focus_value;
  1188. /* 更新全局最佳状态 */
  1189. if (current_sharpness > g_best_sharpness) {
  1190. g_best_sharpness = current_sharpness;
  1191. g_best_pos = current_pos;
  1192. LOG_I("[对焦] 发现更佳位置:%d (清晰度:%.2f)", g_best_pos, g_best_sharpness);
  1193. }
  1194. /* 初始方向判断(首步移动后) */
  1195. if (g_initial_move) {
  1196. if (current_sharpness > g_last_sharpness) {
  1197. LOG_I("[对焦] 初始方向正确,保持方向:%d", dir);
  1198. } else {
  1199. dir = !dir; // 反转方向
  1200. LOG_I("[对焦] 初始方向错误,反转方向:%d", dir);
  1201. }
  1202. g_initial_move = false;
  1203. } else {
  1204. double diff_sharpness = fabs(current_sharpness-g_last_sharpness);
  1205. if(diff_sharpness<1.0)
  1206. {
  1207. counter_sharpness_diff++;
  1208. if(counter_sharpness_diff>=10){ //持续10次
  1209. stepSize = 20;
  1210. }
  1211. }
  1212. if(diff_sharpness > 3.5) //动态调整,发生明显变化,降低步进幅度
  1213. {
  1214. counter_sharpness_diff = 0;
  1215. stepSize = 4;
  1216. }
  1217. LOG_I("[对焦] 清晰度变化累计%d次小于1.0",counter_sharpness_diff);
  1218. if(current_sharpness< (g_last_sharpness-0.3f)){ //持续3次清晰度降低,调整行进方向
  1219. counter_sharpness_reduce++;
  1220. LOG_I("[对焦] 清晰度下降,次数:%d", counter_sharpness_reduce);
  1221. if(counter_sharpness_reduce>=3){
  1222. counter_sharpness_reduce = 0;
  1223. dir = !dir; // 清晰度下降,反转方向
  1224. LOG_I("[对焦] 连续清晰度下降,调转行进方向!");
  1225. }
  1226. }else {
  1227. counter_sharpness_reduce = 0;
  1228. }
  1229. /* 方向策略调整,检测到清晰度变大 */
  1230. if (current_sharpness > (g_last_sharpness - 3.5f)) { // 噪声容忍阈值3.0
  1231. // 保持方向,当接近峰值时自动减小步长
  1232. LOG_I("保持方向,当接近峰值时自动减小步长...");
  1233. g_last_rising = true;
  1234. stepSize += 2; //动态调整步幅
  1235. } else {
  1236. if(g_last_rising){
  1237. g_peak_counter++;
  1238. LOG_I("[对焦] 检测到清晰度波动[%d/3]", g_peak_counter);
  1239. stepSize = 2;
  1240. }else {
  1241. g_peak_counter = 0; // 连续下降则重置
  1242. }
  1243. g_last_rising = false;
  1244. dir = !dir; // 清晰度下降,反转方向
  1245. LOG_I("[对焦] 清晰度下降,反转方向:%d 步长:%d", dir, stepSize);
  1246. }
  1247. }
  1248. /* 记录本次清晰度值 */
  1249. g_last_sharpness = current_sharpness;
  1250. /* 计算并执行下一步移动 */
  1251. current_pos += dir ? stepSize : -stepSize;
  1252. LOG_I("[对焦] 方向:%d 移动步长:%d 新位置:%d", dir, stepSize, current_pos);
  1253. if(counter_sharpness_diff>=30){
  1254. dir = !dir;
  1255. stepSize += 10; //增大步幅
  1256. counter_sharpness_diff = 0;
  1257. }
  1258. LOG_I("[对焦] 最佳位置:%d (清晰度:%.2f)", g_best_pos, g_best_sharpness);
  1259. if(g_peak_counter>=3&&current_sharpness>g_best_sharpness*0.95){
  1260. focus_done = true;
  1261. }else{
  1262. focus_done = false;
  1263. g_best_sharpness -= 2.0f; //降低阈值,防止找不到
  1264. }
  1265. /* 终止条件检测 */
  1266. if (counter_sharpness_diff>=100||maxIterations <= 0 || focus_done==true) {
  1267. counter_sharpness_reduce = 0;
  1268. maxIterations = 100;
  1269. LOG_I("[对焦] 完成搜索,最佳位置:%d (清晰度:%.2f)", g_best_pos, g_best_sharpness);
  1270. if (g_peak_counter >= 3) {
  1271. LOG_I("[对焦] 连续三次波动确认峰值");
  1272. }
  1273. focus_done = false;
  1274. LOG_I("[对焦] 已到达最佳对焦位置");
  1275. if(maxIterations==0)
  1276. {
  1277. stepSize = 20;
  1278. dir = !dir;
  1279. current_pos = 0;
  1280. }else{
  1281. counter_sharpness_diff = 0;
  1282. char *c0 = makeJson_Warn("auto_focus","auto_focus");
  1283. DTU_Write("%s",c0);
  1284. rt_free(c0);
  1285. device_status.autotakepic = 0; // 退出自动对焦模式
  1286. return;
  1287. }
  1288. }
  1289. slide_para.ystep = stepSize;
  1290. slide_para.ydir = dir;
  1291. slide_para.ydis = stepSize;
  1292. slide_para.en_slide = 6; // 触发移动
  1293. while (slide_para.en_slide != 0) {
  1294. rt_thread_delay(500); // 等待移动完成
  1295. }
  1296. }
  1297. }
  1298. void thread2_signal_handler(int sig)
  1299. {
  1300. rt_timer_stop(timer_takephoto);
  1301. maxIterations--;
  1302. LOG_I("[Y] 清晰度值更新:%.2f", device_status.focus_value);
  1303. slide_para.pulse_period = mapSpeed(50); // 更新电机速度参数
  1304. if(device_status.calibrate_slide==20&& maxIterations >= 0){
  1305. float current_sharpness = device_status.focus_value;
  1306. /* 更新全局最佳状态 */
  1307. if (current_sharpness > g_best_sharpness) {
  1308. g_best_sharpness = current_sharpness;
  1309. LOG_I("[Y] 发现更佳位置(清晰度:%.2f)",g_best_sharpness);
  1310. }
  1311. /* 初始方向判断(首步移动后) */
  1312. if (g_initial_move) {
  1313. if (current_sharpness > g_last_sharpness) {
  1314. LOG_I("[对焦] 初始方向正确,保持方向:%d", dir);
  1315. }
  1316. g_initial_move = false;
  1317. }else{
  1318. double diff_sharpness = fabs(current_sharpness-g_last_sharpness);
  1319. if(diff_sharpness<1.0)
  1320. {
  1321. counter_sharpness_diff++;
  1322. }
  1323. LOG_I("[Y] 清晰度变化累计%d次小于1.0",counter_sharpness_diff);
  1324. if(counter_sharpness_diff>1&&diff_sharpness>100.0){
  1325. //停止,保存当前位置
  1326. device_status.calibrate_slide = 0;
  1327. device_status.y_init_dis = slide_para.ystep;
  1328. LOG_I("设置Y轴初始位置:%d",slide_para.ystep);
  1329. ef_set_env_blob("y_init_dis", &device_status.y_init_dis, sizeof(device_status.y_init_dis));
  1330. return;
  1331. }
  1332. /* 记录本次清晰度值 */
  1333. g_last_sharpness = current_sharpness;
  1334. }
  1335. /* 终止条件检测 */
  1336. if (maxIterations <= 0) {
  1337. device_status.calibrate_slide = 0;
  1338. return;
  1339. }
  1340. slide_para.ydir = dir;
  1341. slide_para.ydis = 80;
  1342. slide_para.ystep += 80;
  1343. slide_para.en_slide = 6; // 触发移动
  1344. while (slide_para.en_slide != 0) {
  1345. rt_thread_delay(500); // 等待移动完成
  1346. }
  1347. }
  1348. }
  1349. //入口函数-电机控制及
  1350. static void Ctr_Mortor_entry(void* parameter)
  1351. {
  1352. /*孢子仪2.0*/
  1353. rt_signal_install(SIGUSR1, thread1_signal_handler); //自动对焦信号
  1354. rt_signal_install(SIGUSR2, thread2_signal_handler); //坐标校准信号
  1355. rt_signal_unmask(SIGUSR1);
  1356. rt_signal_unmask(SIGUSR2);
  1357. static uint32_t t_takephoto = 0;
  1358. while(1)
  1359. {
  1360. if(device_status.calibrate_slide==2)
  1361. {
  1362. device_status.autotakepic=1;
  1363. device_status.calibrate_slide = 20;
  1364. }
  1365. if(device_status.autotakepic==2)
  1366. {
  1367. t_takephoto++;
  1368. if(t_takephoto==36000)
  1369. {
  1370. t_takephoto = 0;
  1371. device_status.autotakepic = 0;
  1372. }
  1373. }else {
  1374. t_takephoto = 0;
  1375. }
  1376. /*对焦拍照,滑台初始化*/
  1377. if(device_status.autotakepic==1)
  1378. {
  1379. device_status.step_focus = 1;
  1380. slide_para.en_slide =3; //X轴复位
  1381. slide_para.pulse_period = mapSpeed(50);
  1382. while(slide_para.en_slide!=0){
  1383. LOG_D("等待X轴电机完成复位!");
  1384. rt_thread_delay(1000);
  1385. }
  1386. slide_para.en_slide = 4; //Z轴复位
  1387. slide_para.pulse_period = mapSpeed(50);
  1388. while(slide_para.en_slide!=0){
  1389. LOG_D("等待Z轴电机完成复位!");
  1390. rt_thread_delay(1000);
  1391. }
  1392. slide_para.en_slide = 7; //Y轴复位
  1393. slide_para.pulse_period = mapSpeed(50);
  1394. while(slide_para.en_slide!=0){
  1395. LOG_D("等待Y轴电机完成复位!");
  1396. rt_thread_delay(1000);
  1397. }
  1398. //X轴电机运行到胶带位置
  1399. if(rt_pin_read(Limit_Motor_X)==1){
  1400. slide_para.xdir = 1; //1位反向,0位正向;
  1401. slide_para.xdis = device_status.x_init_dis; //1200
  1402. slide_para.pulse_period = mapSpeed(50);
  1403. slide_para.en_slide = 1;
  1404. while(slide_para.en_slide!=0){
  1405. LOG_D("Again:等待X轴电机到位!");
  1406. rt_thread_delay(1000);
  1407. }
  1408. }
  1409. if(device_status.calibrate_slide==0){
  1410. device_status.autotakepic = 3;
  1411. }else{
  1412. slide_para.en_slide = 7; //Y轴复位
  1413. slide_para.pulse_period = mapSpeed(50);
  1414. while(slide_para.en_slide!=0){
  1415. LOG_D("等待Y轴电机完成复位!");
  1416. rt_thread_delay(1000);
  1417. }
  1418. rt_thread_delay(1000);
  1419. //生成JSON指令
  1420. char *photo = makeJson_Warn("mov_init_y","takephoto_done");
  1421. DTU_Write("%s",photo);
  1422. rt_free(photo);
  1423. device_status.autotakepic = 4;
  1424. rt_timer_start(timer_takephoto);
  1425. }
  1426. }
  1427. if(device_status.autotakepic==3)
  1428. {
  1429. slide_para.en_slide = 7; //Y轴复位
  1430. slide_para.pulse_period = mapSpeed(50);
  1431. while(slide_para.en_slide!=0){
  1432. LOG_D("等待Y轴电机完成复位!");
  1433. rt_thread_delay(1000);
  1434. }
  1435. rt_thread_delay(1000);
  1436. //Y轴电机运行到指定距离
  1437. slide_para.ydir = 1;
  1438. //slide_para.ydis = 3350;
  1439. slide_para.ydis = device_status.y_init_dis; //9000
  1440. slide_para.pulse_period = mapSpeed(50);
  1441. slide_para.en_slide = 6;
  1442. while(slide_para.en_slide!=0){
  1443. LOG_D("Again:等待y轴电机到位!");
  1444. rt_thread_delay(1000);
  1445. }
  1446. //生成JSON指令
  1447. char *photo = makeJson_Warn("mov_init_y","takephoto_done");
  1448. DTU_Write("%s",photo);
  1449. rt_free(photo);
  1450. device_status.autotakepic = 4;
  1451. rt_timer_start(timer_takephoto);
  1452. }
  1453. if(device_status.autotakepic == 4)
  1454. {
  1455. device_status.autotakepic = 2;
  1456. g_best_sharpness = 0.0; // 全局最佳清晰度
  1457. g_best_pos = 0; // 全局最佳位置
  1458. g_last_sharpness = 0.0; // 上一次移动后的清晰度
  1459. g_initial_move = true; // 初始方向判断标志
  1460. maxIterations = 100;
  1461. counter_sharpness_diff = 0;
  1462. dir = 1;
  1463. stepSize = 20; // 初始步长(单位:步进电机脉冲数)
  1464. current_pos = 0; // 根据实际类型修改(int/float等)
  1465. slide_para.ystep = 0;
  1466. g_peak_counter = 0;
  1467. g_last_rising = false;
  1468. focus_done = false;
  1469. //启动超时检测定时器
  1470. }
  1471. if(device_status.fsl_sw==1)
  1472. {
  1473. ctr_Petroleum_jelly(10);
  1474. }
  1475. //一键测试
  1476. if(device_status.auto_test_sw==1)
  1477. {
  1478. //开始采集风机持续5S
  1479. ctr_router(ON);
  1480. rt_thread_delay(1000*60);
  1481. //关闭采集风机
  1482. ctr_router(OFF);
  1483. //胶带传送
  1484. turnlimit();
  1485. rt_thread_delay(1000);
  1486. //滑台归位,自动对焦
  1487. //拍一张照片上传到平台
  1488. device_status.autotakepic=1;
  1489. device_status.auto_test_sw=0;
  1490. char *take_start = makeJson_Warn("start", "autofocus");
  1491. DTU_Write(take_start);
  1492. rt_free(take_start);
  1493. }
  1494. rt_thread_delay(100);
  1495. }
  1496. }
  1497. /*屏幕写数据*/
  1498. int Hmi_Fresh(void)
  1499. {
  1500. #if 0
  1501. //初始化串口4
  1502. HMI_uart4_init();
  1503. rt_err_t ret = RT_EOK;
  1504. /* 初始化消息队列 */
  1505. rt_mq_init(&rx_mq, "rx_mq",
  1506. msg_pool, /* 存放消息的缓冲区 */
  1507. sizeof(struct rx_msg), /* 一条消息的最大长度 */
  1508. sizeof(msg_pool), /* 存放消息的缓冲区大小 */
  1509. RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
  1510. /* 设置接收回调函数 */
  1511. rt_device_set_rx_indicate(serial, uart_input);
  1512. /* 创建线程*/
  1513. hmi1 = rt_thread_create("Hmi_Fresh_thread",
  1514. Hmi_thread_entry,
  1515. RT_NULL,
  1516. HMI_THREAD_STACK_SIZE,
  1517. HMI_THREAD_PRIORITY,
  1518. HMI_THREAD_TIMESLICE);
  1519. /* 如果获得线程控制块,启动这个线程 */
  1520. if (hmi1 != RT_NULL)
  1521. {
  1522. rt_thread_startup(hmi1);
  1523. }
  1524. else
  1525. {
  1526. LOG_E("rt_thread_startup hmi1 failed!");
  1527. }
  1528. #endif
  1529. /*部分控制的线程*/
  1530. ctr_motor = rt_thread_create("Ctr motor",
  1531. Ctr_Mortor_entry,
  1532. RT_NULL,
  1533. CONTROL_MOTOR_STACK_SIZE,
  1534. CONTROL_MOTOR_PRIORITY,
  1535. CONTROL_MOTOR_TIMESLICE);
  1536. if(ctr_motor != RT_NULL)
  1537. {
  1538. rt_thread_startup(ctr_motor);
  1539. }
  1540. //定时器,串口通信超时监测20S
  1541. timer_uart2 = rt_timer_create("t_uart2",t_uart2_callback,RT_NULL, 20000, RT_TIMER_FLAG_ONE_SHOT);
  1542. if(timer_uart2==RT_NULL)
  1543. {
  1544. LOG_E("rt_timer_create timer_uart2 failed!");
  1545. }
  1546. return 0;
  1547. }
  1548. void Check_ParamConfig(void)
  1549. {
  1550. time_t now = time(RT_NULL);
  1551. static struct tm tm;
  1552. gmtime_r(&now, &tm);
  1553. if(easyflash_init()==EF_NO_ERR) //初始化成功
  1554. {
  1555. ef_get_env_blob("reboot_time", &device_status.rebootcnt, sizeof(device_status.rebootcnt), NULL); //读出reboot_time的值
  1556. ef_get_env_blob("update_flag", &device_status.update_flag, sizeof(device_status.update_flag), NULL); //读出updateflag的值
  1557. ef_get_env_blob("iSave", &device_status.iSave, sizeof(device_status.iSave), NULL); //判断是否用户配置了程序
  1558. device_status.rebootcnt++;
  1559. ef_set_env_blob("reboot_time", &device_status.rebootcnt, sizeof(device_status.rebootcnt));
  1560. LOG_I("REBOOT_TIME:%d-%d-%d",device_status.rebootcnt,device_status.iSave,device_status.update_flag);
  1561. }
  1562. else {
  1563. LOG_E("easyflash init ERRPR!!!");
  1564. }
  1565. //保存默认配置
  1566. if(device_status.iSave!=FLASH_FLAG)
  1567. {
  1568. device_status.iSave=FLASH_FLAG;
  1569. /*设备类型,7孢子仪,3测报灯,默认设置测报灯*/
  1570. device_status.dtype = 7;
  1571. device_status.version_type = 7; //4.0
  1572. device_status.Power_Type = 0; //0-交流供电;1-直流供电
  1573. device_status.clt_time = 10; //收集时间
  1574. device_status.heattime = 10; //加热时间
  1575. device_status.heatvalue=100; //加热温度
  1576. device_status.thsp = 300; //默认熄屏时间5分钟
  1577. device_status.sunBar = 9; //光控定时时长
  1578. device_status.startBar = 20; //定时开始时间
  1579. device_status.stopBar = 0; //定时结束时间
  1580. device_status.tph = 60; //高温保护阈值
  1581. device_status.tpl = 10; //低温保护阈值
  1582. device_status.shake_ws = 1; //震动开关,默认开
  1583. device_status.shake_sec = 1; //震动时间,s
  1584. device_status.vol = 7; //音量
  1585. device_status.timeSwitch = 0; //工作模式:光控
  1586. device_status.passWord = 8888; //屏幕密码
  1587. device_status.dattim = 10; //数据上传间隔
  1588. device_status.ds = 1; //设备状态
  1589. device_status.update_flag = 0;
  1590. /*孢子仪*/
  1591. device_status.collecting_sh1 = 11;
  1592. device_status.collecting_eh1 = 16;
  1593. device_status.collecting_sh2 = 0;
  1594. device_status.collecting_eh2 = 0;
  1595. device_status.collecting_sh3 = 0;
  1596. device_status.collecting_eh3 = 0;
  1597. device_status.collt_min_sh1 = 0;
  1598. device_status.collt_min_eh1 = 0;
  1599. device_status.collt_min_sh2 = 0;
  1600. device_status.collt_min_eh2 = 0;
  1601. device_status.collt_min_sh3 = 0;
  1602. device_status.collt_min_eh3 = 0;
  1603. device_status.cold_sw = 0;
  1604. device_status.heat_sw =0;
  1605. device_status.cul_time = 1;
  1606. device_status.run_sta = 0;
  1607. device_status.handle_bugger = 0; //默认排虫
  1608. sprintf(device_status.driver, "%s",VERSION_BZY);
  1609. sprintf(device_status.deviceId,"0");
  1610. rt_sprintf(device_status.tempture, "-15");
  1611. rt_sprintf(device_status.humidity, "31");
  1612. rt_sprintf(device_status.mqttcfg1, "8.136.98.49");
  1613. device_status.x_init_dis = 0;
  1614. device_status.y_init_dis = 0;
  1615. ef_set_env_blob("iSave", &device_status.iSave, sizeof(device_status.iSave)); //
  1616. ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
  1617. ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time));
  1618. ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar));
  1619. ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar));
  1620. ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
  1621. ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar));
  1622. ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime));
  1623. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
  1624. ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph));
  1625. ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl));
  1626. ef_set_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws)); //震动开关
  1627. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec)); //震动时间
  1628. ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
  1629. ef_set_env_blob("passWord", &device_status.passWord,sizeof(device_status.passWord));
  1630. ef_set_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim)); //上传时间
  1631. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1632. ef_set_env_blob("env_t",&device_status.tempture,sizeof(device_status.tempture)); //温度
  1633. ef_set_env_blob("env_h",&device_status.humidity,sizeof(device_status.humidity)); //湿度
  1634. ef_set_env_blob("platform",&device_status.mqttcfg1,sizeof(device_status.mqttcfg1)); //上传平台
  1635. ef_set_env_blob("cul_time",&device_status.cul_time,sizeof(device_status.cul_time)); //采集时间
  1636. ef_set_env_blob("cold_sw",&device_status.cold_sw,sizeof(device_status.cold_sw)); //制冷开关
  1637. ef_set_env_blob("heat_sw",&device_status.heat_sw,sizeof(device_status.heat_sw)); //加热开关
  1638. ef_set_env_blob("collecting_sh1",&device_status.collecting_sh1,sizeof(device_status.collecting_sh1)); //风机开关
  1639. ef_set_env_blob("collecting_eh1",&device_status.collecting_eh1,sizeof(device_status.collecting_eh1)); //采集时间
  1640. ef_set_env_blob("collecting_sh2",&device_status.collecting_sh2,sizeof(device_status.collecting_sh2)); //采集时间
  1641. ef_set_env_blob("collecting_eh2",&device_status.collecting_eh2,sizeof(device_status.collecting_eh2)); //采集时间
  1642. ef_set_env_blob("collecting_sh3",&device_status.collecting_sh3,sizeof(device_status.collecting_sh3)); //采集时间
  1643. ef_set_env_blob("collecting_eh3",&device_status.collecting_eh3,sizeof(device_status.collecting_eh3)); //采集时间
  1644. ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta)); //
  1645. ef_set_env_blob("Power_Type", &device_status.Power_Type, sizeof(device_status.Power_Type));
  1646. ef_set_env_blob("handle_bugger", &device_status.handle_bugger, sizeof(device_status.handle_bugger));
  1647. ef_set_env_blob("x_init_dis", &device_status.x_init_dis, sizeof(device_status.x_init_dis));
  1648. ef_set_env_blob("y_init_dis", &device_status.y_init_dis, sizeof(device_status.y_init_dis));
  1649. ef_set_env_blob("collt_min_sh1", &device_status.collt_min_sh1, sizeof(device_status.collt_min_sh1));
  1650. ef_set_env_blob("collt_min_eh1", &device_status.collt_min_eh1, sizeof(device_status.collt_min_eh1));
  1651. ef_set_env_blob("collt_min_sh2", &device_status.collt_min_sh2, sizeof(device_status.collt_min_sh2));
  1652. ef_set_env_blob("collt_min_eh2", &device_status.collt_min_eh2, sizeof(device_status.collt_min_eh2));
  1653. ef_set_env_blob("collt_min_sh3", &device_status.collt_min_sh3, sizeof(device_status.collt_min_sh3));
  1654. ef_set_env_blob("collt_min_eh3", &device_status.collt_min_eh3, sizeof(device_status.collt_min_eh3));
  1655. LOG_I("SOFTWARE VERSION:%s",device_status.driver);
  1656. LOG_I("Default paramconf is:");
  1657. LOG_I("stime:%d",device_status.iSave);
  1658. LOG_I("stime:%d",device_status.clt_time);
  1659. LOG_I("startBar:%d",device_status.startBar);
  1660. LOG_I("stopBar:%d",device_status.stopBar);
  1661. LOG_I("timeSwitch:%d",device_status.timeSwitch);
  1662. LOG_I("sunBar:%d",device_status.sunBar);
  1663. LOG_I("jtime:%d",device_status.heattime);
  1664. LOG_I("jheat:%d",device_status.heatvalue);
  1665. LOG_I("thsp:%d", device_status.thsp);
  1666. LOG_I("passwd:%d",device_status.passWord);
  1667. flash_flag = RT_FALSE;
  1668. }//从easyflash读取参数配置
  1669. else if (device_status.iSave==FLASH_FLAG) {
  1670. ef_get_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype),NULL); //设备类型
  1671. ef_get_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim),NULL); //数据上传间隔
  1672. ef_get_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time), NULL); //收集时间
  1673. ef_get_env_blob("startBar", &device_status.startBar,sizeof(device_status.startBar), NULL); //开始时间
  1674. ef_get_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar), NULL); //结束时间
  1675. ef_get_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch), NULL); //模式
  1676. ef_get_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar), NULL); //光控时间
  1677. ef_get_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime), NULL); //加热时间
  1678. ef_get_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue), NULL); //加热温度
  1679. ef_get_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws), NULL); //震动开关
  1680. ef_get_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec), NULL); //震动时间
  1681. ef_get_env_blob("tph", &device_status.tph, sizeof(device_status.tph), NULL); //高温保护
  1682. ef_get_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl), NULL); //低温保护
  1683. ef_get_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim), NULL); //数据上传间隔
  1684. ef_get_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp), NULL); //熄屏时间
  1685. ef_get_env_blob("passWord",&device_status.passWord,sizeof(device_status.passWord),NULL); //密码
  1686. ef_get_env_blob("imei",&device_status.imei,sizeof(device_status.imei),NULL); //IMEI
  1687. ef_get_env_blob("iccid",&device_status.iccid,sizeof(device_status.iccid),NULL); //ICCID
  1688. ef_get_env_blob("lat",&device_status.lat,sizeof(device_status.lat),NULL);
  1689. ef_get_env_blob("lng",&device_status.lng,sizeof(device_status.lng),NULL);
  1690. ef_get_env_blob("ds",&device_status.ds,sizeof(device_status.ds),NULL); //开关机状态
  1691. ef_get_env_blob("env_t",&device_status.tempture,sizeof(device_status.tempture),NULL); //环境温度
  1692. ef_get_env_blob("env_h",&device_status.humidity,sizeof(device_status.humidity),NULL); //环境湿度
  1693. ef_get_env_blob("json_w_clock",&device_status.json_w_clock,sizeof(device_status.json_w_clock),NULL); //时间
  1694. ef_get_env_blob("json_w_reset",&device_status.json_w_reset,sizeof(device_status.json_w_reset),NULL); //重置
  1695. ef_get_env_blob("json_w_platform",&device_status.json_w_platform,sizeof(device_status.json_w_platform),NULL);//平台
  1696. ef_get_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type),NULL); //版本类型
  1697. ef_get_env_blob("driver_bl",&device_status.driver_bl,sizeof(device_status.driver_bl),NULL); //BL版本号 进BL的时候存储到FLASH
  1698. ef_get_env_blob("platform",&device_status.mqttcfg1,sizeof(device_status.mqttcfg1),NULL); //平台地址
  1699. /*孢子仪*/
  1700. ef_get_env_blob("cul_time",&device_status.cul_time,sizeof(device_status.cul_time),NULL); //培养时间
  1701. ef_get_env_blob("cold_sw",&device_status.cold_sw,sizeof(device_status.cold_sw),NULL); //制冷开关
  1702. ef_get_env_blob("heat_sw",&device_status.heat_sw,sizeof(device_status.heat_sw),NULL); //加热开关
  1703. ef_get_env_blob("collecting_sh1",&device_status.collecting_sh1,sizeof(device_status.collecting_sh1),NULL); //采集开始1
  1704. ef_get_env_blob("collecting_eh1",&device_status.collecting_eh1,sizeof(device_status.collecting_eh1),NULL); //采集结束1
  1705. ef_get_env_blob("collecting_sh2",&device_status.collecting_sh2,sizeof(device_status.collecting_sh2),NULL); //采集开始2
  1706. ef_get_env_blob("collecting_eh2",&device_status.collecting_eh2,sizeof(device_status.collecting_eh2),NULL); //采集结束2
  1707. ef_get_env_blob("collecting_sh3",&device_status.collecting_sh3,sizeof(device_status.collecting_sh3),NULL); //采集开始3
  1708. ef_get_env_blob("collecting_eh3",&device_status.collecting_eh3,sizeof(device_status.collecting_eh3),NULL); //采集结束3
  1709. ef_get_env_blob("run_sta",&device_status.run_sta,sizeof(device_status.run_sta),NULL); //运行状态
  1710. ef_get_env_blob("Power_Type",&device_status.Power_Type,sizeof(device_status.Power_Type),NULL); //供电类型
  1711. ef_get_env_blob("deviceId",&device_status.deviceId,sizeof(device_status.deviceId),NULL); //设备编码
  1712. ef_get_env_blob("handle_bugger",&device_status.handle_bugger,sizeof(device_status.handle_bugger),NULL); //排虫
  1713. ef_get_env_blob("x_init_dis",&device_status.x_init_dis,sizeof(device_status.x_init_dis),NULL);
  1714. ef_get_env_blob("y_init_dis",&device_status.y_init_dis,sizeof(device_status.y_init_dis),NULL);
  1715. ef_get_env_blob("collt_min_sh1", &device_status.collt_min_sh1, sizeof(device_status.collt_min_sh1), NULL);
  1716. ef_get_env_blob("collt_min_eh1", &device_status.collt_min_eh1, sizeof(device_status.collt_min_eh1), NULL);
  1717. ef_get_env_blob("collt_min_sh2", &device_status.collt_min_sh2, sizeof(device_status.collt_min_sh2), NULL);
  1718. ef_get_env_blob("collt_min_eh2", &device_status.collt_min_eh2, sizeof(device_status.collt_min_eh2), NULL);
  1719. ef_get_env_blob("collt_min_sh3", &device_status.collt_min_sh3, sizeof(device_status.collt_min_sh3), NULL);
  1720. ef_get_env_blob("collt_min_eh3", &device_status.collt_min_eh3, sizeof(device_status.collt_min_eh3), NULL);
  1721. //光控计数
  1722. device_status.work_done = 0;
  1723. //工作完成标志位
  1724. if(device_status.dattim==0)
  1725. {
  1726. device_status.dattim = 10;
  1727. }
  1728. /*设备类型,7孢子仪*/
  1729. device_status.dtype = 7;
  1730. device_status.version_type = 7;
  1731. if (device_status.version_type==7) {
  1732. rt_sprintf(device_status.driver, "%s",VERSION_BZY);
  1733. //异常处理:20230331,修复第一段开始时间等于第二段结束时间的情况
  1734. if(device_status.collecting_eh1==device_status.collecting_eh2&&device_status.collecting_sh2!=device_status.collecting_eh2)
  1735. {
  1736. device_status.collecting_eh2 = device_status.collecting_sh2 + 2;
  1737. }
  1738. }
  1739. LOG_I("SOFTWARE VERSION:%s-imei:%s--iccid:%s",device_status.driver,device_status.imei,device_status.iccid);
  1740. LOG_I("read flash paramconfis:%d-%s-%s",device_status.version_type,device_status.driver_bl,device_status.driver_compile);
  1741. LOG_I("开机次数:%d",device_status.rebootcnt);
  1742. LOG_I("收集时间:%d",device_status.clt_time);
  1743. LOG_I("定时开始时间:%d",device_status.startBar);
  1744. LOG_I("定时结束时间:%d",device_status.stopBar);
  1745. LOG_I("timeSwitch:%d",device_status.timeSwitch);
  1746. LOG_I("光控定时时长:%d",device_status.sunBar);
  1747. LOG_I("收集时间:%d",device_status.heattime);
  1748. LOG_I("加热温度:%d",device_status.heatvalue);
  1749. LOG_I("震动开关:%d",device_status.shake_ws);
  1750. LOG_I("震动时间:%d",device_status.shake_sec);
  1751. LOG_I("高温保护:%d",device_status.tph);
  1752. LOG_I("低温保护:%d",device_status.tpl);
  1753. LOG_I("thsp:%d", device_status.thsp);
  1754. LOG_I("密码:%d",device_status.passWord);
  1755. LOG_I("数据上传间隔:%d",device_status.dattim);
  1756. LOG_I("温湿度数据:%d-%d",atoi(device_status.tempture),atoi(device_status.humidity));
  1757. LOG_I("上传平台:%s",device_status.mqttcfg1);
  1758. LOG_I("孢子仪运行状态:%d",device_status.run_sta);
  1759. LOG_I("孢子仪采集时间:[%d:%d-%d:%d,%d:%d-%d:%d,%d:%d-%d:%d]",
  1760. device_status.collecting_sh1,device_status.collt_min_sh1,
  1761. device_status.collecting_eh1,device_status.collt_min_eh1,
  1762. device_status.collecting_sh2,device_status.collt_min_sh2,
  1763. device_status.collecting_eh2,device_status.collt_min_eh2,
  1764. device_status.collecting_sh3,device_status.collt_min_sh3,
  1765. device_status.collecting_eh3,device_status.collt_min_eh3);
  1766. LOG_I("PowerType:%d",device_status.Power_Type);
  1767. LOG_I("x_init_dis:%d",device_status.x_init_dis);
  1768. LOG_I("y_init_dis:%d",device_status.y_init_dis);
  1769. }
  1770. //RTC时间判断,如果当前年份小于2020,提示需要设置时间,切换为光控
  1771. {
  1772. device_status.update_flag = 0;
  1773. //初始化联网状态
  1774. rt_sprintf(device_status.net,"1");
  1775. rt_sprintf(device_status.csq,"0");
  1776. rt_sprintf(device_status.sim,"1");
  1777. rt_sprintf(device_status.gps, "0");
  1778. Hmi_Fresh();
  1779. dtu_data_play();
  1780. sht30_collect();
  1781. slide_Fresh();
  1782. ctr_photo(ON);
  1783. #if SENSOR_EN==1
  1784. mb_master_samlpe();
  1785. #endif
  1786. uint16_t connect_num=0;
  1787. //等待联网,
  1788. while(atoi(device_status.net)==1)
  1789. {
  1790. rt_thread_delay(10000);
  1791. connect_num++;
  1792. LOG_I("联网中......%d",connect_num);
  1793. //超时时间为100S
  1794. if(connect_num==10)
  1795. {
  1796. LOG_W("Time not set:%04d-%02d-%02d %02d:%02d:%02d",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  1797. break;
  1798. }
  1799. }
  1800. rt_thread_delay(2000);
  1801. DTU_Write("%s",makeJson_Warn("turnon", "device_stm32"));
  1802. rt_thread_delay(1000);
  1803. if(flash_flag==RT_TRUE){
  1804. backup_param_to_rtu();
  1805. }else{
  1806. recover_param_from_rtu();
  1807. }
  1808. Workstatus_check();
  1809. Work_Loop_Task();
  1810. }
  1811. }
  1812. void ResetParamDefault(void)
  1813. {
  1814. device_status.ds = 1; //设备状态开机
  1815. device_status.thsp = 300;
  1816. device_status.passWord = 8888;
  1817. device_status.dattim = 10;
  1818. device_status.collecting_sh1 = 11;
  1819. device_status.collecting_eh1 = 16;
  1820. device_status.collecting_sh2 = 0;
  1821. device_status.collecting_eh2 = 0;
  1822. device_status.collecting_sh3 = 0;
  1823. device_status.collecting_eh3 = 0;
  1824. device_status.collt_min_sh1 = 0;
  1825. device_status.collt_min_eh1 = 0;
  1826. device_status.collt_min_sh2 = 0;
  1827. device_status.collt_min_eh2 = 0;
  1828. device_status.collt_min_sh3 = 0;
  1829. device_status.collt_min_eh3 = 0;
  1830. device_status.Power_Type = 0;
  1831. device_status.tph = 70; //高温保护阈值
  1832. device_status.tpl = 10; //低温保护阈值
  1833. ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph));
  1834. ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl));
  1835. ef_set_env_blob("Power_Type", &device_status.Power_Type, sizeof(device_status.Power_Type));
  1836. ef_set_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim)); //上传时间
  1837. if(device_status.dtype==7)
  1838. {
  1839. device_status.run_sta = 0;
  1840. device_status.heatvalue = 25; //培养温度
  1841. ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta));
  1842. ef_set_env_blob("collecting_sh1", &device_status.collecting_sh1, sizeof(device_status.collecting_sh1)); //采集时间
  1843. ef_set_env_blob("collecting_eh1", &device_status.collecting_eh1, sizeof(device_status.collecting_eh1)); //采集时间
  1844. ef_set_env_blob("collecting_sh2", &device_status.collecting_sh2, sizeof(device_status.collecting_sh2)); //采集时间
  1845. ef_set_env_blob("collecting_eh2", &device_status.collecting_eh2, sizeof(device_status.collecting_eh2)); //采集时间
  1846. ef_set_env_blob("collecting_sh3", &device_status.collecting_sh3, sizeof(device_status.collecting_sh3)); //采集时间
  1847. ef_set_env_blob("collecting_eh3", &device_status.collecting_eh3, sizeof(device_status.collecting_eh3)); //采集时间
  1848. ef_set_env_blob("collt_min_sh1", &device_status.collt_min_sh1, sizeof(device_status.collt_min_sh1));
  1849. ef_set_env_blob("collt_min_eh1", &device_status.collt_min_eh1, sizeof(device_status.collt_min_eh1));
  1850. ef_set_env_blob("collt_min_sh2", &device_status.collt_min_sh2, sizeof(device_status.collt_min_sh2));
  1851. ef_set_env_blob("collt_min_eh2", &device_status.collt_min_eh2, sizeof(device_status.collt_min_eh2));
  1852. ef_set_env_blob("collt_min_sh3", &device_status.collt_min_sh3, sizeof(device_status.collt_min_sh3));
  1853. ef_set_env_blob("collt_min_eh3", &device_status.collt_min_eh3, sizeof(device_status.collt_min_eh3));
  1854. }
  1855. ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
  1856. ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
  1857. ef_set_env_blob("passWord", &device_status.passWord,sizeof(device_status.passWord));
  1858. DTU_Write("reset.mp3");
  1859. rt_thread_delay(1000);
  1860. }
  1861. void ResetParamDefault_DC(void)
  1862. {
  1863. //将温度数据设置为-15摄氏度,以便上传的数据中包含温控数据
  1864. rt_sprintf(device_status.tempture,"%d",-15);
  1865. device_status.clt_time = 10; //收集时间
  1866. device_status.dattim = 10; //数据上传间隔
  1867. #if LAMP_EN == 0
  1868. device_status.startBar = 20; //定时开始时间
  1869. device_status.stopBar = 0; //定时结束时间
  1870. #endif
  1871. device_status.heattime = 10; //加热时间
  1872. device_status.shake_ws = 1; //针对开关
  1873. device_status.shake_sec = 1; //震动时间1-100ms
  1874. device_status.tph = 70; //高温保护阈值
  1875. device_status.tpl = 10; //低温保护阈值
  1876. device_status.timeSwitch = 0; //定时模式
  1877. device_status.heatvalue=100;
  1878. device_status.ds = 1; //设备状态开机
  1879. device_status.thsp = 300;
  1880. device_status.passWord = 8888;
  1881. device_status.timeSwitch = 0;
  1882. device_status.Power_Type = 1;
  1883. device_status.handle_bugger = 0;
  1884. ef_set_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim)); //上传时间
  1885. ef_set_env_blob("handle_bugger", &device_status.handle_bugger, sizeof(device_status.handle_bugger));
  1886. ef_set_env_blob("Power_Type", &device_status.Power_Type, sizeof(device_status.Power_Type));
  1887. ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
  1888. ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time));
  1889. ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar));
  1890. ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar));
  1891. ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
  1892. ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar));
  1893. ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime));
  1894. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
  1895. ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph));
  1896. ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl));
  1897. ef_set_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws)); //震动开关
  1898. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec)); //震动时间
  1899. ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
  1900. ef_set_env_blob("passWord", &device_status.passWord,sizeof(device_status.passWord));
  1901. DTU_Write("reset.mp3");
  1902. rt_thread_delay(1000);
  1903. }
  1904. //固件备份
  1905. void app_partion_backup(void)
  1906. {
  1907. rt_kprintf("固件恢复!!!\n");
  1908. const char *dst_part_name = "df_area";
  1909. const char *src_part_name = "app";
  1910. const struct fal_partition *dst_part;
  1911. const struct fal_partition *src_part;
  1912. dst_part = fal_partition_find(dst_part_name);
  1913. src_part = fal_partition_find(src_part_name);
  1914. if (dst_part == RT_NULL || src_part == RT_NULL)
  1915. {
  1916. if (dst_part == RT_NULL)
  1917. rt_kprintf("%s partition is not exist!\n", dst_part_name);
  1918. if (src_part == RT_NULL)
  1919. rt_kprintf("%s partition is not exist!\n", src_part_name);
  1920. }
  1921. else
  1922. {
  1923. rt_kprintf("Clone %s partition to %s partition:\n", src_part_name, dst_part_name);
  1924. if (fal_partition_erase(dst_part, 0, dst_part->len) >= 0)
  1925. {
  1926. int clone_pos = 0;
  1927. int clone_len = 0, clone_tol_len;
  1928. rt_uint8_t *buf = rt_malloc(4096);
  1929. if (dst_part->len < src_part->len)
  1930. clone_tol_len = dst_part->len;
  1931. else
  1932. clone_tol_len = src_part->len;
  1933. while ((clone_pos < clone_tol_len) && (buf != RT_NULL))
  1934. {
  1935. clone_len = fal_partition_read(src_part, clone_pos, buf, 4096);
  1936. if (clone_len < 0)
  1937. {
  1938. rt_kprintf("\nread %s partition failed, clone stop!\n", src_part_name);
  1939. break;
  1940. }
  1941. if (fal_partition_write(dst_part, clone_pos, buf, clone_len) < 0)
  1942. {
  1943. rt_kprintf("\nwrite %s partition failed, clone stop!\n", dst_part_name);
  1944. break;
  1945. }
  1946. HMI_WRITE("#");
  1947. rt_kprintf("#");
  1948. clone_pos += clone_len;
  1949. }
  1950. if (clone_pos >= clone_tol_len)
  1951. {
  1952. rt_kprintf("\nClone partition success, total %d bytes!\n", clone_tol_len);
  1953. }
  1954. else
  1955. {
  1956. rt_kprintf("Clone partition failed!");
  1957. }
  1958. if (buf)
  1959. rt_free(buf);
  1960. }
  1961. }
  1962. }
  1963. void Debug_log_ON(void)
  1964. {
  1965. rt_console_set_device("uart4");
  1966. finsh_set_device("uart4");
  1967. }
  1968. void Debug_log_OFF(void)
  1969. {
  1970. rt_console_set_device("uart1");
  1971. finsh_set_device("uart1");
  1972. rt_mq_init(&rx_mq, "rx_mq",
  1973. msg_pool, /* 存放消息的缓冲区 */
  1974. sizeof(struct rx_msg), /* 一条消息的最大长度 */
  1975. sizeof(msg_pool), /* 存放消息的缓冲区大小 */
  1976. RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
  1977. rt_device_set_rx_indicate(serial, uart_input);
  1978. hmi1 = rt_thread_create("Hmi_Fresh_thread",
  1979. Hmi_thread_entry,
  1980. RT_NULL,
  1981. HMI_THREAD_STACK_SIZE,
  1982. HMI_THREAD_PRIORITY,
  1983. HMI_THREAD_TIMESLICE);
  1984. if (hmi1 != RT_NULL)
  1985. {
  1986. rt_thread_startup(hmi1);
  1987. }
  1988. }
  1989. int delete_thread_ctr_motor()
  1990. {
  1991. if(ctr_motor!=RT_NULL)
  1992. {
  1993. rt_thread_delete(ctr_motor);
  1994. }
  1995. }
  1996. MSH_CMD_EXPORT(Debug_log_ON,Debug_log_ON);
  1997. MSH_CMD_EXPORT(Debug_log_OFF,Debug_log_OFF);
  1998. MSH_CMD_EXPORT(ResetParamDefault,ResetParamDefault);
  1999. //
  2000. //INIT_BOARD_EXPORT(HMI_uart4_init);