Key.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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-20 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 30 // 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, 1, 1}, // 1000ms灭/100ms亮
  70. [ws_wait0] = {10, 2, 2}, // 1000ms灭双闪
  71. [ws_wait_coll] = {10, 10, 1}, // 1000ms周期
  72. [ws_waitTig] = {10, 2, 3}, // 三连闪
  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, OFF);
  126. rt_pin_write(Key_IN, OFF);
  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. if(rt_pin_read(RTU_DTU) != ON)
  149. {
  150. ctr_RTU_DTU(ON);
  151. }
  152. LED_ON();
  153. rt_thread_mdelay(150);
  154. LED_OFF();
  155. rt_thread_mdelay(150);
  156. }
  157. ws_now = res;
  158. GetIn = false;
  159. GetOut = false;
  160. rtu_status ? ctr_RTU_DTU(ON) : ctr_RTU_DTU(OFF);
  161. }
  162. static void led_delay(uint16_t times)
  163. {
  164. for (uint16_t a = 0; a < times; a++)
  165. {
  166. rt_thread_mdelay(DELAY_UNIT);
  167. if (GetIn)
  168. {
  169. ManualMode();
  170. }
  171. }
  172. }
  173. static void led_thread_entry(void *param)
  174. {
  175. int current_mode = 0;
  176. while (1)
  177. {
  178. current_mode = ws_now;
  179. if (ws_now >= ws_takephoto && ws_now <= ws_once_finsh)
  180. {
  181. current_mode = ws_takephoto;
  182. }
  183. LED_OFF();
  184. led_delay(mode_pattern[current_mode].off_duration);
  185. // if(ws_now == 10)
  186. //rt_kprintf("ws now == tig : %d-%d-%d\n" , mode_pattern[current_mode].off_duration , mode_pattern[current_mode].on_duration,mode_pattern[current_mode].blink_count);
  187. if (mode_pattern[current_mode].blink_count > 1)
  188. {
  189. for (int i = 0; i < mode_pattern[current_mode].blink_count; i++)
  190. {
  191. LED_ON();
  192. led_delay(mode_pattern[current_mode].on_duration);
  193. if (current_mode != ws_now && current_mode != ws_takephoto)
  194. if (current_mode != ws_now)
  195. break;
  196. LED_OFF();
  197. led_delay(mode_pattern[current_mode].on_duration);
  198. }
  199. }
  200. else
  201. {
  202. LED_ON();
  203. led_delay(mode_pattern[current_mode].on_duration);
  204. }
  205. //rt_kprintf("current_mode > %d \n", current_mode);
  206. rt_thread_mdelay(100);
  207. }
  208. }
  209. int led_init(void)
  210. {
  211. rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT);
  212. rt_thread_t tid = rt_thread_create("led", led_thread_entry,
  213. RT_NULL, 512, 20, 10);
  214. return tid ? rt_thread_startup(tid) : -1;
  215. }
  216. INIT_APP_EXPORT(led_init);
  217. static void Key_thread_entry(void *param)
  218. {
  219. ExtKeyInit();
  220. while (1)
  221. {
  222. while (KeyTigCnt == 0)
  223. {
  224. rt_thread_mdelay(10);
  225. }
  226. for (char a = 0; a < 100; a++)
  227. {
  228. rt_thread_mdelay(30);
  229. if (KeyTigCnt >= 3)
  230. {
  231. break;
  232. }
  233. }
  234. if (KeyTigCnt >= 3 && GetIn != true)
  235. {
  236. rt_kprintf("KeyTigCnt GetIn\n");
  237. GetIn = true;
  238. KeyTigCnt = 0;
  239. }
  240. else if (KeyTigCnt >= 3 && GetOut != true)
  241. {
  242. rt_kprintf("KeyTigCnt GetOut\n");
  243. GetOut = true;
  244. KeyTigCnt = 0;
  245. }
  246. else
  247. {
  248. KeyTigCnt = 0;
  249. }
  250. }
  251. }
  252. void KeyThreadStart(void)
  253. {
  254. rt_thread_t Key_thread = rt_thread_create("Key_thread",
  255. Key_thread_entry,
  256. RT_NULL,
  257. KEY_THREAD_STACK_SIZE,
  258. KEY_THREAD_PRIORITY,
  259. KEY_THREAD_TIMESLICE);
  260. if (Key_thread != RT_NULL)
  261. {
  262. rt_thread_startup(Key_thread);
  263. }
  264. else
  265. {
  266. rt_kprintf("Key_thread start faild \n");
  267. }
  268. }
  269. INIT_APP_EXPORT(KeyThreadStart);
  270. #endif /* APPLICATIONS_KEY_C_ */