From ede898fe5f786d261e1b1791ac102e9cf3fa7366 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sun, 28 Jul 2013 14:12:59 +0200 Subject: inital commit Signed-off-by: Florian Pritz --- src/Bounce.cpp | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/Bounce.cpp (limited to 'src/Bounce.cpp') diff --git a/src/Bounce.cpp b/src/Bounce.cpp new file mode 100644 index 0000000..40709e4 --- /dev/null +++ b/src/Bounce.cpp @@ -0,0 +1,90 @@ + +// Please read Bounce.h for information about the liscence and authors + +#include +#include "Bounce.h" + +Bounce::Bounce() { +} + +Bounce::Bounce(uint8_t pin,unsigned long interval_millis) +{ + interval(interval_millis); + previous_millis = millis(); + state = digitalRead(pin); + this->pin = pin; +} + + +void Bounce::write(int new_state) + { + this->state = new_state; + digitalWrite(pin,state); + } + + +void Bounce::interval(unsigned long interval_millis) +{ + this->interval_millis = interval_millis; + this->rebounce_millis = 0; +} + +void Bounce::rebounce(unsigned long interval) +{ + this->rebounce_millis = interval; +} + + + +int Bounce::update() +{ + if ( debounce() ) { + rebounce(0); + return stateChanged = 1; + } + + // We need to rebounce, so simulate a state change + + if ( rebounce_millis && (millis() - previous_millis >= rebounce_millis) ) { + previous_millis = millis(); + rebounce(0); + return stateChanged = 1; + } + + return stateChanged = 0; +} + + +unsigned long Bounce::duration() +{ + return millis() - previous_millis; +} + + +int Bounce::read() +{ + return (int)state; +} + + +// Protected: debounces the pin +int Bounce::debounce() { + + uint8_t newState = digitalRead(pin); + if (state != newState ) { + if (millis() - previous_millis >= interval_millis) { + previous_millis = millis(); + state = newState; + return 1; + } + } + + return 0; + +} + +// The risingEdge method is true for one scan after the de-bounced input goes from off-to-on. +bool Bounce::risingEdge() { return stateChanged && state; } +// The fallingEdge method it true for one scan after the de-bounced input goes from on-to-off. +bool Bounce::fallingEdge() { return stateChanged && !state; } + -- cgit v1.2.3-24-g4f1b