libreria para el manejo de entradas

Dependents:   Prueba_Ascensor Ascensor NewAscensor

Committer:
jclondonol
Date:
Tue Jun 06 15:24:02 2017 +0000
Revision:
0:1ad8929cc2b4
listo

Who changed what in which revision?

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