Library for debouncing inputs, originally by Andres Mora Bedoya. Updated to include PinMode capability and class documentation.
Fork of DebouncedIn by
DebouncedIn.cpp@4:e88ec98d2b7e, 2014-10-16 (annotated)
- Committer:
- faucherb94
- Date:
- Thu Oct 16 17:24:35 2014 +0000
- Revision:
- 4:e88ec98d2b7e
- Parent:
- 3:41d314732786
Updated documentation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
faucherb94 | 4:e88ec98d2b7e | 1 | /* |
faucherb94 | 2:261228f701a1 | 2 | * DebouncedIn class version 1.0 |
faucherb94 | 2:261228f701a1 | 3 | * Created by Andres Moya Bedoya, updated by Ben Faucher |
faucherb94 | 2:261228f701a1 | 4 | */ |
faucherb94 | 2:261228f701a1 | 5 | |
cmorab | 0:dc1131de43e8 | 6 | #include "DebouncedIn.h" |
cmorab | 0:dc1131de43e8 | 7 | #include "mbed.h" |
faucherb94 | 3:41d314732786 | 8 | |
cmorab | 0:dc1131de43e8 | 9 | DebouncedIn::DebouncedIn(PinName in) |
cmorab | 0:dc1131de43e8 | 10 | : _in(in) { |
cmorab | 0:dc1131de43e8 | 11 | |
faucherb94 | 2:261228f701a1 | 12 | // Reset all the flags and counters |
cmorab | 0:dc1131de43e8 | 13 | _samples = 0; |
cmorab | 0:dc1131de43e8 | 14 | _output = 0; |
cmorab | 0:dc1131de43e8 | 15 | _output_last = 0; |
cmorab | 0:dc1131de43e8 | 16 | _rising_flag = 0; |
cmorab | 0:dc1131de43e8 | 17 | _falling_flag = 0; |
cmorab | 0:dc1131de43e8 | 18 | _state_counter = 0; |
cmorab | 0:dc1131de43e8 | 19 | |
cmorab | 0:dc1131de43e8 | 20 | // Attach ticker |
cmorab | 0:dc1131de43e8 | 21 | _ticker.attach(this, &DebouncedIn::_sample, 0.005); |
cmorab | 0:dc1131de43e8 | 22 | } |
faucherb94 | 1:7b8a80c09b8c | 23 | |
faucherb94 | 1:7b8a80c09b8c | 24 | DebouncedIn::DebouncedIn(PinName in, PinMode mode) |
faucherb94 | 1:7b8a80c09b8c | 25 | : _in(in, mode) { |
faucherb94 | 1:7b8a80c09b8c | 26 | |
faucherb94 | 2:261228f701a1 | 27 | // Reset all the flags and counters |
faucherb94 | 1:7b8a80c09b8c | 28 | _samples = 0; |
faucherb94 | 1:7b8a80c09b8c | 29 | _output = 0; |
faucherb94 | 1:7b8a80c09b8c | 30 | _output_last = 0; |
faucherb94 | 1:7b8a80c09b8c | 31 | _rising_flag = 0; |
faucherb94 | 1:7b8a80c09b8c | 32 | _falling_flag = 0; |
faucherb94 | 1:7b8a80c09b8c | 33 | _state_counter = 0; |
faucherb94 | 1:7b8a80c09b8c | 34 | |
faucherb94 | 1:7b8a80c09b8c | 35 | // Attach ticker |
faucherb94 | 1:7b8a80c09b8c | 36 | _ticker.attach(this, &DebouncedIn::_sample, 0.005); |
faucherb94 | 1:7b8a80c09b8c | 37 | } |
faucherb94 | 2:261228f701a1 | 38 | |
faucherb94 | 2:261228f701a1 | 39 | // Public member functions |
faucherb94 | 2:261228f701a1 | 40 | |
faucherb94 | 2:261228f701a1 | 41 | int DebouncedIn::read(void) { |
faucherb94 | 2:261228f701a1 | 42 | return(_output); |
faucherb94 | 2:261228f701a1 | 43 | } |
faucherb94 | 3:41d314732786 | 44 | |
faucherb94 | 2:261228f701a1 | 45 | DebouncedIn::operator int() { |
faucherb94 | 2:261228f701a1 | 46 | return read(); |
faucherb94 | 2:261228f701a1 | 47 | } |
faucherb94 | 2:261228f701a1 | 48 | |
faucherb94 | 2:261228f701a1 | 49 | // return number of rising edges |
faucherb94 | 2:261228f701a1 | 50 | int DebouncedIn::rising(void) { |
faucherb94 | 2:261228f701a1 | 51 | int return_value = _rising_flag; |
faucherb94 | 2:261228f701a1 | 52 | _rising_flag = 0; |
faucherb94 | 2:261228f701a1 | 53 | return(return_value); |
faucherb94 | 2:261228f701a1 | 54 | } |
faucherb94 | 2:261228f701a1 | 55 | |
faucherb94 | 2:261228f701a1 | 56 | // return number of falling edges |
faucherb94 | 2:261228f701a1 | 57 | int DebouncedIn::falling(void) { |
faucherb94 | 2:261228f701a1 | 58 | int return_value = _falling_flag; |
faucherb94 | 2:261228f701a1 | 59 | _falling_flag = 0; |
faucherb94 | 2:261228f701a1 | 60 | return(return_value); |
faucherb94 | 2:261228f701a1 | 61 | } |
faucherb94 | 2:261228f701a1 | 62 | |
faucherb94 | 2:261228f701a1 | 63 | // return number of ticks we've bene steady for |
faucherb94 | 2:261228f701a1 | 64 | int DebouncedIn::steady(void) { |
faucherb94 | 2:261228f701a1 | 65 | return(_state_counter); |
faucherb94 | 2:261228f701a1 | 66 | } |
faucherb94 | 2:261228f701a1 | 67 | |
faucherb94 | 2:261228f701a1 | 68 | // Private member functions |
cmorab | 0:dc1131de43e8 | 69 | void DebouncedIn::_sample() { |
cmorab | 0:dc1131de43e8 | 70 | |
cmorab | 0:dc1131de43e8 | 71 | // take a sample |
faucherb94 | 2:261228f701a1 | 72 | _samples = _samples >> 1; // shift right 1 bit |
cmorab | 0:dc1131de43e8 | 73 | |
cmorab | 0:dc1131de43e8 | 74 | if (_in) { |
cmorab | 0:dc1131de43e8 | 75 | _samples |= 0x80; |
cmorab | 0:dc1131de43e8 | 76 | } |
cmorab | 0:dc1131de43e8 | 77 | |
cmorab | 0:dc1131de43e8 | 78 | // examine the sample window, look for steady state |
cmorab | 0:dc1131de43e8 | 79 | if (_samples == 0x00) { |
cmorab | 0:dc1131de43e8 | 80 | _output = 0; |
cmorab | 0:dc1131de43e8 | 81 | } |
cmorab | 0:dc1131de43e8 | 82 | else if (_samples == 0xFF) { |
cmorab | 0:dc1131de43e8 | 83 | _output = 1; |
cmorab | 0:dc1131de43e8 | 84 | } |
cmorab | 0:dc1131de43e8 | 85 | |
cmorab | 0:dc1131de43e8 | 86 | |
cmorab | 0:dc1131de43e8 | 87 | // Rising edge detection |
cmorab | 0:dc1131de43e8 | 88 | if ((_output == 1) && (_output_last == 0)) { |
cmorab | 0:dc1131de43e8 | 89 | _rising_flag++; |
cmorab | 0:dc1131de43e8 | 90 | _state_counter = 0; |
cmorab | 0:dc1131de43e8 | 91 | } |
cmorab | 0:dc1131de43e8 | 92 | |
cmorab | 0:dc1131de43e8 | 93 | // Falling edge detection |
cmorab | 0:dc1131de43e8 | 94 | else if ((_output == 0) && (_output_last == 1)) { |
cmorab | 0:dc1131de43e8 | 95 | _falling_flag++; |
cmorab | 0:dc1131de43e8 | 96 | _state_counter = 0; |
cmorab | 0:dc1131de43e8 | 97 | } |
cmorab | 0:dc1131de43e8 | 98 | |
cmorab | 0:dc1131de43e8 | 99 | // steady state |
cmorab | 0:dc1131de43e8 | 100 | else { |
cmorab | 0:dc1131de43e8 | 101 | _state_counter++; |
cmorab | 0:dc1131de43e8 | 102 | } |
cmorab | 0:dc1131de43e8 | 103 | |
cmorab | 0:dc1131de43e8 | 104 | // update the output |
cmorab | 0:dc1131de43e8 | 105 | _output_last = _output; |
cmorab | 0:dc1131de43e8 | 106 | |
cmorab | 0:dc1131de43e8 | 107 | } |