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