Buttonhandling.ino 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 onButtonHeld(Button& btn, uint16_t duration, uint16_t repeatCount) {
  14. if (buttonPlus.isPressed()) {
  15. plusButtonAction();
  16. }
  17. if (buttonMinus.isPressed()) {
  18. minusButtonAction();
  19. }
  20. }
  21. void onButtonHeldNoRepeat(Button& btn, uint16_t duration) {
  22. if (buttonMode.isPressed()) {
  23. modeButtonHoldAction();
  24. }
  25. if (pirSensor.isPressed()) {
  26. pirSensorOnAction();
  27. }
  28. }
  29. // duration reports back the total time that the button was held down
  30. void onButtonReleased(Button& btn, uint16_t duration) {
  31. if (btn.is(buttonPlus)) {
  32. plusButtonAction();
  33. }
  34. else if (btn.is(buttonMinus)) {
  35. minusButtonAction();
  36. }
  37. else if (btn.is(buttonMode)) {
  38. modeButtonAction();
  39. }
  40. else if (btn.is(pirSensor)) {
  41. pirSensorOffAction();
  42. }
  43. }
  44. void plusButtonAction() {
  45. Serial.println("Btn +");
  46. if (!displayActive) {
  47. enableDisplay();
  48. }
  49. else {
  50. setTempStepUp();
  51. extendDisplayTimeout();
  52. }
  53. }
  54. void minusButtonAction() {
  55. Serial.println("Btn -");
  56. if (!displayActive) {
  57. enableDisplay();
  58. }
  59. else {
  60. setTempStepDown();
  61. extendDisplayTimeout();
  62. }
  63. }
  64. void modeButtonAction() {
  65. Serial.println("Btn mode");
  66. if (!displayActive) {
  67. enableDisplay();
  68. }
  69. else {
  70. toggleHeatingMode();
  71. extendDisplayTimeout();
  72. }
  73. }
  74. void modeButtonHoldAction() {
  75. Serial.println("Btn mode held - toggle on/off");
  76. extendDisplayTimeout();
  77. toggleThermostatOnOff();
  78. }
  79. void pirSensorOnAction() {
  80. PIRSensorOn = true;
  81. Serial.println("PIR sensor ON");
  82. publishCurrentPIRValue();
  83. sendToDomoticz_PIR();
  84. if(PIR_enablesDisplay) enableDisplay();
  85. }
  86. void pirSensorOffAction() {
  87. PIRSensorOn = false;
  88. Serial.println("PIR sensor OFF");
  89. publishCurrentPIRValue();
  90. sendToDomoticz_PIR();
  91. }