Fabio Andres Ibañez Diaz
/
FUNCIONACORONRDY
GPS NUEVO
DebouncedIn.cpp@0:b11acb885c4c, 2015-06-19 (annotated)
- Committer:
- obifandres
- Date:
- Fri Jun 19 10:44:48 2015 +0000
- Revision:
- 0:b11acb885c4c
GPS NUEVO
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
obifandres | 0:b11acb885c4c | 1 | #include "DebouncedIn.h" |
obifandres | 0:b11acb885c4c | 2 | #include "mbed.h" |
obifandres | 0:b11acb885c4c | 3 | |
obifandres | 0:b11acb885c4c | 4 | /* |
obifandres | 0:b11acb885c4c | 5 | * Constructor |
obifandres | 0:b11acb885c4c | 6 | */ |
obifandres | 0:b11acb885c4c | 7 | DebouncedIn::DebouncedIn(PinName in) |
obifandres | 0:b11acb885c4c | 8 | : _in(in) { |
obifandres | 0:b11acb885c4c | 9 | |
obifandres | 0:b11acb885c4c | 10 | // reset all the flags and counters |
obifandres | 0:b11acb885c4c | 11 | _samples = 0; |
obifandres | 0:b11acb885c4c | 12 | _output = 0; |
obifandres | 0:b11acb885c4c | 13 | _output_last = 0; |
obifandres | 0:b11acb885c4c | 14 | _rising_flag = 0; |
obifandres | 0:b11acb885c4c | 15 | _falling_flag = 0; |
obifandres | 0:b11acb885c4c | 16 | _state_counter = 0; |
obifandres | 0:b11acb885c4c | 17 | |
obifandres | 0:b11acb885c4c | 18 | // Attach ticker |
obifandres | 0:b11acb885c4c | 19 | _ticker.attach(this, &DebouncedIn::_sample, 0.005); |
obifandres | 0:b11acb885c4c | 20 | } |
obifandres | 0:b11acb885c4c | 21 | |
obifandres | 0:b11acb885c4c | 22 | void DebouncedIn::_sample() { |
obifandres | 0:b11acb885c4c | 23 | |
obifandres | 0:b11acb885c4c | 24 | // take a sample |
obifandres | 0:b11acb885c4c | 25 | _samples = _samples >> 1; // shift left |
obifandres | 0:b11acb885c4c | 26 | |
obifandres | 0:b11acb885c4c | 27 | if (_in) { |
obifandres | 0:b11acb885c4c | 28 | _samples |= 0x80; |
obifandres | 0:b11acb885c4c | 29 | } |
obifandres | 0:b11acb885c4c | 30 | |
obifandres | 0:b11acb885c4c | 31 | // examine the sample window, look for steady state |
obifandres | 0:b11acb885c4c | 32 | if (_samples == 0x00) { |
obifandres | 0:b11acb885c4c | 33 | _output = 0; |
obifandres | 0:b11acb885c4c | 34 | } |
obifandres | 0:b11acb885c4c | 35 | else if (_samples == 0xFF) { |
obifandres | 0:b11acb885c4c | 36 | _output = 1; |
obifandres | 0:b11acb885c4c | 37 | } |
obifandres | 0:b11acb885c4c | 38 | |
obifandres | 0:b11acb885c4c | 39 | |
obifandres | 0:b11acb885c4c | 40 | // Rising edge detection |
obifandres | 0:b11acb885c4c | 41 | if ((_output == 1) && (_output_last == 0)) { |
obifandres | 0:b11acb885c4c | 42 | _rising_flag++; |
obifandres | 0:b11acb885c4c | 43 | _state_counter = 0; |
obifandres | 0:b11acb885c4c | 44 | } |
obifandres | 0:b11acb885c4c | 45 | |
obifandres | 0:b11acb885c4c | 46 | // Falling edge detection |
obifandres | 0:b11acb885c4c | 47 | else if ((_output == 0) && (_output_last == 1)) { |
obifandres | 0:b11acb885c4c | 48 | _falling_flag++; |
obifandres | 0:b11acb885c4c | 49 | _state_counter = 0; |
obifandres | 0:b11acb885c4c | 50 | } |
obifandres | 0:b11acb885c4c | 51 | |
obifandres | 0:b11acb885c4c | 52 | // steady state |
obifandres | 0:b11acb885c4c | 53 | else { |
obifandres | 0:b11acb885c4c | 54 | _state_counter++; |
obifandres | 0:b11acb885c4c | 55 | } |
obifandres | 0:b11acb885c4c | 56 | |
obifandres | 0:b11acb885c4c | 57 | // update the output |
obifandres | 0:b11acb885c4c | 58 | _output_last = _output; |
obifandres | 0:b11acb885c4c | 59 | |
obifandres | 0:b11acb885c4c | 60 | } |
obifandres | 0:b11acb885c4c | 61 | |
obifandres | 0:b11acb885c4c | 62 | |
obifandres | 0:b11acb885c4c | 63 | |
obifandres | 0:b11acb885c4c | 64 | // return number of rising edges |
obifandres | 0:b11acb885c4c | 65 | int DebouncedIn::rising(void) { |
obifandres | 0:b11acb885c4c | 66 | int return_value = _rising_flag; |
obifandres | 0:b11acb885c4c | 67 | _rising_flag = 0; |
obifandres | 0:b11acb885c4c | 68 | return(return_value); |
obifandres | 0:b11acb885c4c | 69 | } |
obifandres | 0:b11acb885c4c | 70 | |
obifandres | 0:b11acb885c4c | 71 | // return number of falling edges |
obifandres | 0:b11acb885c4c | 72 | int DebouncedIn::falling(void) { |
obifandres | 0:b11acb885c4c | 73 | int return_value = _falling_flag; |
obifandres | 0:b11acb885c4c | 74 | _falling_flag = 0; |
obifandres | 0:b11acb885c4c | 75 | return(return_value); |
obifandres | 0:b11acb885c4c | 76 | } |
obifandres | 0:b11acb885c4c | 77 | |
obifandres | 0:b11acb885c4c | 78 | // return number of ticsk we've bene steady for |
obifandres | 0:b11acb885c4c | 79 | int DebouncedIn::steady(void) { |
obifandres | 0:b11acb885c4c | 80 | return(_state_counter); |
obifandres | 0:b11acb885c4c | 81 | } |
obifandres | 0:b11acb885c4c | 82 | |
obifandres | 0:b11acb885c4c | 83 | // return the debounced status |
obifandres | 0:b11acb885c4c | 84 | int DebouncedIn::read(void) { |
obifandres | 0:b11acb885c4c | 85 | return(_output); |
obifandres | 0:b11acb885c4c | 86 | } |
obifandres | 0:b11acb885c4c | 87 | |
obifandres | 0:b11acb885c4c | 88 | // shorthand for read() |
obifandres | 0:b11acb885c4c | 89 | DebouncedIn::operator int() { |
obifandres | 0:b11acb885c4c | 90 | return read(); |
obifandres | 0:b11acb885c4c | 91 | } |
obifandres | 0:b11acb885c4c | 92 | |
obifandres | 0:b11acb885c4c | 93 |