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.

113 lines
2.9 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. static bool pending = false;
  36. bool changed = false;
  37. _event = EVENT_NONE;
  38. if (digitalRead(_pin) != _status) {
  39. // Debounce
  40. delay(_delay);
  41. uint8_t newStatus = digitalRead(_pin);
  42. if (newStatus != _status) {
  43. changed = true;
  44. pending = false;
  45. _status = newStatus;
  46. // released
  47. if (_status == _defaultStatus) {
  48. // get event
  49. if (millis() - _this_start > LONG_CLICK_DELAY) {
  50. _event = EVENT_LONG_CLICK;
  51. } else if (millis() - _last_start < DOUBLE_CLICK_DELAY ) {
  52. _event = EVENT_DOUBLE_CLICK;
  53. } else {
  54. changed = false;
  55. pending = true;
  56. //_event = EVENT_SINGLE_CLICK;
  57. }
  58. // pressed
  59. } else {
  60. _last_start = _this_start;
  61. _this_start = millis();
  62. _event = EVENT_PRESSED;
  63. }
  64. }
  65. }
  66. if (pending && (millis() - _this_start > DOUBLE_CLICK_DELAY) && (!changed) && (_status == _defaultStatus)) {
  67. pending = false;
  68. changed = true;
  69. _event = EVENT_SINGLE_CLICK;
  70. }
  71. if (changed) {
  72. if (_callback) {
  73. _callback(_pin, EVENT_CHANGED);
  74. _callback(_pin, _event);
  75. }
  76. }
  77. return changed;
  78. }
  79. bool DebounceEvent::pressed() {
  80. return (_status != _defaultStatus);
  81. }
  82. uint8_t DebounceEvent::getEvent() {
  83. return _event;
  84. }