handleButton_NEC.ino 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Button handling for IR remotes NEC code
  2. // - These DO NOT send a toggle bit on every new button press, BUT they send a 0 bit NEC code as repetition instead of the
  3. // button code, so it is easy to differ between hold and re-pressing
  4. // - if a Button configured as BUTTONMODE_ONCE the repetitions will be ignored
  5. // - On re-pressing, this function will send the button again as fast as possible
  6. // - If a button is held down, the remote will go on sending the 0 bit repeate code code every 70-100ms or so.
  7. // This function will then:
  8. // - BUTTONMODE_REPEAT:
  9. // 1. ignore the first couple of repetition codes to prevent multi triggering on single button presses
  10. // 2. send repeats as they come in from the remote
  11. // - BUTTONMODE_REPEAT_EXTENDED:
  12. // 1. ignore the first couple of repetition codes to prevent multi triggering on single button presses
  13. // 2. start with some slow repeats
  14. // 3. switch to medium repeat rate
  15. // 4. switch to fast repeat rate OR HOLD if configured
  16. // - BUTTONMODE_HOLD:
  17. // - press the key and dont release it until no more repetitions come in from the RC, then release
  18. // configuration variables for NEC button handling
  19. uint8_t handleButton_NEC_ignoreFirstRepeats = 2; // repeats to ignore when a button is beeing held (the first one is always sent), both for BUTTONMODE_REPEAT and BUTTONMODE_FILTERED_REPEAT
  20. uint8_t handleButton_NEC_ignoreFirstRepeats_Ext = 6; // repeats to ignore when a button is beeing held (the first one is always sent), both for BUTTONMODE_REPEAT and BUTTONMODE_FILTERED_REPEAT
  21. uint8_t handleButton_NEC_slowRepeats = 0; // amount of slow repeats to send
  22. uint8_t handleButton_NEC_slowRepeatsNumFastRepeats = 4; // how many fast repeats (=sending rate of the RC remote) represent one slow repeat?
  23. uint8_t handleButton_NEC_mediumRepeats = 5; // amount of medium repeats to send
  24. uint8_t handleButton_NEC_mediumRepeatsNumFastRepeats = 2; // how many fast repeats (=sending rate of the RC remote) represent one medium repeat?
  25. bool handleButton_NEC_useHoldInsteadOfFastRepeat = true;
  26. // hold button mode (key via USB keyboard emulation is not release until the RC stops sending repeats + this timeout)
  27. uint8_t handleButton_NEC_holdButton_releaseTimeout = 120; // release keys after a button was held (_btnMode=BUTTON_HOLD) after this ms if triggered by handleButton_NEC function
  28. // global variables for NEC button handling
  29. unsigned int handleButton_NEC_btnRepeatsCounter;
  30. uint8_t handleButton_NEC_btnSlowRepeatsCounter, handleButton_NEC_btnSlowRepeatsCounter2;
  31. uint8_t handleButton_NEC_btnMediumRepeatsCounter, handleButton_NEC_btnMediumRepeatsCounter2;
  32. uint8_t handleButton_NEC_lastButton = 0;
  33. uint8_t handleButton_NEC_lastButtonMode = 0;
  34. void handleButton_NEC(uint8_t _btn, uint8_t _btnMode) {
  35. bool _repeatMode = false;
  36. bool _extRepeatMode = false;
  37. bool _holdMode = false;
  38. if (_btn == BTN_REPETITION && _btnMode == BUTTONMODE_LAST) {
  39. if (handleButton_NEC_lastButtonMode == BUTTONMODE_REPEAT) _repeatMode = true;
  40. else if (handleButton_NEC_lastButtonMode == BUTTONMODE_EXTENDED_REPEAT) _extRepeatMode = true;
  41. else if (handleButton_NEC_lastButtonMode == BUTTONMODE_HOLD) _holdMode = true;
  42. }
  43. else {
  44. if (_btnMode == BUTTONMODE_REPEAT) _repeatMode = true;
  45. else if (_btnMode == BUTTONMODE_EXTENDED_REPEAT) _extRepeatMode = true;
  46. else if (_btnMode == BUTTONMODE_HOLD) {
  47. _holdMode = true;
  48. handleHoldButton_setTimeout(handleButton_NEC_holdButton_releaseTimeout);
  49. }
  50. }
  51. // single/first button press
  52. if (_btn != BTN_REPETITION) {
  53. handleButton_NEC_lastButtonMode = _btnMode;
  54. if (_btn == BTN_UNKNOWN) {
  55. // reset lastButton to 0 / BTN_UNKNOWN to avoid triggering a key if an unknown/unimplemented button is held
  56. handleButton_NEC_lastButton = BTN_UNKNOWN;
  57. }
  58. else if (_btn == BTN_POWER) {
  59. handleButton_NEC_lastButton = _btn;
  60. handlePowerButton();
  61. }
  62. else if (_btn == BTN_TASKSWITCH) {
  63. handleButton_NEC_lastButton = _btn;
  64. handleTaskSwitchButton();
  65. }
  66. else {
  67. handleButton_NEC_lastButton = _btn;
  68. if (_btnMode == BUTTONMODE_HOLD) {
  69. releaseAllKeys();
  70. sendKey(_btn, true);
  71. }
  72. else sendKey(_btn, false);
  73. }
  74. handleButton_NEC_btnRepeatsCounter = 0;
  75. handleButton_NEC_btnSlowRepeatsCounter = 0;
  76. handleButton_NEC_btnMediumRepeatsCounter = 0;
  77. }
  78. // BUTTONMODE_REPEAT (direct send a keypress on every repeat received from the RC, except of the first ones)
  79. else if (_btn == BTN_REPETITION && _repeatMode) {
  80. if (handleButton_NEC_lastButton == BTN_POWER) {
  81. handlePowerButton();
  82. }
  83. else if (handleButton_NEC_lastButton == BTN_TASKSWITCH) {
  84. handleTaskSwitchButton();
  85. }
  86. else {
  87. handleButton_NEC_btnRepeatsCounter++;
  88. if (debug2) {
  89. Serial.print(F("REP #"));
  90. Serial.println(handleButton_NEC_btnRepeatsCounter);
  91. }
  92. if (handleButton_NEC_btnRepeatsCounter > handleButton_NEC_ignoreFirstRepeats) {
  93. // filter out 1st repetitions
  94. sendKey(handleButton_NEC_lastButton, false);
  95. }
  96. }
  97. }
  98. // BUTTONMODE_EXTENDED_REPEAT (first slow, then getting faster)
  99. else if (_btn == BTN_REPETITION && _extRepeatMode) {
  100. if (handleButton_NEC_btnRepeatsCounter >= handleButton_NEC_ignoreFirstRepeats_Ext) {
  101. if (handleButton_NEC_btnSlowRepeatsCounter == 0) {
  102. handleButton_NEC_btnSlowRepeatsCounter++;
  103. handleButton_NEC_btnSlowRepeatsCounter2 = 0;
  104. if (debug2) {
  105. Serial.print(F("NEC BUTTON REPEAT SLOW #"));
  106. Serial.println(handleButton_NEC_btnSlowRepeatsCounter);
  107. }
  108. sendKey(handleButton_NEC_lastButton, false);
  109. }
  110. else if (handleButton_NEC_btnSlowRepeatsCounter < handleButton_NEC_slowRepeats && handleButton_NEC_slowRepeats > 0) {
  111. if (handleButton_NEC_btnSlowRepeatsCounter2 < handleButton_NEC_slowRepeatsNumFastRepeats) {
  112. handleButton_NEC_btnSlowRepeatsCounter2++;
  113. }
  114. else {
  115. handleButton_NEC_btnSlowRepeatsCounter++;
  116. handleButton_NEC_btnSlowRepeatsCounter2 = 0;
  117. if (debug2) {
  118. Serial.print(F("NEC BUTTON REPEAT SLOW #"));
  119. Serial.println(handleButton_NEC_btnSlowRepeatsCounter);
  120. }
  121. if (handleButton_NEC_btnSlowRepeatsCounter <= (handleButton_NEC_slowRepeats - 1)) {
  122. // do not send keypress on last medium repeat before going on with fast repeat mode
  123. sendKey(handleButton_NEC_lastButton, false);
  124. }
  125. }
  126. }
  127. else if (handleButton_NEC_btnMediumRepeatsCounter <= handleButton_NEC_mediumRepeats && handleButton_NEC_mediumRepeats > 0) {
  128. if (handleButton_NEC_btnMediumRepeatsCounter2 < handleButton_NEC_mediumRepeatsNumFastRepeats) {
  129. handleButton_NEC_btnMediumRepeatsCounter2++;
  130. }
  131. else {
  132. handleButton_NEC_btnMediumRepeatsCounter++;
  133. handleButton_NEC_btnMediumRepeatsCounter2 = 0;
  134. if (debug2) {
  135. Serial.print(F("NEC BUTTON REPEAT MEDIUM #"));
  136. Serial.println(handleButton_NEC_btnMediumRepeatsCounter);
  137. }
  138. if (handleButton_NEC_btnMediumRepeatsCounter <= (handleButton_NEC_mediumRepeats - 1)) {
  139. // do not send keypress on last medium repeat before going on with fast repeat mode
  140. sendKey(handleButton_NEC_lastButton, false);
  141. }
  142. }
  143. }
  144. else {
  145. if (debug2) {
  146. Serial.println(F("NEC BUTTON REPEAT FAST"));
  147. }
  148. if (handleButton_NEC_useHoldInsteadOfFastRepeat) {
  149. if (handleHoldButton_active()) handleHoldButton();
  150. else {
  151. sendKey(handleButton_NEC_lastButton, true);
  152. }
  153. }
  154. else sendKey(handleButton_NEC_lastButton, false);
  155. }
  156. }
  157. handleButton_NEC_btnRepeatsCounter++;
  158. }
  159. // HOLD
  160. else if ((_btn == BTN_REPETITION) && (_holdMode || _extRepeatMode)) {
  161. handleHoldButton();
  162. }
  163. }