input.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. if(input->cli) {
  72. cli_add_command(input->cli, "input", CliCommandFlagParallelSafe, input_cli, input);
  73. }
  74. #endif
  75. input->pin_states = malloc(input_pins_count * sizeof(InputPinState));
  76. for(size_t i = 0; i < input_pins_count; i++) {
  77. furi_hal_gpio_add_int_callback(input_pins[i].gpio, input_isr, NULL);
  78. input->pin_states[i].pin = &input_pins[i];
  79. input->pin_states[i].state = GPIO_Read(input->pin_states[i]);
  80. input->pin_states[i].debounce = INPUT_DEBOUNCE_TICKS_HALF;
  81. input->pin_states[i].press_timer = furi_timer_alloc(
  82. input_press_timer_callback, FuriTimerTypePeriodic, &input->pin_states[i]);
  83. input->pin_states[i].press_counter = 0;
  84. }
  85. while(1) {
  86. bool is_changing = false;
  87. for(size_t i = 0; i < input_pins_count; i++) {
  88. bool state = GPIO_Read(input->pin_states[i]);
  89. if(state) {
  90. if(input->pin_states[i].debounce < INPUT_DEBOUNCE_TICKS)
  91. input->pin_states[i].debounce += 1;
  92. } else {
  93. if(input->pin_states[i].debounce > 0) input->pin_states[i].debounce -= 1;
  94. }
  95. if(input->pin_states[i].debounce > 0 &&
  96. input->pin_states[i].debounce < INPUT_DEBOUNCE_TICKS) {
  97. is_changing = true;
  98. } else if(input->pin_states[i].state != state) {
  99. input->pin_states[i].state = state;
  100. // Common state info
  101. InputEvent event;
  102. event.key = input->pin_states[i].pin->key;
  103. // Short / Long / Repeat timer routine
  104. if(state) {
  105. input->counter++;
  106. input->pin_states[i].counter = input->counter;
  107. event.sequence = input->pin_states[i].counter;
  108. input_timer_start(input->pin_states[i].press_timer, INPUT_PRESS_TICKS);
  109. } else {
  110. event.sequence = input->pin_states[i].counter;
  111. input_timer_stop(input->pin_states[i].press_timer);
  112. if(input->pin_states[i].press_counter < INPUT_LONG_PRESS_COUNTS) {
  113. event.type = InputTypeShort;
  114. furi_pubsub_publish(input->event_pubsub, &event);
  115. }
  116. input->pin_states[i].press_counter = 0;
  117. }
  118. // Send Press/Release event
  119. event.type = input->pin_states[i].state ? InputTypePress : InputTypeRelease;
  120. furi_pubsub_publish(input->event_pubsub, &event);
  121. }
  122. }
  123. if(is_changing) {
  124. #if INPUT_DEBUG
  125. furi_hal_gpio_write(&gpio_ext_pa4, 1);
  126. #endif
  127. furi_delay_tick(1);
  128. } else {
  129. #if INPUT_DEBUG
  130. furi_hal_gpio_write(&gpio_ext_pa4, 0);
  131. #endif
  132. furi_thread_flags_wait(INPUT_THREAD_FLAG_ISR, FuriFlagWaitAny, FuriWaitForever);
  133. }
  134. }
  135. return 0;
  136. }