Simple program to use the debouncing library
Dependencies: DebouncedIn mbed
Fork of DebouncedIn_HelloWorld by
Revision 1:7e4accb72e06, committed 2016-05-24
- Comitter:
- janhavi
- Date:
- Tue May 24 08:57:39 2016 +0000
- Parent:
- 0:672241227e0d
- Child:
- 2:b3d5973afde7
- Commit message:
- Simple debouncing library for switches
Changed in this revision
--- a/DebouncedIn.cpp Fri Nov 27 16:05:32 2009 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,93 +0,0 @@ -#include "DebouncedIn.h" -#include "mbed.h" - -/* - * Constructor - */ -DebouncedIn::DebouncedIn(PinName in) - : _in(in) { - - // reset all the flags and counters - _samples = 0; - _output = 0; - _output_last = 0; - _rising_flag = 0; - _falling_flag = 0; - _state_counter = 0; - - // Attach ticker - _ticker.attach(this, &DebouncedIn::_sample, 0.005); -} - -void DebouncedIn::_sample() { - - // take a sample - _samples = _samples >> 1; // shift left - - if (_in) { - _samples |= 0x80; - } - - // examine the sample window, look for steady state - if (_samples == 0x00) { - _output = 0; - } - else if (_samples == 0xFF) { - _output = 1; - } - - - // Rising edge detection - if ((_output == 1) && (_output_last == 0)) { - _rising_flag++; - _state_counter = 0; - } - - // Falling edge detection - else if ((_output == 0) && (_output_last == 1)) { - _falling_flag++; - _state_counter = 0; - } - - // steady state - else { - _state_counter++; - } - - // 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(); -} - -
--- a/DebouncedIn.h Fri Nov 27 16:05:32 2009 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,31 +0,0 @@ -#include "mbed.h" - - class DebouncedIn { - public: - DebouncedIn(PinName in); - - int read (void); - operator int(); - - int rising(void); - int falling(void); - int steady(void); - - private : - // objects - DigitalIn _in; - Ticker _ticker; - - // function to take a sample, and update flags - void _sample(void); - - // counters and flags - int _samples; - int _output; - int _output_last; - int _rising_flag; - int _falling_flag; - int _state_counter; - - }; - \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DebouncedIn.lib Tue May 24 08:57:39 2016 +0000 @@ -0,0 +1,1 @@ +DebouncedIn#e2ba40ab11e8
--- a/main.cpp Fri Nov 27 16:05:32 2009 +0000 +++ b/main.cpp Tue May 24 08:57:39 2016 +0000 @@ -3,7 +3,7 @@ DigitalOut led(LED1); -DebouncedIn button(p21); +DebouncedIn button(PTC9); int main() {
--- a/mbed.bld Fri Nov 27 16:05:32 2009 +0000 +++ b/mbed.bld Tue May 24 08:57:39 2016 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/20a79241b4a0 +http://mbed.org/users/mbed_official/code/mbed/builds/7c328cabac7e \ No newline at end of file