input.c 4.7 KB

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