hmi.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  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. #define DBG_TAG "hmi.c"
  26. #define DBG_LVL LOG_LVL_INFO
  27. #include <ulog.h>
  28. #include <shell.h>
  29. #include "dtu_uart2.h"
  30. #include <fal.h>
  31. #include <syswatch.h>
  32. #define HMI_THREAD_PRIORITY 21 //线程优先级
  33. #define HMI_THREAD_STACK_SIZE 1024 //线程栈空间大小
  34. #define HMI_THREAD_TIMESLICE 5 //时间片
  35. #define CONTROL_MOTOR_PRIORITY 18
  36. #define CONTROL_MOTOR_STACK_SIZE 2048
  37. #define CONTROL_MOTOR_TIMESLICE 5
  38. static char msg_pool[256];
  39. static char rx_buffer[HMI_UART_RXBUFFER_SIZE + 1];
  40. /**/
  41. Device_Status_typedef device_status;
  42. time_t time(time_t *t);
  43. void list_mem(void);
  44. /* 串口接收消息结构*/
  45. struct rx_msg
  46. {
  47. rt_device_t dev;
  48. rt_size_t size;
  49. };
  50. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; /*初始化配置参数*/
  51. static rt_thread_t hmi1 = RT_NULL;
  52. static rt_thread_t ctr_motor = RT_NULL;
  53. rt_device_t serial; /*串口设备句柄*/
  54. /* 消息队列控制块 */
  55. static struct rt_messagequeue rx_mq;//接收数据消息队列
  56. static void HMI_Decode(char *cmd,uint8_t cmd_len);
  57. static rt_err_t uart_input(rt_device_t dev, rt_size_t size);
  58. void HMI_uart4_init(void)
  59. {
  60. serial = rt_device_find(UART_NAME);
  61. /*修改串口配置参数*/
  62. config.baud_rate = BAUD_RATE_9600; //修改波特率9600
  63. config.data_bits = DATA_BITS_8; //数据位8
  64. config.stop_bits = STOP_BITS_1; //停止位1
  65. config.bufsz = 512; //缓冲区128字节
  66. config.parity = PARITY_NONE; //无奇偶校验
  67. /* step3:控制串口设备。通过控制接口传入命令控制字,与控制参数 */
  68. rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
  69. // /* step4:打开串口设备。以中断接收及轮询发送模式打开串口设备 */
  70. //rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
  71. /* 以 DMA 接收及轮询发送模式打开串口设备 */
  72. rt_device_open(serial, RT_DEVICE_FLAG_DMA_RX);
  73. }
  74. /*串口屏写数据*/
  75. void HMI_WRITE(char *fmt, ...)
  76. {
  77. va_list ap;
  78. //末尾需要拼接的3个ff
  79. const char hmi_hex[3]={0xff,0xff,0xff};
  80. static char str[MAX_SIZE];
  81. //memset(str,'\0',sizeof(str));
  82. rt_size_t str_length;
  83. va_start(ap,fmt);
  84. str_length = rt_vsnprintf(str, sizeof(str) - 1, fmt, ap);
  85. if (str_length > MAX_SIZE - 1)
  86. str_length = MAX_SIZE - 1;
  87. rt_device_write(serial, 0, str, str_length);
  88. va_end(ap);
  89. rt_device_write(serial, 0,hmi_hex,3);
  90. }
  91. /* 接收数据回调函数 */
  92. static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
  93. {
  94. struct rx_msg msg;
  95. rt_err_t result;
  96. msg.dev = dev;
  97. msg.size = size;
  98. result = rt_mq_send(&rx_mq, &msg, sizeof(msg));
  99. if ( result == -RT_EFULL)
  100. {
  101. /* 消息队列满 */
  102. rt_kprintf("message queue full!\n");
  103. }
  104. return result;
  105. }
  106. /*入口函数*/
  107. static void Hmi_thread_entry(void *parameter)
  108. {
  109. struct rx_msg msg;
  110. rt_err_t result;
  111. rt_uint32_t rx_length;
  112. while(1)
  113. {
  114. rt_memset(&msg, 0, sizeof(msg));
  115. /* 从消息队列中读取消息*/
  116. result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), 10);
  117. if (result == RT_EOK)
  118. {
  119. rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size);
  120. rx_buffer[rx_length] = '\0';
  121. LOG_I("%s\n",rx_buffer);
  122. HMI_Decode(rx_buffer,rx_length);
  123. }
  124. /*********************************************************************************************************************/
  125. //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);
  126. //rt_kprintf("workSta.vbat.txt=%c%d.%02d%c\n",hmi_str,voltage/100,voltage%100,hmi_str);
  127. }
  128. }
  129. /*******************************************************************************************/
  130. /***************************** 解析从屏幕收到的数据 begin **********************************/
  131. static void HMI_Decode(char *cmd,uint8_t cmd_len)
  132. {
  133. char *var;
  134. if(device_status.refresh_num==0) device_status.refresh_num = 10;
  135. if(strstr(rx_buffer,"getFullData") != NULL){
  136. time_t now = time(RT_NULL);
  137. static struct tm tm;
  138. gmtime_r(&now, &tm);
  139. if(device_status.refresh_num==5)
  140. {
  141. char *readinfo = makeJson_ReadInfo();
  142. device_status.net = 1;
  143. device_status.sim = 1;
  144. device_status.csq = 1;
  145. device_status.refresh_num = 15;
  146. DTU_Write("%s",readinfo);
  147. list_mem();
  148. rt_free(readinfo);
  149. device_status.hmi_fresh = 1;
  150. HMI_WRITE("temHum.env_hum.val=%d",atoi(device_status.humidity));
  151. HMI_WRITE("temHum.env_tem.val=%d",atoi(device_status.tempture));
  152. HMI_WRITE("workSta.env_hum.val=%d",atoi(device_status.humidity));
  153. HMI_WRITE("workSta.env_tem.val=%d",atoi(device_status.tempture));
  154. }
  155. device_status.refresh_num--;
  156. //更新时间
  157. 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);
  158. //更新电压
  159. HMI_WRITE("workSta.vbat.txt=\"%s\"",device_status.voltage);
  160. //更新电流
  161. HMI_WRITE("workSta.power.txt=\"%s\"",device_status.power);
  162. //更新电池状态
  163. HMI_WRITE("workSta.bat_sta.val=%d",device_status.bat_status);
  164. //更新散热风机状态
  165. HMI_WRITE("workSta.cool_fan.val=%d",device_status.cool_fan_status);
  166. //更新开关状态
  167. HMI_WRITE("workSta.on_off.val=%d",device_status.ds);
  168. //更新工作状态
  169. HMI_WRITE("workSta.work_sta.val=%d",device_status.work_status);
  170. //箱内温度-外壳
  171. HMI_WRITE("workSta.box_tmp.txt=\"%s\"",device_status.tempture);
  172. HMI_WRITE("temHum.box_tmp.txt=\"%s\"",device_status.tempture);
  173. //制冷开关
  174. HMI_WRITE("workSta.cold_sw.val=%d",device_status.cold_sw);
  175. HMI_WRITE("sysSet.cold_sw.val=%d",device_status.cold_sw);
  176. //制冷状态
  177. HMI_WRITE("workSta.cold_sta.val=%d",device_status.cold_status);
  178. //培养温度
  179. HMI_WRITE("workSta.set_temp.val=%d",device_status.set_tempture);
  180. HMI_WRITE("sysSet.keep.val=%d",device_status.set_tempture);
  181. //当前温度-培养仓
  182. HMI_WRITE("workSta.pre_temp.val=%d",device_status.culture_tempture);
  183. //设定培养时间
  184. HMI_WRITE("workSta.cul_time.val=%d",device_status.culture_time);
  185. HMI_WRITE("clockSet.stay.val=%d",device_status.culture_time);
  186. //已培养时间
  187. //滴液时间
  188. HMI_WRITE("clockSet.drop.val=%d",device_status.drop_sec);
  189. //采集开始时间1
  190. HMI_WRITE("clockSet.start1.val=%d",device_status.culture_sh1);
  191. //采集开始时间2
  192. HMI_WRITE("clockSet.start2.val=%d",device_status.culture_sh2);
  193. //采集结束时间1
  194. HMI_WRITE("clockSet.end1.val=%d",device_status.culture_eh1);
  195. //采集结束时间2
  196. HMI_WRITE("clockSet.end2.val=%d",device_status.culture_eh2);
  197. //更新联网状态
  198. if(device_status.net==1)
  199. {
  200. HMI_WRITE("workSta.net.val=1");
  201. }
  202. else {
  203. HMI_WRITE("workSta.net.val=0");
  204. }
  205. if(device_status.csq==1)
  206. {
  207. HMI_WRITE("workSta.net.val=2");
  208. }
  209. //更新CSQ
  210. static char csq_str[10];
  211. itoa(device_status.csq,csq_str,10);
  212. HMI_WRITE("workSta.csq.txt=\"%s\"",csq_str);
  213. //更新加热状态
  214. HMI_WRITE("workSta.heatStatus.val=%d",device_status.heat_status);
  215. //更新熄屏时间
  216. HMI_WRITE("sysSet.lcd_time.thsp=%d",device_status.thsp/60);
  217. HMI_WRITE("thsp=%d",device_status.thsp);
  218. //更新密码
  219. static char pwd_char[10];
  220. //整型转字符串
  221. itoa(device_status.passWord,pwd_char,10);
  222. HMI_WRITE("inputPasswd.pwdData.txt=\"%s\"",pwd_char);
  223. //SIM卡状态
  224. HMI_WRITE("workSta.bt0.val=%d",device_status.sim);
  225. //ID
  226. HMI_WRITE("workSta.equipid.txt=\"%s\"",device_status.imei);
  227. //数据上传间隔
  228. HMI_WRITE("sysSet.datt.val=%d",device_status.dattim);
  229. //语音音量
  230. HMI_WRITE("sysSet.vol.val=%d",device_status.vol);
  231. //固件版本
  232. HMI_WRITE("debug.t3.txt=\"%s\"",VERSION);
  233. //编译时间
  234. HMI_WRITE("debug.t4.txt=\"%s\"",COMPILE);
  235. }
  236. else if (strstr(rx_buffer,"button")!=NULL) {
  237. rt_kprintf("按键!\n");
  238. DTU_Write("button");
  239. }
  240. //拍照
  241. else if(strstr(rx_buffer,"putTest.switchPic=1")!=NULL) {
  242. rt_kprintf("\n测试拍照!\n");
  243. HK_TakePhoto();
  244. }
  245. //转盘复位
  246. else if (strstr(rx_buffer,"turn_init")!=NULL) {
  247. rt_kprintf("转仓复位!\n");
  248. ctr_turn(0);
  249. }
  250. //转盘
  251. else if (strstr(rx_buffer,"turn")!=NULL) {
  252. rt_kprintf("转仓!\n");
  253. ctr_turn(90);
  254. }
  255. //打开电磁阀
  256. else if (strstr(rx_buffer,"putTest.switchDrop=1")!=NULL) {
  257. LOG_I("开始滴液!\n");
  258. ctr_DiYe(device_status.drop_sec);
  259. }
  260. //关闭电磁阀
  261. else if (strstr(rx_buffer,"putTest.switchDrop=0")!=NULL) {
  262. LOG_I("停止滴液!\n");
  263. ctr_DiYe(OFF);
  264. }
  265. //打开补光
  266. else if (strstr(rx_buffer,"putTest.switchLed=1")!=NULL) {
  267. rt_kprintf("\n打开补光!!!\n");
  268. ctr_led(ON);
  269. }
  270. //关闭补光
  271. else if (strstr(rx_buffer,"putTest.switchLed=0")!=NULL) {
  272. rt_kprintf("\n关闭补光!!!\n");
  273. ctr_led(OFF);
  274. }
  275. //关闭加热
  276. else if(strstr(rx_buffer,"putTest.switchHeat=0")!=NULL) {
  277. device_status.heat_status = 0;
  278. ctr_Heat(OFF);
  279. rt_kprintf("Close Heat!\n");
  280. }
  281. //打开加热
  282. else if(strstr(rx_buffer,"putTest.switchHeat=1")!=NULL) {
  283. device_status.heat_status = 1;
  284. ctr_Heat(ON);
  285. rt_kprintf("Open Heat!\n");
  286. }
  287. //打开制冷
  288. else if(strstr(rx_buffer,"putTest.switchCold=1")!=NULL) {
  289. ctr_Cold(ON);
  290. rt_kprintf("Open Cold!\n");
  291. }
  292. //关闭制冷
  293. else if(strstr(rx_buffer,"putTest.switchCold=0")!=NULL) {
  294. ctr_Cold(OFF);
  295. rt_kprintf("Close Cold!\n");
  296. }
  297. //打开采集
  298. else if(strstr(rx_buffer,"putTest.switchCollect=1")!=NULL){
  299. ctr_Collect_Fan(ON);
  300. rt_kprintf("采集风机已打开!\n");
  301. }
  302. //关闭采集
  303. else if(strstr(rx_buffer,"putTest.switchCollect=0")!=NULL){
  304. ctr_Collect_Fan(OFF);
  305. rt_kprintf("采集风机已关闭!\n");
  306. }
  307. //打开散热
  308. else if(strstr(rx_buffer,"putTest.switchCool=1")!=NULL){
  309. ctr_Cool_Fan(ON);
  310. rt_kprintf("散热风机已打开!\n");
  311. }
  312. //关闭散热
  313. else if(strstr(rx_buffer,"putTest.switchCool=0")!=NULL){
  314. ctr_Cool_Fan(OFF);
  315. rt_kprintf("散热风机已关闭!\n");
  316. }
  317. //步进上行开始
  318. else if(strstr(rx_buffer,"putTest.switchUp=1")!=NULL){
  319. ctr_Motor_Driver(1, 0,500000,200);
  320. rt_kprintf("开始步进上行!\n");
  321. }
  322. //步进上行停止
  323. else if(strstr(rx_buffer,"putTest.switchUp=0")!=NULL){
  324. ctr_Motor_Driver(0, 0,0,0);
  325. rt_kprintf("停止步进上行!\n");
  326. }
  327. //步进下行
  328. else if(strstr(rx_buffer,"putTest.switchDown=1")!=NULL){
  329. ctr_Motor_Driver(1, 1,500000,200);
  330. rt_kprintf("开始步进下行!\n");
  331. }
  332. //步进下行停止
  333. else if(strstr(rx_buffer,"putTest.switchDown=0")!=NULL){
  334. ctr_Motor_Driver(0, 0,0,0);
  335. rt_kprintf("停止步进下行!\n");
  336. }
  337. //开始对焦拍照
  338. else if(strstr(rx_buffer,"putTest.switchAutopic=1")!=NULL){
  339. device_status.autotakepic=1;
  340. rt_kprintf("开始对焦拍照!\n");
  341. }
  342. //停止对焦拍照
  343. else if(strstr(rx_buffer,"putTest.switchAutopic=0")!=NULL){
  344. device_status.autotakepic=0;
  345. }
  346. //粘附测试-开
  347. else if(strstr(rx_buffer,"putTest.switchFsl=1")!=NULL){
  348. rt_kprintf("粘附测试开!\n");
  349. device_status.fsl_sw = 1;
  350. }
  351. //粘附测试-关
  352. else if(strstr(rx_buffer,"putTest.switchFsl=0")!=NULL){
  353. rt_kprintf("粘附测试关!\n");
  354. pwm_off(2, 0);
  355. pwm_off(3,0);
  356. }
  357. //定时设置
  358. else if (strstr(rx_buffer,"saveClockSet")!=NULL) {
  359. rt_kprintf("时间设置!\n");
  360. var = strstr(rx_buffer,"=");
  361. var ++;
  362. device_status.culture_sh1 = *((int *)var); //采集1开始
  363. ef_set_env_blob("culture_sh1", &device_status.culture_sh1, sizeof(device_status.culture_sh1));
  364. var+=4;
  365. device_status.culture_eh1 = *((int *)var); //采集1结束
  366. ef_set_env_blob("culture_eh1", &device_status.culture_eh1, sizeof(device_status.culture_eh1));
  367. var+=4;
  368. device_status.culture_sh2 = *((int *)var); //采集2开始
  369. ef_set_env_blob("culture_sh2", &device_status.culture_sh2, sizeof(device_status.culture_sh2));
  370. var+=4;
  371. device_status.culture_eh2 = *((int *)var); //采集2结束
  372. ef_set_env_blob("culture_eh2", &device_status.culture_eh2, sizeof(device_status.culture_eh2));
  373. var+=4;
  374. device_status.drop_sec = *((int *)var); //滴液时间
  375. ef_set_env_blob("drop_sec", &device_status.drop_sec, sizeof(device_status.drop_sec));
  376. var+=4;
  377. device_status.culture_time = *((int *)var); //培养时间
  378. ef_set_env_blob("culture_time", &device_status.culture_time, sizeof(device_status.culture_time));
  379. MA_save();
  380. rt_kprintf("参数已保存:%d-%d-%d-%d-%d-%d",device_status.culture_sh1,device_status.culture_eh1,device_status.culture_sh2,device_status.culture_eh2,device_status.drop_sec,device_status.culture_time);
  381. }
  382. else if (strstr(rx_buffer,"saveSystemSet")!=NULL) {
  383. rt_kprintf("系统设置!\n");
  384. var = strstr(rx_buffer,"=");
  385. var ++;
  386. device_status.cold_sw = *((int *)var); //制冷开关
  387. ef_set_env_blob("cold_sw", &device_status.cold_sw, sizeof(device_status.cold_sw));
  388. var+=4;
  389. device_status.vol = *((int *)var); //音量
  390. var+=4;
  391. device_status.set_tempture = *((int *)var); //培养温度
  392. ef_set_env_blob("set_tempture", &device_status.set_tempture, sizeof(device_status.set_tempture));
  393. var+=4;
  394. device_status.dattim = *((int *)var); //数据上传间隔
  395. ef_set_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim));
  396. var+=4;
  397. device_status.thsp = *((int *)var); //自动熄屏
  398. ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
  399. MA_save();
  400. rt_kprintf("制冷开关:%d\n",device_status.cold_sw);
  401. rt_kprintf("系统设置已保存:自动熄屏-%d,音量-%d,培养温度-%d,数据上传间隔-%d\n",device_status.thsp,device_status.vol,device_status.culture_tempture,device_status.dattim);
  402. }
  403. //恢复出厂设置
  404. else if (strstr(rx_buffer,"resetDefault")!=NULL)
  405. {
  406. ResetParamDefault();
  407. MA_save();
  408. rt_kprintf("恢复出厂设置!!!");
  409. }
  410. //获取SIM卡信息
  411. else if(strstr(rx_buffer,"getSim")!=NULL)
  412. {
  413. //http://sim.openluat.com/api/wechat/openapidoc
  414. DTU_Write("getsiminfo");
  415. }
  416. else if (strstr(rx_buffer,"DebugEnable")!=NULL) {
  417. Debug_log_ON();
  418. }
  419. else if (strstr(rx_buffer,"DebugDisable")!=NULL) {
  420. Debug_log_OFF();
  421. }
  422. else if (strstr(rx_buffer,"putTest.switchAutotest=1")!=NULL)
  423. {
  424. rt_kprintf("开始一键测试!!!\n");
  425. device_status.auto_test_sw=1;
  426. }
  427. else if (strstr(rx_buffer,"putTest.switchAutotest=1")!=NULL)
  428. {
  429. rt_kprintf("停止一键测试!!!\n");
  430. device_status.auto_test_sw=0;
  431. }
  432. else if(strstr(rx_buffer,"DisplayPower")!=RT_NULL)
  433. {
  434. HMI_WRITE("sysTest.sysTest_power.txt=\"%s\"",device_status.power);
  435. }
  436. //进入bootloader模式
  437. else if (strstr(rx_buffer,"bootloader")!=NULL) {
  438. device_status.stop_in_bootloader = 1;
  439. ef_set_env_blob("stop_in_bootloader", &device_status.stop_in_bootloader, sizeof(device_status.stop_in_bootloader));
  440. rt_hw_cpu_reset();
  441. }
  442. //软件重启
  443. else if (strstr(rx_buffer,"reboot")!=NULL) {
  444. rt_hw_cpu_reset();
  445. }
  446. }
  447. //入口函数-对焦拍照
  448. static void Ctr_Mortor_entry(void* parameter)
  449. {
  450. rt_uint8_t photo_setup = 0;
  451. while(1)
  452. {
  453. if(device_status.autotakepic==1)
  454. {
  455. //打开补光灯
  456. ctr_led(ON);
  457. //电机向下运动
  458. ctr_Motor_Driver(1, 1, 500000,200);
  459. rt_thread_delay(1000);
  460. LOG_I("电机到达下方!!!");
  461. HK_TakePhoto();
  462. rt_thread_delay(1000);
  463. LOG_I("电机开始向上!!!");
  464. rt_pin_write(Motor_EN, 1);
  465. rt_pin_write(Motor_DIR, 0);
  466. beep_set(500000, 500000/5); //2kzh - 50%
  467. while(rt_pin_read(LIMIT_UP)==1)
  468. {
  469. rt_thread_delay(10);
  470. photo_setup++;
  471. rt_pin_write(Motor_EN, 0); //驱动器失能
  472. HK_TakePhoto();
  473. rt_thread_delay(1000);
  474. rt_pin_write(Motor_EN, 1); //驱动器使能
  475. if(photo_setup==100)
  476. {
  477. ctr_led(OFF);
  478. device_status.autotakepic=0;
  479. rt_pin_write(Motor_EN, 0); //驱动器失能
  480. beep_set(500000, 0); //2kzh - 50%
  481. HMI_WRITE("sysTest.switchAutopic.val=%d",0);
  482. break;
  483. }
  484. rt_kprintf("对焦拍照:%d\n",photo_setup);
  485. }
  486. ctr_led(OFF);
  487. rt_pin_write(Motor_EN, 0); //驱动器失能
  488. beep_set(500000, 0); //2kzh - 50%
  489. HMI_WRITE("sysTest.switchAutopic.val=%d",0);
  490. char w_temp[20];
  491. rt_sprintf(w_temp,"%s%d","autoTakePhoto success!!!",photo_setup);
  492. char *autotake_warn = makeJson_Warn(w_temp, "Motor");
  493. DTU_Write("%s",autotake_warn);
  494. rt_free(autotake_warn);
  495. device_status.autotakepic=0;
  496. photo_setup = 0;
  497. HMI_WRITE("sysTest.switchAutopic.val=%d",0);
  498. }
  499. if(device_status.fsl_sw==1)
  500. {
  501. ctr_Petroleum_jelly(10);
  502. }
  503. //一键测试
  504. if(device_status.auto_test_sw==1)
  505. {
  506. //转盘复位
  507. rt_kprintf("转盘复位!!!\n");
  508. rt_thread_delay(2000);
  509. ctr_turn(0);
  510. //转90
  511. rt_kprintf("第一次转盘!!!\n");
  512. ctr_turn(90);
  513. rt_thread_delay(2000);
  514. //推杆电机:测试粘附:
  515. rt_kprintf("涂凡士林!!!\n");
  516. ctr_Petroleum_jelly(10);
  517. rt_thread_delay(2000);
  518. //转90
  519. rt_kprintf("第二次转盘!!!\n");
  520. ctr_turn(90);
  521. rt_thread_delay(2000);
  522. //采集,打开风机
  523. rt_kprintf("采集,打开风机!!!\n");
  524. ctr_Collect_Fan(ON);
  525. rt_thread_delay(5000);
  526. rt_kprintf("采集结束,关闭风机!!!\n");
  527. rt_thread_delay(2000);
  528. ctr_Collect_Fan(OFF);
  529. //散热
  530. rt_kprintf("打开散热!!!\n");
  531. rt_thread_delay(2000);
  532. ctr_Cool_Fan(ON);
  533. rt_thread_delay(5000);
  534. rt_kprintf("关闭散热!!!\n");
  535. rt_thread_delay(2000);
  536. ctr_Cool_Fan(OFF);
  537. //转90
  538. rt_kprintf("第三次转盘!!!\n");
  539. ctr_turn(90);
  540. rt_thread_delay(5000);
  541. //滴液
  542. rt_kprintf("开始滴液!!!\n");
  543. rt_thread_delay(2000);
  544. ctr_DiYe(device_status.drop_sec);
  545. rt_thread_delay(5000);
  546. rt_kprintf("停止滴液!!!\n");
  547. rt_thread_delay(2000);
  548. //加热
  549. rt_kprintf("开始加热!!!\n");
  550. rt_thread_delay(2000);
  551. ctr_Heat(ON);
  552. rt_thread_delay(5000);
  553. rt_kprintf("停止加热!!!\n");
  554. rt_thread_delay(2000);
  555. ctr_Heat(OFF);
  556. //制冷
  557. rt_kprintf("开始制冷!!!\n");
  558. rt_thread_delay(2000);
  559. ctr_Cold(ON);
  560. rt_thread_delay(5000);
  561. rt_kprintf("停止制冷!!!\n");
  562. rt_thread_delay(2000);
  563. ctr_Cold(OFF);
  564. //转90
  565. rt_kprintf("第四次转盘!!!\n");
  566. ctr_turn(90);
  567. rt_kprintf("打开补光灯!!!\n");
  568. rt_thread_delay(2000);
  569. ctr_led(ON);
  570. device_status.autotakepic=1;
  571. device_status.auto_test_sw=0;
  572. }
  573. rt_thread_delay(100);
  574. }
  575. }
  576. /*屏幕写数据*/
  577. int Hmi_Fresh(void)
  578. {
  579. //初始化串口4
  580. rt_err_t ret = RT_EOK;
  581. /* 初始化消息队列 */
  582. rt_mq_init(&rx_mq, "rx_mq",
  583. msg_pool, /* 存放消息的缓冲区 */
  584. sizeof(struct rx_msg), /* 一条消息的最大长度 */
  585. sizeof(msg_pool), /* 存放消息的缓冲区大小 */
  586. RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
  587. /* 设置接收回调函数 */
  588. rt_device_set_rx_indicate(serial, uart_input);
  589. /* 创建线程*/
  590. hmi1 = rt_thread_create("Hmi_Fresh_thread",
  591. Hmi_thread_entry,
  592. RT_NULL,
  593. HMI_THREAD_STACK_SIZE,
  594. HMI_THREAD_PRIORITY,
  595. HMI_THREAD_TIMESLICE);
  596. /*打开上仓门的线程*/
  597. ctr_motor = rt_thread_create("Ctr motor",
  598. Ctr_Mortor_entry,
  599. RT_NULL,
  600. CONTROL_MOTOR_STACK_SIZE,
  601. CONTROL_MOTOR_PRIORITY,
  602. CONTROL_MOTOR_TIMESLICE);
  603. if(ctr_motor != RT_NULL)
  604. {
  605. rt_thread_startup(ctr_motor);
  606. }
  607. /* 如果获得线程控制块,启动这个线程 */
  608. if (hmi1 != RT_NULL)
  609. {
  610. rt_thread_startup(hmi1);
  611. }
  612. else
  613. {
  614. ret = RT_ERROR;
  615. }
  616. return ret;
  617. }
  618. void Check_ParamConfig(void)
  619. {
  620. Hmi_Fresh();
  621. time_t now = time(RT_NULL);
  622. static struct tm tm;
  623. gmtime_r(&now, &tm);
  624. if(easyflash_init()==EF_NO_ERR) //初始化成功
  625. {
  626. ef_get_env_blob("reboot_time", &device_status.rebootcnt, sizeof(device_status.rebootcnt), NULL); //读出reboot_time的值
  627. ef_get_env_blob("update_flag", &device_status.update_flag, sizeof(device_status.update_flag), NULL); //读出updateflag的值
  628. ef_get_env_blob("iSave", &device_status.iSave, sizeof(device_status.iSave), NULL); //判断是否用户配置了程序
  629. device_status.rebootcnt++;
  630. ef_set_env_blob("reboot_time", &device_status.rebootcnt, sizeof(device_status.rebootcnt));
  631. rt_kprintf("REBOOT_TIME:%d-%d-%d\n",device_status.rebootcnt,device_status.iSave,device_status.update_flag);
  632. HMI_WRITE("hardcheck.flash.txt=\"READ FLASH OK\"");
  633. HMI_WRITE("hardcheck.p8.pic=%d",70);
  634. }
  635. else {
  636. LOG_W("easyflash init failed!!!");
  637. HMI_WRITE("hardcheck.flash.txt=\"ERR\"");
  638. HMI_WRITE("hardcheck.p8.pic=%d",72);
  639. }
  640. //保存默认屏幕配置
  641. if(device_status.iSave!=FLASH_FLAG)
  642. {
  643. app_partion_backup();
  644. device_status.iSave=FLASH_FLAG;
  645. device_status.culture_time = 1; //设定培养时间-1小时
  646. device_status.culture_tempture = 25; //设定培养仓温度
  647. device_status.thsp = 300; //默认熄屏时间5分钟
  648. device_status.drop_sec = 1; //滴液时间,s
  649. device_status.cold_sw = 0; //制冷开关,默认关
  650. device_status.culture_sh1 = 10;
  651. device_status.culture_eh1 = 12;
  652. device_status.culture_sh2 = 13;
  653. device_status.culture_eh2 = 17;
  654. device_status.culture_sh3 = 17;
  655. device_status.culture_eh3 = 17;
  656. device_status.culture_time = 1;
  657. device_status.vol = 7; //音量
  658. device_status.passWord = 8888; //屏幕密码
  659. device_status.work_done = 0; //工作完成标志位
  660. device_status.dattim = 20; //数据上传间隔
  661. device_status.ds = 1; //设备状态
  662. device_status.dtype = 7; //版本类型
  663. ef_set_env_blob("iSave", &device_status.iSave, sizeof(device_status.iSave)); //
  664. ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
  665. ef_set_env_blob("cold_sw", &device_status.cold_sw, sizeof(device_status.cold_sw));
  666. ef_set_env_blob("culture_sh1", &device_status.culture_sh1, sizeof(device_status.culture_sh1));
  667. ef_set_env_blob("culture_sh2", &device_status.culture_sh2, sizeof(device_status.culture_sh2));
  668. ef_set_env_blob("culture_sh3", &device_status.culture_sh3, sizeof(device_status.culture_sh3));
  669. ef_set_env_blob("culture_eh1", &device_status.culture_eh1, sizeof(device_status.culture_eh1));
  670. ef_set_env_blob("culture_eh2", &device_status.culture_eh2, sizeof(device_status.culture_eh2));
  671. ef_set_env_blob("culture_eh3", &device_status.culture_eh3, sizeof(device_status.culture_eh3));
  672. ef_set_env_blob("culture_time", &device_status.culture_time, sizeof(device_status.culture_time));
  673. ef_set_env_blob("culture_tempture", &device_status.culture_tempture, sizeof(device_status.culture_tempture));
  674. ef_set_env_blob("drop_sec", &device_status.drop_sec, sizeof(device_status.drop_sec)); //震动时间
  675. ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
  676. ef_set_env_blob("passWord", &device_status.passWord,sizeof(device_status.passWord));
  677. ef_set_env_blob("lat",&device_status.lat,sizeof(device_status.lat)); //纬度
  678. ef_set_env_blob("lng",&device_status.lng,sizeof(device_status.lng)); //经度
  679. ef_set_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim)); //上传时间
  680. sprintf(device_status.driver, "%s",VERSION);
  681. LOG_I("SOFTWARE VERSION:%s\n",device_status.driver);
  682. rt_kprintf("set flash paramconfis:\n");
  683. rt_kprintf("iSave:%d\n",device_status.iSave);
  684. rt_kprintf("culture_time:%d\n",device_status.culture_time);
  685. rt_kprintf("jheat:%d\n",device_status.culture_tempture);
  686. rt_kprintf("thsp:%d\n", device_status.thsp);
  687. rt_kprintf("密码:%d\n",device_status.passWord);
  688. }//从easyflash读取参数配置
  689. else if (device_status.iSave==FLASH_FLAG) {
  690. ef_get_env_blob("culture_sh1",&device_status.culture_sh1,sizeof(device_status.culture_sh1),NULL); //采集开始时间1
  691. ef_get_env_blob("culture_sh2",&device_status.culture_sh2,sizeof(device_status.culture_sh2),NULL); //采集开始时间2
  692. ef_get_env_blob("culture_sh3",&device_status.culture_sh3,sizeof(device_status.culture_sh3),NULL); //采集开始时间2
  693. ef_get_env_blob("culture_eh1",&device_status.culture_eh1,sizeof(device_status.culture_eh1),NULL); //采集结束时间1
  694. ef_get_env_blob("culture_eh2",&device_status.culture_eh2,sizeof(device_status.culture_eh2),NULL); //采集结束时间2
  695. ef_get_env_blob("culture_eh3",&device_status.culture_eh3,sizeof(device_status.culture_eh3),NULL); //采集结束时间3
  696. ef_get_env_blob("cold_sw",&device_status.cold_sw,sizeof(device_status.cold_sw),NULL); //数据上传间隔
  697. ef_get_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim),NULL); //数据上传间隔
  698. ef_get_env_blob("culture_time", &device_status.culture_time, sizeof(device_status.culture_time), NULL); //培养时间
  699. ef_get_env_blob("culture_tempture", &device_status.culture_tempture, sizeof(device_status.culture_tempture), NULL); //设定培养温度
  700. ef_get_env_blob("drop_sec", &device_status.drop_sec, sizeof(device_status.drop_sec), NULL); //滴液时间
  701. ef_get_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim), NULL); //数据上传间隔
  702. ef_get_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp), NULL); //熄屏时间
  703. ef_get_env_blob("passWord",&device_status.passWord,sizeof(device_status.passWord),NULL); //密码
  704. ef_get_env_blob("imei",&device_status.imei,sizeof(device_status.imei),NULL); //IMEI
  705. ef_get_env_blob("iccid",&device_status.iccid,sizeof(device_status.iccid),NULL); //ICCID
  706. ef_get_env_blob("ds",&device_status.ds,sizeof(device_status.ds),NULL); //开关机状态
  707. ef_get_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type),NULL); //版本类型
  708. device_status.work_done = 0;
  709. //工作完成标志位
  710. if(device_status.dattim==0)
  711. {
  712. device_status.dattim = 20;
  713. }
  714. if(device_status.version_type==1)
  715. {
  716. app_partion_backup(); //固件备份至df_area
  717. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type));
  718. }
  719. rt_sprintf(device_status.driver, "%s",VERSION);
  720. LOG_I("SOFTWARE VERSION:%s\n",device_status.driver);
  721. rt_kprintf("read flash paramconfis:%d\r\n",device_status.version_type);
  722. rt_kprintf("开机次数:%d\n",device_status.rebootcnt);
  723. rt_kprintf("收集时间:%d\n",device_status.culture_time);
  724. rt_kprintf("加热温度:%d\n",device_status.culture_tempture);
  725. rt_kprintf("震动时间:%d\n",device_status.drop_sec);
  726. rt_kprintf("thsp:%d\n", device_status.thsp);
  727. rt_kprintf("密码:%d\n",device_status.passWord);
  728. rt_kprintf("数据上传间隔:%d\n",device_status.dattim);
  729. }
  730. 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);
  731. 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);
  732. HMI_WRITE("hardcheck.p0.pic=%d",70);
  733. //RTC时间判断,如果当前年份小于2020,提示需要设置时间,切换为光控
  734. if(tm.tm_year+1900<2020)
  735. {
  736. LOG_E("Time not set:%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);
  737. 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);
  738. HMI_WRITE("hardcheck.p0.pic=%d",71);
  739. set_date(2020, 1, 1);
  740. set_time(0, 0, 0);
  741. }
  742. if(device_status.update_flag==1)
  743. {
  744. device_status.update_flag = 0;
  745. ef_set_env_blob("update_flag", &device_status.update_flag, sizeof(device_status.update_flag));
  746. ymodem_ota(1,0);
  747. }
  748. }
  749. void ResetParamDefault(void)
  750. {
  751. device_status.culture_time = 1; //培养时间
  752. device_status.dattim = 20; //数据上传间隔
  753. device_status.drop_sec = 1; //滴液时间1-100ms
  754. device_status.culture_tempture = 25; //培养温度
  755. device_status.ds = 1; //设备状态开机
  756. device_status.thsp = 300;
  757. device_status.passWord = 8888;
  758. device_status.culture_sh1 = 10;
  759. device_status.culture_eh1 = 12;
  760. device_status.culture_sh2 = 12;
  761. device_status.culture_eh2 = 17;
  762. ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
  763. ef_set_env_blob("culture_sh1", &device_status.culture_sh1, sizeof(device_status.culture_sh1));
  764. ef_set_env_blob("culture_eh1", &device_status.culture_eh1, sizeof(device_status.culture_eh1));
  765. ef_set_env_blob("culture_sh2", &device_status.culture_sh2, sizeof(device_status.culture_sh2));
  766. ef_set_env_blob("culture_eh2", &device_status.culture_eh2, sizeof(device_status.culture_eh2));
  767. ef_set_env_blob("culture_time", &device_status.culture_time, sizeof(device_status.culture_time));
  768. ef_set_env_blob("culture_tempture", &device_status.culture_tempture, sizeof(device_status.culture_tempture));
  769. ef_set_env_blob("drop_sec", &device_status.drop_sec, sizeof(device_status.drop_sec)); //震动时间
  770. ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
  771. ef_set_env_blob("passWord", &device_status.passWord,sizeof(device_status.passWord));
  772. }
  773. //固件备份
  774. void app_partion_backup(void)
  775. {
  776. rt_kprintf("固件备份!!!\n");
  777. const char *dst_part_name = "df_area";
  778. const char *src_part_name = "app";
  779. const struct fal_partition *dst_part;
  780. const struct fal_partition *src_part;
  781. dst_part = fal_partition_find(dst_part_name);
  782. src_part = fal_partition_find(src_part_name);
  783. if (dst_part == RT_NULL || src_part == RT_NULL)
  784. {
  785. if (dst_part == RT_NULL)
  786. rt_kprintf("%s partition is not exist!\n", dst_part_name);
  787. if (src_part == RT_NULL)
  788. rt_kprintf("%s partition is not exist!\n", src_part_name);
  789. }
  790. else
  791. {
  792. rt_kprintf("Clone %s partition to %s partition:\n", src_part_name, dst_part_name);
  793. if (fal_partition_erase(dst_part, 0, dst_part->len) >= 0)
  794. {
  795. int clone_pos = 0;
  796. int clone_len = 0, clone_tol_len;
  797. rt_uint8_t *buf = rt_malloc(4096);
  798. if (dst_part->len < src_part->len)
  799. clone_tol_len = dst_part->len;
  800. else
  801. clone_tol_len = src_part->len;
  802. while ((clone_pos < clone_tol_len) && (buf != RT_NULL))
  803. {
  804. clone_len = fal_partition_read(src_part, clone_pos, buf, 4096);
  805. if (clone_len < 0)
  806. {
  807. rt_kprintf("\nread %s partition failed, clone stop!\n", src_part_name);
  808. break;
  809. }
  810. if (fal_partition_write(dst_part, clone_pos, buf, clone_len) < 0)
  811. {
  812. rt_kprintf("\nwrite %s partition failed, clone stop!\n", dst_part_name);
  813. break;
  814. }
  815. HMI_WRITE("#");
  816. rt_kprintf("#");
  817. clone_pos += clone_len;
  818. }
  819. if (clone_pos >= clone_tol_len)
  820. {
  821. rt_kprintf("\nClone partition success, total %d bytes!\n", clone_tol_len);
  822. }
  823. else
  824. {
  825. rt_kprintf("Clone partition failed!");
  826. }
  827. if (buf)
  828. rt_free(buf);
  829. }
  830. }
  831. }
  832. void Debug_log_ON(void)
  833. {
  834. rt_console_set_device("uart4");
  835. finsh_set_device("uart4");
  836. }
  837. void Debug_log_OFF(void)
  838. {
  839. rt_console_set_device("uart1");
  840. finsh_set_device("uart1");
  841. rt_mq_init(&rx_mq, "rx_mq",
  842. msg_pool, /* 存放消息的缓冲区 */
  843. sizeof(struct rx_msg), /* 一条消息的最大长度 */
  844. sizeof(msg_pool), /* 存放消息的缓冲区大小 */
  845. RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
  846. rt_device_set_rx_indicate(serial, uart_input);
  847. hmi1 = rt_thread_create("Hmi_Fresh_thread",
  848. Hmi_thread_entry,
  849. RT_NULL,
  850. HMI_THREAD_STACK_SIZE,
  851. HMI_THREAD_PRIORITY,
  852. HMI_THREAD_TIMESLICE);
  853. if (hmi1 != RT_NULL)
  854. {
  855. rt_thread_startup(hmi1);
  856. }
  857. }
  858. MSH_CMD_EXPORT(Debug_log_ON,Debug_log_ON);
  859. MSH_CMD_EXPORT(Debug_log_OFF,Debug_log_OFF);
  860. INIT_BOARD_EXPORT(HMI_uart4_init);