Library for debouncing inputs, originally by Andres Mora Bedoya. Updated to include PinMode capability and class documentation.

Fork of DebouncedIn by Andrés Mora Bedoya

Committer:
faucherb94
Date:
Wed Oct 08 15:14:31 2014 +0000
Revision:
2:261228f701a1
Parent:
1:7b8a80c09b8c
Child:
3:41d314732786
Updated documentation.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
faucherb94 2:261228f701a1 1 /**
faucherb94 2:261228f701a1 2 * DebouncedIn class version 1.0
faucherb94 2:261228f701a1 3 * Created by Andres Moya Bedoya, updated by Ben Faucher
faucherb94 2:261228f701a1 4 */
faucherb94 2:261228f701a1 5
cmorab 0:dc1131de43e8 6 #include "DebouncedIn.h"
cmorab 0:dc1131de43e8 7 #include "mbed.h"
cmorab 0:dc1131de43e8 8
faucherb94 1:7b8a80c09b8c 9 /**
cmorab 0:dc1131de43e8 10 * Constructor
cmorab 0:dc1131de43e8 11 */
cmorab 0:dc1131de43e8 12 DebouncedIn::DebouncedIn(PinName in)
cmorab 0:dc1131de43e8 13 : _in(in) {
cmorab 0:dc1131de43e8 14
faucherb94 2:261228f701a1 15 // Reset all the flags and counters
cmorab 0:dc1131de43e8 16 _samples = 0;
cmorab 0:dc1131de43e8 17 _output = 0;
cmorab 0:dc1131de43e8 18 _output_last = 0;
cmorab 0:dc1131de43e8 19 _rising_flag = 0;
cmorab 0:dc1131de43e8 20 _falling_flag = 0;
cmorab 0:dc1131de43e8 21 _state_counter = 0;
cmorab 0:dc1131de43e8 22
cmorab 0:dc1131de43e8 23 // Attach ticker
cmorab 0:dc1131de43e8 24 _ticker.attach(this, &DebouncedIn::_sample, 0.005);
cmorab 0:dc1131de43e8 25 }
faucherb94 1:7b8a80c09b8c 26
faucherb94 1:7b8a80c09b8c 27 DebouncedIn::DebouncedIn(PinName in, PinMode mode)
faucherb94 1:7b8a80c09b8c 28 : _in(in, mode) {
faucherb94 1:7b8a80c09b8c 29
faucherb94 2:261228f701a1 30 // Reset all the flags and counters
faucherb94 1:7b8a80c09b8c 31 _samples = 0;
faucherb94 1:7b8a80c09b8c 32 _output = 0;
faucherb94 1:7b8a80c09b8c 33 _output_last = 0;
faucherb94 1:7b8a80c09b8c 34 _rising_flag = 0;
faucherb94 1:7b8a80c09b8c 35 _falling_flag = 0;
faucherb94 1:7b8a80c09b8c 36 _state_counter = 0;
faucherb94 1:7b8a80c09b8c 37
faucherb94 1:7b8a80c09b8c 38 // Attach ticker
faucherb94 1:7b8a80c09b8c 39 _ticker.attach(this, &DebouncedIn::_sample, 0.005);
faucherb94 1:7b8a80c09b8c 40 }
faucherb94 2:261228f701a1 41
faucherb94 2:261228f701a1 42 // Public member functions
faucherb94 2:261228f701a1 43
faucherb94 2:261228f701a1 44 /** Read the input state, represented as 0 or 1 (int)
faucherb94 2:261228f701a1 45 *
faucherb94 2:261228f701a1 46 * @returns
faucherb94 2:261228f701a1 47 * An integer representing the state of the input pin,
faucherb94 2:261228f701a1 48 * 0 for logical 0, 1 for logical 1. State changes when input
faucherb94 2:261228f701a1 49 * has been steady for at least 40ms (8 ticker cycles of 5ms).
faucherb94 2:261228f701a1 50 */
faucherb94 2:261228f701a1 51 int DebouncedIn::read(void) {
faucherb94 2:261228f701a1 52 return(_output);
faucherb94 2:261228f701a1 53 }
faucherb94 2:261228f701a1 54
faucherb94 2:261228f701a1 55 /** An operator shorthand for read()
faucherb94 2:261228f701a1 56 */
faucherb94 2:261228f701a1 57 DebouncedIn::operator int() {
faucherb94 2:261228f701a1 58 return read();
faucherb94 2:261228f701a1 59 }
faucherb94 2:261228f701a1 60
faucherb94 2:261228f701a1 61 // return number of rising edges
faucherb94 2:261228f701a1 62 /** Rising edge count (int)
faucherb94 2:261228f701a1 63 *
faucherb94 2:261228f701a1 64 * @returns
faucherb94 2:261228f701a1 65 * An integer representing the number of times the switch has
faucherb94 2:261228f701a1 66 * changed from low to high. Count resets to zero when this
faucherb94 2:261228f701a1 67 * function is called.
faucherb94 2:261228f701a1 68 */
faucherb94 2:261228f701a1 69 int DebouncedIn::rising(void) {
faucherb94 2:261228f701a1 70 int return_value = _rising_flag;
faucherb94 2:261228f701a1 71 _rising_flag = 0;
faucherb94 2:261228f701a1 72 return(return_value);
faucherb94 2:261228f701a1 73 }
faucherb94 2:261228f701a1 74
faucherb94 2:261228f701a1 75 // return number of falling edges
faucherb94 2:261228f701a1 76 /** Falling edge count (int)
faucherb94 2:261228f701a1 77 *
faucherb94 2:261228f701a1 78 * @returns
faucherb94 2:261228f701a1 79 * An integer representing the number of times the switch has
faucherb94 2:261228f701a1 80 * changed from high to low. Count resets to zero when this
faucherb94 2:261228f701a1 81 * function is called.
faucherb94 2:261228f701a1 82 */
faucherb94 2:261228f701a1 83 int DebouncedIn::falling(void) {
faucherb94 2:261228f701a1 84 int return_value = _falling_flag;
faucherb94 2:261228f701a1 85 _falling_flag = 0;
faucherb94 2:261228f701a1 86 return(return_value);
faucherb94 2:261228f701a1 87 }
faucherb94 2:261228f701a1 88
faucherb94 2:261228f701a1 89 // return number of ticks we've bene steady for
faucherb94 2:261228f701a1 90 /** Steady state tick count (int)
faucherb94 2:261228f701a1 91 *
faucherb94 2:261228f701a1 92 * @returns
faucherb94 2:261228f701a1 93 * An integer representing how many ticker cycles the input has been
faucherb94 2:261228f701a1 94 * steady for. Ticker cycles every 5ms.
faucherb94 2:261228f701a1 95 */
faucherb94 2:261228f701a1 96 int DebouncedIn::steady(void) {
faucherb94 2:261228f701a1 97 return(_state_counter);
faucherb94 2:261228f701a1 98 }
faucherb94 2:261228f701a1 99
faucherb94 2:261228f701a1 100 // Private member functions
cmorab 0:dc1131de43e8 101 void DebouncedIn::_sample() {
cmorab 0:dc1131de43e8 102
cmorab 0:dc1131de43e8 103 // take a sample
faucherb94 2:261228f701a1 104 _samples = _samples >> 1; // shift right 1 bit
cmorab 0:dc1131de43e8 105
cmorab 0:dc1131de43e8 106 if (_in) {
cmorab 0:dc1131de43e8 107 _samples |= 0x80;
cmorab 0:dc1131de43e8 108 }
cmorab 0:dc1131de43e8 109
cmorab 0:dc1131de43e8 110 // examine the sample window, look for steady state
cmorab 0:dc1131de43e8 111 if (_samples == 0x00) {
cmorab 0:dc1131de43e8 112 _output = 0;
cmorab 0:dc1131de43e8 113 }
cmorab 0:dc1131de43e8 114 else if (_samples == 0xFF) {
cmorab 0:dc1131de43e8 115 _output = 1;
cmorab 0:dc1131de43e8 116 }
cmorab 0:dc1131de43e8 117
cmorab 0:dc1131de43e8 118
cmorab 0:dc1131de43e8 119 // Rising edge detection
cmorab 0:dc1131de43e8 120 if ((_output == 1) && (_output_last == 0)) {
cmorab 0:dc1131de43e8 121 _rising_flag++;
cmorab 0:dc1131de43e8 122 _state_counter = 0;
cmorab 0:dc1131de43e8 123 }
cmorab 0:dc1131de43e8 124
cmorab 0:dc1131de43e8 125 // Falling edge detection
cmorab 0:dc1131de43e8 126 else if ((_output == 0) && (_output_last == 1)) {
cmorab 0:dc1131de43e8 127 _falling_flag++;
cmorab 0:dc1131de43e8 128 _state_counter = 0;
cmorab 0:dc1131de43e8 129 }
cmorab 0:dc1131de43e8 130
cmorab 0:dc1131de43e8 131 // steady state
cmorab 0:dc1131de43e8 132 else {
cmorab 0:dc1131de43e8 133 _state_counter++;
cmorab 0:dc1131de43e8 134 }
cmorab 0:dc1131de43e8 135
cmorab 0:dc1131de43e8 136 // update the output
cmorab 0:dc1131de43e8 137 _output_last = _output;
cmorab 0:dc1131de43e8 138
cmorab 0:dc1131de43e8 139 }