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