input.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include "input_i.h"
  2. #define GPIO_Read(input_pin) \
  3. (HAL_GPIO_ReadPin((GPIO_TypeDef*)input_pin.pin->port, input_pin.pin->pin) ^ \
  4. input_pin.pin->inverted)
  5. static Input* input = NULL;
  6. inline static void input_timer_start(osTimerId_t timer_id, uint32_t ticks) {
  7. TimerHandle_t hTimer = (TimerHandle_t)timer_id;
  8. furi_check(xTimerChangePeriod(hTimer, ticks, portMAX_DELAY) == pdPASS);
  9. }
  10. inline static void input_timer_stop(osTimerId_t timer_id) {
  11. TimerHandle_t hTimer = (TimerHandle_t)timer_id;
  12. furi_check(xTimerStop(hTimer, portMAX_DELAY) == pdPASS);
  13. // xTimerStop is not actually stopping timer,
  14. // Instead it places stop event into timer queue
  15. // This code ensures that timer is stopped
  16. while(xTimerIsTimerActive(hTimer) == pdTRUE) osDelay(1);
  17. }
  18. void input_press_timer_callback(void* arg) {
  19. InputPinState* input_pin = arg;
  20. InputEvent event;
  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. notify_pubsub(&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. notify_pubsub(&input->event_pubsub, &event);
  30. }
  31. }
  32. void input_isr(void* _ctx) {
  33. osThreadFlagsSet(input->thread, INPUT_THREAD_FLAG_ISR);
  34. }
  35. void input_cli_send(string_t args, void* context) {
  36. InputEvent event;
  37. // Get first word as key name
  38. string_t key_name;
  39. string_init(key_name);
  40. size_t ws = string_search_char(args, ' ');
  41. if(ws == STRING_FAILURE) {
  42. printf("Invalid arguments. Use `input_send KEY TYPE`.");
  43. string_clear(key_name);
  44. return;
  45. } else {
  46. string_set_n(key_name, args, 0, ws);
  47. string_right(args, ws);
  48. string_strim(args);
  49. }
  50. // Check key name and set event key
  51. if(!string_cmp(key_name, "up")) {
  52. event.key = InputKeyUp;
  53. } else if(!string_cmp(key_name, "down")) {
  54. event.key = InputKeyDown;
  55. } else if(!string_cmp(key_name, "left")) {
  56. event.key = InputKeyLeft;
  57. } else if(!string_cmp(key_name, "right")) {
  58. event.key = InputKeyRight;
  59. } else if(!string_cmp(key_name, "ok")) {
  60. event.key = InputKeyOk;
  61. } else if(!string_cmp(key_name, "back")) {
  62. event.key = InputKeyBack;
  63. } else {
  64. printf("Invalid key name. Valid keys: `up`, `down`, `left`, `right`, `back`, `ok`.");
  65. string_clear(key_name);
  66. return;
  67. }
  68. string_clear(key_name);
  69. // Check the rest of args string and set event type
  70. if(!string_cmp(args, "press")) {
  71. event.type = InputTypePress;
  72. } else if(!string_cmp(args, "release")) {
  73. event.type = InputTypeRelease;
  74. } else if(!string_cmp(args, "short")) {
  75. event.type = InputTypeShort;
  76. } else if(!string_cmp(args, "long")) {
  77. event.type = InputTypeLong;
  78. } else {
  79. printf("Ivalid type. Valid types: `press`, `release`, `short`, `long`.");
  80. return;
  81. }
  82. // Publish input event
  83. notify_pubsub(&input->event_pubsub, &event);
  84. }
  85. int32_t input_task() {
  86. input = furi_alloc(sizeof(Input));
  87. input->thread = osThreadGetId();
  88. init_pubsub(&input->event_pubsub);
  89. furi_record_create("input_events", &input->event_pubsub);
  90. input->cli = furi_record_open("cli");
  91. if(input->cli) {
  92. cli_add_command(input->cli, "input_send", input_cli_send, input);
  93. }
  94. const size_t pin_count = input_pins_count;
  95. input->pin_states = furi_alloc(pin_count * sizeof(InputPinState));
  96. for(size_t i = 0; i < pin_count; i++) {
  97. GpioPin gpio = {(GPIO_TypeDef*)input_pins[i].port, (uint16_t)input_pins[i].pin};
  98. hal_gpio_add_int_callback(&gpio, input_isr, NULL);
  99. input->pin_states[i].pin = &input_pins[i];
  100. input->pin_states[i].state = GPIO_Read(input->pin_states[i]);
  101. input->pin_states[i].debounce = INPUT_DEBOUNCE_TICKS_HALF;
  102. input->pin_states[i].press_timer =
  103. osTimerNew(input_press_timer_callback, osTimerPeriodic, &input->pin_states[i], NULL);
  104. input->pin_states[i].press_counter = 0;
  105. }
  106. while(1) {
  107. bool is_changing = false;
  108. for(size_t i = 0; i < pin_count; i++) {
  109. bool state = GPIO_Read(input->pin_states[i]);
  110. if(input->pin_states[i].debounce > 0 &&
  111. input->pin_states[i].debounce < INPUT_DEBOUNCE_TICKS) {
  112. is_changing = true;
  113. input->pin_states[i].debounce += (state ? 1 : -1);
  114. } else if(input->pin_states[i].state != state) {
  115. input->pin_states[i].state = state;
  116. // Common state info
  117. InputEvent event;
  118. event.key = input->pin_states[i].pin->key;
  119. event.type = input->pin_states[i].state ? InputTypePress : InputTypeRelease;
  120. // Send Press/Release event
  121. notify_pubsub(&input->event_pubsub, &event);
  122. // Short / Long / Repeat timer routine
  123. if(state) {
  124. input_timer_start(input->pin_states[i].press_timer, INPUT_PRESS_TICKS);
  125. } else {
  126. input_timer_stop(input->pin_states[i].press_timer);
  127. if(input->pin_states[i].press_counter < INPUT_LONG_PRESS_COUNTS) {
  128. event.type = InputTypeShort;
  129. notify_pubsub(&input->event_pubsub, &event);
  130. }
  131. input->pin_states[i].press_counter = 0;
  132. }
  133. }
  134. }
  135. if(is_changing) {
  136. osDelay(1);
  137. } else {
  138. osThreadFlagsWait(INPUT_THREAD_FLAG_ISR, osFlagsWaitAny, osWaitForever);
  139. }
  140. }
  141. return 0;
  142. }