hmi_fun.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. upgrade_df();
  85. break;
  86. case 0x02:
  87. fota_status_send("即将进入系统", sizeof("即将进入系统"));
  88. rt_thread_mdelay(1000);
  89. rt_fota_start_application();
  90. break;
  91. case 0x03:
  92. fota_status_send("正在重启", sizeof("正在重启"));
  93. rt_hw_cpu_reset();
  94. break;
  95. case 0x04:
  96. firstime_flag = 0;//强制标志位置零
  97. firstime_clone_df();//覆盖出厂固件
  98. break;
  99. default: break;
  100. }
  101. break;
  102. case 0x66://66 12 00 FF FF FF // bootloader
  103. if(Read_CMD[1] == 0x12)
  104. {
  105. char cache[60] = {0};
  106. sprintf(cache , "bootloader.t3.txt=\"%s %s\"" ,IsReady , Version);
  107. Display_fun(cache,NULL,NULL);
  108. Display_fun("t100.txt=\"0\"",NULL,NULL);
  109. Display_fun("j100.val=0",NULL,NULL);
  110. }
  111. break;
  112. default: break;
  113. }
  114. inter_app_timeout_cnt = app_timeout;
  115. }
  116. }
  117. void fota_status_send(const char *data,uint8_t len)
  118. {
  119. int result;
  120. /* 发送消息到消息队列中 */
  121. //rt_kprintf("rt_mq_send MARK A\n");
  122. result = rt_mq_send(&fota_status,data, len);
  123. if (result != RT_EOK)
  124. {
  125. rt_kprintf("fota_status_send ERR\n");
  126. }
  127. }
  128. void fota_progress_bar_send(unsigned char data)
  129. {
  130. int result;
  131. /* 发送消息到消息队列中 */
  132. //rt_kprintf("rt_mq_send MARK B\n");
  133. result = rt_mq_send(&fota_progress_bar,&data, 1);
  134. if (result != RT_EOK)
  135. {
  136. rt_kprintf("fota_progress_bar_send ERR\n");
  137. }
  138. }
  139. /*HMI协议解码线程*/
  140. static void Hmi_decord_thread_entry(void *parameter)
  141. {
  142. while(1)
  143. {
  144. HMI_Decode();
  145. inter_app_timeout_cnt --;
  146. Display_fun("bootloader.t9.txt=\"%d\"",(inter_app_timeout_cnt / 2),NULL);
  147. if((inter_app_timeout_cnt == 0) || (inter_app_timeout_cnt > app_timeout)) //超出设置范围进入应用模式
  148. {
  149. rt_kprintf("rt_fota_start_application\r\n");
  150. rt_fota_start_application();
  151. }
  152. rt_thread_mdelay(500);
  153. }
  154. }
  155. /*HMI刷新线程*/
  156. static void Hmi_flash_thread_entry(void *parameter)
  157. {
  158. rt_uint32_t e;
  159. char *mq_buf = rt_malloc(100); /*显示缓冲区*/
  160. uint8_t buf = 0;
  161. char buff[3] = "0";
  162. while(1)
  163. {
  164. if(rt_event_recv(&hmi_event, SWITCH_HMI,RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,0, &e) == RT_EOK)
  165. {
  166. rt_pin_write(HIM_POWER_PIN, PIN_HIGH);//保证屏幕上电状态
  167. rt_thread_mdelay(100);
  168. Display_fun("sleep=0",NULL,1);//如果休眠则唤醒
  169. rt_thread_mdelay(30);
  170. Display_fun("page bootloader",NULL,1);
  171. Display_fun("sendme",NULL,NULL);
  172. }
  173. /* 从消息队列中接收消息 */
  174. if(rt_mq_recv(&fota_status, mq_buf, 100, 0) == RT_EOK)
  175. {
  176. Display_fun_str("bootloader.t3.txt=\"%.20s\"","\0");
  177. Display_fun_str("bootloader.t3.txt=\"%s\"",mq_buf);
  178. rt_memset(mq_buf,'\0',100);
  179. }
  180. /* 从消息队列中接收消息 */
  181. if(rt_mq_recv(&fota_progress_bar, &buf, 1, 0) == RT_EOK)
  182. {
  183. sprintf(buff,"%d",buf);
  184. Display_fun_str("t100.txt=\"%s\"",buff);
  185. Display_fun("j100.val=%d",buf,NULL);
  186. }
  187. rt_thread_mdelay(10);
  188. }
  189. }
  190. // HMI页面刷新线程
  191. static int HMI_thread(int argc, char *argv[])
  192. {
  193. rt_err_t ret = RT_EOK;
  194. /* 初始化事件对象 */
  195. ret = rt_event_init(&hmi_event, "event", RT_IPC_FLAG_FIFO);
  196. if (ret != RT_EOK)
  197. {
  198. rt_kprintf("init event failed.\n");
  199. return -1;
  200. }
  201. /* 初始化消息队列 */
  202. ret = rt_mq_init(&fota_status,
  203. "fota_status",
  204. &msg_pool[0], /* 内存池指向 msg_pool */
  205. 100, /* 每个消息的大小是 20 字节 */
  206. sizeof(msg_pool), /* 内存池的大小是 msg_pool 的大小 */
  207. RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
  208. if (ret != RT_EOK)
  209. {
  210. rt_kprintf("init message queue failed.\n");
  211. return -1;
  212. }
  213. ret = rt_mq_init(&fota_progress_bar,
  214. "fota_progress_bar",
  215. &msg_pool_1[0], /* 内存池指向 msg_pool */
  216. 1, /* 每个消息的大小是 20 字节 */
  217. sizeof(msg_pool_1), /* 内存池的大小是 msg_pool 的大小 */
  218. RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
  219. if (ret != RT_EOK)
  220. {
  221. rt_kprintf("init message queue failed.\n");
  222. return -1;
  223. }
  224. /* 创建 serial 线程 */
  225. rt_thread_t thread1 = rt_thread_create("Hmi_decord", Hmi_decord_thread_entry, RT_NULL, 2048, 21, 10);//栈空间不能过小,重启需要载入参数受影响
  226. /* 创建成功则启动线程 */
  227. if (thread1 != RT_NULL)
  228. {
  229. rt_thread_startup(thread1);
  230. }
  231. else
  232. {
  233. ret = RT_ERROR;
  234. }
  235. rt_thread_t thread2 = rt_thread_create("Hmi_flash", Hmi_flash_thread_entry, RT_NULL, 768, 20, 10);
  236. /* 创建成功则启动线程 */
  237. if (thread2 != RT_NULL)
  238. {
  239. rt_thread_startup(thread2);
  240. }
  241. else
  242. {
  243. ret = RT_ERROR;
  244. }
  245. return ret;
  246. }
  247. /* 定时器 1 超时函数 */
  248. static void HIM_POWER_CTL(void *parameter)
  249. {
  250. if(rt_pin_read(HIM_POWER_PIN) == 1)//如果电源控制脚为高表示屏幕上电则开始倒计时
  251. {
  252. HIM_POWER_CNT --;
  253. if(HIM_POWER_CNT == 0)
  254. {
  255. rt_pin_write(HIM_POWER_PIN, PIN_LOW);//计时时间到,关闭屏幕电源
  256. rt_timer_stop(HIM_POWER_PIN_TIMER);
  257. }
  258. }
  259. }
  260. void touch_p(void *args)
  261. {
  262. rt_pin_write(HIM_POWER_PIN, PIN_HIGH);
  263. log_w("touch_p testing............");
  264. HIM_POWER_CNT = 60;//给计数变量赋值60s
  265. rt_timer_start(HIM_POWER_PIN_TIMER);
  266. }
  267. static void touchpad_irq(void)
  268. {
  269. /* 触摸引脚为输出模式 */
  270. rt_pin_mode(15, PIN_MODE_INPUT_PULLUP);
  271. /* 默认高电平 */
  272. rt_pin_write(15, PIN_HIGH);
  273. /* 绑定中断,下降沿模式,回调函数名为touch_p */
  274. rt_pin_attach_irq(15, PIN_IRQ_MODE_FALLING, touch_p, RT_NULL);
  275. /* 使能中断 */
  276. rt_pin_irq_enable(15, PIN_IRQ_ENABLE);
  277. }
  278. int hmi_power_timer(void)
  279. {
  280. rt_pin_mode(HIM_POWER_PIN, PIN_MODE_OUTPUT);
  281. rt_pin_write(HIM_POWER_PIN, PIN_HIGH);//上电开机开启屏幕
  282. HIM_POWER_CNT = 60;//给计数变量赋值60s
  283. /* 创建定时器 1 周期定时器 */
  284. HIM_POWER_PIN_TIMER = rt_timer_create("hmi_p_timer", HIM_POWER_CTL,
  285. RT_NULL, 1000,
  286. RT_TIMER_FLAG_PERIODIC);
  287. /* 启动定时器 1 */
  288. if (HIM_POWER_PIN_TIMER != RT_NULL) rt_timer_start(HIM_POWER_PIN_TIMER);
  289. return 0;
  290. }
  291. INIT_COMPONENT_EXPORT(HMI_thread);
  292. INIT_COMPONENT_EXPORT(touchpad_irq);
  293. INIT_COMPONENT_EXPORT(hmi_power_timer);