hmi_fun.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. #include <rtthread.h>
  2. #include <rtdevice.h>
  3. #include <board.h>
  4. #include <stdio.h>
  5. #include "string.h"
  6. #include <rt_fota.h>
  7. #include "sys_config.h"
  8. #define LOG_TAG "hmi.f" // 该模块对应的标签。不定义时,默认:NO_TAG
  9. #define LOG_LVL LOG_LVL_DBG // LOG_LVL_DBG // 该模块对应的日志输出级别。不定义时,默认:调试级别
  10. #include <ulog.h> // 必须在 LOG_TAG 与 LOG_LVL 下面
  11. #define TOUCH_P GET_PIN(A, 15)
  12. #define HIM_POWER_PIN GET_PIN(C, 8)
  13. /* 定时器的控制块 */
  14. rt_timer_t HIM_POWER_PIN_TIMER;
  15. int HIM_POWER_CNT = 0;
  16. static char dis_buf[100]; /*显示缓冲区*/
  17. static uint8_t GET_PAGE_ID[]="sendme"; /*获取页面ID*/
  18. /* 消息队列控制块 */
  19. static struct rt_messagequeue fota_status;
  20. static struct rt_messagequeue fota_progress_bar;
  21. /* 消息队列中用到的放置消息的内存池 */
  22. static rt_uint8_t msg_pool[512];
  23. static rt_uint8_t msg_pool_1[256];
  24. /* 事件控制块 */
  25. struct rt_event hmi_event;
  26. /* HMI显示功能函数 */
  27. extern rt_err_t HMI_Display(void *Data, rt_size_t length);
  28. extern rt_err_t HMI_Display_urgent(void *Data, rt_size_t length);
  29. extern void usb_ota(uint8_t argc, char **argv);
  30. extern uint8_t CH376_check(void);
  31. #define app_timeout 66
  32. uint16_t inter_app_timeout_cnt = app_timeout;//超时进入应用计数,30ms时基延时,20s超时时间
  33. /*HMI整形显示功能函数*/
  34. void Display_fun(const char *position,uint16_t value1,uint16_t value2)
  35. {
  36. rt_memset(dis_buf,'\0',100);
  37. if(value1 == NULL)
  38. {
  39. rt_sprintf(dis_buf,position,NULL);
  40. }
  41. else
  42. rt_sprintf(dis_buf,position,value1);
  43. if(value2 == 1)
  44. HMI_Display_urgent(dis_buf,rt_strlen(dis_buf));//存入队列
  45. else if(value2 == NULL)
  46. HMI_Display(dis_buf,rt_strlen(dis_buf));//存入队列
  47. }
  48. /*HMI字符串显示功能函数*/
  49. void Display_fun_str(const char *position,char *str)
  50. {
  51. rt_memset(dis_buf,'\0',100);
  52. if(str == NULL)
  53. {
  54. rt_sprintf(dis_buf,position,NULL);
  55. }
  56. else
  57. rt_sprintf(dis_buf,position,str);
  58. HMI_Display(dis_buf,rt_strlen(dis_buf));//存入队列
  59. }
  60. extern unsigned char Read_CMD[256]; //一帧信息的缓冲区
  61. extern unsigned char Check_CMD(void);
  62. //HMI返回数据解码
  63. uint8_t HMI_Decode(void)//(unsigned char *Read_CMD)
  64. {
  65. static uint32_t i;
  66. char buf[3];
  67. extern uint8_t firstime_flag;
  68. if(Check_CMD() == 1)
  69. {
  70. switch(Read_CMD[0])
  71. {
  72. case 0x12://12 00 FF FF FF // bootloader
  73. switch(Read_CMD[1])
  74. {
  75. case 0x00:
  76. fota_status_send("即将从U盘写入固件", sizeof("即将从U盘写入固件"));
  77. rt_thread_mdelay(1000);
  78. if(CH376_check() == 1)
  79. usb_ota(RT_NULL,RT_NULL);
  80. break;
  81. case 0x01:
  82. fota_status_send("正在恢复出厂固件", sizeof("正在恢复出厂固件"));
  83. rt_thread_mdelay(1000);
  84. if (upgrade_df() == 0)
  85. {
  86. rt_fota_start_application();
  87. }
  88. break;
  89. case 0x02:
  90. fota_status_send("即将进入系统", sizeof("即将进入系统"));
  91. rt_thread_mdelay(1000);
  92. rt_fota_start_application();
  93. break;
  94. case 0x03:
  95. fota_status_send("正在重启", sizeof("正在重启"));
  96. rt_hw_cpu_reset();
  97. break;
  98. case 0x04:
  99. firstime_flag = 0;//强制标志位置零
  100. firstime_clone_df();//覆盖出厂固件
  101. break;
  102. default: break;
  103. }
  104. break;
  105. case 0x66://66 12 00 FF FF FF // bootloader
  106. if(Read_CMD[1] == 0x12)
  107. {
  108. char cache[60] = {0};
  109. sprintf(cache , "bootloader.t3.txt=\"%s %s\"" ,IsReady , Version);
  110. Display_fun(cache,NULL,NULL);
  111. Display_fun("t100.txt=\"0\"",NULL,NULL);
  112. Display_fun("j100.val=0",NULL,NULL);
  113. }
  114. break;
  115. default: break;
  116. }
  117. inter_app_timeout_cnt = app_timeout;
  118. }
  119. }
  120. void fota_status_send(const char *data,uint8_t len)
  121. {
  122. int result;
  123. /* 发送消息到消息队列中 */
  124. //rt_kprintf("rt_mq_send MARK A\n");
  125. result = rt_mq_send(&fota_status,data, len);
  126. if (result != RT_EOK)
  127. {
  128. rt_kprintf("fota_status_send ERR\n");
  129. }
  130. }
  131. void fota_progress_bar_send(unsigned char data)
  132. {
  133. int result;
  134. /* 发送消息到消息队列中 */
  135. //rt_kprintf("rt_mq_send MARK B\n");
  136. result = rt_mq_send(&fota_progress_bar,&data, 1);
  137. if (result != RT_EOK)
  138. {
  139. rt_kprintf("fota_progress_bar_send ERR\n");
  140. }
  141. }
  142. /*HMI协议解码线程*/
  143. static void Hmi_decord_thread_entry(void *parameter)
  144. {
  145. while(1)
  146. {
  147. HMI_Decode();
  148. inter_app_timeout_cnt --;
  149. Display_fun("bootloader.t9.txt=\"%d\"",(inter_app_timeout_cnt / 2),NULL);
  150. if((inter_app_timeout_cnt == 0) || (inter_app_timeout_cnt > app_timeout)) //超出设置范围进入应用模式
  151. {
  152. rt_kprintf("rt_fota_start_application\r\n");
  153. rt_fota_start_application();
  154. }
  155. rt_thread_mdelay(500);
  156. }
  157. }
  158. /*HMI刷新线程*/
  159. static void Hmi_flash_thread_entry(void *parameter)
  160. {
  161. rt_uint32_t e;
  162. char *mq_buf = rt_malloc(100); /*显示缓冲区*/
  163. uint8_t buf = 0;
  164. char buff[3] = "0";
  165. while(1)
  166. {
  167. if(rt_event_recv(&hmi_event, SWITCH_HMI,RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,0, &e) == RT_EOK)
  168. {
  169. rt_pin_write(HIM_POWER_PIN, PIN_HIGH);//保证屏幕上电状态
  170. rt_thread_mdelay(100);
  171. Display_fun("sleep=0",NULL,1);//如果休眠则唤醒
  172. rt_thread_mdelay(30);
  173. Display_fun("page bootloader",NULL,1);
  174. Display_fun("sendme",NULL,NULL);
  175. }
  176. /* 从消息队列中接收消息 */
  177. if(rt_mq_recv(&fota_status, mq_buf, 100, 0) == RT_EOK)
  178. {
  179. Display_fun_str("bootloader.t3.txt=\"%.20s\"","\0");
  180. Display_fun_str("bootloader.t3.txt=\"%s\"",mq_buf);
  181. rt_memset(mq_buf,'\0',100);
  182. }
  183. /* 从消息队列中接收消息 */
  184. if(rt_mq_recv(&fota_progress_bar, &buf, 1, 0) == RT_EOK)
  185. {
  186. sprintf(buff,"%d",buf);
  187. Display_fun_str("t100.txt=\"%s\"",buff);
  188. Display_fun("j100.val=%d",buf,NULL);
  189. }
  190. rt_thread_mdelay(10);
  191. }
  192. }
  193. // HMI页面刷新线程
  194. static int HMI_thread(int argc, char *argv[])
  195. {
  196. rt_err_t ret = RT_EOK;
  197. /* 初始化事件对象 */
  198. ret = rt_event_init(&hmi_event, "event", RT_IPC_FLAG_FIFO);
  199. if (ret != RT_EOK)
  200. {
  201. rt_kprintf("init event failed.\n");
  202. return -1;
  203. }
  204. /* 初始化消息队列 */
  205. ret = rt_mq_init(&fota_status,
  206. "fota_status",
  207. &msg_pool[0], /* 内存池指向 msg_pool */
  208. 100, /* 每个消息的大小是 20 字节 */
  209. sizeof(msg_pool), /* 内存池的大小是 msg_pool 的大小 */
  210. RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
  211. if (ret != RT_EOK)
  212. {
  213. rt_kprintf("init message queue failed.\n");
  214. return -1;
  215. }
  216. ret = rt_mq_init(&fota_progress_bar,
  217. "fota_progress_bar",
  218. &msg_pool_1[0], /* 内存池指向 msg_pool */
  219. 1, /* 每个消息的大小是 20 字节 */
  220. sizeof(msg_pool_1), /* 内存池的大小是 msg_pool 的大小 */
  221. RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
  222. if (ret != RT_EOK)
  223. {
  224. rt_kprintf("init message queue failed.\n");
  225. return -1;
  226. }
  227. /* 创建 serial 线程 */
  228. rt_thread_t thread1 = rt_thread_create("Hmi_decord", Hmi_decord_thread_entry, RT_NULL, 2048, 21, 10);//栈空间不能过小,重启需要载入参数受影响
  229. /* 创建成功则启动线程 */
  230. if (thread1 != RT_NULL)
  231. {
  232. rt_thread_startup(thread1);
  233. }
  234. else
  235. {
  236. ret = RT_ERROR;
  237. }
  238. rt_thread_t thread2 = rt_thread_create("Hmi_flash", Hmi_flash_thread_entry, RT_NULL, 768, 20, 10);
  239. /* 创建成功则启动线程 */
  240. if (thread2 != RT_NULL)
  241. {
  242. rt_thread_startup(thread2);
  243. }
  244. else
  245. {
  246. ret = RT_ERROR;
  247. }
  248. return ret;
  249. }
  250. /* 定时器 1 超时函数 */
  251. static void HIM_POWER_CTL(void *parameter)
  252. {
  253. if(rt_pin_read(HIM_POWER_PIN) == 1)//如果电源控制脚为高表示屏幕上电则开始倒计时
  254. {
  255. HIM_POWER_CNT --;
  256. if(HIM_POWER_CNT == 0)
  257. {
  258. rt_pin_write(HIM_POWER_PIN, PIN_LOW);//计时时间到,关闭屏幕电源
  259. rt_timer_stop(HIM_POWER_PIN_TIMER);
  260. }
  261. }
  262. }
  263. void touch_p(void *args)
  264. {
  265. rt_pin_write(HIM_POWER_PIN, PIN_HIGH);
  266. log_w("touch_p testing............");
  267. HIM_POWER_CNT = 60;//给计数变量赋值60s
  268. rt_timer_start(HIM_POWER_PIN_TIMER);
  269. }
  270. static void touchpad_irq(void)
  271. {
  272. /* 触摸引脚为输出模式 */
  273. rt_pin_mode(15, PIN_MODE_INPUT_PULLUP);
  274. /* 默认高电平 */
  275. rt_pin_write(15, PIN_HIGH);
  276. /* 绑定中断,下降沿模式,回调函数名为touch_p */
  277. rt_pin_attach_irq(15, PIN_IRQ_MODE_FALLING, touch_p, RT_NULL);
  278. /* 使能中断 */
  279. rt_pin_irq_enable(15, PIN_IRQ_ENABLE);
  280. }
  281. int hmi_power_timer(void)
  282. {
  283. rt_pin_mode(HIM_POWER_PIN, PIN_MODE_OUTPUT);
  284. rt_pin_write(HIM_POWER_PIN, PIN_HIGH);//上电开机开启屏幕
  285. HIM_POWER_CNT = 60;//给计数变量赋值60s
  286. /* 创建定时器 1 周期定时器 */
  287. HIM_POWER_PIN_TIMER = rt_timer_create("hmi_p_timer", HIM_POWER_CTL,
  288. RT_NULL, 1000,
  289. RT_TIMER_FLAG_PERIODIC);
  290. /* 启动定时器 1 */
  291. if (HIM_POWER_PIN_TIMER != RT_NULL) rt_timer_start(HIM_POWER_PIN_TIMER);
  292. return 0;
  293. }
  294. INIT_COMPONENT_EXPORT(HMI_thread);
  295. INIT_COMPONENT_EXPORT(touchpad_irq);
  296. INIT_COMPONENT_EXPORT(hmi_power_timer);