input.c 4.4 KB

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