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_value(config.default_value == types::PinValue::High),
  30. _delay(debounce_delay),
  31. _repeat(repeat),
  32. _value(_default_value),
  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_value ? 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. }
  60. EventEmitter::EventEmitter(types::Pin pin, const types::Config& config, unsigned long delay, unsigned long repeat) :
  61. EventEmitter(pin, nullptr, config, delay, repeat)
  62. {}
  63. bool EventEmitter::isPressed() {
  64. return (_value != _default_value);
  65. }
  66. const types::Pin EventEmitter::getPin() const {
  67. return _pin;
  68. }
  69. const types::Config EventEmitter::getConfig() const {
  70. return _config;
  71. }
  72. unsigned long EventEmitter::getEventLength() {
  73. return _event_length;
  74. }
  75. unsigned long EventEmitter::getEventCount() {
  76. return _event_count;
  77. }
  78. // TODO: current implementation allows pin == nullptr
  79. types::Event EventEmitter::loop() {
  80. static_assert((HIGH) == 1, "Arduino API HIGH is not 1");
  81. static_assert((LOW) == 0, "Arduino API LOW is not 0");
  82. auto event = types::EventNone;
  83. bool value = _pin->digitalRead() == (HIGH);
  84. if (value != _value) {
  85. // TODO: check each loop instead of blocking?
  86. auto start = millis();
  87. while (millis() - start < _delay) delay(1);
  88. value = _pin->digitalRead() == (HIGH);
  89. if (value != _value) {
  90. _value = !_value;
  91. if (_is_switch) {
  92. event = isPressed()
  93. ? types::EventPressed
  94. : types::EventReleased;
  95. } else {
  96. if (_value == _default_value) {
  97. _event_length = millis() - _event_start;
  98. _ready = true;
  99. } else {
  100. event = types::EventPressed;
  101. _event_start = millis();
  102. _event_length = 0;
  103. if (_reset_count) {
  104. _event_count = 1;
  105. _reset_count = false;
  106. } else {
  107. ++_event_count;
  108. }
  109. _ready = false;
  110. }
  111. }
  112. }
  113. }
  114. if (_ready && (millis() - _event_start > _repeat)) {
  115. _ready = false;
  116. _reset_count = true;
  117. event = types::EventReleased;
  118. }
  119. if (_callback && (event != types::EventNone)) {
  120. _callback(*this, event, _event_count, _event_length);
  121. }
  122. return event;
  123. }
  124. } // namespace debounce_event