hmi.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  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. #include "app_flashdb.h"
  32. #define DBG_ENABLE
  33. #define DBG_COLOR
  34. #define DBG_TAG "hmi"
  35. #define DBG_LEVEL DBG_LOG
  36. #include <rtdbg.h> //must after of DBG_LVL, DBG_TAG or other options
  37. #define HMI_THREAD_PRIORITY 5 //线程优先级
  38. #define HMI_THREAD_STACK_SIZE 2048 //线程栈空间大小
  39. #define HMI_THREAD_TIMESLICE 10 //时间片
  40. #define CONTROL_MOTOR_PRIORITY 7
  41. #define CONTROL_MOTOR_STACK_SIZE 2048
  42. #define CONTROL_MOTOR_TIMESLICE 5
  43. static char msg_pool[512];
  44. static char rx_buffer[HMI_UART_RXBUFFER_SIZE + 1];
  45. void list_mem(void);
  46. int mb_master_samlpe();
  47. /**/
  48. Device_Status_typedef device_status;
  49. Device_slide_typedef slide_para;
  50. time_t time(time_t *t);
  51. /* 串口接收消息结构*/
  52. struct rx_msg
  53. {
  54. rt_device_t dev;
  55. rt_size_t size;
  56. };
  57. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; /*初始化配置参数*/
  58. ////
  59. static rt_thread_t hmi1 = RT_NULL;
  60. static rt_thread_t ctr_motor = RT_NULL;
  61. rt_device_t serial; /*串口设备句柄*/
  62. rt_timer_t timer_uart2 = RT_NULL;
  63. /* 消息队列控制块 */
  64. static struct rt_messagequeue rx_mq;//接收数据消息队列
  65. static void HMI_Decode(void);
  66. static rt_err_t uart_input(rt_device_t dev, rt_size_t size);
  67. static int HMI_uart4_init(void)
  68. {
  69. serial = rt_device_find(UART_NAME);
  70. /*修改串口配置参数*/
  71. config.baud_rate = BAUD_RATE_115200; //修改波特率9600
  72. config.data_bits = DATA_BITS_8; //数据位8
  73. config.stop_bits = STOP_BITS_1; //停止位1
  74. #ifdef RT_USING_SERIAL_V2
  75. config.rx_bufsz = 512;
  76. config.tx_bufsz = 128;
  77. #else
  78. config.bufsz = 512; //缓冲区128字节
  79. #endif
  80. config.parity = PARITY_NONE; //无奇偶校验
  81. /* step3:控制串口设备。通过控制接口传入命令控制字,与控制参数 */
  82. rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
  83. // /* step4:打开串口设备。以中断接收及轮询发送模式打开串口设备 */
  84. //rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
  85. /* 以 DMA 接收及轮询发送模式打开串口设备 */
  86. #ifdef RT_USING_SERIAL_V2
  87. rt_err_t uart_res = rt_device_open(serial, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING); // 串口设备使用模式为 (发送阻塞 接收非阻塞) 模式
  88. #else
  89. rt_err_t uart_res = rt_device_open(serial, RT_DEVICE_FLAG_DMA_RX); //RT_DEVICE_FLAG_DMA_RX
  90. #endif
  91. if(uart_res!=RT_EOK)
  92. {
  93. LOG_E("%s打开失败!",UART_NAME);
  94. }else{
  95. LOG_I("%s Open Success!",UART_NAME);
  96. }
  97. return RT_EOK;
  98. }
  99. /*串口屏写数据*/
  100. void HMI_WRITE(char *fmt, ...)
  101. {
  102. va_list ap;
  103. //末尾需要拼接的3个ff
  104. const char hmi_hex[3]={0xff,0xff,0xff};
  105. static char str[MAX_SIZE];
  106. memset(str,'\0',sizeof(str));
  107. rt_size_t str_length;
  108. va_start(ap,fmt);
  109. str_length = rt_vsnprintf(str, sizeof(str) - 1, fmt, ap);
  110. if (str_length > MAX_SIZE - 1)
  111. str_length = MAX_SIZE - 1;
  112. rt_device_write(serial, 0, str, str_length);
  113. va_end(ap);
  114. rt_device_write(serial, 0,hmi_hex,3);
  115. }
  116. /* 接收数据回调函数 */
  117. static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
  118. {
  119. struct rx_msg msg;
  120. rt_err_t result;
  121. msg.dev = dev;
  122. msg.size = size;
  123. result = rt_mq_send(&rx_mq, &msg, sizeof(msg));
  124. if ( result == -RT_EFULL)
  125. {
  126. /* 消息队列满 */
  127. LOG_I("hmi message queue full!\n");
  128. }
  129. return result;
  130. }
  131. /*入口函数*/
  132. static void Hmi_thread_entry(void *parameter)
  133. {
  134. struct rx_msg msg;
  135. rt_err_t result;
  136. rt_uint32_t rx_length;
  137. if(device_status.dtype==3){
  138. device_status.collt_box_sta = 0;
  139. device_status.run_sta = 0;
  140. }
  141. device_status.camsta = 1;
  142. while(1)
  143. {
  144. rt_memset(&msg, 0, sizeof(msg));
  145. /* 从消息队列中读取消息*/
  146. result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), 50);
  147. if (result == RT_EOK)
  148. {
  149. rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size);
  150. rx_buffer[rx_length] = '\0';
  151. //LOG_I("%s",rx_buffer);
  152. //\r 对应的hex为0x0d,0x0a,占两个字节,屏幕发送的数据都以\r结尾
  153. //HMI_Decode();
  154. }
  155. /*********************************************************************************************************************/
  156. //rt_kprintf("%04d-%02d-%02d %02d:%02d:%02d\n",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  157. //rt_kprintf("workSta.vbat.txt=%c%d.%02d%c\n",hmi_str,voltage/100,voltage%100,hmi_str);
  158. }
  159. }
  160. /*******************************************************************************************/
  161. static void t_uart2_callback(void *parameter)
  162. {
  163. HMI_WRITE("sysSet.va0.txt=\"指令超时,请重试...\"");
  164. LOG_E("t_uart2_callback");
  165. }
  166. void Start_timer_Uart2(void)
  167. {
  168. if(timer_uart2!=RT_NULL)
  169. {
  170. rt_timer_start(timer_uart2);
  171. }
  172. else
  173. {
  174. LOG_E("timer_uart2 start Failed!");
  175. }
  176. }
  177. /***************************** 解析从屏幕收到的数据 begin **********************************/
  178. #define EPS 1e-3
  179. // 找出一维数组中最大数的索引
  180. int findMaxIndex(double arr[], int size) {
  181. int maxIndex = 0; // 假设最大值的索引为0
  182. for (int i = 1; i < size; i++) {
  183. if ((arr[i] - arr[maxIndex])>EPS) {
  184. maxIndex = i; // 更新最大值的索引
  185. }
  186. }
  187. return maxIndex;
  188. }
  189. void send_signal(int sig)
  190. {
  191. rt_thread_kill(ctr_motor, sig);
  192. }
  193. /* ========== 自动对焦(Auto Focus)控制块 ========== */
  194. typedef struct {
  195. float best_sharpness; /* 全局最佳清晰度 */
  196. int best_pos; /* 全局最佳位置 */
  197. float last_sharpness; /* 上一次移动后的清晰度 */
  198. int dir; /* 行进方向: 1=正向, 0=反向 */
  199. int step_size; /* 步进电机脉冲数 */
  200. int current_pos; /* 当前位置 */
  201. int peak_counter; /* 连续上升后下降的峰值计数 */
  202. int reduce_counter; /* 清晰度连续降低计数 */
  203. int diff_counter; /* 清晰度变化微小累计次数 */
  204. bool last_rising; /* 上次是否上升 */
  205. int max_iterations; /* 最大迭代次数 */
  206. bool initial_move; /* 初始方向判断标志 */
  207. volatile bool sharpness_updated; /* 信号处理程序置位: 有新清晰度值 */
  208. float new_sharpness; /* 信号处理程序写入的新清晰度值 */
  209. volatile bool fusion_active; /* 景深融合正在进行标志 */
  210. } auto_focus_t;
  211. static auto_focus_t g_af;
  212. /* 轻量级信号处理程序:仅存储清晰度值,不阻塞 */
  213. void thread1_signal_handler(int sig)
  214. {
  215. rt_timer_stop(timer_takephoto);
  216. /* 景深融合期间仍然记录清晰度,但 auto_focus_process 会忽略处理 */
  217. g_af.sharpness_updated = true;
  218. g_af.new_sharpness = device_status.focus_value;
  219. }
  220. /* 初始化自动对焦状态 */
  221. static void auto_focus_init(void)
  222. {
  223. memset(&g_af, 0, sizeof(g_af));
  224. g_af.best_sharpness = -FLT_MAX;
  225. g_af.last_sharpness = 0.0f;
  226. g_af.initial_move = true;
  227. g_af.dir = 1;
  228. g_af.step_size = 20;
  229. g_af.current_pos = 0;
  230. g_af.max_iterations = 100;
  231. }
  232. /* 移动电机一步 */
  233. static void auto_focus_move(void)
  234. {
  235. g_af.current_pos += g_af.dir ? g_af.step_size : -g_af.step_size;
  236. LOG_I("[对焦] 移动:方向=%d 步长=%d 位置=%d", g_af.dir, g_af.step_size, g_af.current_pos);
  237. slide_para.pulse_period = mapSpeed(1);
  238. slide_para.ystep = g_af.step_size;
  239. slide_para.ydir = g_af.dir;
  240. slide_para.ydis = g_af.step_size;
  241. slide_para.en_slide = 6;
  242. while (slide_para.en_slide != 0) {
  243. rt_thread_delay(100);
  244. }
  245. LOG_I("[对焦] 电机到位");
  246. rt_timer_start(timer_takephoto);
  247. }
  248. /* 景深融合:往 dir=1 方向 3 个位置,往 dir=0 方向 6 个位置,共 9 个位置
  249. * 中心位置在 auto_focus 时已保存,无需重复
  250. * 序列:current-6 -> current-5 -> ... -> current-1 -> current+1 -> current+2 -> current+3
  251. * 注意:dir=1 为正向(位置增加),dir=0 为反向(位置减少)
  252. */
  253. static void depth_of_field_fusion(void)
  254. {
  255. int i;
  256. int center_pos = g_af.current_pos;
  257. int step_size = 10;
  258. int wait_count;
  259. int target_pos;
  260. int move_dist;
  261. int positions[9] = {-6, -5, -4, -3, -2, -1, 1, 2, 3};
  262. LOG_I("[景深融合] 开始: 当前位置=%d 步长=%d", center_pos, step_size);
  263. for (i = 0; i < 9; i++) {
  264. target_pos = center_pos + positions[i] * step_size;
  265. move_dist = target_pos - g_af.current_pos;
  266. LOG_I("[景深融合] 位置[%d/9]: %d (移动距离=%d)",
  267. i + 1, target_pos, move_dist);
  268. slide_para.pulse_period = mapSpeed(1);
  269. slide_para.ystep = abs(move_dist);
  270. slide_para.ydir = (move_dist > 0) ? 1 : 0;
  271. slide_para.ydis = abs(move_dist);
  272. slide_para.en_slide = 6;
  273. /* 等待电机到位 */
  274. while (slide_para.en_slide != 0) {
  275. rt_thread_delay(10);
  276. }
  277. g_af.current_pos = target_pos;
  278. /* 等待清晰度更新(通知 android 端保存画面) */
  279. g_af.sharpness_updated = false;
  280. rt_timer_start(timer_takephoto);
  281. wait_count = 0;
  282. while (!g_af.sharpness_updated && wait_count < 200) {
  283. rt_thread_delay(50);
  284. wait_count++;
  285. }
  286. if (g_af.sharpness_updated) {
  287. LOG_I("[景深融合] 收到清晰度:%.2f 位置=%d", g_af.new_sharpness, target_pos);
  288. g_af.sharpness_updated = false;
  289. } else {
  290. LOG_W("[景深融合] 等待清晰度超时 位置=%d", target_pos);
  291. }
  292. rt_thread_delay(1000);
  293. }
  294. LOG_I("[景深融合] 完成: 已扫描 9 个位置,最终位置=%d", g_af.current_pos);
  295. rt_thread_delay(1000);
  296. /* ========== 发送景深融合完成消息 ========== */
  297. char *focus_done_msg = makeJson_Warn("focus_done","focus_done");
  298. DTU_Write("%s", focus_done_msg);
  299. rt_free(focus_done_msg);
  300. device_status.autotakepic = 0;
  301. }
  302. /* 完成对焦 */
  303. static void auto_focus_done(void)
  304. {
  305. char *c0 = makeJson_Warn("auto_focus","auto_focus");
  306. DTU_Write("%s",c0);
  307. rt_free(c0);
  308. LOG_I("[对焦] 完成! 最佳位置:%d 清晰度:%.2f", g_af.best_pos, g_af.best_sharpness);
  309. /* 停止 timer_takephoto,避免景深融合期间产生新的清晰度更新 */
  310. rt_timer_stop(timer_takephoto);
  311. /* 标记景深融合开始,auto_focus_process 会忽略清晰度处理 */
  312. g_af.fusion_active = true;
  313. /* 等待清晰度更新 */
  314. g_af.sharpness_updated = false;
  315. rt_timer_start(timer_takephoto);
  316. uint16_t wait_count = 0;
  317. while (!g_af.sharpness_updated && wait_count < 200) {
  318. rt_thread_delay(50);
  319. wait_count++;
  320. }
  321. /* 执行景深融合 */
  322. depth_of_field_fusion();
  323. g_af.fusion_active = false;
  324. }
  325. /* 处理一次清晰度值,执行对焦算法 */
  326. static void auto_focus_process(void)
  327. {
  328. /* 景深融合期间不处理对焦 */
  329. if (g_af.fusion_active) {
  330. return;
  331. }
  332. float current_sharpness = g_af.new_sharpness;
  333. g_af.sharpness_updated = false;
  334. g_af.max_iterations--;
  335. LOG_I("[对焦] 清晰度:%.2f 位置:%d 剩余:%d",
  336. current_sharpness, g_af.current_pos, g_af.max_iterations);
  337. /* 更新最佳位置 */
  338. if (current_sharpness > g_af.best_sharpness) {
  339. g_af.best_sharpness = current_sharpness;
  340. g_af.best_pos = g_af.current_pos;
  341. LOG_I("[对焦] 新最佳:位置%d 清晰度%.2f", g_af.best_pos, g_af.best_sharpness);
  342. }
  343. /* 初始方向判断:比较首次移动前后的清晰度 */
  344. if (g_af.initial_move) {
  345. g_af.initial_move = false;
  346. if (current_sharpness > g_af.last_sharpness + 0.5f) {
  347. LOG_I("[对焦] 初始方向正确");
  348. } else {
  349. g_af.dir = !g_af.dir;
  350. LOG_I("[对焦] 初始方向错误,反转");
  351. }
  352. g_af.last_sharpness = current_sharpness;
  353. auto_focus_move();
  354. return;
  355. }
  356. /* 计算清晰度变化 */
  357. float diff = current_sharpness - g_af.last_sharpness;
  358. /* 动态调整步长 */
  359. if (fabs(diff) > 3.0f) {
  360. /* 清晰度大幅变化 -> 缩小步长精细搜索 */
  361. g_af.step_size = 4;
  362. g_af.diff_counter = 0;
  363. LOG_I("[对焦] 大幅变化,步长=%d", g_af.step_size);
  364. } else if (fabs(diff) < 1.0f) {
  365. /* 清晰度变化微小 -> 累计平坦区域 */
  366. g_af.diff_counter++;
  367. if (g_af.diff_counter >= 10) {
  368. g_af.step_size = 20;
  369. }
  370. LOG_I("[对焦] 平坦[%d/10] 步长=%d", g_af.diff_counter, g_af.step_size);
  371. } else {
  372. g_af.diff_counter = 0;
  373. }
  374. /* 方向判断 */
  375. bool need_reverse = false;
  376. if (diff > 0.5f) {
  377. /* 上升:保持方向,缩小步长逼近峰值 */
  378. g_af.reduce_counter = 0;
  379. g_af.last_rising = true;
  380. if (g_af.step_size > 4) g_af.step_size -= 2;
  381. LOG_I("[对焦] 上升↑ 步长=%d", g_af.step_size);
  382. } else if (diff < -0.3f) {
  383. /* 下降:累计,超过阈值反转方向 */
  384. g_af.reduce_counter++;
  385. g_af.last_rising = false;
  386. LOG_I("[对焦] 下降↓[%d/3]", g_af.reduce_counter);
  387. if (g_af.reduce_counter >= 3) {
  388. need_reverse = true;
  389. g_af.reduce_counter = 0;
  390. g_af.peak_counter++;
  391. LOG_I("[对焦] 峰值检测[%d/3]", g_af.peak_counter);
  392. }
  393. } else {
  394. g_af.reduce_counter = 0;
  395. LOG_I("[对焦] 平稳=");
  396. }
  397. g_af.last_sharpness = current_sharpness;
  398. if (need_reverse) {
  399. g_af.dir = !g_af.dir;
  400. LOG_I("[对焦] 反转方向");
  401. }
  402. /* 平坦区域超时:强制反转并增大步长 */
  403. if (g_af.diff_counter >= 30) {
  404. g_af.dir = !g_af.dir;
  405. g_af.step_size += 10;
  406. g_af.diff_counter = 0;
  407. LOG_I("[对焦] 平坦超时,增大步长=%d", g_af.step_size);
  408. }
  409. LOG_I("[对焦] 最佳:位置%d 清晰度%.2f", g_af.best_pos, g_af.best_sharpness);
  410. /* 终止条件 */
  411. bool done = false;
  412. if (g_af.peak_counter >= 3 && current_sharpness > g_af.best_sharpness * 0.95f) {
  413. done = true;
  414. LOG_I("[对焦] 峰值确认完成");
  415. }
  416. if (g_af.max_iterations <= 0) {
  417. done = true;
  418. LOG_I("[对焦] 迭代次数耗尽");
  419. }
  420. if (done) {
  421. auto_focus_done();
  422. return;
  423. }
  424. /* 移动电机 */
  425. auto_focus_move();
  426. }
  427. /* ========== Y轴校准(Calibration)控制块 ========== */
  428. typedef struct {
  429. float best_sharpness;
  430. float last_sharpness;
  431. int diff_counter;
  432. bool initial_move;
  433. int dir;
  434. int ystep;
  435. volatile bool sharpness_updated;
  436. float new_sharpness;
  437. } calibrate_t;
  438. static calibrate_t g_cal;
  439. /* 轻量级信号处理程序:仅存储清晰度值,不阻塞 */
  440. void thread2_signal_handler(int sig)
  441. {
  442. rt_timer_stop(timer_takephoto);
  443. g_cal.sharpness_updated = true;
  444. g_cal.new_sharpness = device_status.focus_value;
  445. }
  446. /* 初始化校准状态 */
  447. static void calibrate_init(void)
  448. {
  449. memset(&g_cal, 0, sizeof(g_cal));
  450. g_cal.best_sharpness = -FLT_MAX;
  451. g_cal.last_sharpness = 0.0f;
  452. g_cal.initial_move = true;
  453. g_cal.dir = 1;
  454. g_cal.ystep = 0;
  455. }
  456. /* 移动电机一步 */
  457. static void calibrate_move(void)
  458. {
  459. g_cal.ystep += 80;
  460. slide_para.pulse_period = mapSpeed(50);
  461. slide_para.ydir = g_cal.dir;
  462. slide_para.ydis = 80;
  463. slide_para.ystep = g_cal.ystep;
  464. slide_para.en_slide = 6;
  465. while (slide_para.en_slide != 0) {
  466. rt_thread_delay(500);
  467. }
  468. LOG_I("[Y] 电机到位 位置:%d", g_cal.ystep);
  469. rt_timer_start(timer_takephoto);
  470. }
  471. /* 处理一次校准清晰度值 */
  472. static void calibrate_process(void)
  473. {
  474. float current_sharpness = g_cal.new_sharpness;
  475. g_cal.sharpness_updated = false;
  476. LOG_I("[Y] 清晰度值:%.2f 位置:%d", current_sharpness, g_cal.ystep);
  477. /* 更新最佳位置 */
  478. if (current_sharpness > g_cal.best_sharpness) {
  479. g_cal.best_sharpness = current_sharpness;
  480. LOG_I("[Y] 更佳位置(清晰度:%.2f) 位置:%d", g_cal.best_sharpness, g_cal.ystep);
  481. }
  482. /* 初始方向判断 */
  483. if (g_cal.initial_move) {
  484. g_cal.initial_move = false;
  485. if (current_sharpness > g_cal.last_sharpness) {
  486. LOG_I("[Y] 初始方向正确");
  487. }
  488. g_cal.last_sharpness = current_sharpness;
  489. calibrate_move();
  490. return;
  491. }
  492. /* 计算变化 */
  493. double diff_sharpness = fabs(current_sharpness - g_cal.last_sharpness);
  494. if (diff_sharpness < 1.0) {
  495. g_cal.diff_counter++;
  496. }
  497. LOG_I("[Y] 平坦累计:%d 变化量:%.2f", g_cal.diff_counter, diff_sharpness);
  498. /* 从平坦区域到突变 -> 特征点,保存当前位置 */
  499. if (g_cal.diff_counter > 1 && diff_sharpness > 100.0) {
  500. device_status.calibrate_slide = 0;
  501. device_status.y_init_dis = g_cal.ystep;
  502. LOG_I("设置Y轴初始位置:%d", g_cal.ystep);
  503. ef_set_env_blob("y_init_dis", &device_status.y_init_dis, sizeof(device_status.y_init_dis));
  504. return;
  505. }
  506. g_cal.last_sharpness = current_sharpness;
  507. /* 继续移动 */
  508. calibrate_move();
  509. }
  510. //入口函数-电机控制及
  511. static void Ctr_Mortor_entry(void* parameter)
  512. {
  513. /*孢子仪2.0*/
  514. rt_signal_install(SIGUSR1, thread1_signal_handler); //自动对焦信号
  515. rt_signal_install(SIGUSR2, thread2_signal_handler); //坐标校准信号
  516. rt_signal_unmask(SIGUSR1);
  517. rt_signal_unmask(SIGUSR2);
  518. static uint32_t t_takephoto = 0;
  519. while(1)
  520. {
  521. if(device_status.calibrate_slide==2)
  522. {
  523. device_status.autotakepic=1;
  524. device_status.calibrate_slide = 20;
  525. }
  526. if(device_status.autotakepic==2)
  527. {
  528. t_takephoto++;
  529. if(t_takephoto==18000)
  530. {
  531. t_takephoto = 0;
  532. device_status.autotakepic = 0;
  533. }
  534. /* 自动对焦:有新清晰度值时处理 */
  535. if (g_af.sharpness_updated) {
  536. auto_focus_process();
  537. }
  538. /* Y轴校准:有新清晰度值时处理 */
  539. if (g_cal.sharpness_updated) {
  540. calibrate_process();
  541. }
  542. }else {
  543. t_takephoto = 0;
  544. }
  545. /*对焦拍照,滑台初始化*/
  546. if(device_status.autotakepic==1)
  547. {
  548. ctr_led(ON); //冗余
  549. device_status.step_focus = 1;
  550. slide_para.en_slide = 7; //Y轴复位
  551. slide_para.pulse_period = mapSpeed(50);
  552. while(slide_para.en_slide!=0){
  553. LOG_D("等待Y轴电机完成复位!");
  554. rt_thread_delay(1000);
  555. }
  556. if(device_status.calibrate_slide==0){
  557. device_status.autotakepic = 3;
  558. }else{
  559. slide_para.en_slide = 7; //Y轴复位
  560. slide_para.pulse_period = mapSpeed(50);
  561. while(slide_para.en_slide!=0){
  562. LOG_D("等待Y轴电机完成复位!");
  563. rt_thread_delay(1000);
  564. }
  565. rt_thread_delay(1000);
  566. //生成JSON指令
  567. char *photo = makeJson_Warn("mov_init_y","takephoto_done");
  568. DTU_Write("%s",photo);
  569. rt_free(photo);
  570. device_status.autotakepic = 4;
  571. rt_timer_start(timer_takephoto);
  572. }
  573. }
  574. if(device_status.autotakepic==3)
  575. {
  576. slide_para.en_slide = 7; //Y轴复位
  577. slide_para.pulse_period = mapSpeed(50);
  578. while(slide_para.en_slide!=0){
  579. LOG_D("等待Y轴电机完成复位!");
  580. rt_thread_delay(1000);
  581. }
  582. rt_thread_delay(1000);
  583. //Y轴电机运行到指定距离
  584. slide_para.ydir = 1;
  585. //slide_para.ydis = 3350;
  586. slide_para.ydis = device_status.y_init_dis; //9000
  587. slide_para.pulse_period = mapSpeed(50);
  588. slide_para.en_slide = 6;
  589. while(slide_para.en_slide!=0){
  590. LOG_D("Again:等待y轴电机到位!");
  591. rt_thread_delay(1000);
  592. }
  593. //生成JSON指令
  594. char *photo = makeJson_Warn("mov_init_y","takephoto_done");
  595. DTU_Write("%s",photo);
  596. rt_free(photo);
  597. device_status.autotakepic = 4;
  598. rt_timer_start(timer_takephoto);
  599. }
  600. if(device_status.autotakepic == 4)
  601. {
  602. device_status.autotakepic = 2;
  603. auto_focus_init();
  604. calibrate_init();
  605. slide_para.ystep = 0;
  606. }
  607. if(device_status.fsl_sw==1)
  608. {
  609. ctr_Petroleum_jelly(10);
  610. }
  611. //一键测试
  612. if(device_status.auto_test_sw==1)
  613. {
  614. //开始采集风机持续5S
  615. ctr_router(ON);
  616. rt_thread_delay(1000*60);
  617. //关闭采集风机
  618. ctr_router(OFF);
  619. //胶带传送
  620. turnlimit();
  621. rt_thread_delay(1000);
  622. turnlimit(); //再转
  623. //滑台归位,自动对焦
  624. //拍一张照片上传到平台
  625. device_status.autotakepic=1;
  626. device_status.auto_test_sw=0;
  627. char *take_start = makeJson_Warn("start", "autofocus");
  628. DTU_Write(take_start);
  629. rt_free(take_start);
  630. }
  631. rt_thread_delay(100);
  632. }
  633. }
  634. /*屏幕写数据*/
  635. int Hmi_Fresh(void)
  636. {
  637. #if 0
  638. //初始化串口4
  639. HMI_uart4_init();
  640. rt_err_t ret = RT_EOK;
  641. /* 初始化消息队列 */
  642. rt_mq_init(&rx_mq, "rx_mq",
  643. msg_pool, /* 存放消息的缓冲区 */
  644. sizeof(struct rx_msg), /* 一条消息的最大长度 */
  645. sizeof(msg_pool), /* 存放消息的缓冲区大小 */
  646. RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
  647. /* 设置接收回调函数 */
  648. rt_device_set_rx_indicate(serial, uart_input);
  649. /* 创建线程*/
  650. hmi1 = rt_thread_create("Hmi_Fresh_thread",
  651. Hmi_thread_entry,
  652. RT_NULL,
  653. HMI_THREAD_STACK_SIZE,
  654. HMI_THREAD_PRIORITY,
  655. HMI_THREAD_TIMESLICE);
  656. /* 如果获得线程控制块,启动这个线程 */
  657. if (hmi1 != RT_NULL)
  658. {
  659. rt_thread_startup(hmi1);
  660. }
  661. else
  662. {
  663. LOG_E("rt_thread_startup hmi1 failed!");
  664. }
  665. #endif
  666. /*部分控制的线程*/
  667. ctr_motor = rt_thread_create("Ctr motor",
  668. Ctr_Mortor_entry,
  669. RT_NULL,
  670. CONTROL_MOTOR_STACK_SIZE,
  671. CONTROL_MOTOR_PRIORITY,
  672. CONTROL_MOTOR_TIMESLICE);
  673. if(ctr_motor != RT_NULL)
  674. {
  675. rt_thread_startup(ctr_motor);
  676. }
  677. //定时器,串口通信超时监测20S
  678. timer_uart2 = rt_timer_create("t_uart2",t_uart2_callback,RT_NULL, 20000, RT_TIMER_FLAG_ONE_SHOT);
  679. if(timer_uart2==RT_NULL)
  680. {
  681. LOG_E("rt_timer_create timer_uart2 failed!");
  682. }
  683. return 0;
  684. }
  685. void Check_ParamConfig(void)
  686. {
  687. time_t now = time(RT_NULL);
  688. static struct tm tm;
  689. gmtime_r(&now, &tm);
  690. if(easyflash_init()==EF_NO_ERR) //初始化成功
  691. {
  692. ef_get_env_blob("reboot_time", &device_status.rebootcnt, sizeof(device_status.rebootcnt), NULL); //读出reboot_time的值
  693. ef_get_env_blob("update_flag", &device_status.update_flag, sizeof(device_status.update_flag), NULL); //读出updateflag的值
  694. ef_get_env_blob("iSave", &device_status.iSave, sizeof(device_status.iSave), NULL); //判断是否用户配置了程序
  695. device_status.rebootcnt++;
  696. ef_set_env_blob("reboot_time", &device_status.rebootcnt, sizeof(device_status.rebootcnt));
  697. LOG_I("REBOOT_TIME:%d-%d-%d",device_status.rebootcnt,device_status.iSave,device_status.update_flag);
  698. }
  699. else {
  700. LOG_E("easyflash init ERRPR!!!");
  701. }
  702. //保存默认配置
  703. if(device_status.iSave!=FLASH_FLAG)
  704. {
  705. device_status.iSave=FLASH_FLAG;
  706. /*设备类型,7孢子仪,3测报灯,默认设置测报灯*/
  707. device_status.dtype = 7;
  708. device_status.version_type = 7; //4.0
  709. device_status.Power_Type = 0; //0-交流供电;1-直流供电
  710. device_status.tph = 60; //高温保护阈值
  711. device_status.tpl = 10; //低温保护阈值
  712. device_status.dattim = 10; //数据上传间隔
  713. device_status.ds = 1; //设备状态
  714. device_status.update_flag = 0;
  715. /*孢子仪*/
  716. device_status.collecting_sh1 = 9;
  717. device_status.collecting_eh1 = 10;
  718. device_status.collecting_sh2 = 14;
  719. device_status.collecting_eh2 = 16;
  720. device_status.cold_sw = 0;
  721. device_status.heat_sw =0;
  722. device_status.cul_time = 1;
  723. device_status.run_sta = 0;
  724. sprintf(device_status.driver, "%s",VERSION_BZY);
  725. sprintf(device_status.deviceId,"0");
  726. rt_sprintf(device_status.tempture, "-15");
  727. rt_sprintf(device_status.humidity, "31");
  728. rt_sprintf(device_status.mqttcfg1, "8.136.98.49");
  729. device_status.x_init_dis = 500;
  730. device_status.y_init_dis = 6000;
  731. device_status.tape_used = 0;
  732. ef_set_env_blob("iSave", &device_status.iSave, sizeof(device_status.iSave)); //
  733. ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
  734. ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph));
  735. ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl));
  736. ef_set_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim)); //上传时间
  737. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  738. ef_set_env_blob("env_t",&device_status.tempture,sizeof(device_status.tempture)); //温度
  739. ef_set_env_blob("env_h",&device_status.humidity,sizeof(device_status.humidity)); //湿度
  740. ef_set_env_blob("collecting_sh1",&device_status.collecting_sh1,sizeof(device_status.collecting_sh1)); //风机开关
  741. ef_set_env_blob("collecting_eh1",&device_status.collecting_eh1,sizeof(device_status.collecting_eh1)); //采集时间
  742. ef_set_env_blob("collecting_sh2",&device_status.collecting_sh2,sizeof(device_status.collecting_sh2)); //采集时间
  743. ef_set_env_blob("collecting_eh2",&device_status.collecting_eh2,sizeof(device_status.collecting_eh2)); //采集时间
  744. ef_set_env_blob("collecting_sh3",&device_status.collecting_sh3,sizeof(device_status.collecting_sh3)); //采集时间
  745. ef_set_env_blob("collecting_eh3",&device_status.collecting_eh3,sizeof(device_status.collecting_eh3)); //采集时间
  746. ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta)); //
  747. ef_set_env_blob("Power_Type", &device_status.Power_Type, sizeof(device_status.Power_Type));
  748. ef_set_env_blob("x_init_dis", &device_status.x_init_dis, sizeof(device_status.x_init_dis));
  749. ef_set_env_blob("y_init_dis", &device_status.y_init_dis, sizeof(device_status.y_init_dis));
  750. ef_set_and_save_env("collt_min_sh1", "0");
  751. ef_set_and_save_env("collt_min_eh1", "0");
  752. ef_set_and_save_env("collt_min_sh2", "0");
  753. ef_set_and_save_env("collt_min_eh2", "0");
  754. ef_set_and_save_env("collt_min_sh3", "0");
  755. ef_set_and_save_env("collt_min_eh3", "0");
  756. LOG_I("SOFTWARE VERSION:%s",device_status.driver);
  757. LOG_I("Default paramconf is:");
  758. LOG_I("开机次数:%d",device_status.rebootcnt);
  759. LOG_I("高温保护:%d",device_status.tph);
  760. LOG_I("低温保护:%d",device_status.tpl);
  761. LOG_I("数据上传间隔:%d",device_status.dattim);
  762. LOG_I("温湿度数据:%d-%d",atoi(device_status.tempture),atoi(device_status.humidity));
  763. LOG_I("孢子仪采集时间:[%d:%d-%d:%d,%d:%d-%d:%d,%d:%d-%d:%d]",
  764. device_status.collecting_sh1,device_status.collt_min_sh1,
  765. device_status.collecting_eh1,device_status.collt_min_eh1,
  766. device_status.collecting_sh2,device_status.collt_min_sh2,
  767. device_status.collecting_eh2,device_status.collt_min_eh2,
  768. device_status.collecting_sh3,device_status.collt_min_sh3,
  769. device_status.collecting_eh3,device_status.collt_min_eh3);
  770. LOG_I("PowerType:%d",device_status.Power_Type);
  771. LOG_I("x_init_dis:%d",device_status.x_init_dis);
  772. LOG_I("y_init_dis:%d",device_status.y_init_dis);
  773. }//从easyflash读取参数配置
  774. else if (device_status.iSave==FLASH_FLAG) {
  775. ef_get_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype),NULL); //设备类型
  776. ef_get_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim),NULL); //数据上传间隔
  777. ef_get_env_blob("tph", &device_status.tph, sizeof(device_status.tph), NULL); //高温保护
  778. ef_get_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl), NULL); //低温保护
  779. ef_get_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim), NULL); //数据上传间隔
  780. ef_get_env_blob("imei",&device_status.imei,sizeof(device_status.imei),NULL); //IMEI
  781. ef_get_env_blob("iccid",&device_status.iccid,sizeof(device_status.iccid),NULL); //ICCID
  782. ef_get_env_blob("lat",&device_status.lat,sizeof(device_status.lat),NULL);
  783. ef_get_env_blob("lng",&device_status.lng,sizeof(device_status.lng),NULL);
  784. ef_get_env_blob("ds",&device_status.ds,sizeof(device_status.ds),NULL); //开关机状态
  785. ef_get_env_blob("env_t",&device_status.tempture,sizeof(device_status.tempture),NULL); //环境温度
  786. ef_get_env_blob("env_h",&device_status.humidity,sizeof(device_status.humidity),NULL); //环境湿度
  787. ef_get_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type),NULL); //版本类型
  788. ef_get_env_blob("driver_bl",&device_status.driver_bl,sizeof(device_status.driver_bl),NULL); //BL版本号 进BL的时候存储到FLASH
  789. ef_get_env_blob("collecting_sh1",&device_status.collecting_sh1,sizeof(device_status.collecting_sh1),NULL); //采集开始1
  790. ef_get_env_blob("collecting_eh1",&device_status.collecting_eh1,sizeof(device_status.collecting_eh1),NULL); //采集结束1
  791. ef_get_env_blob("collecting_sh2",&device_status.collecting_sh2,sizeof(device_status.collecting_sh2),NULL); //采集开始2
  792. ef_get_env_blob("collecting_eh2",&device_status.collecting_eh2,sizeof(device_status.collecting_eh2),NULL); //采集结束2
  793. ef_get_env_blob("collecting_sh3",&device_status.collecting_sh3,sizeof(device_status.collecting_sh3),NULL); //采集开始3
  794. ef_get_env_blob("collecting_eh3",&device_status.collecting_eh3,sizeof(device_status.collecting_eh3),NULL); //采集结束3
  795. ef_get_env_blob("run_sta",&device_status.run_sta,sizeof(device_status.run_sta),NULL); //运行状态
  796. ef_get_env_blob("Power_Type",&device_status.Power_Type,sizeof(device_status.Power_Type),NULL); //供电类型
  797. ef_get_env_blob("x_init_dis",&device_status.x_init_dis,sizeof(device_status.x_init_dis),NULL);
  798. ef_get_env_blob("y_init_dis",&device_status.y_init_dis,sizeof(device_status.y_init_dis),NULL);
  799. ef_get_env_blob("collt_min_sh1", &device_status.collt_min_sh1, sizeof(device_status.collt_min_sh1), NULL);
  800. ef_get_env_blob("collt_min_eh1", &device_status.collt_min_eh1, sizeof(device_status.collt_min_eh1), NULL);
  801. ef_get_env_blob("collt_min_sh2", &device_status.collt_min_sh2, sizeof(device_status.collt_min_sh2), NULL);
  802. ef_get_env_blob("collt_min_eh2", &device_status.collt_min_eh2, sizeof(device_status.collt_min_eh2), NULL);
  803. ef_get_env_blob("collt_min_sh3", &device_status.collt_min_sh3, sizeof(device_status.collt_min_sh3), NULL);
  804. ef_get_env_blob("collt_min_eh3", &device_status.collt_min_eh3, sizeof(device_status.collt_min_eh3), NULL);
  805. //光控计数
  806. device_status.work_done = 0;
  807. //工作完成标志位
  808. if(device_status.dattim==0)
  809. {
  810. device_status.dattim = 10;
  811. }
  812. /*设备类型,7孢子仪*/
  813. device_status.dtype = 7;
  814. device_status.version_type = 7;
  815. if (device_status.version_type==7) {
  816. rt_sprintf(device_status.driver, "%s",VERSION_BZY);
  817. //异常处理:20230331,修复第一段开始时间等于第二段结束时间的情况
  818. if(device_status.collecting_eh1==device_status.collecting_eh2&&device_status.collecting_sh2!=device_status.collecting_eh2)
  819. {
  820. device_status.collecting_eh2 = device_status.collecting_sh2 + 2;
  821. }
  822. }
  823. app_flashdb_init();
  824. LOG_I("SOFTWARE VERSION:%s-imei:%s--iccid:%s",device_status.driver,device_status.imei,device_status.iccid);
  825. LOG_I("read flash paramconfis:%d-%s-%s",device_status.version_type,device_status.driver_bl,device_status.driver_compile);
  826. LOG_I("开机次数:%d",device_status.rebootcnt);
  827. LOG_I("高温保护:%d",device_status.tph);
  828. LOG_I("低温保护:%d",device_status.tpl);
  829. LOG_I("数据上传间隔:%d",device_status.dattim);
  830. LOG_I("温湿度数据:%d-%d",atoi(device_status.tempture),atoi(device_status.humidity));
  831. LOG_I("孢子仪采集时间:[%d:%d-%d:%d,%d:%d-%d:%d,%d:%d-%d:%d]",
  832. device_status.collecting_sh1,device_status.collt_min_sh1,
  833. device_status.collecting_eh1,device_status.collt_min_eh1,
  834. device_status.collecting_sh2,device_status.collt_min_sh2,
  835. device_status.collecting_eh2,device_status.collt_min_eh2,
  836. device_status.collecting_sh3,device_status.collt_min_sh3,
  837. device_status.collecting_eh3,device_status.collt_min_eh3);
  838. LOG_I("PowerType:%d",device_status.Power_Type);
  839. LOG_I("x_init_dis:%d",device_status.x_init_dis);
  840. LOG_I("y_init_dis:%d",device_status.y_init_dis);
  841. }
  842. //RTC时间判断,如果当前年份小于2020,提示需要设置时间,切换为光控
  843. {
  844. device_status.update_flag = 0;
  845. //初始化联网状态
  846. rt_sprintf(device_status.net,"1");
  847. rt_sprintf(device_status.csq,"0");
  848. rt_sprintf(device_status.sim,"1");
  849. rt_sprintf(device_status.gps, "0");
  850. Hmi_Fresh();
  851. dtu_data_play();
  852. sht30_collect();
  853. slide_Fresh();
  854. ctr_photo(ON);
  855. #if SENSOR_EN==1
  856. mb_master_samlpe();
  857. #endif
  858. #if ROBOT_EN == 0
  859. uint16_t connect_num=0;
  860. //等待联网,
  861. while(atoi(device_status.net)==1)
  862. {
  863. connect_num++;
  864. LOG_I("联网中......%d",connect_num);
  865. //超时时间为100S
  866. if(connect_num==60)
  867. {
  868. 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);
  869. break;
  870. }
  871. rt_thread_delay(2000);
  872. }
  873. connect_num = 0;
  874. DTU_Write("%s",makeJson_Warn("turnon", "device_stm32"));
  875. rt_thread_delay(1000);
  876. UpLoad_Data();
  877. Workstatus_check();
  878. Work_Loop_Task();
  879. #endif
  880. }
  881. }
  882. void ResetParamDefault(void)
  883. {
  884. device_status.dtype = 7;
  885. device_status.ds = 1; //设备状态开机
  886. device_status.thsp = 300;
  887. device_status.dattim = 10;
  888. device_status.collecting_sh1 = 9;
  889. device_status.collecting_eh1 = 17;
  890. device_status.collecting_sh2 = 00;
  891. device_status.collecting_eh2 = 00;
  892. device_status.collecting_sh3 = 00;
  893. device_status.collecting_eh3 = 00;
  894. device_status.collt_min_sh1 = 0;
  895. device_status.collt_min_eh1 = 0;
  896. device_status.collt_min_sh2 = 0;
  897. device_status.collt_min_eh2 = 0;
  898. device_status.collt_min_sh3 = 0;
  899. device_status.collt_min_eh3 = 0;
  900. device_status.Power_Type = 0;
  901. device_status.tph = 70; //高温保护阈值
  902. device_status.tpl = 10; //低温保护阈值
  903. ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph));
  904. ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl));
  905. ef_set_env_blob("Power_Type", &device_status.Power_Type, sizeof(device_status.Power_Type));
  906. ef_set_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim)); //上传时间
  907. device_status.run_sta = 0;
  908. device_status.heatvalue = 25; //培养温度
  909. ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta));
  910. ef_set_env_blob("collecting_sh1", &device_status.collecting_sh1, sizeof(device_status.collecting_sh1)); //采集时间
  911. ef_set_env_blob("collecting_eh1", &device_status.collecting_eh1, sizeof(device_status.collecting_eh1)); //采集时间
  912. ef_set_env_blob("collecting_sh2", &device_status.collecting_sh2, sizeof(device_status.collecting_sh2)); //采集时间
  913. ef_set_env_blob("collecting_eh2", &device_status.collecting_eh2, sizeof(device_status.collecting_eh2)); //采集时间
  914. ef_set_env_blob("collecting_sh3", &device_status.collecting_sh3, sizeof(device_status.collecting_sh3)); //采集时间
  915. ef_set_env_blob("collecting_eh3", &device_status.collecting_eh3, sizeof(device_status.collecting_eh3)); //采集时间
  916. ef_set_and_save_env("collt_min_sh1", "0");
  917. ef_set_and_save_env("collt_min_eh1", "0");
  918. ef_set_and_save_env("collt_min_sh2", "0");
  919. ef_set_and_save_env("collt_min_eh2", "0");
  920. ef_set_and_save_env("collt_min_sh3", "0");
  921. ef_set_and_save_env("collt_min_eh3", "0");
  922. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
  923. ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
  924. ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
  925. rt_thread_delay(1000);
  926. }
  927. void ResetParamDefault_DC(void)
  928. {
  929. //将温度数据设置为-15摄氏度,以便上传的数据中包含温控数据
  930. rt_sprintf(device_status.tempture,"%d",-15);
  931. device_status.dattim = 10; //数据上传间隔
  932. device_status.tph = 70; //高温保护阈值
  933. device_status.tpl = 10; //低温保护阈值
  934. device_status.ds = 1; //设备状态开机
  935. device_status.collecting_sh1 = 9;
  936. device_status.collecting_eh1 = 17;
  937. device_status.collecting_sh2 = 00;
  938. device_status.collecting_eh2 = 00;
  939. device_status.collecting_sh3 = 00;
  940. device_status.collecting_eh3 = 00;
  941. device_status.collt_min_sh1 = 0;
  942. device_status.collt_min_eh1 = 0;
  943. device_status.collt_min_sh2 = 0;
  944. device_status.collt_min_eh2 = 0;
  945. device_status.collt_min_sh3 = 0;
  946. device_status.collt_min_eh3 = 0;
  947. device_status.Power_Type = 1;
  948. ef_set_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim)); //上传时间
  949. ef_set_env_blob("Power_Type", &device_status.Power_Type, sizeof(device_status.Power_Type));
  950. ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
  951. ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph));
  952. ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl));
  953. ef_set_env_blob("collecting_sh1", &device_status.collecting_sh1, sizeof(device_status.collecting_sh1)); //采集时间
  954. ef_set_env_blob("collecting_eh1", &device_status.collecting_eh1, sizeof(device_status.collecting_eh1)); //采集时间
  955. ef_set_env_blob("collecting_sh2", &device_status.collecting_sh2, sizeof(device_status.collecting_sh2)); //采集时间
  956. ef_set_env_blob("collecting_eh2", &device_status.collecting_eh2, sizeof(device_status.collecting_eh2)); //采集时间
  957. ef_set_env_blob("collecting_sh3", &device_status.collecting_sh3, sizeof(device_status.collecting_sh3)); //采集时间
  958. ef_set_env_blob("collecting_eh3", &device_status.collecting_eh3, sizeof(device_status.collecting_eh3)); //采集时间
  959. ef_set_and_save_env("collt_min_sh1", "0");
  960. ef_set_and_save_env("collt_min_eh1", "0");
  961. ef_set_and_save_env("collt_min_sh2", "0");
  962. ef_set_and_save_env("collt_min_eh2", "0");
  963. ef_set_and_save_env("collt_min_sh3", "0");
  964. ef_set_and_save_env("collt_min_eh3", "0");
  965. rt_thread_delay(1000);
  966. }
  967. //固件备份
  968. void app_partion_backup(void)
  969. {
  970. rt_kprintf("固件恢复!!!\n");
  971. const char *dst_part_name = "df_area";
  972. const char *src_part_name = "app";
  973. const struct fal_partition *dst_part;
  974. const struct fal_partition *src_part;
  975. dst_part = fal_partition_find(dst_part_name);
  976. src_part = fal_partition_find(src_part_name);
  977. if (dst_part == RT_NULL || src_part == RT_NULL)
  978. {
  979. if (dst_part == RT_NULL)
  980. rt_kprintf("%s partition is not exist!\n", dst_part_name);
  981. if (src_part == RT_NULL)
  982. rt_kprintf("%s partition is not exist!\n", src_part_name);
  983. }
  984. else
  985. {
  986. rt_kprintf("Clone %s partition to %s partition:\n", src_part_name, dst_part_name);
  987. if (fal_partition_erase(dst_part, 0, dst_part->len) >= 0)
  988. {
  989. int clone_pos = 0;
  990. int clone_len = 0, clone_tol_len;
  991. rt_uint8_t *buf = rt_malloc(4096);
  992. if (dst_part->len < src_part->len)
  993. clone_tol_len = dst_part->len;
  994. else
  995. clone_tol_len = src_part->len;
  996. while ((clone_pos < clone_tol_len) && (buf != RT_NULL))
  997. {
  998. clone_len = fal_partition_read(src_part, clone_pos, buf, 4096);
  999. if (clone_len < 0)
  1000. {
  1001. rt_kprintf("\nread %s partition failed, clone stop!\n", src_part_name);
  1002. break;
  1003. }
  1004. if (fal_partition_write(dst_part, clone_pos, buf, clone_len) < 0)
  1005. {
  1006. rt_kprintf("\nwrite %s partition failed, clone stop!\n", dst_part_name);
  1007. break;
  1008. }
  1009. HMI_WRITE("#");
  1010. rt_kprintf("#");
  1011. clone_pos += clone_len;
  1012. }
  1013. if (clone_pos >= clone_tol_len)
  1014. {
  1015. rt_kprintf("\nClone partition success, total %d bytes!\n", clone_tol_len);
  1016. }
  1017. else
  1018. {
  1019. rt_kprintf("Clone partition failed!");
  1020. }
  1021. if (buf)
  1022. rt_free(buf);
  1023. }
  1024. }
  1025. }
  1026. void Debug_log_ON(void)
  1027. {
  1028. rt_console_set_device("uart4");
  1029. finsh_set_device("uart4");
  1030. }
  1031. void Debug_log_OFF(void)
  1032. {
  1033. rt_console_set_device("uart1");
  1034. finsh_set_device("uart1");
  1035. rt_mq_init(&rx_mq, "rx_mq",
  1036. msg_pool, /* 存放消息的缓冲区 */
  1037. sizeof(struct rx_msg), /* 一条消息的最大长度 */
  1038. sizeof(msg_pool), /* 存放消息的缓冲区大小 */
  1039. RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
  1040. rt_device_set_rx_indicate(serial, uart_input);
  1041. hmi1 = rt_thread_create("Hmi_Fresh_thread",
  1042. Hmi_thread_entry,
  1043. RT_NULL,
  1044. HMI_THREAD_STACK_SIZE,
  1045. HMI_THREAD_PRIORITY,
  1046. HMI_THREAD_TIMESLICE);
  1047. if (hmi1 != RT_NULL)
  1048. {
  1049. rt_thread_startup(hmi1);
  1050. }
  1051. }
  1052. int delete_thread_ctr_motor()
  1053. {
  1054. if(ctr_motor!=RT_NULL)
  1055. {
  1056. rt_thread_delete(ctr_motor);
  1057. }
  1058. }
  1059. MSH_CMD_EXPORT(ResetParamDefault,ResetParamDefault);
  1060. MSH_CMD_EXPORT(Debug_log_ON,Debug_log_ON);
  1061. MSH_CMD_EXPORT(Debug_log_OFF,Debug_log_OFF);
  1062. //
  1063. //INIT_BOARD_EXPORT(HMI_uart4_init);