Buttonhandling.ino 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. {
  5. // Set the debounce interval to 15ms - default is 10ms
  6. bouncedButton.interval(15);
  7. }
  8. void setupInputsButtons()
  9. {
  10. //pinMode(PIN_PIRSENSOR, INPUT);
  11. buttonPlus.configureButton(configurePushButton);
  12. //buttonPlus.onPress(onButtonPressed); // When the button is first pressed, call the function onButtonPressed (further down the page)
  13. buttonPlus.onHoldRepeat(1000, 350, onButtonHeld); // Once the button has been held for 1 second (1000ms) call onButtonHeld. Call it again every 350ms until it is let go
  14. buttonPlus.onRelease(50, 500, onButtonReleased); // When the button is held >50ms and released after <500ms, call onButtonReleased
  15. buttonMinus.configureButton(configurePushButton);
  16. //buttonMinus.onPress(onButtonPressed); // When the button is first pressed, call the function onButtonPressed (further down the page)
  17. buttonMinus.onHoldRepeat(1000, 350, onButtonHeld); // Once the button has been held for 1 second (1000ms) call onButtonHeld. Call it again every 350ms until it is let go
  18. buttonMinus.onRelease(50, 500, onButtonReleased); // When the button is held >50ms and released after <500ms, call onButtonReleased
  19. buttonMode.configureButton(configurePushButton);
  20. //buttonMode.onPress(onButtonPressed); // When the button is first pressed, call the function onButtonPressed (further down the page)
  21. //buttonMode.onHold(1000, onButtonHeldNoRepeat); // Once the button has been held for 1 second (1000ms) call onButtonHeld
  22. buttonMode.onHoldRepeat(1000, 1000, onButtonHeld); // Once the button has been held for 1 second (1000ms) call onButtonHeld
  23. buttonMode.onRelease(50, 500, onButtonReleased); // When the button is held >50ms and released after <500ms, call onButtonReleased
  24. pirSensor.configureButton(configurePushButton);
  25. //pirSensor.onPress(onButtonPressed); // When the button is first pressed, call the function onButtonPressed (further down the page)
  26. pirSensor.onHold(150, onButtonHeldNoRepeat); // Once the button has been held for 1 second (1000ms) call onButtonHeld
  27. pirSensor.onRelease(150, onButtonReleased); // When the button is held >50ms and released after <500ms, call onButtonReleased
  28. }
  29. // btn is a reference to the button that fired the event. That means you can use the same event handler for many buttons
  30. //void onButtonPressed(Button& btn) {
  31. //
  32. //}
  33. // duration reports back how long it has been since the button was originally pressed.
  34. // repeatCount tells us how many times this function has been called by this button.
  35. //void onButtonPressed(Button& btn) {
  36. // if (buttonMode.isPressed()) {
  37. //
  38. // }
  39. //}
  40. void onButtonHeld(Button &btn, uint16_t duration, uint16_t repeatCount)
  41. {
  42. if (buttonPlus.isPressed())
  43. {
  44. plusButtonAction();
  45. }
  46. if (buttonMinus.isPressed())
  47. {
  48. minusButtonAction();
  49. }
  50. if (buttonMode.isPressed())
  51. {
  52. if (repeatCount <= 2)
  53. modeButtonHoldAction();
  54. else if (repeatCount == 4)
  55. modeButtonRebootPendingAction();
  56. else if (repeatCount > 4)
  57. modeButtonRebootAction();
  58. }
  59. }
  60. void onButtonHeldNoRepeat(Button &btn, uint16_t duration)
  61. {
  62. //if (buttonMode.isPressed()) {
  63. // modeButtonHoldAction();
  64. //}
  65. if (pirSensor.isPressed())
  66. {
  67. pirSensorOnAction();
  68. }
  69. }
  70. // duration reports back the total time that the button was held down
  71. void onButtonReleased(Button &btn, uint16_t duration)
  72. {
  73. if (btn.is(buttonPlus) && duration < 200)
  74. {
  75. plusButtonAction();
  76. }
  77. else if (btn.is(buttonMinus) && duration < 200)
  78. {
  79. minusButtonAction();
  80. }
  81. else if (btn.is(buttonMode) && duration < 200)
  82. {
  83. modeButtonAction();
  84. }
  85. else if (btn.is(pirSensor))
  86. {
  87. pirSensorOffAction();
  88. }
  89. }
  90. void plusButtonAction()
  91. {
  92. //Serial.println("Btn +");
  93. sendLog("DEV: Btn +", LOGLEVEL_INFO);
  94. if (!displayActive)
  95. {
  96. enableDisplay();
  97. }
  98. else
  99. {
  100. extendDisplayTimeout();
  101. if (heatingMode == 1 && preset != 0)
  102. setPresetTo(0);
  103. else
  104. setTempStepUp();
  105. }
  106. }
  107. void minusButtonAction()
  108. {
  109. //Serial.println("Btn -");
  110. sendLog("DEV: Btn -", LOGLEVEL_INFO);
  111. if (heatingMode == 1 && preset != 0)
  112. setPresetTo(0);
  113. if (!displayActive)
  114. {
  115. enableDisplay();
  116. }
  117. else
  118. {
  119. extendDisplayTimeout();
  120. if (heatingMode == 1 && preset != 0)
  121. setPresetTo(0);
  122. else
  123. setTempStepDown();
  124. }
  125. }
  126. void modeButtonAction()
  127. {
  128. //Serial.println("Btn mode");
  129. sendLog("DEV: Btn MODE", LOGLEVEL_INFO);
  130. if (!displayActive)
  131. {
  132. enableDisplay();
  133. //displayShowLine2OverlayMessage(currentPresetName);
  134. }
  135. else
  136. {
  137. extendDisplayTimeout();
  138. if (pendingRestart)
  139. {
  140. doRestart = true;
  141. updateDisplay();
  142. }
  143. else
  144. {
  145. if (heatingMode > 0)
  146. {
  147. if (pendingPresetToggle)
  148. {
  149. togglePreset();
  150. }
  151. else
  152. {
  153. pendingPresetToggle = true;
  154. displayShowLine2OverlayMessage(currentPresetName);
  155. }
  156. }
  157. }
  158. }
  159. }
  160. void modeButtonHoldAction()
  161. {
  162. //Serial.println("Btn mode held - toggle on/off");
  163. sendLog("DEV: Btn MODE HOLD", LOGLEVEL_INFO);
  164. extendDisplayTimeout();
  165. if (pendingRestart)
  166. {
  167. doRestart = true;
  168. updateDisplay();
  169. }
  170. else
  171. {
  172. toggleOnOff();
  173. }
  174. }
  175. void modeButtonRebootPendingAction()
  176. {
  177. extendDisplayTimeout();
  178. pendingRestart = true;
  179. pendingRestart_lastMillis = millis();
  180. updateDisplay();
  181. }
  182. void modeButtonRebootAction()
  183. {
  184. extendDisplayTimeout();
  185. doRestart = true;
  186. updateDisplay();
  187. }
  188. void pirSensorOnAction()
  189. {
  190. PIRSensorOn = true;
  191. //Serial.println("PIR sensor ON");
  192. //sendLog("SENS: PIR=ON", LOGLEVEL_INFO);
  193. publishCurrentPIRValue();
  194. if (confBas.PIR_enablesDisplay)
  195. {
  196. if (confBas.PIR_enablesDisplay_preset0only && preset == 0)
  197. enableDisplay();
  198. else if (!confBas.PIR_enablesDisplay_preset0only)
  199. enableDisplay();
  200. }
  201. }
  202. void pirSensorOffAction()
  203. {
  204. PIRSensorOn = false;
  205. //Serial.println("PIR sensor OFF");
  206. //sendLog("SENS: PIR=OFF", LOGLEVEL_INFO);
  207. publishCurrentPIRValue();
  208. }