input.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "input_i.h"
  2. // #define INPUT_DEBUG
  3. #define GPIO_Read(input_pin) (furi_hal_gpio_read(input_pin.pin->gpio) ^ (input_pin.pin->inverted))
  4. static Input* input = NULL;
  5. inline static void input_timer_start(FuriTimer* timer_id, uint32_t ticks) {
  6. TimerHandle_t hTimer = (TimerHandle_t)timer_id;
  7. furi_check(xTimerChangePeriod(hTimer, ticks, portMAX_DELAY) == pdPASS);
  8. }
  9. inline static void input_timer_stop(FuriTimer* timer_id) {
  10. TimerHandle_t hTimer = (TimerHandle_t)timer_id;
  11. furi_check(xTimerStop(hTimer, portMAX_DELAY) == pdPASS);
  12. // xTimerStop is not actually stopping timer,
  13. // Instead it places stop event into timer queue
  14. // This code ensures that timer is stopped
  15. while(xTimerIsTimerActive(hTimer) == pdTRUE) furi_delay_tick(1);
  16. }
  17. void input_press_timer_callback(void* arg) {
  18. InputPinState* input_pin = arg;
  19. InputEvent event;
  20. event.sequence = input_pin->counter;
  21. event.key = input_pin->pin->key;
  22. input_pin->press_counter++;
  23. if(input_pin->press_counter == INPUT_LONG_PRESS_COUNTS) {
  24. event.type = InputTypeLong;
  25. furi_pubsub_publish(input->event_pubsub, &event);
  26. } else if(input_pin->press_counter > INPUT_LONG_PRESS_COUNTS) {
  27. input_pin->press_counter--;
  28. event.type = InputTypeRepeat;
  29. furi_pubsub_publish(input->event_pubsub, &event);
  30. }
  31. }
  32. void input_isr(void* _ctx) {
  33. UNUSED(_ctx);
  34. furi_thread_flags_set(input->thread_id, INPUT_THREAD_FLAG_ISR);
  35. }
  36. const char* input_get_key_name(InputKey key) {
  37. for(size_t i = 0; i < input_pins_count; i++) {
  38. if(input_pins[i].key == key) {
  39. return input_pins[i].name;
  40. }
  41. }
  42. return "Unknown";
  43. }
  44. const char* input_get_type_name(InputType type) {
  45. switch(type) {
  46. case InputTypePress:
  47. return "Press";
  48. case InputTypeRelease:
  49. return "Release";
  50. case InputTypeShort:
  51. return "Short";
  52. case InputTypeLong:
  53. return "Long";
  54. case InputTypeRepeat:
  55. return "Repeat";
  56. default:
  57. return "Unknown";
  58. }
  59. }
  60. int32_t input_srv(void* p) {
  61. UNUSED(p);
  62. input = malloc(sizeof(Input));
  63. input->thread_id = furi_thread_get_current_id();
  64. input->event_pubsub = furi_pubsub_alloc();
  65. furi_record_create(RECORD_INPUT_EVENTS, input->event_pubsub);
  66. #if INPUT_DEBUG
  67. furi_hal_gpio_init_simple(&gpio_ext_pa4, GpioModeOutputPushPull);
  68. #endif
  69. #ifdef SRV_CLI
  70. input->cli = furi_record_open(RECORD_CLI);
  71. cli_add_command(input->cli, "input", CliCommandFlagParallelSafe, input_cli, input);
  72. #endif
  73. input->pin_states = malloc(input_pins_count * sizeof(InputPinState));
  74. for(size_t i = 0; i < input_pins_count; i++) {
  75. furi_hal_gpio_add_int_callback(input_pins[i].gpio, input_isr, NULL);
  76. input->pin_states[i].pin = &input_pins[i];
  77. input->pin_states[i].state = GPIO_Read(input->pin_states[i]);
  78. input->pin_states[i].debounce = INPUT_DEBOUNCE_TICKS_HALF;
  79. input->pin_states[i].press_timer = furi_timer_alloc(
  80. input_press_timer_callback, FuriTimerTypePeriodic, &input->pin_states[i]);
  81. input->pin_states[i].press_counter = 0;
  82. }
  83. while(1) {
  84. bool is_changing = false;
  85. for(size_t i = 0; i < input_pins_count; i++) {
  86. bool state = GPIO_Read(input->pin_states[i]);
  87. if(state) {
  88. if(input->pin_states[i].debounce < INPUT_DEBOUNCE_TICKS)
  89. input->pin_states[i].debounce += 1;
  90. } else {
  91. if(input->pin_states[i].debounce > 0) input->pin_states[i].debounce -= 1;
  92. }
  93. if(input->pin_states[i].debounce > 0 &&
  94. input->pin_states[i].debounce < INPUT_DEBOUNCE_TICKS) {
  95. is_changing = true;
  96. } else if(input->pin_states[i].state != state) {
  97. input->pin_states[i].state = state;
  98. // Common state info
  99. InputEvent event;
  100. event.key = input->pin_states[i].pin->key;
  101. // Short / Long / Repeat timer routine
  102. if(state) {
  103. input->counter++;
  104. input->pin_states[i].counter = input->counter;
  105. event.sequence = input->pin_states[i].counter;
  106. input_timer_start(input->pin_states[i].press_timer, INPUT_PRESS_TICKS);
  107. } else {
  108. event.sequence = input->pin_states[i].counter;
  109. input_timer_stop(input->pin_states[i].press_timer);
  110. if(input->pin_states[i].press_counter < INPUT_LONG_PRESS_COUNTS) {
  111. event.type = InputTypeShort;
  112. furi_pubsub_publish(input->event_pubsub, &event);
  113. }
  114. input->pin_states[i].press_counter = 0;
  115. }
  116. // Send Press/Release event
  117. event.type = input->pin_states[i].state ? InputTypePress : InputTypeRelease;
  118. furi_pubsub_publish(input->event_pubsub, &event);
  119. }
  120. }
  121. if(is_changing) {
  122. #if INPUT_DEBUG
  123. furi_hal_gpio_write(&gpio_ext_pa4, 1);
  124. #endif
  125. furi_delay_tick(1);
  126. } else {
  127. #if INPUT_DEBUG
  128. furi_hal_gpio_write(&gpio_ext_pa4, 0);
  129. #endif
  130. furi_thread_flags_wait(INPUT_THREAD_FLAG_ISR, FuriFlagWaitAny, FuriWaitForever);
  131. }
  132. }
  133. return 0;
  134. }