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