input.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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(Cli* cli, 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. const char* input_get_key_name(InputKey key) {
  86. for(size_t i = 0; i < input_pins_count; i++) {
  87. if(input_pins[i].key == key) {
  88. return input_pins[i].name;
  89. }
  90. }
  91. return "Unknown";
  92. }
  93. const char* input_get_type_name(InputType type) {
  94. switch(type) {
  95. case InputTypePress:
  96. return "Press";
  97. case InputTypeRelease:
  98. return "Release";
  99. case InputTypeShort:
  100. return "Short";
  101. case InputTypeLong:
  102. return "Long";
  103. case InputTypeRepeat:
  104. return "Repeat";
  105. }
  106. return "Unknown";
  107. }
  108. int32_t input_srv() {
  109. input = furi_alloc(sizeof(Input));
  110. input->thread = osThreadGetId();
  111. init_pubsub(&input->event_pubsub);
  112. furi_record_create("input_events", &input->event_pubsub);
  113. input->cli = furi_record_open("cli");
  114. if(input->cli) {
  115. cli_add_command(
  116. input->cli, "input_send", CliCommandFlagParallelSafe, input_cli_send, input);
  117. }
  118. input->pin_states = furi_alloc(input_pins_count * sizeof(InputPinState));
  119. for(size_t i = 0; i < input_pins_count; i++) {
  120. GpioPin gpio = {(GPIO_TypeDef*)input_pins[i].port, (uint16_t)input_pins[i].pin};
  121. hal_gpio_add_int_callback(&gpio, input_isr, NULL);
  122. input->pin_states[i].pin = &input_pins[i];
  123. input->pin_states[i].state = GPIO_Read(input->pin_states[i]);
  124. input->pin_states[i].debounce = INPUT_DEBOUNCE_TICKS_HALF;
  125. input->pin_states[i].press_timer =
  126. osTimerNew(input_press_timer_callback, osTimerPeriodic, &input->pin_states[i], NULL);
  127. input->pin_states[i].press_counter = 0;
  128. }
  129. while(1) {
  130. bool is_changing = false;
  131. for(size_t i = 0; i < input_pins_count; i++) {
  132. bool state = GPIO_Read(input->pin_states[i]);
  133. if(input->pin_states[i].debounce > 0 &&
  134. input->pin_states[i].debounce < INPUT_DEBOUNCE_TICKS) {
  135. is_changing = true;
  136. input->pin_states[i].debounce += (state ? 1 : -1);
  137. } else if(input->pin_states[i].state != state) {
  138. input->pin_states[i].state = state;
  139. // Common state info
  140. InputEvent event;
  141. event.key = input->pin_states[i].pin->key;
  142. // Short / Long / Repeat timer routine
  143. if(state) {
  144. input_timer_start(input->pin_states[i].press_timer, INPUT_PRESS_TICKS);
  145. } else {
  146. input_timer_stop(input->pin_states[i].press_timer);
  147. if(input->pin_states[i].press_counter < INPUT_LONG_PRESS_COUNTS) {
  148. event.type = InputTypeShort;
  149. notify_pubsub(&input->event_pubsub, &event);
  150. }
  151. input->pin_states[i].press_counter = 0;
  152. }
  153. // Send Press/Release event
  154. event.type = input->pin_states[i].state ? InputTypePress : InputTypeRelease;
  155. notify_pubsub(&input->event_pubsub, &event);
  156. }
  157. }
  158. if(is_changing) {
  159. osDelay(1);
  160. } else {
  161. osThreadFlagsWait(INPUT_THREAD_FLAG_ISR, osFlagsWaitAny, osWaitForever);
  162. }
  163. }
  164. return 0;
  165. }