input.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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.sequence = 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. notify_pubsub(&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. notify_pubsub(&input->event_pubsub, &event);
  31. }
  32. }
  33. void input_isr(void* _ctx) {
  34. osThreadFlagsSet(input->thread, INPUT_THREAD_FLAG_ISR);
  35. }
  36. void input_cli_send(Cli* cli, string_t args, void* context) {
  37. InputEvent event;
  38. // Get first word as key name
  39. string_t key_name;
  40. string_init(key_name);
  41. size_t ws = string_search_char(args, ' ');
  42. if(ws == STRING_FAILURE) {
  43. printf("Invalid arguments. Use `input_send KEY TYPE`.");
  44. string_clear(key_name);
  45. return;
  46. } else {
  47. string_set_n(key_name, args, 0, ws);
  48. string_right(args, ws);
  49. string_strim(args);
  50. }
  51. // Check key name and set event key
  52. if(!string_cmp(key_name, "up")) {
  53. event.key = InputKeyUp;
  54. } else if(!string_cmp(key_name, "down")) {
  55. event.key = InputKeyDown;
  56. } else if(!string_cmp(key_name, "left")) {
  57. event.key = InputKeyLeft;
  58. } else if(!string_cmp(key_name, "right")) {
  59. event.key = InputKeyRight;
  60. } else if(!string_cmp(key_name, "ok")) {
  61. event.key = InputKeyOk;
  62. } else if(!string_cmp(key_name, "back")) {
  63. event.key = InputKeyBack;
  64. } else {
  65. printf("Invalid key name. Valid keys: `up`, `down`, `left`, `right`, `back`, `ok`.");
  66. string_clear(key_name);
  67. return;
  68. }
  69. string_clear(key_name);
  70. // Check the rest of args string and set event type
  71. if(!string_cmp(args, "press")) {
  72. event.type = InputTypePress;
  73. } else if(!string_cmp(args, "release")) {
  74. event.type = InputTypeRelease;
  75. } else if(!string_cmp(args, "short")) {
  76. event.type = InputTypeShort;
  77. } else if(!string_cmp(args, "long")) {
  78. event.type = InputTypeLong;
  79. } else {
  80. printf("Ivalid type. Valid types: `press`, `release`, `short`, `long`.");
  81. return;
  82. }
  83. // Publish input event
  84. notify_pubsub(&input->event_pubsub, &event);
  85. }
  86. const char* input_get_key_name(InputKey key) {
  87. for(size_t i = 0; i < input_pins_count; i++) {
  88. if(input_pins[i].key == key) {
  89. return input_pins[i].name;
  90. }
  91. }
  92. return "Unknown";
  93. }
  94. const char* input_get_type_name(InputType type) {
  95. switch(type) {
  96. case InputTypePress:
  97. return "Press";
  98. case InputTypeRelease:
  99. return "Release";
  100. case InputTypeShort:
  101. return "Short";
  102. case InputTypeLong:
  103. return "Long";
  104. case InputTypeRepeat:
  105. return "Repeat";
  106. }
  107. return "Unknown";
  108. }
  109. int32_t input_srv() {
  110. input = furi_alloc(sizeof(Input));
  111. input->thread = osThreadGetId();
  112. init_pubsub(&input->event_pubsub);
  113. furi_record_create("input_events", &input->event_pubsub);
  114. input->cli = furi_record_open("cli");
  115. if(input->cli) {
  116. cli_add_command(
  117. input->cli, "input_send", CliCommandFlagParallelSafe, input_cli_send, input);
  118. }
  119. input->pin_states = furi_alloc(input_pins_count * sizeof(InputPinState));
  120. for(size_t i = 0; i < input_pins_count; i++) {
  121. GpioPin gpio = {(GPIO_TypeDef*)input_pins[i].port, (uint16_t)input_pins[i].pin};
  122. hal_gpio_add_int_callback(&gpio, input_isr, NULL);
  123. input->pin_states[i].pin = &input_pins[i];
  124. input->pin_states[i].state = GPIO_Read(input->pin_states[i]);
  125. input->pin_states[i].debounce = INPUT_DEBOUNCE_TICKS_HALF;
  126. input->pin_states[i].press_timer =
  127. osTimerNew(input_press_timer_callback, osTimerPeriodic, &input->pin_states[i], NULL);
  128. input->pin_states[i].press_counter = 0;
  129. }
  130. while(1) {
  131. bool is_changing = false;
  132. for(size_t i = 0; i < input_pins_count; i++) {
  133. bool state = GPIO_Read(input->pin_states[i]);
  134. if(input->pin_states[i].debounce > 0 &&
  135. input->pin_states[i].debounce < INPUT_DEBOUNCE_TICKS) {
  136. is_changing = true;
  137. input->pin_states[i].debounce += (state ? 1 : -1);
  138. } else if(input->pin_states[i].state != state) {
  139. input->pin_states[i].state = state;
  140. // Common state info
  141. InputEvent event;
  142. event.key = input->pin_states[i].pin->key;
  143. // Short / Long / Repeat timer routine
  144. if(state) {
  145. input->counter++;
  146. input->pin_states[i].counter = input->counter;
  147. event.sequence = input->pin_states[i].counter;
  148. input_timer_start(input->pin_states[i].press_timer, INPUT_PRESS_TICKS);
  149. } else {
  150. event.sequence = input->pin_states[i].counter;
  151. input_timer_stop(input->pin_states[i].press_timer);
  152. if(input->pin_states[i].press_counter < INPUT_LONG_PRESS_COUNTS) {
  153. event.type = InputTypeShort;
  154. notify_pubsub(&input->event_pubsub, &event);
  155. }
  156. input->pin_states[i].press_counter = 0;
  157. }
  158. // Send Press/Release event
  159. event.type = input->pin_states[i].state ? InputTypePress : InputTypeRelease;
  160. notify_pubsub(&input->event_pubsub, &event);
  161. }
  162. }
  163. if(is_changing) {
  164. osDelay(1);
  165. } else {
  166. osThreadFlagsWait(INPUT_THREAD_FLAG_ISR, osFlagsWaitAny, osWaitForever);
  167. }
  168. }
  169. return 0;
  170. }