input.c 3.6 KB

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