btn.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include "clock.h"
  2. #include "log.h"
  3. #include "gpio.h"
  4. #include "btn.h"
  5. #include "light.h"
  6. uint8 task_btn_id;
  7. #define KEY_DEMO_ONCE_TIMER 0x0001
  8. #define KEY_DEMO_CYCLE_TIMER 0x0002
  9. int last_release_btn_ind = -1;
  10. void evt_press_release(int i){
  11. // 短按抬起 400ms 后检测是否还处于按下状态
  12. // LOG("evt_press_release %d\n", i);
  13. last_release_btn_ind = i;
  14. osal_stop_timerEx(task_btn_id, BTN_EVT_TIME_CHECK);
  15. }
  16. void changeTemp(int i)
  17. {
  18. LOG("changeTemp \n");
  19. int temp = light_data.temp;
  20. switch (i)
  21. {
  22. case 0:
  23. temp = temp + TEMP_STEP;
  24. if (temp > TEMP_MAX){ temp = TEMP_MAX;}
  25. break;
  26. case 1:
  27. temp = temp - TEMP_STEP;
  28. if (temp < TEMP_MIN){ temp = TEMP_MIN;}
  29. break;
  30. default:
  31. break;
  32. }
  33. LOG("changeTemp temp=>>> %d\n", temp);
  34. temp_set(temp, 0, NULL);
  35. }
  36. void changeLight(int i)
  37. {
  38. int light = light_data.light;
  39. switch (i)
  40. {
  41. case 0:
  42. light = light + LIGHT_STEP;
  43. if (light > 100){ light = 100;}
  44. break;
  45. case 1:
  46. light = light - LIGHT_STEP;
  47. if (light < 0){ light = 0;}
  48. break;
  49. default:
  50. break;
  51. }
  52. light_set(light, 0, NULL);
  53. }
  54. static void key_press_evt(uint8_t i,key_evt_t key_evt)
  55. {
  56. // LOG("\nkey index:%d gpio:%d \n",i,key_state.key[i].pin);
  57. switch(key_evt)
  58. {
  59. case HAL_KEY_EVT_PRESS:
  60. // LOG("key(press down)\n");
  61. osal_start_timerEx(task_btn_id, BTN_EVT_TIME_CHECK, HAL_KEY_LONG_PRESS_TIME + TIME_CHECK_TEMP);
  62. changeLight(i);
  63. #ifdef HAL_KEY_SUPPORT_LONG_PRESS
  64. osal_start_timerEx(task_btn_id, KEY_DEMO_LONG_PRESS_EVT, HAL_KEY_LONG_PRESS_TIME);
  65. #endif
  66. break;
  67. case HAL_KEY_EVT_RELEASE:
  68. // LOG("key(press release)\n");
  69. evt_press_release(i);
  70. break;
  71. #ifdef HAL_KEY_SUPPORT_LONG_PRESS
  72. case HAL_KEY_EVT_LONG_RELEASE:
  73. // LOG("key(long press release)\n");
  74. break;
  75. #endif
  76. default:
  77. LOG("unexpect\n");
  78. break;
  79. }
  80. }
  81. // key_state 引入
  82. // 初始化按钮
  83. void btn_init(uint8 taskId){
  84. LOG("[btn_init]\n");
  85. task_btn_id = taskId;
  86. // 初始化pwm
  87. for (int i = 0; i < HAL_KEY_NUM; i++)
  88. {
  89. hal_gpio_pin_init(btn_pins[i], IE);
  90. key_state.key[i].pin = btn_pins[i];
  91. key_state.key[i].state = HAL_KEY_EVT_PRESS;
  92. key_state.key[i].idle_level = HAL_HIGH_IDLE;
  93. }
  94. // 注册同时按下事件
  95. key_state.task_id = task_btn_id;
  96. key_state.key_callbank = key_press_evt;
  97. key_init();
  98. }
  99. // 按钮事件处理函数
  100. uint16 Key_ProcessEvent( uint8 task_id, uint16 events )
  101. {
  102. if(task_id != task_btn_id){
  103. return 0;
  104. }
  105. if( events & KEY_DEMO_ONCE_TIMER){
  106. // LOG("once timer\n\n");
  107. osal_start_timerEx( task_btn_id, KEY_DEMO_ONCE_TIMER , 5000);
  108. return (events ^ KEY_DEMO_ONCE_TIMER);
  109. }
  110. if( events & KEY_DEMO_CYCLE_TIMER){
  111. // LOG("recycle timer\n\n");
  112. return (events ^ KEY_DEMO_CYCLE_TIMER);
  113. }
  114. if( events & HAL_KEY_EVENT){
  115. // LOG("HAL_KEY_EVENT \n"); //do not modify,key will use it
  116. for (uint8 i = 0; i < HAL_KEY_NUM; ++i){
  117. if ((key_state.temp[i].in_enable == TRUE)||
  118. (key_state.key[i].state == HAL_STATE_KEY_RELEASE_DEBOUNCE)){
  119. gpio_key_timer_handler(i);
  120. }
  121. }
  122. return (events ^ HAL_KEY_EVENT);
  123. }
  124. #ifdef HAL_KEY_SUPPORT_LONG_PRESS
  125. if( events & KEY_DEMO_LONG_PRESS_EVT){
  126. // LOG("KEY_DEMO_LONG_PRESS_EVT\n");
  127. for (int i = 0; i <= HAL_KEY_NUM; i++){
  128. // LOG("%d --- state [%d]",i, key_state.key[i].state);
  129. if(key_state.key[i].state == HAL_STATE_KEY_PRESS)
  130. {
  131. LOG("long press --- \n");
  132. osal_start_timerEx(task_btn_id, KEY_DEMO_LONG_PRESS_EVT, HAL_KEY_LONG_PRESS_TIME);
  133. }
  134. }
  135. return (events ^ KEY_DEMO_LONG_PRESS_EVT);
  136. }
  137. #endif
  138. if( events & BTN_EVT_TIME_CHECK){
  139. // LOG("BTN_EVT_TIME_CHECK i: %d \n", last_release_btn_ind);
  140. if(last_release_btn_ind != -1)
  141. {
  142. // LOG(" io state %d", key_state.key[last_release_btn_ind].state );
  143. if (
  144. key_state.key[last_release_btn_ind].state == HAL_STATE_KEY_PRESS
  145. )
  146. {
  147. // LOG("long press --- \n");
  148. osal_start_timerEx(task_btn_id, BTN_EVT_TIME_CHECK, TIME_CHECK_TEMP);
  149. changeTemp(last_release_btn_ind);
  150. }
  151. }
  152. return (events ^ BTN_EVT_TIME_CHECK);
  153. }
  154. return 0;
  155. }