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.cpp 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #include "JC_Button.h"
  6. /*----------------------------------------------------------------------*
  7. / initialize a Button object and the pin it's connected to. *
  8. /-----------------------------------------------------------------------*/
  9. void Button::begin()
  10. {
  11. pinMode(m_pin, m_puEnable ? INPUT_PULLUP : INPUT);
  12. m_state = digitalRead(m_pin);
  13. if (m_invert) m_state = !m_state;
  14. m_time = millis();
  15. m_lastState = m_state;
  16. m_changed = false;
  17. m_lastChange = m_time;
  18. }
  19. /*----------------------------------------------------------------------*
  20. / returns the state of the button, true if pressed, false if released. *
  21. / does debouncing, captures and maintains times, previous state, etc. *
  22. /-----------------------------------------------------------------------*/
  23. bool Button::read()
  24. {
  25. uint32_t ms = millis();
  26. bool pinVal = digitalRead(m_pin);
  27. if (m_invert) pinVal = !pinVal;
  28. if (ms - m_lastChange < m_dbTime)
  29. {
  30. m_changed = false;
  31. }
  32. else
  33. {
  34. m_lastState = m_state;
  35. m_state = pinVal;
  36. m_changed = (m_state != m_lastState);
  37. if (m_changed) m_lastChange = ms;
  38. }
  39. m_time = ms;
  40. return m_state;
  41. }
  42. /*----------------------------------------------------------------------*
  43. * isPressed() and isReleased() check the button state when it was last *
  44. * read, and return false (0) or true (!=0) accordingly. *
  45. * These functions do not cause the button to be read. *
  46. *----------------------------------------------------------------------*/
  47. bool Button::isPressed()
  48. {
  49. return m_state;
  50. }
  51. bool Button::isReleased()
  52. {
  53. return !m_state;
  54. }
  55. /*----------------------------------------------------------------------*
  56. * wasPressed() and wasReleased() check the button state to see if it *
  57. * changed between the last two reads and return false (0) or *
  58. * true (!=0) accordingly. *
  59. * These functions do not cause the button to be read. *
  60. *----------------------------------------------------------------------*/
  61. bool Button::wasPressed()
  62. {
  63. return m_state && m_changed;
  64. }
  65. bool Button::wasReleased()
  66. {
  67. return !m_state && m_changed;
  68. }
  69. /*----------------------------------------------------------------------*
  70. * pressedFor(ms) and releasedFor(ms) check to see if the button is *
  71. * pressed (or released), and has been in that state for the specified *
  72. * time in milliseconds. Returns false (0) or true (!=0) accordingly. *
  73. * These functions do not cause the button to be read. *
  74. *----------------------------------------------------------------------*/
  75. bool Button::pressedFor(uint32_t ms)
  76. {
  77. return m_state && m_time - m_lastChange >= ms;
  78. }
  79. bool Button::releasedFor(uint32_t ms)
  80. {
  81. return !m_state && m_time - m_lastChange >= ms;
  82. }
  83. /*----------------------------------------------------------------------*
  84. * lastChange() returns the time the button last changed state, *
  85. * in milliseconds. *
  86. *----------------------------------------------------------------------*/
  87. uint32_t Button::lastChange()
  88. {
  89. return m_lastChange;
  90. }