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.

JC_Button.h 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Arduino Button Library
  2. // https://github.com/JChristensen/JC_Button
  3. // Copyright (C) 2018 by Jack Christensen and licensed under
  4. // GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
  5. #ifndef JC_BUTTON_H_INCLUDED
  6. #define JC_BUTTON_H_INCLUDED
  7. #include <Arduino.h>
  8. class Button
  9. {
  10. public:
  11. // Button(pin, dbTime, puEnable, invert) instantiates a button object.
  12. //
  13. // Required parameter:
  14. // pin The Arduino pin the button is connected to
  15. //
  16. // Optional parameters:
  17. // dbTime Debounce time in milliseconds (default 25ms)
  18. // puEnable true to enable the AVR internal pullup resistor (default true)
  19. // invert true to interpret a low logic level as pressed (default true)
  20. Button(uint8_t pin, uint32_t dbTime=25, uint8_t puEnable=true, uint8_t invert=true)
  21. : m_pin(pin), m_dbTime(dbTime), m_puEnable(puEnable), m_invert(invert) {}
  22. // Initialize a Button object and the pin it's connected to
  23. void begin();
  24. // Returns the current debounced button state, true for pressed,
  25. // false for released. Call this function frequently to ensure
  26. // the sketch is responsive to user input.
  27. bool read();
  28. // Returns true if the button state was pressed at the last call to read().
  29. // Does not cause the button to be read.
  30. bool isPressed();
  31. // Returns true if the button state was released at the last call to read().
  32. // Does not cause the button to be read.
  33. bool isReleased();
  34. // Returns true if the button state at the last call to read() was pressed,
  35. // and this was a change since the previous read.
  36. bool wasPressed();
  37. // Returns true if the button state at the last call to read() was released,
  38. // and this was a change since the previous read.
  39. bool wasReleased();
  40. // Returns true if the button state at the last call to read() was pressed,
  41. // and has been in that state for at least the given number of milliseconds.
  42. bool pressedFor(uint32_t ms);
  43. // Returns true if the button state at the last call to read() was released,
  44. // and has been in that state for at least the given number of milliseconds.
  45. bool releasedFor(uint32_t ms);
  46. // Returns the time in milliseconds (from millis) that the button last
  47. // changed state.
  48. uint32_t lastChange();
  49. private:
  50. uint8_t m_pin; // arduino pin number connected to button
  51. uint32_t m_dbTime; // debounce time (ms)
  52. bool m_puEnable; // internal pullup resistor enabled
  53. bool m_invert; // if true, interpret logic low as pressed, else interpret logic high as pressed
  54. bool m_state; // current button state, true=pressed
  55. bool m_lastState; // previous button state
  56. bool m_changed; // state changed since last read
  57. uint32_t m_time; // time of current state (ms from millis)
  58. uint32_t m_lastChange; // time of last state change (ms)
  59. };
  60. // a derived class for a "push-on, push-off" (toggle) type button.
  61. // initial state can be given, default is off (false).
  62. class ToggleButton : public Button
  63. {
  64. public:
  65. // constructor is similar to Button, but includes the initial state for the toggle.
  66. ToggleButton(uint8_t pin, bool initialState=false, uint32_t dbTime=25, uint8_t puEnable=true, uint8_t invert=true)
  67. : Button(pin, dbTime, puEnable, invert), m_toggleState(initialState) {}
  68. // read the button and return its state.
  69. // should be called frequently.
  70. bool read()
  71. {
  72. Button::read();
  73. if (wasPressed())
  74. {
  75. m_toggleState = !m_toggleState;
  76. m_changed = true;
  77. }
  78. else
  79. {
  80. m_changed = false;
  81. }
  82. return m_toggleState;
  83. }
  84. // has the state changed?
  85. bool changed() {return m_changed;}
  86. // return the current state
  87. bool toggleState() {return m_toggleState;}
  88. private:
  89. bool m_toggleState;
  90. bool m_changed;
  91. };
  92. #endif