Library for debouncing inputs, originally by Andres Mora Bedoya. Updated to include PinMode capability and class documentation.
Fork of DebouncedIn by
Diff: DebouncedIn.cpp
- Revision:
- 2:261228f701a1
- Parent:
- 1:7b8a80c09b8c
- Child:
- 3:41d314732786
--- a/DebouncedIn.cpp Wed Oct 08 14:44:47 2014 +0000 +++ b/DebouncedIn.cpp Wed Oct 08 15:14:31 2014 +0000 @@ -1,3 +1,8 @@ +/** + * DebouncedIn class version 1.0 + * Created by Andres Moya Bedoya, updated by Ben Faucher + */ + #include "DebouncedIn.h" #include "mbed.h" @@ -7,7 +12,7 @@ DebouncedIn::DebouncedIn(PinName in) : _in(in) { - // reset all the flags and counters + // Reset all the flags and counters _samples = 0; _output = 0; _output_last = 0; @@ -22,7 +27,7 @@ DebouncedIn::DebouncedIn(PinName in, PinMode mode) : _in(in, mode) { - // reset all the flags and counters + // Reset all the flags and counters _samples = 0; _output = 0; _output_last = 0; @@ -33,11 +38,70 @@ // Attach ticker _ticker.attach(this, &DebouncedIn::_sample, 0.005); } - + +// Public member functions + +/** Read the input state, represented as 0 or 1 (int) + * + * @returns + * An integer representing the state of the input pin, + * 0 for logical 0, 1 for logical 1. State changes when input + * has been steady for at least 40ms (8 ticker cycles of 5ms). + */ +int DebouncedIn::read(void) { + return(_output); +} + +/** An operator shorthand for read() + */ +DebouncedIn::operator int() { + return read(); +} + +// return number of rising edges +/** Rising edge count (int) + * + * @returns + * An integer representing the number of times the switch has + * changed from low to high. Count resets to zero when this + * function is called. + */ +int DebouncedIn::rising(void) { + int return_value = _rising_flag; + _rising_flag = 0; + return(return_value); +} + +// return number of falling edges +/** Falling edge count (int) + * + * @returns + * An integer representing the number of times the switch has + * changed from high to low. Count resets to zero when this + * function is called. + */ +int DebouncedIn::falling(void) { + int return_value = _falling_flag; + _falling_flag = 0; + return(return_value); +} + +// return number of ticks we've bene steady for +/** Steady state tick count (int) + * + * @returns + * An integer representing how many ticker cycles the input has been + * steady for. Ticker cycles every 5ms. + */ +int DebouncedIn::steady(void) { +return(_state_counter); +} + +// Private member functions void DebouncedIn::_sample() { // take a sample - _samples = _samples >> 1; // shift left + _samples = _samples >> 1; // shift right 1 bit if (_in) { _samples |= 0x80; @@ -72,35 +136,4 @@ // update the output _output_last = _output; -} - - - -// return number of rising edges -int DebouncedIn::rising(void) { - int return_value = _rising_flag; - _rising_flag = 0; - return(return_value); -} - -// return number of falling edges -int DebouncedIn::falling(void) { - int return_value = _falling_flag; - _falling_flag = 0; - return(return_value); -} - -// return number of ticsk we've bene steady for -int DebouncedIn::steady(void) { -return(_state_counter); -} - -// return the debounced status -int DebouncedIn::read(void) { - return(_output); -} - -// shorthand for read() -DebouncedIn::operator int() { - return read(); } \ No newline at end of file