Library for debouncing inputs, originally by Andres Mora Bedoya. Updated to include PinMode capability and class documentation.
Fork of DebouncedIn by
DebouncedIn.h@1:7b8a80c09b8c, 2014-10-08 (annotated)
- Committer:
- faucherb94
- Date:
- Wed Oct 08 14:44:47 2014 +0000
- Revision:
- 1:7b8a80c09b8c
- Parent:
- 0:dc1131de43e8
- Child:
- 2:261228f701a1
Added PinMode capability, added documentation tags into header file
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
faucherb94 | 1:7b8a80c09b8c | 1 | /** |
faucherb94 | 1:7b8a80c09b8c | 2 | * DebouncedIn class version 1.0 |
faucherb94 | 1:7b8a80c09b8c | 3 | * Created by Andres Moya Bedoya, updated by Ben Faucher |
faucherb94 | 1:7b8a80c09b8c | 4 | */ |
faucherb94 | 1:7b8a80c09b8c | 5 | |
cmorab | 0:dc1131de43e8 | 6 | #include "mbed.h" |
faucherb94 | 1:7b8a80c09b8c | 7 | |
faucherb94 | 1:7b8a80c09b8c | 8 | #ifndef _DEBOUNCED_IN_H_ |
faucherb94 | 1:7b8a80c09b8c | 9 | #define _DEBOUNCED_IN_H_ |
faucherb94 | 1:7b8a80c09b8c | 10 | |
faucherb94 | 1:7b8a80c09b8c | 11 | /** |
faucherb94 | 1:7b8a80c09b8c | 12 | * DebouncedIn object, uses software tickers to debounce mechanical inputs. |
faucherb94 | 1:7b8a80c09b8c | 13 | */ |
faucherb94 | 1:7b8a80c09b8c | 14 | |
cmorab | 0:dc1131de43e8 | 15 | class DebouncedIn { |
cmorab | 0:dc1131de43e8 | 16 | public: |
faucherb94 | 1:7b8a80c09b8c | 17 | /** Create a DebouncedIn connected to the specified pin |
faucherb94 | 1:7b8a80c09b8c | 18 | * |
faucherb94 | 1:7b8a80c09b8c | 19 | * @param in DigitalIn pin to connect to |
faucherb94 | 1:7b8a80c09b8c | 20 | * @param mode (optional) Set pull mode - PullUp, PullDown, PullNone, OpenDrain |
faucherb94 | 1:7b8a80c09b8c | 21 | */ |
cmorab | 0:dc1131de43e8 | 22 | DebouncedIn(PinName in); |
faucherb94 | 1:7b8a80c09b8c | 23 | DebouncedIn(PinName in, PinMode mode); |
cmorab | 0:dc1131de43e8 | 24 | |
faucherb94 | 1:7b8a80c09b8c | 25 | /** Read the input state, represented as 0 or 1 (int) |
faucherb94 | 1:7b8a80c09b8c | 26 | * |
faucherb94 | 1:7b8a80c09b8c | 27 | * @returns |
faucherb94 | 1:7b8a80c09b8c | 28 | * An integer representing the state of the input pin, |
faucherb94 | 1:7b8a80c09b8c | 29 | * 0 for logical 0, 1 for logical 1 |
faucherb94 | 1:7b8a80c09b8c | 30 | */ |
cmorab | 0:dc1131de43e8 | 31 | int read (void); |
faucherb94 | 1:7b8a80c09b8c | 32 | |
faucherb94 | 1:7b8a80c09b8c | 33 | /** An operator shorthand for read() |
faucherb94 | 1:7b8a80c09b8c | 34 | */ |
cmorab | 0:dc1131de43e8 | 35 | operator int(); |
cmorab | 0:dc1131de43e8 | 36 | |
faucherb94 | 1:7b8a80c09b8c | 37 | /** Register the input changing from low to high (int) |
faucherb94 | 1:7b8a80c09b8c | 38 | * |
faucherb94 | 1:7b8a80c09b8c | 39 | * @returns |
faucherb94 | 1:7b8a80c09b8c | 40 | * An integer representing if the input changed from low to high, |
faucherb94 | 1:7b8a80c09b8c | 41 | * 0 for logical 0, 1 for logical 1 |
faucherb94 | 1:7b8a80c09b8c | 42 | */ |
cmorab | 0:dc1131de43e8 | 43 | int rising(void); |
faucherb94 | 1:7b8a80c09b8c | 44 | |
faucherb94 | 1:7b8a80c09b8c | 45 | /** Register the input changing from high to low (int) |
faucherb94 | 1:7b8a80c09b8c | 46 | * |
faucherb94 | 1:7b8a80c09b8c | 47 | * @returns |
faucherb94 | 1:7b8a80c09b8c | 48 | * An integer representing if the input changed from high to low, |
faucherb94 | 1:7b8a80c09b8c | 49 | * 0 for logical 0, 1 for logical 1 |
faucherb94 | 1:7b8a80c09b8c | 50 | */ |
cmorab | 0:dc1131de43e8 | 51 | int falling(void); |
faucherb94 | 1:7b8a80c09b8c | 52 | |
faucherb94 | 1:7b8a80c09b8c | 53 | /** Check steady state of input (int) |
faucherb94 | 1:7b8a80c09b8c | 54 | * |
faucherb94 | 1:7b8a80c09b8c | 55 | * @returns |
faucherb94 | 1:7b8a80c09b8c | 56 | * An integer representing if the input has remained |
faucherb94 | 1:7b8a80c09b8c | 57 | * steady for a set amount of time, |
faucherb94 | 1:7b8a80c09b8c | 58 | * 0 for logical 0, 1 for logical 1 |
faucherb94 | 1:7b8a80c09b8c | 59 | */ |
cmorab | 0:dc1131de43e8 | 60 | int steady(void); |
cmorab | 0:dc1131de43e8 | 61 | |
cmorab | 0:dc1131de43e8 | 62 | private : |
cmorab | 0:dc1131de43e8 | 63 | // objects |
cmorab | 0:dc1131de43e8 | 64 | DigitalIn _in; |
cmorab | 0:dc1131de43e8 | 65 | Ticker _ticker; |
cmorab | 0:dc1131de43e8 | 66 | |
cmorab | 0:dc1131de43e8 | 67 | // function to take a sample, and update flags |
cmorab | 0:dc1131de43e8 | 68 | void _sample(void); |
cmorab | 0:dc1131de43e8 | 69 | |
cmorab | 0:dc1131de43e8 | 70 | // counters and flags |
cmorab | 0:dc1131de43e8 | 71 | int _samples; |
cmorab | 0:dc1131de43e8 | 72 | int _output; |
cmorab | 0:dc1131de43e8 | 73 | int _output_last; |
cmorab | 0:dc1131de43e8 | 74 | int _rising_flag; |
cmorab | 0:dc1131de43e8 | 75 | int _falling_flag; |
cmorab | 0:dc1131de43e8 | 76 | int _state_counter; |
cmorab | 0:dc1131de43e8 | 77 | |
faucherb94 | 1:7b8a80c09b8c | 78 | }; |
faucherb94 | 1:7b8a80c09b8c | 79 | |
faucherb94 | 1:7b8a80c09b8c | 80 | #endif |