input.c 7.8 KB

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