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.

Bounce2.h 2.7KB

3 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. // Uncomment the following line for "LOCK-OUT" debounce method
  26. //#define BOUNCE_LOCK_OUT
  27. // Uncomment the following line for "BOUNCE_WITH_PROMPT_DETECTION" debounce method
  28. //#define BOUNCE_WITH_PROMPT_DETECTION
  29. #include <inttypes.h>
  30. #ifndef _BV
  31. #define _BV(n) (1<<(n))
  32. #endif
  33. class Bounce
  34. {
  35. public:
  36. // Create an instance of the bounce library
  37. Bounce();
  38. // Attach to a pin (and also sets initial state)
  39. void attach(int pin);
  40. // Attach to a pin (and also sets initial state) and sets pin to mode (INPUT/INPUT_PULLUP/OUTPUT)
  41. void attach(int pin, int mode);
  42. // Sets the debounce interval
  43. void interval(uint16_t interval_millis);
  44. // Updates the pin
  45. // Returns 1 if the state changed
  46. // Returns 0 if the state did not change
  47. bool update();
  48. // Returns the updated pin state
  49. bool read();
  50. // Returns the falling pin state
  51. bool fell();
  52. // Returns the rising pin state
  53. bool rose();
  54. // Partial compatibility for programs written with Bounce version 1
  55. bool risingEdge() { return rose(); }
  56. bool fallingEdge() { return fell(); }
  57. Bounce(uint8_t pin, unsigned long interval_millis ) : Bounce() {
  58. attach(pin);
  59. interval(interval_millis);
  60. }
  61. protected:
  62. unsigned long previous_millis;
  63. uint16_t interval_millis;
  64. uint8_t state;
  65. uint8_t pin;
  66. };
  67. #endif