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.

3 jaren geleden
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Please read Bounce.h for information about the liscence and authors
  2. #include <Arduino.h>
  3. #include "Bounce.h"
  4. Bounce::Bounce(uint8_t pin,unsigned long interval_millis)
  5. {
  6. interval(interval_millis);
  7. previous_millis = millis();
  8. state = digitalRead(pin);
  9. this->pin = pin;
  10. }
  11. void Bounce::write(int new_state)
  12. {
  13. this->state = new_state;
  14. digitalWrite(pin,state);
  15. }
  16. void Bounce::interval(unsigned long interval_millis)
  17. {
  18. this->interval_millis = interval_millis;
  19. this->rebounce_millis = 0;
  20. }
  21. void Bounce::rebounce(unsigned long interval)
  22. {
  23. this->rebounce_millis = interval;
  24. }
  25. int Bounce::update()
  26. {
  27. if ( debounce() ) {
  28. rebounce(0);
  29. return stateChanged = 1;
  30. }
  31. // We need to rebounce, so simulate a state change
  32. if ( rebounce_millis && (millis() - previous_millis >= rebounce_millis) ) {
  33. previous_millis = millis();
  34. rebounce(0);
  35. return stateChanged = 1;
  36. }
  37. return stateChanged = 0;
  38. }
  39. unsigned long Bounce::duration()
  40. {
  41. return millis() - previous_millis;
  42. }
  43. int Bounce::read()
  44. {
  45. return (int)state;
  46. }
  47. // Protected: debounces the pin
  48. int Bounce::debounce() {
  49. uint8_t newState = digitalRead(pin);
  50. if (state != newState ) {
  51. if (millis() - previous_millis >= interval_millis) {
  52. previous_millis = millis();
  53. state = newState;
  54. return 1;
  55. }
  56. }
  57. return 0;
  58. }
  59. // The risingEdge method is true for one scan after the de-bounced input goes from off-to-on.
  60. bool Bounce::risingEdge() { return stateChanged && state; }
  61. // The fallingEdge method it true for one scan after the de-bounced input goes from on-to-off.
  62. bool Bounce::fallingEdge() { return stateChanged && !state; }