input.c 3.4 KB

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