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.

122 lines
3.1 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. };
  38. enum class PinMode {
  39. Input,
  40. InputPullup,
  41. InputPulldown
  42. };
  43. struct Config {
  44. Mode mode;
  45. PinValue default_value;
  46. PinMode pin_mode;
  47. };
  48. enum Event {
  49. EventNone,
  50. EventPressed,
  51. EventReleased
  52. };
  53. using Pin = std::shared_ptr<BasePin>;
  54. using EventHandler = std::function<void(const EventEmitter& self, types::Event event, uint8_t count, unsigned long length)>;
  55. }
  56. class EventEmitter {
  57. public:
  58. 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);
  59. 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);
  60. types::Event loop();
  61. bool isPressed();
  62. const types::Pin getPin() const;
  63. const types::Config getConfig() const;
  64. unsigned long getEventLength();
  65. unsigned long getEventCount();
  66. private:
  67. types::Pin _pin;
  68. types::EventHandler _callback;
  69. const types::Config _config;
  70. const bool _is_switch;
  71. const bool _default_value;
  72. const unsigned long _delay;
  73. const unsigned long _repeat;
  74. bool _value;
  75. bool _ready;
  76. bool _reset_count;
  77. unsigned long _event_start;
  78. unsigned long _event_length;
  79. unsigned char _event_count;
  80. };
  81. } // namespace debounce_event