This class carries out software debouncing of Digitial in, and provides some method to look at details such as how many debounced rising and falling there have been, and how long the input has been in its current stable state.

Dependencies:   mbed

Committer:
chris
Date:
Fri Nov 27 16:05:32 2009 +0000
Revision:
0:672241227e0d

        

Who changed what in which revision?

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