input.c 3.5 KB

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