h

Dependencies:   QEI RTC-DS1307 TextLCD mbed

Committer:
Gambetica
Date:
Wed Nov 29 01:30:37 2017 +0000
Revision:
0:448768f23e05
h

Who changed what in which revision?

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