gpio_ctrl.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. /*
  2. * GPIO控制模块 - v3.5 (中断+轮询版+组合键)
  3. *
  4. * 功能:
  5. * - 灯管开关控制(PB5)
  6. * - 高压输出控制(PB6)
  7. * - 按键A检测(PB4,下降沿中断触发)
  8. * - 按键B检测(PB3,下降沿中断触发)
  9. * - 中断记录时间,线程轮询电平判断长按/短按
  10. * - 双按键组合长按5秒切换光控模式来源
  11. *
  12. * Version: 3.5.2
  13. */
  14. /*********************↓ include ↓************************/
  15. #define DBG_TAG "GPIO"
  16. #define DBG_LVL DBG_INFO
  17. #include <rtdbg.h>
  18. #include <rtthread.h>
  19. #include <rtdevice.h>
  20. #include <board.h>
  21. #include <string.h>
  22. #include "ecode.h"
  23. #include "ym310_protecl.h"
  24. /*********************↑ include ↑************************/
  25. /*********************↓ define ↓************************/
  26. /* 灯管开关 */
  27. #define LAMP_PIN GET_PIN(B, 5)
  28. #define LAMP_ON PIN_HIGH
  29. #define LAMP_OFF PIN_LOW
  30. /* 高压输出 */
  31. #define HV_PIN GET_PIN(B, 6)
  32. #define HV_ON PIN_HIGH
  33. #define HV_OFF PIN_LOW
  34. /* 按键 */
  35. #define KEY_A_PIN GET_PIN(B, 4) /* PB4 - 按键A(开关键) */
  36. #define KEY_B_PIN GET_PIN(B, 3) /* PB3 - 按键B(设置键) */
  37. #define KEY_DEBOUNCE_MS 90 /* 防抖时间 */
  38. #define KEY_LONG_MS 5000 /* 5秒长按 */
  39. #define KEY_COMBINED_LONG_MS 5000 /* 组合键长按时间:5秒 */
  40. #define KEY_COMBINED_WINDOW_MS 1000 /* 组合键时间窗口:1秒 */
  41. /* 测试模式时长 */
  42. #define TEST_MODE_MS (60 * 1000)
  43. /* 光控时长范围 */
  44. #define LIGHT_DUR_DEFAULT 4
  45. #define LIGHT_DUR_MAX 8
  46. #define LIGHT_DUR_MIN 0
  47. /* 4G电源 */
  48. #define LTE_PIN GET_PIN(B, 2)
  49. #define LTE_ON PIN_HIGH
  50. #define LTE_OFF PIN_LOW
  51. /*********************↑ define ↑************************/
  52. /*********************↓ 全局变量 ↓************************/
  53. static rt_bool_t s_lamp_state = RT_FALSE;
  54. static rt_bool_t s_hv_state = RT_FALSE;
  55. /* 按键A */
  56. static volatile rt_tick_t s_key_a_press_tick = 0;
  57. static volatile rt_uint8_t s_key_a_pending = 0;
  58. /* 按键B */
  59. static volatile rt_tick_t s_key_b_press_tick = 0;
  60. static volatile rt_uint8_t s_key_b_pending = 0;
  61. static rt_timer_t s_test_timer = RT_NULL;
  62. volatile char KEY_IN_TEST_TIME = 0;
  63. /* 组合键检测 */
  64. static volatile rt_tick_t s_key_combined_press_tick_a = 0;
  65. static volatile rt_tick_t s_key_combined_press_tick_b = 0;
  66. static volatile rt_uint8_t s_key_combined_pending = 0;
  67. static volatile rt_uint8_t s_key_a_pressed_in_combined = 0;
  68. static volatile rt_uint8_t s_key_b_pressed_in_combined = 0;
  69. static volatile rt_uint8_t s_key_combined_valid = 0; /* 组合键是否有效(时间窗口内) */
  70. static volatile rt_uint8_t s_key_combined_mode = 0; /* 组合键模式激活,屏蔽单个按键 */
  71. static volatile rt_uint8_t s_key_combined_matched = 0; /* 组合键已匹配(间隔<1秒),本次按下次不再处理单个按键 */
  72. /*********************↑ 全局变量 ↑************************/
  73. void gpio_lamp_ctrl(rt_bool_t on);
  74. void gpio_hv_ctrl(rt_bool_t on);
  75. void gpio_lte_ctrl(rt_bool_t on);
  76. void once_test_tig(void);
  77. /*********************↓ 外部函数 ↓************************/
  78. extern void led_trigger_set_effect(void);
  79. extern void save_dev_info(void);
  80. extern void ym310_data_report(rt_uint8_t client_idx);
  81. extern int ym310_warn_report(const char *detail);
  82. extern uint8_t g_work_state;
  83. /*********************↑ 外部函数 ↑************************/
  84. /*********************↓ 按键处理函数 ↓************************/
  85. static void led_effect(void)
  86. {
  87. led_trigger_set_effect();
  88. }
  89. static void test_mode_timeout(void *param)
  90. {
  91. rt_pin_write(LAMP_PIN, LAMP_OFF);
  92. rt_pin_write(HV_PIN, HV_OFF);
  93. s_lamp_state = RT_FALSE;
  94. s_hv_state = RT_FALSE;
  95. LOG_I("Test mode ended");
  96. KEY_IN_TEST_TIME = 0;
  97. }
  98. /* 按键A:单击 - 切换开关机状态 */
  99. static void key_a_short(void)
  100. {
  101. char warn_buf[128];
  102. dev_info.power_state = (dev_info.power_state == 1) ? 0 : 1;
  103. save_dev_info();
  104. led_effect();
  105. rt_snprintf(warn_buf, sizeof(warn_buf), "开关键:开关机状态=%d", dev_info.power_state);
  106. LOG_I("%s", warn_buf);
  107. ym310_warn_report(warn_buf);
  108. report_now();
  109. }
  110. /* 按键A:长按5秒 - 光控工作时长重置为4小时 */
  111. static void key_a_long(void)
  112. {
  113. char warn_buf[128];
  114. dev_info.light_work_duration = LIGHT_DUR_DEFAULT;
  115. save_dev_info();
  116. led_effect();
  117. rt_snprintf(warn_buf, sizeof(warn_buf), "开关键:复位光控时长=%d", dev_info.light_work_duration);
  118. LOG_I("%s", warn_buf);
  119. ym310_warn_report(warn_buf);
  120. if ((dev_info.power_state == 1) && (dev_info.time_ctrl_enable == 0) && (g_work_state != 0)) // 生效工作状态
  121. {
  122. // rt_kprintf("g_work_state >> %d \n", g_work_state);
  123. dev_info.power_state = 0;
  124. rt_thread_mdelay(300);
  125. dev_info.power_state = 1;
  126. Recode_e_code(e_CMD_restartwork, 1, "按键指令:复位工作状态");
  127. }
  128. report_now();
  129. }
  130. /**
  131. * @brief 手动触发一次测试(高压和灯管通电60秒)
  132. */
  133. void once_test_tig(void)
  134. {
  135. rt_pin_write(LAMP_PIN, LAMP_ON);
  136. rt_pin_write(HV_PIN, HV_ON);
  137. s_lamp_state = RT_TRUE;
  138. s_hv_state = RT_TRUE;
  139. KEY_IN_TEST_TIME = VALID;
  140. rt_kprintf("KEY_IN_TEST_TIME = VAILD\n");
  141. rt_timer_start(s_test_timer);
  142. }
  143. /* 按键B:单击 - 开启测试模式 */
  144. static void key_b_short(void)
  145. {
  146. char warn_buf[128];
  147. once_test_tig();
  148. led_effect();
  149. rt_snprintf(warn_buf, sizeof(warn_buf), "设置键:开启测试模式");
  150. LOG_I("%s", warn_buf);
  151. ym310_warn_report(warn_buf);
  152. }
  153. /* 按键B:长按5秒 - 光控时长+1(0-8循环) */
  154. static void key_b_long(void)
  155. {
  156. char warn_buf[128];
  157. uint16_t dur = dev_info.light_work_duration + 1;
  158. if (dur > LIGHT_DUR_MAX)
  159. dur = LIGHT_DUR_MIN;
  160. dev_info.light_work_duration = dur;
  161. save_dev_info();
  162. led_effect();
  163. rt_snprintf(warn_buf, sizeof(warn_buf), "设置键:光控时长=%d", dev_info.light_work_duration);
  164. LOG_I("%s", warn_buf);
  165. ym310_warn_report(warn_buf);
  166. if ((dev_info.power_state == 1) && (dev_info.time_ctrl_enable == 0) && (g_work_state != 0)) // 生效工作状态
  167. {
  168. // rt_kprintf("g_work_state >> %d \n", g_work_state);
  169. dev_info.power_state = 0;
  170. rt_thread_mdelay(300);
  171. dev_info.power_state = 1;
  172. Recode_e_code(e_CMD_restartwork, 2, "按键指令:复位工作状态");
  173. }
  174. report_now();
  175. }
  176. /**
  177. * @brief 组合键长按回调(两个按键同时按下5秒)
  178. * 切换光控模式来源
  179. */
  180. static void key_combined_long(void)
  181. {
  182. char warn_buf[128];
  183. if (dev_info.light_flag == 0)
  184. {
  185. dev_info.light_flag = 1;
  186. rt_snprintf(warn_buf, sizeof(warn_buf), "组合键:光控源=光敏管");
  187. }
  188. else
  189. {
  190. dev_info.light_flag = 0;
  191. rt_snprintf(warn_buf, sizeof(warn_buf), "组合键:光控源=太阳能板");
  192. }
  193. save_dev_info();
  194. LOG_I("%s", warn_buf);
  195. ym310_warn_report(warn_buf);
  196. led_effect();
  197. report_now();
  198. }
  199. /*********************↑ 按键处理函数 ↑************************/
  200. /*********************↓ 中断服务函数 ↓************************/
  201. static void key_a_isr(void *args)
  202. {
  203. rt_tick_t now = rt_tick_get();
  204. /* 记录按下时间,标记有待处理按键(pending=1表示等待消抖检测) */
  205. s_key_a_press_tick = now;
  206. s_key_a_pending = 1;
  207. /* 记录组合键按下时间 */
  208. s_key_combined_press_tick_a = now;
  209. s_key_a_pressed_in_combined = 1;
  210. /* 检查组合键有效性:两个按键按下的时间差是否在1秒内 */
  211. rt_tick_t diff;
  212. if (s_key_b_pressed_in_combined)
  213. {
  214. diff = (now > s_key_combined_press_tick_b)
  215. ? (now - s_key_combined_press_tick_b)
  216. : (s_key_combined_press_tick_b - now);
  217. if (diff < rt_tick_from_millisecond(KEY_COMBINED_WINDOW_MS))
  218. {
  219. s_key_combined_valid = 1;
  220. s_key_combined_pending = 1;
  221. s_key_combined_mode = 1;
  222. s_key_combined_matched = 1;
  223. /* 组合键已匹配,立即清除单个按键的pending,防止线程提前处理 */
  224. s_key_a_pending = 0;
  225. s_key_b_pending = 0;
  226. }
  227. else
  228. {
  229. s_key_combined_valid = 0;
  230. }
  231. }
  232. }
  233. static void key_b_isr(void *args)
  234. {
  235. rt_tick_t now = rt_tick_get();
  236. /* 记录按下时间,标记有待处理按键(pending=1表示等待消抖检测) */
  237. s_key_b_press_tick = now;
  238. s_key_b_pending = 1;
  239. /* 记录组合键按下时间 */
  240. s_key_combined_press_tick_b = now;
  241. s_key_b_pressed_in_combined = 1;
  242. /* 检查组合键有效性:两个按键按下的时间差是否在1秒内 */
  243. rt_tick_t diff;
  244. if (s_key_a_pressed_in_combined)
  245. {
  246. diff = (now > s_key_combined_press_tick_a)
  247. ? (now - s_key_combined_press_tick_a)
  248. : (s_key_combined_press_tick_a - now);
  249. if (diff < rt_tick_from_millisecond(KEY_COMBINED_WINDOW_MS))
  250. {
  251. s_key_combined_valid = 1;
  252. s_key_combined_pending = 1;
  253. s_key_combined_mode = 1;
  254. s_key_combined_matched = 1;
  255. /* 组合键已匹配,立即清除单个按键的pending,防止线程提前处理 */
  256. s_key_a_pending = 0;
  257. s_key_b_pending = 0;
  258. }
  259. else
  260. {
  261. s_key_combined_valid = 0;
  262. }
  263. }
  264. }
  265. /*********************↑ 中断服务函数 ↑************************/
  266. /*********************↓ 按键检测线程 ↓************************/
  267. static void key_thread_entry(void *param)
  268. {
  269. rt_tick_t now;
  270. rt_tick_t duration;
  271. int pin_level;
  272. rt_uint8_t combined_active = 0;
  273. while (1)
  274. {
  275. combined_active = 0;
  276. /* ========== 优先处理组合键 ========== */
  277. if (s_key_combined_pending && s_key_combined_valid)
  278. {
  279. /* 再次清空单个按键的pending,确保安全 */
  280. s_key_a_pending = 0;
  281. s_key_b_pending = 0;
  282. /* 等待防抖时间后检测 */
  283. rt_thread_mdelay(KEY_DEBOUNCE_MS);
  284. int pin_a_level = rt_pin_read(KEY_A_PIN);
  285. int pin_b_level = rt_pin_read(KEY_B_PIN);
  286. /* 防抖检测:两个电平都应该是低电平才是有效按下 */
  287. if (pin_a_level == PIN_LOW && pin_b_level == PIN_LOW)
  288. {
  289. /* 两个按键都按着,组合键模式激活,等待5秒 */
  290. while (rt_pin_read(KEY_A_PIN) == PIN_LOW && rt_pin_read(KEY_B_PIN) == PIN_LOW)
  291. {
  292. now = rt_tick_get();
  293. /* 取两个按键中较晚按下的时间作为起始点 */
  294. rt_tick_t press_start = (s_key_combined_press_tick_a > s_key_combined_press_tick_b)
  295. ? s_key_combined_press_tick_a
  296. : s_key_combined_press_tick_b;
  297. duration = now - press_start;
  298. if (duration >= rt_tick_from_millisecond(KEY_COMBINED_LONG_MS))
  299. {
  300. key_combined_long();
  301. s_key_combined_pending = 0;
  302. s_key_combined_valid = 0;
  303. s_key_combined_mode = 0;
  304. s_key_combined_matched = 0;
  305. LOG_D("Combined keys long press (%d ms)", duration);
  306. goto clear_flags;
  307. }
  308. rt_thread_mdelay(10);
  309. }
  310. /* 5秒内松开,取消组合键 */
  311. LOG_I("Combined key cancelled: released before 5s");
  312. s_key_combined_pending = 0;
  313. s_key_combined_valid = 0;
  314. s_key_combined_mode = 0;
  315. s_key_combined_matched = 0;
  316. }
  317. else
  318. {
  319. /* 防抖后电平已恢复,放弃此次组合键触发 */
  320. LOG_I("Combined key debounce failed: released before validation");
  321. s_key_combined_pending = 0;
  322. s_key_combined_valid = 0;
  323. s_key_combined_mode = 0;
  324. s_key_combined_matched = 0;
  325. }
  326. /* 跳过单个按键处理 */
  327. goto clear_flags;
  328. }
  329. else if (s_key_combined_pending && !s_key_combined_valid)
  330. {
  331. /* 组合键无效(时间差超过1秒),清除组合键标记,不激活组合键模式 */
  332. s_key_combined_pending = 0;
  333. s_key_combined_mode = 0;
  334. s_key_combined_matched = 0;
  335. /* 清除单个按键的待处理标记 */
  336. s_key_a_pending = 0;
  337. s_key_b_pending = 0;
  338. }
  339. /* ========== 处理按键A(仅在非组合键模式下) ========== */
  340. if (!s_key_combined_mode && s_key_a_pending)
  341. {
  342. /* 等待防抖时间 */
  343. rt_thread_mdelay(KEY_DEBOUNCE_MS);
  344. /* 防抖后再次检查组合键模式,如果被激活则放弃 */
  345. if (s_key_combined_mode)
  346. {
  347. s_key_a_pending = 0;
  348. goto skip_a;
  349. }
  350. if (rt_pin_read(KEY_A_PIN) == PIN_LOW)
  351. {
  352. /* 有效按下,等待松开或判断长按 */
  353. while (rt_pin_read(KEY_A_PIN) == PIN_LOW)
  354. {
  355. /* 实时检查组合键模式,如果被激活则立即退出放弃 */
  356. if (s_key_combined_mode)
  357. {
  358. s_key_a_pending = 0;
  359. goto skip_a;
  360. }
  361. now = rt_tick_get();
  362. duration = now - s_key_a_press_tick;
  363. if (duration >= rt_tick_from_millisecond(KEY_LONG_MS))
  364. {
  365. /* 长按前再次检查组合键模式 */
  366. if (s_key_combined_mode)
  367. {
  368. s_key_a_pending = 0;
  369. goto skip_a;
  370. }
  371. key_a_long();
  372. s_key_a_pending = 0;
  373. LOG_D("KeyA long press (%d ms)", duration);
  374. goto skip_a;
  375. }
  376. rt_thread_mdelay(10);
  377. }
  378. /* 松开后判断是否是短按(仅在非组合键模式下) */
  379. if (s_key_a_pending && !s_key_combined_mode)
  380. {
  381. now = rt_tick_get();
  382. duration = now - s_key_a_press_tick;
  383. if (duration < rt_tick_from_millisecond(KEY_LONG_MS))
  384. {
  385. key_a_short();
  386. LOG_D("KeyA short press (%d ms)", duration);
  387. }
  388. s_key_a_pending = 0;
  389. }
  390. else if (s_key_a_pending && s_key_combined_mode)
  391. {
  392. s_key_a_pending = 0;
  393. }
  394. }
  395. else
  396. {
  397. /* 防抖后电平已恢复,放弃此次触发 */
  398. LOG_I("KeyA debounce failed: released before validation");
  399. s_key_a_pending = 0;
  400. }
  401. }
  402. else if (s_key_combined_mode && s_key_a_pending)
  403. {
  404. /* 组合键模式下,清除A的待处理标记,不触发任何功能 */
  405. s_key_a_pending = 0;
  406. }
  407. skip_a:
  408. /* ========== 处理按键B(仅在非组合键模式下) ========== */
  409. if (!s_key_combined_mode && s_key_b_pending)
  410. {
  411. /* 等待防抖时间 */
  412. rt_thread_mdelay(KEY_DEBOUNCE_MS);
  413. /* 防抖后再次检查组合键模式,如果被激活则放弃 */
  414. if (s_key_combined_mode)
  415. {
  416. s_key_b_pending = 0;
  417. goto skip_b;
  418. }
  419. if (rt_pin_read(KEY_B_PIN) == PIN_LOW)
  420. {
  421. /* 有效按下,等待松开或判断长按 */
  422. while (rt_pin_read(KEY_B_PIN) == PIN_LOW)
  423. {
  424. /* 实时检查组合键模式,如果被激活则立即退出放弃 */
  425. if (s_key_combined_mode)
  426. {
  427. s_key_b_pending = 0;
  428. goto skip_b;
  429. }
  430. now = rt_tick_get();
  431. duration = now - s_key_b_press_tick;
  432. if (duration >= rt_tick_from_millisecond(KEY_LONG_MS))
  433. {
  434. /* 长按前再次检查组合键模式 */
  435. if (s_key_combined_mode)
  436. {
  437. s_key_b_pending = 0;
  438. goto skip_b;
  439. }
  440. key_b_long();
  441. s_key_b_pending = 0;
  442. LOG_D("KeyB long press (%d ms)", duration);
  443. goto skip_b;
  444. }
  445. rt_thread_mdelay(10);
  446. }
  447. /* 松开后判断是否是短按(仅在非组合键模式下) */
  448. if (s_key_b_pending && !s_key_combined_mode)
  449. {
  450. now = rt_tick_get();
  451. duration = now - s_key_b_press_tick;
  452. if (duration < rt_tick_from_millisecond(KEY_LONG_MS))
  453. {
  454. key_b_short();
  455. LOG_D("KeyB short press (%d ms)", duration);
  456. }
  457. s_key_b_pending = 0;
  458. }
  459. else if (s_key_b_pending && s_key_combined_mode)
  460. {
  461. s_key_b_pending = 0;
  462. }
  463. }
  464. else
  465. {
  466. /* 防抖后电平已恢复,放弃此次触发 */
  467. LOG_I("KeyB debounce failed: released before validation");
  468. s_key_b_pending = 0;
  469. }
  470. }
  471. else if (s_key_combined_mode && s_key_b_pending)
  472. {
  473. /* 组合键模式下,清除B的待处理标记,不触发任何功能 */
  474. s_key_b_pending = 0;
  475. }
  476. skip_b:
  477. clear_flags:
  478. /* 组合键匹配有效期内:强制禁止单个按键处理(双重保险) */
  479. if (s_key_combined_matched)
  480. {
  481. s_key_a_pending = 0;
  482. s_key_b_pending = 0;
  483. }
  484. /* 清除组合键按下状态标记(当按键松开时) */
  485. if (rt_pin_read(KEY_A_PIN) == PIN_HIGH)
  486. {
  487. s_key_a_pressed_in_combined = 0;
  488. }
  489. if (rt_pin_read(KEY_B_PIN) == PIN_HIGH)
  490. {
  491. s_key_b_pressed_in_combined = 0;
  492. }
  493. /* 如果两个按键都已松开,清除有效性标记和组合键模式 */
  494. if (s_key_a_pressed_in_combined == 0 && s_key_b_pressed_in_combined == 0)
  495. {
  496. s_key_combined_valid = 0;
  497. s_key_combined_mode = 0;
  498. s_key_combined_matched = 0;
  499. }
  500. rt_thread_mdelay(10);
  501. }
  502. }
  503. /*********************↑ 按键检测线程 ↑************************/
  504. /*********************↓ 初始化 ↓************************/
  505. static void gpio_init(void)
  506. {
  507. /* 初始化灯管 */
  508. rt_pin_mode(LAMP_PIN, PIN_MODE_OUTPUT);
  509. rt_pin_write(LAMP_PIN, LAMP_OFF);
  510. /* 初始化高压 */
  511. rt_pin_mode(HV_PIN, PIN_MODE_OUTPUT);
  512. rt_pin_write(HV_PIN, HV_OFF);
  513. /* 初始化4G */
  514. rt_pin_mode(LTE_PIN, PIN_MODE_OUTPUT);
  515. rt_pin_write(LTE_PIN, LTE_ON);
  516. /* 初始化按键引脚(上拉输入) */
  517. rt_pin_mode(KEY_A_PIN, PIN_MODE_INPUT_PULLUP);
  518. rt_pin_mode(KEY_B_PIN, PIN_MODE_INPUT_PULLUP);
  519. }
  520. static void key_init(void)
  521. {
  522. /* 绑定中断 */
  523. rt_pin_attach_irq(KEY_A_PIN, PIN_IRQ_MODE_FALLING, key_a_isr, NULL);
  524. rt_pin_attach_irq(KEY_B_PIN, PIN_IRQ_MODE_FALLING, key_b_isr, NULL);
  525. /* 使能中断 */
  526. rt_pin_irq_enable(KEY_A_PIN, PIN_IRQ_ENABLE);
  527. rt_pin_irq_enable(KEY_B_PIN, PIN_IRQ_ENABLE);
  528. /* 创建测试模式定时器 */
  529. s_test_timer = rt_timer_create("test_mode", test_mode_timeout, NULL,
  530. TEST_MODE_MS, RT_TIMER_FLAG_ONE_SHOT);
  531. if (s_test_timer == RT_NULL)
  532. {
  533. LOG_E("Failed to create test timer");
  534. }
  535. }
  536. static void key_thread_start(void)
  537. {
  538. rt_thread_t tid = rt_thread_create("key_mon", key_thread_entry, NULL,
  539. 2048, 15, 10);
  540. if (tid)
  541. rt_thread_startup(tid);
  542. }
  543. /*********************↑ 初始化 ↑************************/
  544. /*********************↓ 公共API ↓************************/
  545. void gpio_lamp_ctrl(rt_bool_t on)
  546. {
  547. rt_pin_write(LAMP_PIN, on ? LAMP_ON : LAMP_OFF);
  548. s_lamp_state = on;
  549. LOG_I("Lamp: %s", on ? "ON" : "OFF");
  550. }
  551. void gpio_hv_ctrl(rt_bool_t on)
  552. {
  553. rt_pin_write(HV_PIN, on ? HV_ON : HV_OFF);
  554. s_hv_state = on;
  555. LOG_I("HV: %s", on ? "ON" : "OFF");
  556. }
  557. void gpio_lte_ctrl(rt_bool_t on)
  558. {
  559. rt_pin_write(LTE_PIN, on ? LTE_ON : LTE_OFF);
  560. }
  561. rt_bool_t gpio_lamp_get_status(void)
  562. {
  563. return rt_pin_read(LAMP_PIN);
  564. }
  565. rt_bool_t gpio_hv_get_status(void)
  566. {
  567. return rt_pin_read(HV_PIN);
  568. }
  569. rt_bool_t gpio_lte_get_status(void)
  570. {
  571. return rt_pin_read(LTE_PIN);
  572. }
  573. void gpio_lamp_toggle(void) { gpio_lamp_ctrl(!s_lamp_state); }
  574. void gpio_hv_toggle(void) { gpio_hv_ctrl(!s_hv_state); }
  575. void_lte_power_on(void)
  576. {
  577. rt_kprintf("void_lte_power_on\n");
  578. gpio_lte_ctrl(1);
  579. }
  580. void_lte_power_off(void)
  581. {
  582. rt_kprintf("void_lte_power_off\n");
  583. gpio_lte_ctrl(0);
  584. }
  585. void charge_status(void)
  586. {
  587. rt_pin_mode(GET_PIN(C, 4), PIN_MODE_INPUT);
  588. rt_pin_mode(GET_PIN(C, 5), PIN_MODE_INPUT);
  589. rt_kprintf("charge_status is %d-%d\n", rt_pin_read(GET_PIN(C, 4)), rt_pin_read(GET_PIN(C, 5)));
  590. }
  591. MSH_CMD_EXPORT(void_lte_power_on, void_lte_power_on);
  592. MSH_CMD_EXPORT(void_lte_power_off, void_lte_power_off);
  593. MSH_CMD_EXPORT(charge_status, charge_status);
  594. /*********************↑ 公共API ↑************************/
  595. /*********************↓ 自动初始化 ↓************************/
  596. static int gpio_auto_init(void)
  597. {
  598. gpio_init();
  599. key_init();
  600. key_thread_start();
  601. return 0;
  602. }
  603. INIT_APP_EXPORT(gpio_auto_init);
  604. /*********************↑ 自动初始化 ↑************************/
  605. /*********************↓ MSH命令 ↓************************/
  606. static void cmd_lamp(int argc, char **argv)
  607. {
  608. if (argc < 2)
  609. {
  610. rt_kprintf("Usage: lamp [on|off|toggle|status]\n");
  611. return;
  612. }
  613. if (strcmp(argv[1], "on") == 0)
  614. gpio_lamp_ctrl(RT_TRUE);
  615. else if (strcmp(argv[1], "off") == 0)
  616. gpio_lamp_ctrl(RT_FALSE);
  617. else if (strcmp(argv[1], "toggle") == 0)
  618. gpio_lamp_toggle();
  619. else if (strcmp(argv[1], "status") == 0)
  620. rt_kprintf("Lamp: %s\n", s_lamp_state ? "ON" : "OFF");
  621. else
  622. rt_kprintf("Unknown: %s\n", argv[1]);
  623. }
  624. MSH_CMD_EXPORT(cmd_lamp, Control lamp);
  625. static void cmd_hv(int argc, char **argv)
  626. {
  627. if (argc < 2)
  628. {
  629. rt_kprintf("Usage: hv [on|off|toggle|status]\n");
  630. return;
  631. }
  632. if (strcmp(argv[1], "on") == 0)
  633. gpio_hv_ctrl(RT_TRUE);
  634. else if (strcmp(argv[1], "off") == 0)
  635. gpio_hv_ctrl(RT_FALSE);
  636. else if (strcmp(argv[1], "toggle") == 0)
  637. gpio_hv_toggle();
  638. else if (strcmp(argv[1], "status") == 0)
  639. rt_kprintf("HV: %s\n", s_hv_state ? "ON" : "OFF");
  640. else
  641. rt_kprintf("Unknown: %s\n", argv[1]);
  642. }
  643. MSH_CMD_EXPORT(cmd_hv, Control HV output);
  644. static void cmd_gpio_status(int argc, char **argv)
  645. {
  646. rt_kprintf("\n=== GPIO STATUS ===\n");
  647. rt_kprintf("LAMP (PB5): %s\n", s_lamp_state ? "ON" : "OFF");
  648. rt_kprintf("HV (PB6): %s\n", s_hv_state ? "ON" : "OFF");
  649. rt_kprintf("KeyA (PB4): %s\n", rt_pin_read(KEY_A_PIN) == PIN_LOW ? "PRESSED" : "RELEASED");
  650. rt_kprintf("KeyB (PB3): %s\n", rt_pin_read(KEY_B_PIN) == PIN_LOW ? "PRESSED" : "RELEASED");
  651. rt_kprintf("Power state: %s\n", dev_info.power_state ? "ON" : "OFF");
  652. rt_kprintf("Light duration: %d hours\n", dev_info.light_work_duration);
  653. rt_kprintf("==================\n");
  654. }
  655. MSH_CMD_EXPORT(cmd_gpio_status, Show GPIO status);
  656. static void cmd_test_combined_key(int argc, char **argv)
  657. {
  658. rt_kprintf("Simulating combined key long press...\n");
  659. key_combined_long();
  660. }
  661. MSH_CMD_EXPORT(cmd_test_combined_key, Test combined key long press);
  662. /*********************↑ MSH命令 ↑************************/