input.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include <input/input.h>
  2. #include <input_priv.h>
  3. #include <stdio.h>
  4. #include <furi.h>
  5. static volatile bool initialized = false;
  6. static SemaphoreHandle_t event;
  7. static InputState input_state = {
  8. false,
  9. };
  10. void input_task(void* p) {
  11. uint32_t state_bits = 0;
  12. StaticSemaphore_t event_semaphore;
  13. uint8_t debounce_counters[INPUT_COUNT];
  14. event = xSemaphoreCreateCountingStatic(1, 0, &event_semaphore);
  15. if(!furi_create("input_state", (void*)&input_state, sizeof(input_state))) {
  16. printf("[input_task] cannot create the input_state record\n");
  17. furiac_exit(NULL);
  18. }
  19. FuriRecordSubscriber* input_state_record =
  20. furi_open("input_state", false, false, NULL, NULL, NULL);
  21. if(input_state_record == NULL) {
  22. printf("[input_task] cannot open the input_state record\n");
  23. furiac_exit(NULL);
  24. }
  25. if(!furi_create("input_events", NULL, 0)) {
  26. printf("[input_task] cannot create the input_events record\n");
  27. furiac_exit(NULL);
  28. }
  29. FuriRecordSubscriber* input_events_record =
  30. furi_open("input_events", false, false, NULL, NULL, NULL);
  31. if(input_events_record == NULL) {
  32. printf("[input_task] cannot open the input_events record\n");
  33. furiac_exit(NULL);
  34. }
  35. initialized = true;
  36. // Force state update
  37. for(uint32_t i = 0; i < INPUT_COUNT; i++) {
  38. debounce_counters[i] = DEBOUNCE_TICKS / 2;
  39. }
  40. for(;;) {
  41. bool changed = false;
  42. for(uint32_t i = 0; i < INPUT_COUNT; i++) {
  43. bool input_state = app_gpio_read(input_gpio[i]) ^ input_invert[i];
  44. if(input_state) {
  45. if(debounce_counters[i] < DEBOUNCE_TICKS) {
  46. debounce_counters[i] += 1;
  47. changed = true;
  48. }
  49. } else {
  50. if(debounce_counters[i] > 0) {
  51. debounce_counters[i] -= 1;
  52. changed = true;
  53. }
  54. }
  55. }
  56. if(!changed) {
  57. uint32_t new_state_bits = 0;
  58. for(uint32_t i = 0; i < INPUT_COUNT; i++) {
  59. if(debounce_counters[i] == DEBOUNCE_TICKS) {
  60. new_state_bits |= (1 << i);
  61. }
  62. }
  63. uint32_t changed_bits = new_state_bits ^ state_bits;
  64. if(changed_bits != 0) {
  65. // printf("[input] %02x -> %02x\n", state_bits, new_state_bits);
  66. InputState new_state = _BITS2STATE(new_state_bits);
  67. furi_write(input_state_record, &new_state, sizeof(new_state));
  68. state_bits = new_state_bits;
  69. for(uint32_t i = 0; i < INPUT_COUNT; i++) {
  70. if((changed_bits & (1 << i)) != 0) {
  71. bool state = (new_state_bits & (1 << i)) != 0;
  72. InputEvent event = {i, state};
  73. furi_write(input_events_record, &event, sizeof(event));
  74. }
  75. }
  76. }
  77. // Sleep: wait for event
  78. xSemaphoreTake(event, portMAX_DELAY);
  79. } else {
  80. osDelay(1);
  81. }
  82. }
  83. }
  84. void HAL_GPIO_EXTI_Callback(uint16_t pin) {
  85. if(!initialized) return;
  86. BaseType_t task_woken = pdFALSE;
  87. // Ignore the result, as we do not care about repeated event during event processing.
  88. xSemaphoreGiveFromISR(event, &task_woken);
  89. if(task_woken) {
  90. portYIELD_FROM_ISR(task_woken);
  91. }
  92. }