Buttonhandling.ino 6.9 KB

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