input.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // we ready to work
  36. furiac_ready();
  37. initialized = true;
  38. // Force state update
  39. for(uint32_t i = 0; i < INPUT_COUNT; i++) {
  40. debounce_counters[i] = DEBOUNCE_TICKS / 2;
  41. }
  42. for(;;) {
  43. bool changed = false;
  44. for(uint32_t i = 0; i < INPUT_COUNT; i++) {
  45. bool input_state = app_gpio_read(input_gpio[i]) ^ input_invert[i];
  46. if(input_state) {
  47. if(debounce_counters[i] < DEBOUNCE_TICKS) {
  48. debounce_counters[i] += 1;
  49. changed = true;
  50. }
  51. } else {
  52. if(debounce_counters[i] > 0) {
  53. debounce_counters[i] -= 1;
  54. changed = true;
  55. }
  56. }
  57. }
  58. if(!changed) {
  59. uint32_t new_state_bits = 0;
  60. for(uint32_t i = 0; i < INPUT_COUNT; i++) {
  61. if(debounce_counters[i] == DEBOUNCE_TICKS) {
  62. new_state_bits |= (1 << i);
  63. }
  64. }
  65. uint32_t changed_bits = new_state_bits ^ state_bits;
  66. if(changed_bits != 0) {
  67. // printf("[input] %02x -> %02x\n", state_bits, new_state_bits);
  68. InputState new_state = _BITS2STATE(new_state_bits);
  69. furi_write(input_state_record, &new_state, sizeof(new_state));
  70. state_bits = new_state_bits;
  71. for(uint32_t i = 0; i < INPUT_COUNT; i++) {
  72. if((changed_bits & (1 << i)) != 0) {
  73. bool state = (new_state_bits & (1 << i)) != 0;
  74. InputEvent event = {i, state};
  75. furi_write(input_events_record, &event, sizeof(event));
  76. }
  77. }
  78. }
  79. // Sleep: wait for event
  80. xSemaphoreTake(event, portMAX_DELAY);
  81. } else {
  82. osDelay(1);
  83. }
  84. }
  85. }
  86. void HAL_GPIO_EXTI_Callback(uint16_t pin) {
  87. if(!initialized) return;
  88. BaseType_t task_woken = pdFALSE;
  89. // Ignore the result, as we do not care about repeated event during event processing.
  90. xSemaphoreGiveFromISR(event, &task_woken);
  91. if(task_woken) {
  92. portYIELD_FROM_ISR(task_woken);
  93. }
  94. }