input.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include <input/input.h>
  2. #include <input_priv.h>
  3. #include <stdio.h>
  4. #include <flipper_v2.h>
  5. #ifdef APP_NFC
  6. void st25r3916Isr(void);
  7. #endif
  8. static volatile bool initialized = false;
  9. static ValueManager input_state_record;
  10. static PubSub input_events_record;
  11. static Event event;
  12. static InputState input_state = {
  13. false,
  14. };
  15. void input_task(void* p) {
  16. uint32_t state_bits = 0;
  17. uint8_t debounce_counters[INPUT_COUNT];
  18. if(!init_managed(&input_state_record, &input_state, sizeof(input_state))) {
  19. printf("[input_task] cannot initialize ValueManager for input_state\n");
  20. furiac_exit(NULL);
  21. }
  22. if(!init_pubsub(&input_events_record)) {
  23. printf("[input_task] cannot initialize PubSub for input_events\n");
  24. furiac_exit(NULL);
  25. }
  26. if(!init_event(&event)) {
  27. printf("[input_task] cannot initialize Event\n");
  28. furiac_exit(NULL);
  29. }
  30. if(!furi_create("input_state", &input_state_record)) {
  31. printf("[input_task] cannot create the input_state record\n");
  32. furiac_exit(NULL);
  33. }
  34. if(!furi_create("input_events", &input_events_record)) {
  35. printf("[input_task] cannot create the input_events record\n");
  36. furiac_exit(NULL);
  37. }
  38. // we ready to work
  39. furiac_ready();
  40. initialized = true;
  41. // Force state update
  42. for(uint32_t i = 0; i < INPUT_COUNT; i++) {
  43. debounce_counters[i] = DEBOUNCE_TICKS / 2;
  44. }
  45. for(;;) {
  46. bool changed = false;
  47. for(uint32_t i = 0; i < INPUT_COUNT; i++) {
  48. bool input_state = gpio_read(&input_gpio[i]) ^ input_invert[i];
  49. if(input_state) {
  50. if(debounce_counters[i] < DEBOUNCE_TICKS) {
  51. debounce_counters[i] += 1;
  52. changed = true;
  53. }
  54. } else {
  55. if(debounce_counters[i] > 0) {
  56. debounce_counters[i] -= 1;
  57. changed = true;
  58. }
  59. }
  60. }
  61. if(!changed) {
  62. uint32_t new_state_bits = 0;
  63. for(uint32_t i = 0; i < INPUT_COUNT; i++) {
  64. if(debounce_counters[i] == DEBOUNCE_TICKS) {
  65. new_state_bits |= (1 << i);
  66. }
  67. }
  68. uint32_t changed_bits = new_state_bits ^ state_bits;
  69. if(changed_bits != 0) {
  70. // printf("[input] %02x -> %02x\n", state_bits, new_state_bits);
  71. InputState new_state = _BITS2STATE(new_state_bits);
  72. write_managed(&input_state_record, &new_state, sizeof(new_state), osWaitForever);
  73. state_bits = new_state_bits;
  74. for(uint32_t i = 0; i < INPUT_COUNT; i++) {
  75. if((changed_bits & (1 << i)) != 0) {
  76. bool state = (new_state_bits & (1 << i)) != 0;
  77. InputEvent event = {i, state};
  78. notify_pubsub(&input_events_record, &event);
  79. }
  80. }
  81. }
  82. // Sleep: wait for event
  83. wait_event(&event);
  84. } else {
  85. osDelay(1);
  86. }
  87. }
  88. }
  89. void HAL_GPIO_EXTI_Callback(uint16_t pin) {
  90. #ifdef APP_NFC
  91. if(pin == RFID_PULL_Pin) {
  92. st25r3916Isr();
  93. return;
  94. }
  95. #endif
  96. if(!initialized) return;
  97. signal_event(&event);
  98. }