Buttonhandling.ino 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. bool buttonCurrentState[3] = {HIGH, HIGH, HIGH};
  2. bool buttonLastState[3] = {HIGH, HIGH, HIGH};
  3. unsigned long buttonDownMillis[3] = {0, 0, 0};
  4. unsigned long buttonCurrentHoldTime[3] = {0, 0, 0};
  5. bool buttonFired[3] = {false, false, false};
  6. bool buttonHoldFired[3] = {false, false, false};
  7. unsigned long lastButtonPress[3] = {0, 0, 0};
  8. void buttonAction(byte btn) {
  9. lastSwitchSource[btn] = 0;
  10. relaisToggle(btn);
  11. switch (btn) {
  12. case 0:
  13. if (strlen(mqtt_topic_out_1) > 0) mqttclient.publish(mqtt_topic_out_1, mqtt_payload_out_1, mqtt_btnRetain);
  14. break;
  15. case 1:
  16. if (strlen(mqtt_topic_out_2) > 0) mqttclient.publish(mqtt_topic_out_2, mqtt_payload_out_2, mqtt_btnRetain);
  17. break;
  18. case 2:
  19. if (strlen(mqtt_topic_out_3) > 0) mqttclient.publish(mqtt_topic_out_3, mqtt_payload_out_3, mqtt_btnRetain);
  20. break;
  21. }
  22. if (led_enabled[btn]) {
  23. // blink LED when hold action is triggered
  24. bool ledState = digitalRead(leds_pins[btn]);
  25. digitalWrite(leds_pins[btn], !ledState);
  26. delay(250);
  27. digitalWrite(leds_pins[btn], ledState);
  28. }
  29. }
  30. void buttonHoldAction(byte btn) {
  31. if (hldToRel[btn] > 0) {
  32. relaisToggle( hldToRel[ btn - 1 ] );
  33. }
  34. switch (btn) {
  35. case 0:
  36. if (strlen(mqtt_topic_out_hold_1) > 0) mqttclient.publish(mqtt_topic_out_hold_1, mqtt_payload_out_hold_1, mqtt_btnRetain);
  37. break;
  38. case 1:
  39. if (strlen(mqtt_topic_out_hold_2) > 0) mqttclient.publish(mqtt_topic_out_hold_2, mqtt_payload_out_hold_2, mqtt_btnRetain);
  40. break;
  41. case 2:
  42. if (strlen(mqtt_topic_out_hold_3) > 0) mqttclient.publish(mqtt_topic_out_hold_3, mqtt_payload_out_hold_3, mqtt_btnRetain);
  43. break;
  44. }
  45. if (led_enabled[btn]) {
  46. // blink LED when hold action is triggered
  47. bool ledState = digitalRead(leds_pins[btn]);
  48. digitalWrite(leds_pins[btn], !ledState);
  49. delay(250);
  50. digitalWrite(leds_pins[btn], ledState);
  51. }
  52. }
  53. void checkButtonStates() {
  54. for (int i = 0; i < BUTTONS_COUNT; i++) {
  55. if (button_enabled[i]) checkButtonState(i);
  56. }
  57. }
  58. void checkButtonState(byte btn) {
  59. buttonCurrentHoldTime[btn] = millis() - buttonDownMillis[btn];
  60. buttonCurrentState[btn] = digitalRead(buttons_pins[btn]);
  61. if (buttonCurrentState[btn] == LOW && buttonLastState[btn] == HIGH) { // button was unpressed and is millis() pressed
  62. buttonDownMillis[btn] = millis();
  63. buttonLastState[btn] = buttonCurrentState[btn];
  64. }
  65. else if (buttonCurrentState[btn] == LOW && buttonLastState[btn] == LOW) { // button is held
  66. if ( buttonCurrentHoldTime[btn] > debounceTime ) {
  67. buttonLastState[btn] = buttonCurrentState[btn];
  68. if ( buttonCurrentHoldTime[btn] > buttonHoldTime && !buttonHoldFired[btn] ) {
  69. // button is held longer
  70. if ( (millis() - lastButtonPress[btn]) > 500 ) { // avoid double triggering
  71. lastButtonPress[btn] = millis();
  72. Serial.print("Button ");
  73. Serial.print(btn + 1);
  74. Serial.println(" long pressed");
  75. buttonHoldAction(btn);
  76. buttonHoldFired[btn] = true;
  77. }
  78. }
  79. }
  80. }
  81. else if (buttonCurrentState[btn] == HIGH && buttonLastState[btn] == LOW) { // button is released again
  82. if (buttonCurrentHoldTime[btn] > debounceTime) { // entprellung
  83. buttonLastState[btn] = buttonCurrentState[btn];
  84. if (!buttonHoldFired[btn] && !buttonFired[btn]) {
  85. if ( (millis() - lastButtonPress[btn]) > 500 ) { // avoid double triggering
  86. lastButtonPress[btn] = millis();
  87. Serial.print("Button ");
  88. Serial.print(btn + 1);
  89. Serial.println(" short pressed");
  90. buttonAction(btn);
  91. }
  92. }
  93. buttonFired[btn] = false;
  94. buttonHoldFired[btn] = false;
  95. }
  96. }
  97. }