PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
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.

Bounce.h 2.3KB

3 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  15. * MA 02110-1301, USA.
  16. */
  17. /* * * * * * * * * * * * * * * * * * * * * * * * * * * *
  18. Main code by Thomas O Fredericks
  19. Rebounce and duration functions contributed by Eric Lowry
  20. Write function contributed by Jim Schimpf
  21. risingEdge and fallingEdge contributed by Tom Harkaway
  22. * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  23. #ifndef Bounce_h
  24. #define Bounce_h
  25. #include <inttypes.h>
  26. class Bounce
  27. {
  28. public:
  29. // Initialize
  30. Bounce(uint8_t pin, unsigned long interval_millis );
  31. // Sets the debounce interval
  32. void interval(unsigned long interval_millis);
  33. // Updates the pin
  34. // Returns 1 if the state changed
  35. // Returns 0 if the state did not change
  36. int update();
  37. // Forces the pin to signal a change (through update()) in X milliseconds
  38. // even if the state does not actually change
  39. // Example: press and hold a button and have it repeat every X milliseconds
  40. void rebounce(unsigned long interval);
  41. // Returns the updated pin state
  42. int read();
  43. // Sets the stored pin state
  44. void write(int new_state);
  45. // Returns the number of milliseconds the pin has been in the current state
  46. unsigned long duration();
  47. // The risingEdge method is true for one scan after the de-bounced input goes from off-to-on.
  48. bool risingEdge();
  49. // The fallingEdge method it true for one scan after the de-bounced input goes from on-to-off.
  50. bool fallingEdge();
  51. protected:
  52. int debounce();
  53. unsigned long previous_millis, interval_millis, rebounce_millis;
  54. uint8_t state;
  55. uint8_t pin;
  56. uint8_t stateChanged;
  57. };
  58. #endif