input.c 5.4 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_source = INPUT_SEQUENCE_SOURCE_HARDWARE;
  21. event.sequence_counter = input_pin->counter;
  22. event.key = input_pin->pin->key;
  23. input_pin->press_counter++;
  24. if(input_pin->press_counter == INPUT_LONG_PRESS_COUNTS) {
  25. event.type = InputTypeLong;
  26. furi_pubsub_publish(input->event_pubsub, &event);
  27. } else if(input_pin->press_counter > INPUT_LONG_PRESS_COUNTS) {
  28. input_pin->press_counter--;
  29. event.type = InputTypeRepeat;
  30. furi_pubsub_publish(input->event_pubsub, &event);
  31. }
  32. }
  33. void input_isr(void* _ctx) {
  34. UNUSED(_ctx);
  35. furi_thread_flags_set(input->thread_id, INPUT_THREAD_FLAG_ISR);
  36. }
  37. const char* input_get_key_name(InputKey key) {
  38. for(size_t i = 0; i < input_pins_count; i++) {
  39. if(input_pins[i].key == key) {
  40. return input_pins[i].name;
  41. }
  42. }
  43. return "Unknown";
  44. }
  45. const char* input_get_type_name(InputType type) {
  46. switch(type) {
  47. case InputTypePress:
  48. return "Press";
  49. case InputTypeRelease:
  50. return "Release";
  51. case InputTypeShort:
  52. return "Short";
  53. case InputTypeLong:
  54. return "Long";
  55. case InputTypeRepeat:
  56. return "Repeat";
  57. default:
  58. return "Unknown";
  59. }
  60. }
  61. int32_t input_srv(void* p) {
  62. UNUSED(p);
  63. input = malloc(sizeof(Input));
  64. input->thread_id = furi_thread_get_current_id();
  65. input->event_pubsub = furi_pubsub_alloc();
  66. furi_record_create(RECORD_INPUT_EVENTS, input->event_pubsub);
  67. #if INPUT_DEBUG
  68. furi_hal_gpio_init_simple(&gpio_ext_pa4, GpioModeOutputPushPull);
  69. #endif
  70. #ifdef SRV_CLI
  71. input->cli = furi_record_open(RECORD_CLI);
  72. cli_add_command(input->cli, "input", CliCommandFlagParallelSafe, input_cli, input);
  73. #endif
  74. input->pin_states = malloc(input_pins_count * sizeof(InputPinState));
  75. for(size_t i = 0; i < input_pins_count; i++) {
  76. furi_hal_gpio_add_int_callback(input_pins[i].gpio, input_isr, NULL);
  77. input->pin_states[i].pin = &input_pins[i];
  78. input->pin_states[i].state = GPIO_Read(input->pin_states[i]);
  79. input->pin_states[i].debounce = INPUT_DEBOUNCE_TICKS_HALF;
  80. input->pin_states[i].press_timer = furi_timer_alloc(
  81. input_press_timer_callback, FuriTimerTypePeriodic, &input->pin_states[i]);
  82. input->pin_states[i].press_counter = 0;
  83. }
  84. while(1) {
  85. bool is_changing = false;
  86. for(size_t i = 0; i < input_pins_count; i++) {
  87. bool state = GPIO_Read(input->pin_states[i]);
  88. if(state) {
  89. if(input->pin_states[i].debounce < INPUT_DEBOUNCE_TICKS)
  90. input->pin_states[i].debounce += 1;
  91. } else {
  92. if(input->pin_states[i].debounce > 0) input->pin_states[i].debounce -= 1;
  93. }
  94. if(input->pin_states[i].debounce > 0 &&
  95. input->pin_states[i].debounce < INPUT_DEBOUNCE_TICKS) {
  96. is_changing = true;
  97. } else if(input->pin_states[i].state != state) {
  98. input->pin_states[i].state = state;
  99. // Common state info
  100. InputEvent event;
  101. event.sequence_source = INPUT_SEQUENCE_SOURCE_HARDWARE;
  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_counter = input->pin_states[i].counter;
  108. input_timer_start(input->pin_states[i].press_timer, INPUT_PRESS_TICKS);
  109. } else {
  110. event.sequence_counter = 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. }