Library for debouncing inputs, originally by Andres Mora Bedoya. Updated to include PinMode capability and class documentation.
Fork of DebouncedIn by
DebouncedIn.h@4:e88ec98d2b7e, 2014-10-16 (annotated)
- Committer:
- faucherb94
- Date:
- Thu Oct 16 17:24:35 2014 +0000
- Revision:
- 4:e88ec98d2b7e
- Parent:
- 2:261228f701a1
Updated documentation
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 | 4:e88ec98d2b7e | 20 | */ |
faucherb94 | 4:e88ec98d2b7e | 21 | DebouncedIn(PinName in); |
faucherb94 | 4:e88ec98d2b7e | 22 | |
faucherb94 | 4:e88ec98d2b7e | 23 | /** Same as before, with option to specify pin mode |
faucherb94 | 4:e88ec98d2b7e | 24 | * |
faucherb94 | 4:e88ec98d2b7e | 25 | * @param in DigitalIn pin to connect to |
faucherb94 | 1:7b8a80c09b8c | 26 | * @param mode (optional) Set pull mode - PullUp, PullDown, PullNone, OpenDrain |
faucherb94 | 1:7b8a80c09b8c | 27 | */ |
faucherb94 | 1:7b8a80c09b8c | 28 | DebouncedIn(PinName in, PinMode mode); |
cmorab | 0:dc1131de43e8 | 29 | |
faucherb94 | 1:7b8a80c09b8c | 30 | /** Read the input state, represented as 0 or 1 (int) |
faucherb94 | 1:7b8a80c09b8c | 31 | * |
faucherb94 | 1:7b8a80c09b8c | 32 | * @returns |
faucherb94 | 1:7b8a80c09b8c | 33 | * An integer representing the state of the input pin, |
faucherb94 | 2:261228f701a1 | 34 | * 0 for logical 0, 1 for logical 1. State changes when input |
faucherb94 | 2:261228f701a1 | 35 | * has been steady for at least 40ms (8 ticker cycles of 5ms). |
faucherb94 | 1:7b8a80c09b8c | 36 | */ |
cmorab | 0:dc1131de43e8 | 37 | int read (void); |
faucherb94 | 1:7b8a80c09b8c | 38 | |
faucherb94 | 1:7b8a80c09b8c | 39 | /** An operator shorthand for read() |
faucherb94 | 1:7b8a80c09b8c | 40 | */ |
cmorab | 0:dc1131de43e8 | 41 | operator int(); |
cmorab | 0:dc1131de43e8 | 42 | |
faucherb94 | 2:261228f701a1 | 43 | /** Rising edge count (int) |
faucherb94 | 1:7b8a80c09b8c | 44 | * |
faucherb94 | 1:7b8a80c09b8c | 45 | * @returns |
faucherb94 | 2:261228f701a1 | 46 | * An integer representing the number of times the switch has |
faucherb94 | 2:261228f701a1 | 47 | * changed from low to high. Count resets to zero when this |
faucherb94 | 2:261228f701a1 | 48 | * function is called. |
faucherb94 | 1:7b8a80c09b8c | 49 | */ |
cmorab | 0:dc1131de43e8 | 50 | int rising(void); |
faucherb94 | 1:7b8a80c09b8c | 51 | |
faucherb94 | 2:261228f701a1 | 52 | /** Falling edge count (int) |
faucherb94 | 1:7b8a80c09b8c | 53 | * |
faucherb94 | 1:7b8a80c09b8c | 54 | * @returns |
faucherb94 | 2:261228f701a1 | 55 | * An integer representing the number of times the switch has |
faucherb94 | 2:261228f701a1 | 56 | * changed from high to low. Count resets to zero when this |
faucherb94 | 2:261228f701a1 | 57 | * function is called. |
faucherb94 | 1:7b8a80c09b8c | 58 | */ |
cmorab | 0:dc1131de43e8 | 59 | int falling(void); |
faucherb94 | 1:7b8a80c09b8c | 60 | |
faucherb94 | 2:261228f701a1 | 61 | /** Steady state tick count (int) |
faucherb94 | 1:7b8a80c09b8c | 62 | * |
faucherb94 | 1:7b8a80c09b8c | 63 | * @returns |
faucherb94 | 2:261228f701a1 | 64 | * An integer representing how many ticker cycles the input has been |
faucherb94 | 2:261228f701a1 | 65 | * steady for. Ticker cycles every 5ms. |
faucherb94 | 1:7b8a80c09b8c | 66 | */ |
cmorab | 0:dc1131de43e8 | 67 | int steady(void); |
cmorab | 0:dc1131de43e8 | 68 | |
cmorab | 0:dc1131de43e8 | 69 | private : |
cmorab | 0:dc1131de43e8 | 70 | // objects |
cmorab | 0:dc1131de43e8 | 71 | DigitalIn _in; |
cmorab | 0:dc1131de43e8 | 72 | Ticker _ticker; |
cmorab | 0:dc1131de43e8 | 73 | |
cmorab | 0:dc1131de43e8 | 74 | // function to take a sample, and update flags |
cmorab | 0:dc1131de43e8 | 75 | void _sample(void); |
cmorab | 0:dc1131de43e8 | 76 | |
cmorab | 0:dc1131de43e8 | 77 | // counters and flags |
cmorab | 0:dc1131de43e8 | 78 | int _samples; |
cmorab | 0:dc1131de43e8 | 79 | int _output; |
cmorab | 0:dc1131de43e8 | 80 | int _output_last; |
cmorab | 0:dc1131de43e8 | 81 | int _rising_flag; |
cmorab | 0:dc1131de43e8 | 82 | int _falling_flag; |
cmorab | 0:dc1131de43e8 | 83 | int _state_counter; |
cmorab | 0:dc1131de43e8 | 84 | |
faucherb94 | 1:7b8a80c09b8c | 85 | }; |
faucherb94 | 1:7b8a80c09b8c | 86 | |
faucherb94 | 1:7b8a80c09b8c | 87 | #endif |