handleButton_RCx.ino 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 = 4; // repeats to ignore when a button is beeing held (the first one is always sent)
  9. uint8_t handleButton_RCx_slowRepeats = 4; // amount of slow repeats to send
  10. uint8_t handleButton_RCx_slowRepeatsNumFastRepeats = 4; // how many fast repeats (=sending rate of the RC remote) represent one slow repeat?
  11. uint8_t handleButton_RCx_mediumRepeats = 5; // amount of medium repeats to send
  12. uint8_t handleButton_RCx_mediumRepeatsNumFastRepeats = 2; // how many fast repeats (=sending rate of the RC remote) represent one slow repeat?
  13. uint8_t handleButton_RC5_holdButton_releaseTimeout = 146; // release keys after a button was held (_btnMode=BUTTON_HOLD) after this ms if triggered by handleButton_RCx function
  14. uint8_t handleButton_RC6_holdButton_releaseTimeout = 100; // release keys after a button was held (_btnMode=BUTTON_HOLD) after this ms if triggered by handleButton_RCx function
  15. // global variables for RCx button handling
  16. unsigned int handleButton_RCx_btnRepeatsCounter;
  17. uint8_t handleButton_RCx_lastButton, handleButton_RCx_lastPrefix;
  18. uint8_t handleButton_RCx_btnSlowRepeatsCounter, handleButton_RCx_btnSlowRepeatsCounter2;
  19. uint8_t handleButton_RCx_btnMediumRepeatsCounter, handleButton_RCx_btnMediumRepeatsCounter2;
  20. // the function
  21. //void handleButton_RCx(uint8_t _btn, uint8_t _btnMode, uint8_t _currPrefix) {
  22. // handleButton_RCx(_btn, _btnMode, _currPrefix, REMOTETYPE_RC6);
  23. //}
  24. void handleButton_RCx(uint8_t _btn, uint8_t _btnMode, uint8_t _currPrefix, uint8_t _remoteType) {
  25. bool _repeatMode = false;
  26. bool _holdMode = false;
  27. if (_btnMode == 1) _repeatMode = true;
  28. else if (_btnMode == 2) _holdMode = true;
  29. if (_btn != handleButton_RCx_lastButton || (_btn == handleButton_RCx_lastButton) && (_currPrefix != handleButton_RCx_lastPrefix)) {
  30. if (debug2) {
  31. Serial.println(F("RCx BUTTON INITIAL"));
  32. }
  33. if (_holdMode) {
  34. releaseAllKeys();
  35. sendKey(_btn, true);
  36. if(_remoteType == REMOTETYPE_RC6) holdButton_releaseTimeout = handleButton_RC6_holdButton_releaseTimeout;
  37. else if(_remoteType == REMOTETYPE_RC5) holdButton_releaseTimeout = handleButton_RC5_holdButton_releaseTimeout;
  38. holdButton_lastTriggeredMillis = millis();
  39. }
  40. else sendKey(_btn, false);
  41. handleButton_RCx_btnRepeatsCounter = 0;
  42. handleButton_RCx_btnSlowRepeatsCounter = 0;
  43. handleButton_RCx_btnMediumRepeatsCounter = 0;
  44. }
  45. // REPEAT BUTTON
  46. else if ((_btn == handleButton_RCx_lastButton) && (_currPrefix == handleButton_RCx_lastPrefix) && _repeatMode) {
  47. if (handleButton_RCx_btnRepeatsCounter >= handleButton_RCx_ignoreFirstRepeats) {
  48. if (handleButton_RCx_btnSlowRepeatsCounter == 0) {
  49. handleButton_RCx_btnSlowRepeatsCounter++;
  50. handleButton_RCx_btnSlowRepeatsCounter2 = 0;
  51. if (debug2) {
  52. Serial.print(F("RCx BUTTON REPEAT SLOW #"));
  53. Serial.println(handleButton_RCx_btnSlowRepeatsCounter);
  54. }
  55. sendKey(_btn, false);
  56. }
  57. else if (handleButton_RCx_btnSlowRepeatsCounter < handleButton_RCx_slowRepeats) {
  58. if (handleButton_RCx_btnSlowRepeatsCounter2 < handleButton_RCx_slowRepeatsNumFastRepeats) {
  59. handleButton_RCx_btnSlowRepeatsCounter2++;
  60. }
  61. else {
  62. handleButton_RCx_btnSlowRepeatsCounter++;
  63. handleButton_RCx_btnSlowRepeatsCounter2 = 0;
  64. if (debug2) {
  65. Serial.print(F("RCx BUTTON REPEAT SLOW #"));
  66. Serial.println(handleButton_RCx_btnSlowRepeatsCounter);
  67. }
  68. sendKey(_btn, false);
  69. }
  70. }
  71. else if (handleButton_RCx_btnMediumRepeatsCounter <= handleButton_RCx_mediumRepeats) {
  72. if (handleButton_RCx_btnMediumRepeatsCounter2 < handleButton_RCx_mediumRepeatsNumFastRepeats) {
  73. handleButton_RCx_btnMediumRepeatsCounter2++;
  74. }
  75. else {
  76. handleButton_RCx_btnMediumRepeatsCounter++;
  77. handleButton_RCx_btnMediumRepeatsCounter2 = 0;
  78. if (debug2) {
  79. Serial.print(F("RCx BUTTON REPEAT MEDIUM #"));
  80. Serial.println(handleButton_RCx_btnMediumRepeatsCounter);
  81. }
  82. sendKey(_btn, false);
  83. }
  84. }
  85. else {
  86. if (debug2) {
  87. Serial.println(F("RCx BUTTON REPEAT FAST"));
  88. }
  89. sendKey(_btn, false);
  90. }
  91. }
  92. handleButton_RCx_btnRepeatsCounter++;
  93. }
  94. // HOLD BUTTON
  95. else if ((_btn == handleButton_RCx_lastButton) && (_currPrefix == handleButton_RCx_lastPrefix) && _holdMode) {
  96. holdButton_lastTriggeredMillis = millis();
  97. }
  98. handleButton_RCx_lastPrefix = _currPrefix;
  99. handleButton_RCx_lastButton = _btn;
  100. }