Debaunced Input like InterruptIn.
Dependents: LcdClock idd_hw2_martincowell_bicyclehid IDD_FALL15_HW2_JMARQUIS_Keypadentry
DebouncedEdgeIn.h@0:471239ea932b, 2014-02-22 (annotated)
- Committer:
- togayan
- Date:
- Sat Feb 22 23:55:10 2014 +0000
- Revision:
- 0:471239ea932b
1st release.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
togayan | 0:471239ea932b | 1 | #ifndef DEBAUNCED_EDGE_IN_H |
togayan | 0:471239ea932b | 2 | #define DEBAUNCED_EDGE_IN_H |
togayan | 0:471239ea932b | 3 | |
togayan | 0:471239ea932b | 4 | #include "mbed.h" |
togayan | 0:471239ea932b | 5 | #include "FunctionPointer.h" |
togayan | 0:471239ea932b | 6 | |
togayan | 0:471239ea932b | 7 | class DebouncedEdgeIn |
togayan | 0:471239ea932b | 8 | { |
togayan | 0:471239ea932b | 9 | public: |
togayan | 0:471239ea932b | 10 | DebouncedEdgeIn(PinName in); |
togayan | 0:471239ea932b | 11 | virtual ~DebouncedEdgeIn(); |
togayan | 0:471239ea932b | 12 | |
togayan | 0:471239ea932b | 13 | int read (void); |
togayan | 0:471239ea932b | 14 | operator int(); |
togayan | 0:471239ea932b | 15 | |
togayan | 0:471239ea932b | 16 | /** Attach a function to call when a rising edge occurs on the input |
togayan | 0:471239ea932b | 17 | * |
togayan | 0:471239ea932b | 18 | * @param fptr A pointer to a void function, or 0 to set as none |
togayan | 0:471239ea932b | 19 | */ |
togayan | 0:471239ea932b | 20 | void rise(void (*fptr)(void)); |
togayan | 0:471239ea932b | 21 | |
togayan | 0:471239ea932b | 22 | /** Attach a member function to call when a rising edge occurs on the input |
togayan | 0:471239ea932b | 23 | * |
togayan | 0:471239ea932b | 24 | * @param tptr pointer to the object to call the member function on |
togayan | 0:471239ea932b | 25 | * @param mptr pointer to the member function to be called |
togayan | 0:471239ea932b | 26 | */ |
togayan | 0:471239ea932b | 27 | template<typename T> |
togayan | 0:471239ea932b | 28 | void rise(T* tptr, void (T::*mptr)(void)) { |
togayan | 0:471239ea932b | 29 | _rise.attach(tptr, mptr); |
togayan | 0:471239ea932b | 30 | } |
togayan | 0:471239ea932b | 31 | |
togayan | 0:471239ea932b | 32 | /** Attach a function to call when a falling edge occurs on the input |
togayan | 0:471239ea932b | 33 | * |
togayan | 0:471239ea932b | 34 | * @param fptr A pointer to a void function, or 0 to set as none |
togayan | 0:471239ea932b | 35 | */ |
togayan | 0:471239ea932b | 36 | void fall(void (*fptr)(void)); |
togayan | 0:471239ea932b | 37 | |
togayan | 0:471239ea932b | 38 | /** Attach a member function to call when a falling edge occurs on the input |
togayan | 0:471239ea932b | 39 | * |
togayan | 0:471239ea932b | 40 | * @param tptr pointer to the object to call the member function on |
togayan | 0:471239ea932b | 41 | * @param mptr pointer to the member function to be called |
togayan | 0:471239ea932b | 42 | */ |
togayan | 0:471239ea932b | 43 | template<typename T> |
togayan | 0:471239ea932b | 44 | void fall(T* tptr, void (T::*mptr)(void)) { |
togayan | 0:471239ea932b | 45 | _fall.attach(tptr, mptr); |
togayan | 0:471239ea932b | 46 | } |
togayan | 0:471239ea932b | 47 | |
togayan | 0:471239ea932b | 48 | /** Set the input pin mode |
togayan | 0:471239ea932b | 49 | * |
togayan | 0:471239ea932b | 50 | * @param mode PullUp, PullDown, PullNone |
togayan | 0:471239ea932b | 51 | */ |
togayan | 0:471239ea932b | 52 | void mode(PinMode pull); |
togayan | 0:471239ea932b | 53 | |
togayan | 0:471239ea932b | 54 | private : |
togayan | 0:471239ea932b | 55 | // objects |
togayan | 0:471239ea932b | 56 | DigitalIn _in; |
togayan | 0:471239ea932b | 57 | Ticker _ticker; |
togayan | 0:471239ea932b | 58 | FunctionPointer _rise; |
togayan | 0:471239ea932b | 59 | FunctionPointer _fall; |
togayan | 0:471239ea932b | 60 | |
togayan | 0:471239ea932b | 61 | // function to take a sample, and update flags |
togayan | 0:471239ea932b | 62 | void _sample(void); |
togayan | 0:471239ea932b | 63 | |
togayan | 0:471239ea932b | 64 | // counters and flags |
togayan | 0:471239ea932b | 65 | int _samples; |
togayan | 0:471239ea932b | 66 | int _output; |
togayan | 0:471239ea932b | 67 | int _output_last; |
togayan | 0:471239ea932b | 68 | }; |
togayan | 0:471239ea932b | 69 | |
togayan | 0:471239ea932b | 70 | #endif // DEBAUNCED_EDGE_IN_H |