Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.1 KiB

  1. /*
  2. Debounce buttons and trigger events
  3. Copyright (C) 2015 by Xose Pérez <xose dot perez at gmail dot com>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include <Arduino.h>
  16. #include "DebounceEvent.h"
  17. DebounceEvent::DebounceEvent(uint8_t pin, callback_t callback, uint8_t defaultStatus, unsigned long delay) {
  18. _callback = callback;
  19. DebounceEvent(pin, defaultStatus, delay);
  20. }
  21. DebounceEvent::DebounceEvent(uint8_t pin, uint8_t defaultStatus, unsigned long delay) {
  22. // store configuration
  23. _pin = pin;
  24. _status = _defaultStatus = defaultStatus;
  25. _delay = delay;
  26. // set up button
  27. if (_defaultStatus == LOW) {
  28. pinMode(_pin, INPUT);
  29. } else {
  30. pinMode(_pin, INPUT_PULLUP);
  31. }
  32. }
  33. bool DebounceEvent::loop() {
  34. // holds whether status has changed or not
  35. bool changed = false;
  36. _event = EVENT_NONE;
  37. if (digitalRead(_pin) != _status) {
  38. // Debounce
  39. delay(_delay);
  40. uint8_t newStatus = digitalRead(_pin);
  41. if (newStatus != _status) {
  42. changed = true;
  43. _clicked = false;
  44. _status = newStatus;
  45. // released
  46. if (_status == _defaultStatus) {
  47. // get event
  48. if (millis() - _this_start > LONG_CLICK_DELAY) {
  49. _event = EVENT_LONG_CLICK;
  50. } else if (millis() - _last_start < DOUBLE_CLICK_DELAY ) {
  51. _event = EVENT_DOUBLE_CLICK;
  52. } else {
  53. // We are not setting the event type here because we still don't
  54. // know what kind of event it will be (it might be a double click).
  55. // Instead we are setting the _clicked variable to check later
  56. _clicked = true;
  57. changed = false;
  58. }
  59. // pressed
  60. } else {
  61. _last_start = _this_start;
  62. _this_start = millis();
  63. _event = EVENT_PRESSED;
  64. }
  65. }
  66. }
  67. if (_clicked && (millis() - _this_start > DOUBLE_CLICK_DELAY) && (!changed) && (_status == _defaultStatus)) {
  68. _clicked = false;
  69. changed = true;
  70. _event = EVENT_SINGLE_CLICK;
  71. }
  72. if (changed) {
  73. if (_callback) {
  74. _callback(_pin, EVENT_CHANGED);
  75. _callback(_pin, _event);
  76. }
  77. }
  78. return changed;
  79. }
  80. bool DebounceEvent::pressed() {
  81. return (_status != _defaultStatus);
  82. }
  83. uint8_t DebounceEvent::getEvent() {
  84. return _event;
  85. }