Bounce2.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2013 thomasfredericks
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. this software and associated documentation files (the "Software"), to deal in
  6. the Software without restriction, including without limitation the rights to
  7. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. the Software, and to permit persons to whom the Software is furnished to do so,
  9. subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. */
  19. /* * * * * * * * * * * * * * * * * * * * * * * * * * * *
  20. Main code by Thomas O Fredericks (tof@t-o-f.info)
  21. Previous contributions by Eric Lowry, Jim Schimpf and Tom Harkaway
  22. * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  23. #ifndef Bounce2_h
  24. #define Bounce2_h
  25. #if defined(ARDUINO) && ARDUINO >= 100
  26. #include "Arduino.h"
  27. #else
  28. #include "WProgram.h"
  29. #endif
  30. // Uncomment the following line for "LOCK-OUT" debounce method
  31. //#define BOUNCE_LOCK_OUT
  32. // Uncomment the following line for "BOUNCE_WITH_PROMPT_DETECTION" debounce method
  33. //#define BOUNCE_WITH_PROMPT_DETECTION
  34. #include <inttypes.h>
  35. /**
  36. @example bounce.ino
  37. Simple example of the Bounce library that switches the debug LED when a button is pressed.
  38. */
  39. /**
  40. @example change.ino
  41. This example toggles the debug LED (pin 13) on or off when a button on pin 2 is pressed.
  42. */
  43. /**
  44. @example bounce_multiple.ino
  45. Detect the falling edge of multiple buttons. Eight buttons with internal pullups. Toggles a LED when any button is pressed. Buttons on pins 2,3,4,5,6,7,8,9
  46. */
  47. /**
  48. @example bounce2buttons.ino
  49. Example of two instances of the Bounce class that switches the debug LED when either one of the two buttons is pressed.
  50. */
  51. /**
  52. The Bounce class.
  53. */
  54. class Bounce
  55. {
  56. public:
  57. /*!
  58. @brief Create an instance of the Bounce class.
  59. @code
  60. // Create an instance of the Bounce class.
  61. Bounce() button;
  62. @endcode
  63. */
  64. Bounce();
  65. /*!
  66. @brief Attach to a pin and sets that pin's mode (INPUT, INPUT_PULLUP or OUTPUT).
  67. @param pin
  68. The pin that is to be debounced.
  69. @param mode
  70. A valid Arduino pin mode (INPUT, INPUT_PULLUP or OUTPUT).
  71. @return True if the event read was successful, otherwise false.
  72. */
  73. void attach(int pin, int mode);
  74. /**
  75. Attach to a pin for advanced users. Only attach the pin this way once you have previously set it up. Otherwise use attach(int pin, int mode).
  76. */
  77. void attach(int pin);
  78. /**
  79. @brief Sets the debounce interval in milliseconds.
  80. @param interval_millis
  81. The interval time in milliseconds.
  82. */
  83. void interval(uint16_t interval_millis);
  84. /*!
  85. @brief Updates the pin's state.
  86. Because Bounce does not use interrupts, you have to "update" the object before reading its value and it has to be done as often as possible (that means to include it in your loop()). Only call update() once per loop().
  87. @return True if the pin changed state.
  88. */
  89. bool update();
  90. /**
  91. @brief Returns the pin's state (HIGH or LOW).
  92. @return HIGH or LOW.
  93. */
  94. bool read();
  95. /**
  96. @brief Returns true if pin signal transitions from high to low.
  97. */
  98. bool fell();
  99. /**
  100. @brief Returns true if pin signal transitions from low to high.
  101. */
  102. bool rose();
  103. /**
  104. @brief Deprecated (i.e. do not use). Included for partial compatibility for programs written with Bounce version 1
  105. */
  106. bool risingEdge() { return rose(); }
  107. /**
  108. @brief Deprecated (i.e. do not use). Included for partial compatibility for programs written with Bounce version 1
  109. */
  110. bool fallingEdge() { return fell(); }
  111. /**
  112. @brief Deprecated (i.e. do not use). Included for partial compatibility for programs written with Bounce version 1
  113. */
  114. Bounce(uint8_t pin, unsigned long interval_millis ) : Bounce() {
  115. attach(pin);
  116. interval(interval_millis);
  117. }
  118. /**
  119. @brief Returns the duration in milliseconds of the current state.
  120. Is reset to 0 once the pin rises ( rose() ) or falls ( fell() ).
  121. @return The duration in milliseconds (unsigned long) of the current state.
  122. */
  123. unsigned long duration();
  124. // WIP HELD : unsigned long held(); // Returns the duration the previous state was held
  125. protected:
  126. unsigned long previous_millis;
  127. uint16_t interval_millis;
  128. uint8_t state;
  129. uint8_t pin;
  130. unsigned long stateChangeLastTime;
  131. // WIP HELD : unsigned long durationOfPreviousState;
  132. virtual bool readCurrentState() { return digitalRead(pin); }
  133. virtual void setPinMode(int pin, int mode) {
  134. #if defined(ARDUINO_STM_NUCLEO_F103RB) || defined(ARDUINO_GENERIC_STM32F103C)
  135. pinMode(pin, (WiringPinMode)mode);
  136. #else
  137. pinMode(pin, mode);
  138. #endif
  139. }
  140. private:
  141. inline void changeState();
  142. inline void setStateFlag(const uint8_t flag) {state |= flag;}
  143. inline void unsetStateFlag(const uint8_t flag) {state &= ~flag;}
  144. inline void toggleStateFlag(const uint8_t flag) {state ^= flag;}
  145. inline bool getStateFlag(const uint8_t flag) {return((state & flag) != 0);}
  146. };
  147. #endif