Key.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2025-03-24 RuoFeng the first version
  9. */
  10. #ifndef APPLICATIONS_KEY_C_
  11. #define APPLICATIONS_KEY_C_
  12. /*********************↓ define ↓************************/
  13. #define DBG_ENABLE
  14. #define DBG_COLOR
  15. #define DBG_TAG "key"
  16. #define DBG_LEVEL DBG_INFO
  17. #include <rtdbg.h>
  18. #define KEY_THREAD_PRIORITY 11 // 线程优先级
  19. #define KEY_THREAD_STACK_SIZE 512 // 线程栈空间大小
  20. #define KEY_THREAD_TIMESLICE 10 // 时间片
  21. #define LED_PIN Key_OUT
  22. #define LED_ON() rt_pin_write(LED_PIN, PIN_HIGH)
  23. #define LED_OFF() rt_pin_write(LED_PIN, PIN_LOW)
  24. #define DELAY_UNIT 100
  25. #define ManualTime 60 // mins
  26. /*********************↑ define ↑************************/
  27. /**
  28. *
  29. *
  30. *
  31. *
  32. * */
  33. /*********************↓ include ↓************************/
  34. #include <Key.h>
  35. /*********************↑ include ↑************************/
  36. /**
  37. *
  38. *
  39. *
  40. *
  41. * */
  42. /*********************↓ extern ↓************************/
  43. extern works_def ws_now;
  44. /*********************↑ extern ↑************************/
  45. /**
  46. *
  47. *
  48. *
  49. *
  50. * */
  51. /********************↓ declaration ↓***********************/
  52. static rt_timer_t KeyTimer; // 定时器命名
  53. static volatile bool GetIn = false;
  54. static volatile bool GetOut = false;
  55. /**
  56. *
  57. *
  58. *
  59. */
  60. /* 模式参数结构体 */
  61. typedef struct
  62. {
  63. rt_uint16_t off_duration;
  64. rt_uint16_t on_duration;
  65. rt_uint8_t blink_count;
  66. } ModePattern;
  67. /* 模式参数映射表 */
  68. static const ModePattern mode_pattern[ws_typemax] = {
  69. [ws_standby] = {10, 2, 1}, // 1000ms灭/200ms亮
  70. [ws_wait0] = {10, 2, 2}, // 1000ms灭 双闪
  71. [ws_wait_coll] = {10, 10, 1}, // 1000ms周期
  72. // [ws_waitTig] = {10, 2, 3}, // 1000ms灭 三连闪
  73. [ws_KeyTig] = {3, 1, 1}, // 300ms灭/100ms亮
  74. [ws_takephoto] = {5, 30, 1}, // 拍照相关模式
  75. };
  76. /********************↑ declaration ↑***********************/
  77. /**
  78. *
  79. *
  80. *
  81. *
  82. * */
  83. /*********************↓ global variables ↓************************/
  84. volatile uint16_t KeyTigCnt = 0; // 按键触发计数
  85. static rt_bool_t KeyTrigFlag = RT_FALSE;
  86. /*********************↑ global variables ↑************************/
  87. /**
  88. *
  89. *
  90. *
  91. *
  92. * */
  93. /********** Key Drivers ************/
  94. /* 防抖定时器回调 */
  95. static void KeyHandler(void *param)
  96. {
  97. if (KeyTrigFlag == RT_TRUE)
  98. {
  99. rt_timer_stop(KeyTimer);
  100. KeyTigCnt++;
  101. rt_kprintf("[KeyFlags] get > %d\n", KeyTigCnt);
  102. }
  103. KeyTrigFlag = RT_FALSE;
  104. }
  105. static rt_err_t KeyIrqHandler(void *args)
  106. {
  107. // rt_kprintf("[Key] A activated! \n");
  108. if (KeyTrigFlag != RT_TRUE)
  109. {
  110. KeyTrigFlag = RT_TRUE;
  111. rt_timer_start(KeyTimer);
  112. }
  113. return RT_EOK;
  114. }
  115. /**
  116. * @brief 初始化按键
  117. *
  118. * @return int
  119. */
  120. int ExtKeyInit(void)
  121. {
  122. /* GPIO模式配置 */
  123. rt_pin_mode(Key_IN, PIN_MODE_INPUT_PULLDOWN);
  124. rt_pin_mode(Key_OUT, PIN_MODE_OUTPUT);
  125. rt_pin_write(Key_OUT, 1);
  126. rt_pin_write(Key_IN, 0);
  127. rt_pin_attach_irq(Key_IN, PIN_IRQ_MODE_RISING, KeyIrqHandler, RT_NULL);
  128. rt_pin_irq_enable(Key_IN, PIN_IRQ_ENABLE);
  129. KeyTimer = rt_timer_create("KeyTimer",
  130. KeyHandler,
  131. RT_NULL,
  132. 99, // 防抖时间
  133. RT_TIMER_FLAG_ONE_SHOT);
  134. rt_kprintf("[Key] trigger initialized!\n");
  135. return RT_EOK;
  136. }
  137. /********** Key Drivers ************/
  138. static void ManualMode(void)
  139. {
  140. works_def res = ws_standby;
  141. bool rtu_status = rt_pin_read(RTU_DTU);
  142. uint16_t cnt = 0;
  143. res = ws_now;
  144. while (cnt++ < (ManualTime * 200) && GetOut == false)
  145. {
  146. //rt_kprintf("ManualModeExc\n");
  147. ws_now = ws_KeyTig;
  148. ctr_RTU_DTU(ON);
  149. LED_ON();
  150. rt_thread_mdelay(150);
  151. LED_OFF();
  152. rt_thread_mdelay(150);
  153. }
  154. ws_now = res;
  155. if(ws_now == ws_KeyTig)
  156. {
  157. ws_now = ws_standby;
  158. }
  159. GetIn = false;
  160. GetOut = false;
  161. rtu_status ? ctr_RTU_DTU(ON) : ctr_RTU_DTU(OFF);
  162. }
  163. static void led_delay(uint16_t times)
  164. {
  165. for (uint16_t a = 0; a < times; a++)
  166. {
  167. rt_thread_mdelay(DELAY_UNIT);
  168. if (GetIn)
  169. {
  170. ManualMode();
  171. }
  172. }
  173. }
  174. static void led_thread_entry(void *param)
  175. {
  176. int current_mode = 0;
  177. while (1)
  178. {
  179. current_mode = ws_now;
  180. if (ws_now >= ws_takephoto && ws_now < ws_KeyTig)
  181. {
  182. current_mode = ws_takephoto;
  183. }
  184. LED_OFF();
  185. led_delay(mode_pattern[current_mode].off_duration);
  186. if (mode_pattern[current_mode].blink_count > 1)
  187. {
  188. for (int i = 0; i < mode_pattern[current_mode].blink_count; i++)
  189. {
  190. LED_ON();
  191. led_delay(mode_pattern[current_mode].on_duration);
  192. if (current_mode != ws_now && current_mode != ws_takephoto)
  193. if (current_mode != ws_now)
  194. break;
  195. LED_OFF();
  196. led_delay(mode_pattern[current_mode].on_duration);
  197. }
  198. }
  199. else
  200. {
  201. LED_ON();
  202. led_delay(mode_pattern[current_mode].on_duration);
  203. }
  204. //rt_kprintf("current_mode > %d \n", current_mode);
  205. rt_thread_mdelay(100);
  206. }
  207. }
  208. int led_init(void)
  209. {
  210. rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT);
  211. rt_thread_t tid = rt_thread_create("led", led_thread_entry,
  212. RT_NULL, 512, 20, 10);
  213. return tid ? rt_thread_startup(tid) : -1;
  214. }
  215. INIT_APP_EXPORT(led_init);
  216. static void Key_thread_entry(void *param)
  217. {
  218. ExtKeyInit();
  219. while (1)
  220. {
  221. while (KeyTigCnt == 0)
  222. {
  223. rt_thread_mdelay(10);
  224. }
  225. for (char a = 0; a < 100; a++)
  226. {
  227. rt_thread_mdelay(30);
  228. if (KeyTigCnt >= 3)
  229. {
  230. break;
  231. }
  232. }
  233. if (KeyTigCnt >= 3 && GetIn != true)
  234. {
  235. rt_kprintf("KeyTigCnt GetIn\n");
  236. GetIn = true;
  237. KeyTigCnt = 0;
  238. }
  239. else if (KeyTigCnt >= 3 && GetOut != true)
  240. {
  241. rt_kprintf("KeyTigCnt GetOut\n");
  242. GetOut = true;
  243. KeyTigCnt = 0;
  244. }
  245. else
  246. {
  247. KeyTigCnt = 0;
  248. }
  249. }
  250. }
  251. void KeyThreadStart(void)
  252. {
  253. rt_thread_t Key_thread = rt_thread_create("Key_thread",
  254. Key_thread_entry,
  255. RT_NULL,
  256. KEY_THREAD_STACK_SIZE,
  257. KEY_THREAD_PRIORITY,
  258. KEY_THREAD_TIMESLICE);
  259. if (Key_thread != RT_NULL)
  260. {
  261. rt_thread_startup(Key_thread);
  262. }
  263. else
  264. {
  265. rt_kprintf("Key_thread start faild \n");
  266. }
  267. }
  268. INIT_APP_EXPORT(KeyThreadStart);
  269. #endif /* APPLICATIONS_KEY_C_ */