hmi.c 87 KB

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