input.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include <input/input.h>
  2. #include <stdio.h>
  3. #include <furi.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. furi_record_create("input_state", &input_state_record);
  33. furi_record_create("input_events", &input_events_record);
  34. // we ready to work
  35. initialized = true;
  36. // Force state update
  37. for(uint32_t i = 0; i < INPUT_COUNT; i++) {
  38. debounce_counters[i] = DEBOUNCE_TICKS / 2;
  39. }
  40. for(;;) {
  41. bool changed = false;
  42. for(uint32_t i = 0; i < INPUT_COUNT; i++) {
  43. bool input_state = false;
  44. // dirty hack, f3 has no CHARGING pin
  45. // TODO rewrite this
  46. if(i < GPIO_INPUT_PINS_COUNT) {
  47. input_state = gpio_read(&input_gpio[i]) ^ input_invert[i];
  48. }
  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 == NFC_IRQ_Pin) {
  92. nfc_isr();
  93. return;
  94. }
  95. #endif
  96. #ifdef BUILD_CC1101
  97. if(pin == CC1101_G0_Pin) {
  98. cc1101_isr();
  99. return;
  100. }
  101. #endif
  102. if(!initialized) return;
  103. signal_event(&event);
  104. }