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