handleButton_RCx.ino 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Button handling for RC5 and RC6 Remotes
  2. // - these send a toggle bit on every new button press, so it is easy to differ hold and re-pressing
  3. // - on re-pressing this function will send the button again immediately. it uses the _currPrefix which has to be passed by the calling function
  4. // and which must be only the single byte of the RCx code containing the toggle bit
  5. // - if a button is held down, the remote will not toggle this bit and goes on sending the same code every 100ms or so
  6. // this function will then, after some ignored codes, start with some slow repeats, then switch to medium repeat rate and finally repeat with the rate of the incoming codes
  7. // configuration variables for RCx button handling
  8. uint8_t handleButton_RCx_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
  9. uint8_t handleButton_RCx_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
  10. uint8_t handleButton_RCx_slowRepeats = 0; // amount of slow repeats to send
  11. uint8_t handleButton_RCx_slowRepeatsNumFastRepeats = 4; // how many fast repeats (=sending rate of the RC remote) represent one slow repeat?
  12. uint8_t handleButton_RCx_mediumRepeats = 5; // amount of medium repeats to send
  13. uint8_t handleButton_RCx_mediumRepeatsNumFastRepeats = 2; // how many fast repeats (=sending rate of the RC remote) represent one medium repeat?
  14. bool handleButton_RCx_useHoldInsteadOfFastRepeat = true;
  15. // hold button mode (key via USB keyboard emulation is not release until the RC stops sending repeats + this timeout)
  16. uint8_t handleButton_RC5_holdButton_releaseTimeout = 150; // release keys after a button was held (_btnMode=BUTTON_HOLD) after this ms if triggered by handleButton_RCx function
  17. uint8_t handleButton_RC6_holdButton_releaseTimeout = 120; // release keys after a button was held (_btnMode=BUTTON_HOLD) after this ms if triggered by handleButton_RCx function
  18. // global variables for RCx button handling
  19. unsigned int handleButton_RCx_btnRepeatsCounter;
  20. uint8_t handleButton_RCx_lastButton, handleButton_RCx_lastPrefix;
  21. uint8_t handleButton_RCx_btnSlowRepeatsCounter, handleButton_RCx_btnSlowRepeatsCounter2;
  22. uint8_t handleButton_RCx_btnMediumRepeatsCounter, handleButton_RCx_btnMediumRepeatsCounter2;
  23. // the function
  24. //void handleButton_RCx(uint8_t _btn, uint8_t _btnMode, uint8_t _currPrefix) {
  25. // handleButton_RCx(_btn, _btnMode, _currPrefix, REMOTETYPE_RC6);
  26. //}
  27. void handleButton_RCx(uint8_t _btn, uint8_t _btnMode, uint8_t _currPrefix, uint8_t _remoteType) {
  28. if (_btn != handleButton_RCx_lastButton || (_btn == handleButton_RCx_lastButton) && (_currPrefix != handleButton_RCx_lastPrefix)) {
  29. if (debug2) {
  30. Serial.println(F("RCx BUTTON INITIAL"));
  31. }
  32. if (_btn == BTN_POWER) {
  33. handlePowerButton();
  34. }
  35. else if (_btn == BTN_TASKSWITCH) {
  36. handleTaskSwitchButton();
  37. }
  38. else {
  39. if (_remoteType == REMOTETYPE_RC6) handleHoldButton_setTimeout(handleButton_RC6_holdButton_releaseTimeout);
  40. else if (_remoteType == REMOTETYPE_RC5) handleHoldButton_setTimeout(handleButton_RC5_holdButton_releaseTimeout);
  41. //if (_btnMode == BUTTONMODE_HOLD || _btnMode == BUTTONMODE_EXTENDED_REPEAT && !handleTaskSwitchButton_isActive()) {
  42. //if (_btnMode == BUTTONMODE_HOLD && !handleTaskSwitchButton_isActive()) {
  43. if (_btnMode == BUTTONMODE_HOLD) {
  44. releaseAllKeys();
  45. sendKey(_btn, true);
  46. handleHoldButton();
  47. }
  48. else sendKey(_btn, false);
  49. }
  50. handleButton_RCx_btnRepeatsCounter = 0;
  51. handleButton_RCx_btnSlowRepeatsCounter = 0;
  52. handleButton_RCx_btnMediumRepeatsCounter = 0;
  53. }
  54. // BUTTONMODE_REPEAT (direct send a keypress on every repeat received from the RC, except of the first ones)
  55. else if ((_btn == handleButton_RCx_lastButton) && (_currPrefix == handleButton_RCx_lastPrefix) && _btnMode == BUTTONMODE_REPEAT) {
  56. if (_btn == BTN_POWER) {
  57. handlePowerButton();
  58. }
  59. else if (_btn == BTN_TASKSWITCH) {
  60. handleTaskSwitchButton();
  61. }
  62. else {
  63. handleButton_RCx_btnRepeatsCounter++;
  64. if (debug2) {
  65. Serial.print("REP #");
  66. Serial.println(handleButton_RCx_btnRepeatsCounter);
  67. }
  68. if (handleButton_RCx_btnRepeatsCounter > handleButton_RCx_ignoreFirstRepeats) {
  69. // filter out 1st repetition
  70. sendKey(_btn, false);
  71. }
  72. }
  73. }
  74. // BUTTONMODE_EXTENDED_REPEAT (first slow, then getting faster)
  75. else if ((_btn == handleButton_RCx_lastButton) && (_currPrefix == handleButton_RCx_lastPrefix) && _btnMode == BUTTONMODE_EXTENDED_REPEAT) {
  76. if (handleButton_RCx_btnRepeatsCounter >= handleButton_RCx_ignoreFirstRepeats_Ext) {
  77. if (handleButton_RCx_btnSlowRepeatsCounter == 0) {
  78. handleButton_RCx_btnSlowRepeatsCounter++;
  79. handleButton_RCx_btnSlowRepeatsCounter2 = 0;
  80. if (debug2) {
  81. Serial.print(F("RCx BUTTON REPEAT SLOW #"));
  82. Serial.println(handleButton_RCx_btnSlowRepeatsCounter);
  83. }
  84. sendKey(_btn, false);
  85. }
  86. else if (handleButton_RCx_btnSlowRepeatsCounter <= handleButton_RCx_slowRepeats && handleButton_RCx_slowRepeats > 0) {
  87. if (handleButton_RCx_btnSlowRepeatsCounter2 < handleButton_RCx_slowRepeatsNumFastRepeats) {
  88. handleButton_RCx_btnSlowRepeatsCounter2++;
  89. }
  90. else {
  91. handleButton_RCx_btnSlowRepeatsCounter++;
  92. handleButton_RCx_btnSlowRepeatsCounter2 = 0;
  93. if (debug2) {
  94. Serial.print(F("RCx BUTTON REPEAT SLOW #"));
  95. Serial.println(handleButton_RCx_btnSlowRepeatsCounter);
  96. }
  97. if (handleButton_RCx_btnSlowRepeatsCounter <= (handleButton_RCx_slowRepeats - 1)) {
  98. // do not send keypress on last medium repeat before going on with fast repeat mode
  99. sendKey(_btn, false);
  100. }
  101. }
  102. }
  103. else if (handleButton_RCx_btnMediumRepeatsCounter <= handleButton_RCx_mediumRepeats && handleButton_RCx_mediumRepeats > 0) {
  104. if (handleButton_RCx_btnMediumRepeatsCounter2 < handleButton_RCx_mediumRepeatsNumFastRepeats) {
  105. handleButton_RCx_btnMediumRepeatsCounter2++;
  106. }
  107. else {
  108. handleButton_RCx_btnMediumRepeatsCounter++;
  109. handleButton_RCx_btnMediumRepeatsCounter2 = 0;
  110. if (debug2) {
  111. Serial.print(F("RCx BUTTON REPEAT MEDIUM #"));
  112. Serial.println(handleButton_RCx_btnMediumRepeatsCounter);
  113. }
  114. if (handleButton_RCx_btnMediumRepeatsCounter <= (handleButton_RCx_mediumRepeats - 1)) {
  115. // do not send keypress on last medium repeat before going on with fast repeat mode
  116. sendKey(_btn, false);
  117. }
  118. }
  119. }
  120. else {
  121. if (debug2) {
  122. Serial.println(F("RCx BUTTON REPEAT FAST"));
  123. }
  124. if(handleButton_RCx_useHoldInsteadOfFastRepeat) {
  125. if(handleHoldButton_active()) handleHoldButton();
  126. else {
  127. sendKey(_btn, true);
  128. }
  129. }
  130. else sendKey(_btn, false);
  131. }
  132. }
  133. handleButton_RCx_btnRepeatsCounter++;
  134. }
  135. // BUTTONMODE_HOLD
  136. else if ((_btn == handleButton_RCx_lastButton) && (_currPrefix == handleButton_RCx_lastPrefix) && _btnMode == BUTTONMODE_HOLD) {
  137. handleHoldButton();
  138. }
  139. handleButton_RCx_lastPrefix = _currPrefix;
  140. handleButton_RCx_lastButton = _btn;
  141. }