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 DefaultState {
  35. Low,
  36. High
  37. };
  38. enum class PinMode {
  39. Input,
  40. InputPullup,
  41. InputPulldown
  42. };
  43. struct Config {
  44. Mode mode;
  45. DefaultState default_state;
  46. PinMode pin_mode;
  47. };
  48. enum Event {
  49. EventNone,
  50. EventChanged,
  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::DefaultState::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::DefaultState::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;
  72. const bool _default_status;
  73. const unsigned long _delay;
  74. const unsigned long _repeat;
  75. bool _status;
  76. bool _ready;
  77. bool _reset_count;
  78. unsigned long _event_start;
  79. unsigned long _event_length;
  80. unsigned char _event_count;
  81. };
  82. } // namespace debounce_event