input.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include "input_i.h"
  2. #include <m-string.h>
  3. #define GPIO_Read(input_pin) \
  4. (HAL_GPIO_ReadPin((GPIO_TypeDef*)input_pin.pin->port, input_pin.pin->pin) ^ \
  5. input_pin.pin->inverted)
  6. static Input* input = NULL;
  7. void input_press_timer_callback(void* arg) {
  8. InputPinState* input_pin = arg;
  9. InputEvent event;
  10. event.key = input_pin->pin->key;
  11. if(!input_pin->repeat_event) {
  12. event.type = InputTypeLong;
  13. notify_pubsub(&input->event_pubsub, &event);
  14. input_pin->repeat_event = true;
  15. } else {
  16. event.type = InputTypeRepeat;
  17. notify_pubsub(&input->event_pubsub, &event);
  18. }
  19. osTimerStart(input_pin->press_timer, INPUT_REPEATE_PRESS_TICKS);
  20. }
  21. void input_isr(void* _pin, void* _ctx) {
  22. osThreadFlagsSet(input->thread, INPUT_THREAD_FLAG_ISR);
  23. }
  24. void input_cli_send(string_t args, void* context) {
  25. InputEvent event;
  26. // Get first word as key name
  27. string_t key_name;
  28. string_init(key_name);
  29. size_t ws = string_search_char(args, ' ');
  30. if(ws == STRING_FAILURE) {
  31. printf("Wrong input");
  32. string_clear(key_name);
  33. return;
  34. } else {
  35. string_set_n(key_name, args, 0, ws);
  36. string_right(args, ws);
  37. string_strim(args);
  38. }
  39. // Check key name and set event key
  40. if(!string_cmp(key_name, "up")) {
  41. event.key = InputKeyUp;
  42. } else if(!string_cmp(key_name, "down")) {
  43. event.key = InputKeyDown;
  44. } else if(!string_cmp(key_name, "left")) {
  45. event.key = InputKeyLeft;
  46. } else if(!string_cmp(key_name, "right")) {
  47. event.key = InputKeyRight;
  48. } else if(!string_cmp(key_name, "ok")) {
  49. event.key = InputKeyOk;
  50. } else if(!string_cmp(key_name, "back")) {
  51. event.key = InputKeyBack;
  52. } else {
  53. printf("Wrong argument");
  54. string_clear(key_name);
  55. return;
  56. }
  57. string_clear(key_name);
  58. // Check the rest of args string and set event type
  59. if(!string_cmp(args, "press")) {
  60. event.type = InputTypePress;
  61. } else if(!string_cmp(args, "release")) {
  62. event.type = InputTypeRelease;
  63. } else if(!string_cmp(args, "short")) {
  64. event.type = InputTypeShort;
  65. } else if(!string_cmp(args, "long")) {
  66. event.type = InputTypeLong;
  67. } else {
  68. printf("Wrong argument");
  69. return;
  70. }
  71. // Publish input event
  72. notify_pubsub(&input->event_pubsub, &event);
  73. }
  74. int32_t input_task() {
  75. input = furi_alloc(sizeof(Input));
  76. input->thread = osThreadGetId();
  77. init_pubsub(&input->event_pubsub);
  78. furi_record_create("input_events", &input->event_pubsub);
  79. input->cli = furi_record_open("cli");
  80. if(input->cli) {
  81. cli_add_command(input->cli, "input_send", input_cli_send, input);
  82. }
  83. const size_t pin_count = input_pins_count;
  84. input->pin_states = furi_alloc(pin_count * sizeof(InputPinState));
  85. api_interrupt_add(input_isr, InterruptTypeExternalInterrupt, NULL);
  86. for(size_t i = 0; i < pin_count; i++) {
  87. input->pin_states[i].pin = &input_pins[i];
  88. input->pin_states[i].state = GPIO_Read(input->pin_states[i]);
  89. input->pin_states[i].repeat_event = false;
  90. input->pin_states[i].debounce = INPUT_DEBOUNCE_TICKS_HALF;
  91. input->pin_states[i].press_timer =
  92. osTimerNew(input_press_timer_callback, osTimerOnce, &input->pin_states[i], NULL);
  93. }
  94. while(1) {
  95. bool is_changing = false;
  96. for(size_t i = 0; i < pin_count; i++) {
  97. bool state = GPIO_Read(input->pin_states[i]);
  98. if(input->pin_states[i].debounce > 0 &&
  99. input->pin_states[i].debounce < INPUT_DEBOUNCE_TICKS) {
  100. is_changing = true;
  101. input->pin_states[i].debounce += (state ? 1 : -1);
  102. } else if(input->pin_states[i].state != state) {
  103. input->pin_states[i].state = state;
  104. // Common state info
  105. InputEvent event;
  106. event.type = input->pin_states[i].state ? InputTypePress : InputTypeRelease;
  107. event.key = input->pin_states[i].pin->key;
  108. // Send Press/Release event
  109. notify_pubsub(&input->event_pubsub, &event);
  110. // Short/Long press logic
  111. if(state) {
  112. osTimerStart(input->pin_states[i].press_timer, INPUT_LONG_PRESS_TICKS);
  113. } else if(input->pin_states[i].repeat_event) {
  114. osTimerStop(input->pin_states[i].press_timer);
  115. input->pin_states[i].repeat_event = false;
  116. } else if(osTimerStop(input->pin_states[i].press_timer) == osOK) {
  117. event.type = InputTypeShort;
  118. notify_pubsub(&input->event_pubsub, &event);
  119. }
  120. }
  121. }
  122. if(is_changing) {
  123. osDelay(1);
  124. } else {
  125. osThreadFlagsWait(INPUT_THREAD_FLAG_ISR, osFlagsWaitAny, osWaitForever);
  126. }
  127. }
  128. return 0;
  129. }