input.c 3.6 KB

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