Buttonhandling.ino 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Use this function to configure the internal Bounce object to suit you. See the documentation at: https://github.com/thomasfredericks/Bounce2/wiki
  2. // This function can be left out if the defaults are acceptable - just don't call configureButton
  3. void configurePushButton(Bounce& bouncedButton) {
  4. // Set the debounce interval to 15ms - default is 10ms
  5. bouncedButton.interval(15);
  6. }
  7. // btn is a reference to the button that fired the event. That means you can use the same event handler for many buttons
  8. //void onButtonPressed(Button& btn) {
  9. //
  10. //}
  11. // duration reports back how long it has been since the button was originally pressed.
  12. // repeatCount tells us how many times this function has been called by this button.
  13. //void onButtonPressed(Button& btn) {
  14. // if (buttonMode.isPressed()) {
  15. //
  16. // }
  17. //}
  18. void onButtonHeld(Button& btn, uint16_t duration, uint16_t repeatCount) {
  19. if (buttonPlus.isPressed()) {
  20. plusButtonAction();
  21. }
  22. if (buttonMinus.isPressed()) {
  23. minusButtonAction();
  24. }
  25. if (buttonMode.isPressed()) {
  26. if (repeatCount <= 2) modeButtonHoldAction();
  27. else if (repeatCount == 4) modeButtonRebootPendingAction();
  28. else if (repeatCount > 4) modeButtonRebootAction();
  29. }
  30. }
  31. void onButtonHeldNoRepeat(Button& btn, uint16_t duration) {
  32. //if (buttonMode.isPressed()) {
  33. // modeButtonHoldAction();
  34. //}
  35. if (pirSensor.isPressed()) {
  36. pirSensorOnAction();
  37. }
  38. }
  39. // duration reports back the total time that the button was held down
  40. void onButtonReleased(Button& btn, uint16_t duration) {
  41. if (btn.is(buttonPlus) && duration < 200) {
  42. plusButtonAction();
  43. }
  44. else if (btn.is(buttonMinus) && duration < 200) {
  45. minusButtonAction();
  46. }
  47. else if (btn.is(buttonMode) && duration < 200) {
  48. modeButtonAction();
  49. }
  50. else if (btn.is(pirSensor)) {
  51. pirSensorOffAction();
  52. }
  53. }
  54. void plusButtonAction() {
  55. Serial.println("Btn +");
  56. if (!displayActive) {
  57. enableDisplay();
  58. }
  59. else {
  60. extendDisplayTimeout();
  61. if (heatingMode == 1 && preset != 0) setPresetTo(0);
  62. else setTempStepUp();
  63. }
  64. }
  65. void minusButtonAction() {
  66. Serial.println("Btn -");
  67. if (heatingMode == 1 && preset != 0) setPresetTo(0);
  68. if (!displayActive) {
  69. enableDisplay();
  70. }
  71. else {
  72. extendDisplayTimeout();
  73. if (heatingMode == 1 && preset != 0) setPresetTo(0);
  74. else setTempStepDown();
  75. }
  76. }
  77. void modeButtonAction() {
  78. Serial.println("Btn mode");
  79. if (!displayActive) {
  80. enableDisplay();
  81. //displayShowLine2OverlayMessage(currentPresetName);
  82. }
  83. else {
  84. extendDisplayTimeout();
  85. if (pendingRestart) {
  86. doRestart = true;
  87. updateDisplay();
  88. }
  89. else {
  90. if (heatingMode > 0) {
  91. if (pendingPresetToggle) {
  92. togglePreset();
  93. }
  94. else {
  95. pendingPresetToggle = true;
  96. displayShowLine2OverlayMessage(currentPresetName);
  97. }
  98. }
  99. }
  100. }
  101. }
  102. void modeButtonHoldAction() {
  103. Serial.println("Btn mode held - toggle on/off");
  104. extendDisplayTimeout();
  105. if (pendingRestart) {
  106. doRestart = true;
  107. updateDisplay();
  108. }
  109. else {
  110. toggleOnOff();
  111. }
  112. }
  113. void modeButtonRebootPendingAction() {
  114. extendDisplayTimeout();
  115. pendingRestart = true;
  116. pendingRestart_lastMillis = millis();
  117. updateDisplay();
  118. }
  119. void modeButtonRebootAction() {
  120. extendDisplayTimeout();
  121. doRestart = true;
  122. updateDisplay();
  123. }
  124. void pirSensorOnAction() {
  125. PIRSensorOn = true;
  126. Serial.println("PIR sensor ON");
  127. publishCurrentPIRValue();
  128. if (PIR_enablesDisplay) enableDisplay();
  129. }
  130. void pirSensorOffAction() {
  131. PIRSensorOn = false;
  132. Serial.println("PIR sensor OFF");
  133. publishCurrentPIRValue();
  134. }