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.

157 lines
4.7 KiB

  1. /*
  2. Original code:
  3. Debounce buttons and trigger events
  4. Copyright (C) 2015-2018 by Xose Pérez <xose dot perez at gmail dot com>
  5. The DebounceEvent library is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. The DebounceEvent library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with the DebounceEvent library. If not, see <http://www.gnu.org/licenses/>.
  15. ----------------------------------------------------------------------------------
  16. Copyright (C) 2020 by Maxim Prokhorov <prokhorov dot max at outlook dot com>
  17. Modified to include generic INPUT / OUTPUT pin support through a custom interface.
  18. Definitions are incompatible with DebounceEvent, you should not include it's headers.
  19. */
  20. #include <functional>
  21. #include <memory>
  22. #include "libs/DebounceEvent.h"
  23. namespace debounce_event {
  24. EventEmitter::EventEmitter(types::Pin pin, types::EventHandler callback, const types::Config& config, unsigned long debounce_delay, unsigned long repeat) :
  25. _pin(pin),
  26. _callback(callback),
  27. _config(config),
  28. _is_switch(config.mode == types::Mode::Switch),
  29. _default_status(config.default_state == types::DefaultState::High),
  30. _delay(debounce_delay),
  31. _repeat(repeat),
  32. _status(false),
  33. _ready(false),
  34. _reset_count(true),
  35. _event_start(0),
  36. _event_length(0),
  37. _event_count(0)
  38. {
  39. if (!pin) return;
  40. if (_config.pin_mode == types::PinMode::InputPullup) {
  41. _pin->pinMode(INPUT_PULLUP);
  42. } else if (_config.pin_mode == types::PinMode::InputPulldown) {
  43. // ESP8266 does not have INPUT_PULLDOWN definition, and instead
  44. // has a GPIO16-specific INPUT_PULLDOWN_16:
  45. // - https://github.com/esp8266/Arduino/issues/478
  46. // - https://github.com/esp8266/Arduino/commit/1b3581d55ebf0f8c91e081f9af4cf7433d492ec9
  47. #ifdef ESP8266
  48. if (_pin->pin == 16) {
  49. _pin->pinMode(_default_status ? INPUT : INPUT_PULLDOWN_16);
  50. } else {
  51. _pin->pinMode(INPUT);
  52. }
  53. #else
  54. _pin->pinMode(INPUT_PULLDOWN);
  55. #endif
  56. } else {
  57. _pin->pinMode(INPUT);
  58. }
  59. _status = _is_switch ? (_pin->digitalRead() == (HIGH)) : _default_status;
  60. }
  61. EventEmitter::EventEmitter(types::Pin pin, const types::Config& config, unsigned long delay, unsigned long repeat) :
  62. EventEmitter(pin, nullptr, config, delay, repeat)
  63. {}
  64. bool EventEmitter::isPressed() {
  65. return (_status != _default_status);
  66. }
  67. const types::Pin EventEmitter::getPin() const {
  68. return _pin;
  69. }
  70. const types::Config EventEmitter::getConfig() const {
  71. return _config;
  72. }
  73. unsigned long EventEmitter::getEventLength() {
  74. return _event_length;
  75. }
  76. unsigned long EventEmitter::getEventCount() {
  77. return _event_count;
  78. }
  79. // TODO: current implementation allows pin == nullptr
  80. types::Event EventEmitter::loop() {
  81. static_assert((HIGH) == 1, "Arduino API HIGH is not 1");
  82. static_assert((LOW) == 0, "Arduino API LOW is not 0");
  83. auto event = types::EventNone;
  84. bool status = _pin->digitalRead() == (HIGH);
  85. if (status != _status) {
  86. // TODO: check each loop instead of blocking?
  87. auto start = millis();
  88. while (millis() - start < _delay) delay(1);
  89. status = _pin->digitalRead() == (HIGH);
  90. if (status != _status) {
  91. _status = !_status;
  92. if (_is_switch) {
  93. event = types::EventChanged;
  94. } else {
  95. if (_status == _default_status) {
  96. _event_length = millis() - _event_start;
  97. _ready = true;
  98. } else {
  99. event = types::EventPressed;
  100. _event_start = millis();
  101. _event_length = 0;
  102. if (_reset_count) {
  103. _event_count = 1;
  104. _reset_count = false;
  105. } else {
  106. ++_event_count;
  107. }
  108. _ready = false;
  109. }
  110. }
  111. }
  112. }
  113. if (_ready && (millis() - _event_start > _repeat)) {
  114. _ready = false;
  115. _reset_count = true;
  116. event = types::EventReleased;
  117. }
  118. if (_callback && (event != types::EventNone)) {
  119. _callback(*this, event, _event_count, _event_length);
  120. }
  121. return event;
  122. }
  123. } // namespace debounce_event