handleButton_RCx.ino 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. // global variables for RCx button handling
  14. unsigned int handleButton_RCx_btnRepeatsCounter;
  15. uint8_t handleButton_RCx_lastButton, handleButton_RCx_lastPrefix;
  16. uint8_t handleButton_RCx_btnSlowRepeatsCounter, handleButton_RCx_btnSlowRepeatsCounter2;
  17. uint8_t handleButton_RCx_btnMediumRepeatsCounter, handleButton_RCx_btnMediumRepeatsCounter2;
  18. // the function
  19. void handleButton_RCx(uint8_t _btn, bool _repeat, uint8_t _currPrefix) {
  20. if (_btn != handleButton_RCx_lastButton || (_btn == handleButton_RCx_lastButton) && (_currPrefix != handleButton_RCx_lastPrefix)) {
  21. if (debug2) {
  22. Serial.println(F("RCx BUTTON INITIAL"));
  23. }
  24. sendKey(_btn);
  25. handleButton_RCx_btnRepeatsCounter = 0;
  26. handleButton_RCx_btnSlowRepeatsCounter = 0;
  27. handleButton_RCx_btnMediumRepeatsCounter = 0;
  28. }
  29. else if ((_btn == handleButton_RCx_lastButton) && (_currPrefix == handleButton_RCx_lastPrefix) && _repeat) {
  30. if (handleButton_RCx_btnRepeatsCounter >= handleButton_RCx_ignoreFirstRepeats) {
  31. if (handleButton_RCx_btnSlowRepeatsCounter == 0) {
  32. handleButton_RCx_btnSlowRepeatsCounter++;
  33. handleButton_RCx_btnSlowRepeatsCounter2 = 0;
  34. if (debug2) {
  35. Serial.print(F("RCx BUTTON REPEAT SLOW #"));
  36. Serial.println(handleButton_RCx_btnSlowRepeatsCounter);
  37. }
  38. sendKey(_btn);
  39. }
  40. else if (handleButton_RCx_btnSlowRepeatsCounter < handleButton_RCx_slowRepeats) {
  41. if (handleButton_RCx_btnSlowRepeatsCounter2 < handleButton_RCx_slowRepeatsNumFastRepeats) {
  42. handleButton_RCx_btnSlowRepeatsCounter2++;
  43. }
  44. else {
  45. handleButton_RCx_btnSlowRepeatsCounter++;
  46. handleButton_RCx_btnSlowRepeatsCounter2 = 0;
  47. if (debug2) {
  48. Serial.print(F("RCx BUTTON REPEAT SLOW #"));
  49. Serial.println(handleButton_RCx_btnSlowRepeatsCounter);
  50. }
  51. sendKey(_btn);
  52. }
  53. }
  54. else if (handleButton_RCx_btnMediumRepeatsCounter <= handleButton_RCx_mediumRepeats) {
  55. if (handleButton_RCx_btnMediumRepeatsCounter2 < handleButton_RCx_mediumRepeatsNumFastRepeats) {
  56. handleButton_RCx_btnMediumRepeatsCounter2++;
  57. }
  58. else {
  59. handleButton_RCx_btnMediumRepeatsCounter++;
  60. handleButton_RCx_btnMediumRepeatsCounter2 = 0;
  61. if (debug2) {
  62. Serial.print(F("RCx BUTTON REPEAT MEDIUM #"));
  63. Serial.println(handleButton_RCx_btnMediumRepeatsCounter);
  64. }
  65. sendKey(_btn);
  66. }
  67. }
  68. else {
  69. if (debug2) {
  70. Serial.println(F("RCx BUTTON REPEAT FAST"));
  71. }
  72. sendKey(_btn);
  73. }
  74. }
  75. handleButton_RCx_btnRepeatsCounter++;
  76. }
  77. handleButton_RCx_lastPrefix = _currPrefix;
  78. handleButton_RCx_lastButton = _btn;
  79. }