Library for debouncing

Dependents:   DebouncedIn

Committer:
smhapankar
Date:
Tue May 31 08:52:13 2016 +0000
Revision:
0:0076f717ca86
Library for Debouncing

Who changed what in which revision?

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