buttonhandling.ino 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #ifdef ENABLE_INPUT_BUTTONS
  2. const unsigned int buttonMaxClickTime = 500;
  3. const unsigned int buttonMaxClickTime_2 = 350; // actual max time a button may be pressed to get detected as short press/click
  4. const unsigned int buttonDebounceTime = 50;
  5. const unsigned int buttonRepeatTime = 350;
  6. const unsigned int buttonHoldTime = 1000;
  7. const unsigned int buttonRepeatTime_menuButton = 1000;
  8. // Use this function to configure the internal Bounce object to suit you. See the documentation at: https://github.com/thomasfredericks/Bounce2/wiki
  9. // This function can be left out if the defaults are acceptable - just don't call configureButton
  10. void configurePushButton(Bounce &bouncedButton)
  11. {
  12. // Set the debounce interval to 15ms - default is 10ms
  13. bouncedButton.interval(15);
  14. }
  15. void setupInputsButtons()
  16. {
  17. #if defined(FIRMWARE_VARIANT_THERMOSTAT) || defined(FIRMWARE_VARIANT_HEATCONTROL)
  18. buttonMode.configureButton(configurePushButton);
  19. buttonMode.onPress(onButtonPressed); // When the button is first pressed, call the function onButtonPressed (further down the page)
  20. //buttonMode.onHold(1000, onButtonHeldNoRepeat); // Once the button has been held for 1 second (1000ms) call onButtonHeld
  21. buttonMode.onHoldRepeat(buttonHoldTime, buttonRepeatTime_menuButton, onButtonHeld); // Once the button has been held for 1 second (1000ms) call onButtonHeld
  22. buttonMode.onRelease(buttonDebounceTime, buttonMaxClickTime, onButtonReleased); // When the button is held >50ms and released after <500ms, call onButtonReleased
  23. #endif
  24. #ifdef FIRMWARE_VARIANT_THERMOSTAT
  25. buttonPlus.configureButton(configurePushButton);
  26. //buttonPlus.onPress(onButtonPressed); // When the button is first pressed, call the function onButtonPressed (further down the page)
  27. 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
  28. buttonPlus.onRelease(buttonDebounceTime, buttonMaxClickTime, onButtonReleased); // When the button is held >50ms and released after <500ms, call onButtonReleased
  29. buttonMinus.configureButton(configurePushButton);
  30. //buttonMinus.onPress(onButtonPressed); // When the button is first pressed, call the function onButtonPressed (further down the page)
  31. 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
  32. buttonMinus.onRelease(buttonDebounceTime, buttonMaxClickTime, onButtonReleased); // When the button is held >50ms and released after <500ms, call onButtonReleased
  33. #endif
  34. #ifdef ENABLE_SENSOR_PIR
  35. pirSensor.configureButton(configurePushButton);
  36. //pirSensor.onPress(onButtonPressed); // When the button is first pressed, call the function onButtonPressed (further down the page)
  37. pirSensor.onHold(150, onButtonHeldNoRepeat); // Once the button has been held for 1 second (1000ms) call onButtonHeld
  38. pirSensor.onRelease(150, onButtonReleased); // When the button is held >50ms and released after <500ms, call onButtonReleased
  39. #endif
  40. }
  41. // btn is a reference to the button that fired the event. That means you can use the same event handler for many buttons
  42. //void onButtonPressed(Button& btn) {
  43. //
  44. //}
  45. // duration reports back how long it has been since the button was originally pressed.
  46. // repeatCount tells us how many times this function has been called by this button.
  47. void onButtonPressed(Button& btn) {
  48. if (buttonMode.isPressed()) {
  49. #ifdef FIRMWARE_VARIANT_HEATCONTROL
  50. display_enable();
  51. #endif
  52. }
  53. }
  54. void onButtonHeld(Button &btn, uint16_t duration, uint16_t repeatCount)
  55. {
  56. if (buttonMode.isPressed())
  57. {
  58. if (repeatCount <= 2)
  59. modeButtonHoldAction();
  60. else if (repeatCount == 7)
  61. modeButtonRebootPendingAction();
  62. else if (repeatCount > 7)
  63. modeButtonRebootAction();
  64. }
  65. #ifdef FIRMWARE_VARIANT_THERMOSTAT
  66. if (buttonPlus.isPressed())
  67. {
  68. plusButtonAction();
  69. }
  70. if (buttonMinus.isPressed())
  71. {
  72. minusButtonAction();
  73. }
  74. #endif
  75. }
  76. void onButtonHeldNoRepeat(Button &btn, uint16_t duration)
  77. {
  78. //if (buttonMode.isPressed()) {
  79. // modeButtonHoldAction();
  80. //}
  81. #ifdef ENABLE_SENSOR_PIR
  82. if (pirSensor.isPressed())
  83. {
  84. pirSensorOnAction();
  85. }
  86. #endif
  87. }
  88. // duration reports back the total time that the button was held down
  89. void onButtonReleased(Button &btn, uint16_t duration)
  90. {
  91. if (btn.is(buttonMode) && duration < buttonMaxClickTime_2)
  92. {
  93. modeButtonAction();
  94. }
  95. #ifdef FIRMWARE_VARIANT_THERMOSTAT
  96. else if (btn.is(buttonPlus) && duration < buttonMaxClickTime_2)
  97. {
  98. plusButtonAction();
  99. }
  100. else if (btn.is(buttonMinus) && duration < buttonMaxClickTime_2)
  101. {
  102. minusButtonAction();
  103. }
  104. #endif
  105. #ifdef ENABLE_SENSOR_PIR
  106. else if (btn.is(pirSensor))
  107. {
  108. pirSensorOffAction();
  109. }
  110. #endif
  111. }
  112. #ifdef FIRMWARE_VARIANT_THERMOSTAT
  113. void plusButtonAction()
  114. {
  115. //Serial.println("Btn +");
  116. sendLog("DEV: Btn +", LOGLEVEL_INFO);
  117. if (!display_active)
  118. {
  119. display_enable();
  120. }
  121. else
  122. {
  123. display_extendTimeout();
  124. if (thermostat_heatingMode == 1 && thermostat_preset != 0)
  125. thermostat_setPresetTo(0);
  126. else
  127. thermostat_setTempStepUp();
  128. }
  129. }
  130. void minusButtonAction()
  131. {
  132. //Serial.println("Btn -");
  133. sendLog("DEV: Btn -", LOGLEVEL_INFO);
  134. if (thermostat_heatingMode == 1 && thermostat_preset != 0)
  135. thermostat_setPresetTo(0);
  136. if (!display_active)
  137. {
  138. display_enable();
  139. }
  140. else
  141. {
  142. display_extendTimeout();
  143. #ifdef FIRMWARE_VARIANT_THERMOSTAT
  144. if (thermostat_heatingMode == 1 && thermostat_preset != 0)
  145. thermostat_setPresetTo(0);
  146. else
  147. thermostat_setTempStepDown();
  148. #endif
  149. }
  150. }
  151. #endif
  152. void modeButtonAction()
  153. {
  154. //Serial.println("Btn mode");
  155. sendLog("DEV: Btn MODE", LOGLEVEL_INFO);
  156. if (!display_active)
  157. {
  158. display_enable();
  159. //display_showRow2OverlayMessage(currentPresetName);
  160. }
  161. else
  162. {
  163. display_extendTimeout();
  164. if (system_pendingRestart)
  165. {
  166. system_doRestart = true;
  167. display_update();
  168. }
  169. #ifdef FIRMWARE_VARIANT_THERMOSTAT
  170. else {
  171. if (thermostat_heatingMode > 0)
  172. {
  173. if (thermostat_pendingPresetToggle) thermostat_togglePreset();
  174. else {
  175. thermostat_pendingPresetToggle = true;
  176. display_showRow2OverlayMessage(thermostat_currentPresetName);
  177. }
  178. }
  179. }
  180. #endif
  181. #ifdef FIRMWARE_VARIANT_HEATCONTROL
  182. else {
  183. display_toggleFullscreenMessage();
  184. }
  185. #endif
  186. }
  187. }
  188. void modeButtonHoldAction()
  189. {
  190. //Serial.println("Btn mode held - toggle on/off");
  191. sendLog("DEV: Btn MODE HOLD", LOGLEVEL_INFO);
  192. display_extendTimeout();
  193. if (system_pendingRestart)
  194. {
  195. system_doRestart = true;
  196. display_update();
  197. }
  198. #ifdef FIRMWARE_VARIANT_THERMOSTAT
  199. else {
  200. thermostat_toggleOnOff();
  201. }
  202. #endif
  203. #ifdef FIRMWARE_VARIANT_HEATCONTROL
  204. else {
  205. heatcontrol_testMode_toggle();
  206. }
  207. #endif
  208. }
  209. void modeButtonRebootPendingAction()
  210. {
  211. display_extendTimeout();
  212. system_pendingRestart = true;
  213. system_pendingRestart_lastMillis = millis();
  214. display_update();
  215. }
  216. void modeButtonRebootAction()
  217. {
  218. display_extendTimeout();
  219. system_doRestart = true;
  220. display_update();
  221. }
  222. #ifdef ENABLE_SENSOR_PIR
  223. void pirSensorOnAction()
  224. {
  225. PIRSensorOn = true;
  226. //Serial.println("PIR sensor ON");
  227. //sendLog("SENS: PIR=ON", LOGLEVEL_INFO);
  228. publishCurrentPIRValue();
  229. if (confDisplay.PIRenablesDisplay)
  230. {
  231. #ifdef FIRMWARE_VARIANT_THERMOSTAT
  232. if (confTherm.PIRenablesDisplay_preset0only && thermostat_preset == 0)
  233. display_enable();
  234. else if (!confTherm.PIRenablesDisplay_preset0only)
  235. display_enable();
  236. #else
  237. display_enable();
  238. #endif
  239. }
  240. }
  241. void pirSensorOffAction()
  242. {
  243. PIRSensorOn = false;
  244. //Serial.println("PIR sensor OFF");
  245. //sendLog("SENS: PIR=OFF", LOGLEVEL_INFO);
  246. publishCurrentPIRValue();
  247. }
  248. #endif // ENABLE_SENSOR_PIR
  249. #endif // ENABLE_INPUT_BUTTONS