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.

123 lines
3.2 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. #pragma once
  21. #include <Arduino.h>
  22. #include <functional>
  23. #include <memory>
  24. #include "BasePin.h"
  25. namespace debounce_event {
  26. constexpr const unsigned long DebounceDelay = 50UL;
  27. constexpr const unsigned long RepeatDelay = 500UL;
  28. class EventEmitter;
  29. namespace types {
  30. enum class Mode {
  31. Pushbutton,
  32. Switch
  33. };
  34. enum class PinValue {
  35. Low,
  36. High,
  37. Initial
  38. };
  39. enum class PinMode {
  40. Input,
  41. InputPullup,
  42. InputPulldown
  43. };
  44. struct Config {
  45. Mode mode;
  46. PinValue default_value;
  47. PinMode pin_mode;
  48. };
  49. enum Event {
  50. EventNone,
  51. EventPressed,
  52. EventReleased
  53. };
  54. using Pin = std::shared_ptr<BasePin>;
  55. using EventHandler = std::function<void(const EventEmitter& self, types::Event event, uint8_t count, unsigned long length)>;
  56. }
  57. class EventEmitter {
  58. public:
  59. EventEmitter(types::Pin pin, const types::Config& config = {types::Mode::Pushbutton, types::PinValue::High, types::PinMode::Input}, unsigned long delay = DebounceDelay, unsigned long repeat = RepeatDelay);
  60. EventEmitter(types::Pin pin, types::EventHandler callback, const types::Config& = {types::Mode::Pushbutton, types::PinValue::High, types::PinMode::Input}, unsigned long delay = DebounceDelay, unsigned long repeat = RepeatDelay);
  61. types::Event loop();
  62. bool isPressed();
  63. const types::Pin getPin() const;
  64. const types::Config getConfig() const;
  65. unsigned long getEventLength();
  66. unsigned long getEventCount();
  67. private:
  68. types::Pin _pin;
  69. types::EventHandler _callback;
  70. const types::Config _config;
  71. const bool _is_switch { false };
  72. const unsigned long _delay { 0ul };
  73. const unsigned long _repeat { 0ul };
  74. bool _default_value { true };
  75. bool _value { true };
  76. bool _ready { false };
  77. bool _reset_count { true };
  78. unsigned long _event_start { 0ul };
  79. unsigned long _event_length { 0ul };
  80. unsigned char _event_count { 0ul };
  81. };
  82. } // namespace debounce_event