A replacement for InterruptIn that debounces the interrupt.
Dependents: D7A_Demo-Get-started CVtoOSCConverter EE3501keypad D7A_Localisation ... more
Fork of DebouncedInterrupt by
Example code:
#include "DebouncedInterrupt.h" DebouncedInterrupt up_button(USER_BUTTON); void onUp() { // Do Something } int main() { // Will immediatly call function and ignore other interrupts until timeout up_button.attach(&onUp, IRQ_FALL, 1000, true); // Will call function only if button has been held for the specified time //up_button.attach(&onUp, IRQ_FALL, 500, false); while(1) {} }
DebounceInterrupts.cpp@0:ca5a0fee9f52, 2014-02-18 (annotated)
- Committer:
- kandangath
- Date:
- Tue Feb 18 01:05:10 2014 +0000
- Revision:
- 0:ca5a0fee9f52
- Child:
- 3:e4b7033508d1
Init version of class to debounce InterruptIn
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kandangath | 0:ca5a0fee9f52 | 1 | #include "DebounceInterrupts.h" |
kandangath | 0:ca5a0fee9f52 | 2 | |
kandangath | 0:ca5a0fee9f52 | 3 | Timeout timeout; |
kandangath | 0:ca5a0fee9f52 | 4 | |
kandangath | 0:ca5a0fee9f52 | 5 | DebounceInterrupts::DebounceInterrupts(void (*fptr)(void), |
kandangath | 0:ca5a0fee9f52 | 6 | InterruptIn *interruptIn, |
kandangath | 0:ca5a0fee9f52 | 7 | const bool& rise, |
kandangath | 0:ca5a0fee9f52 | 8 | const unsigned int& debounce_ms) |
kandangath | 0:ca5a0fee9f52 | 9 | { |
kandangath | 0:ca5a0fee9f52 | 10 | fCallback = fptr; |
kandangath | 0:ca5a0fee9f52 | 11 | if (rise) { |
kandangath | 0:ca5a0fee9f52 | 12 | interruptIn->rise(this, &DebounceInterrupts::onInterrupt); |
kandangath | 0:ca5a0fee9f52 | 13 | } else { |
kandangath | 0:ca5a0fee9f52 | 14 | interruptIn->fall(this, &DebounceInterrupts::onInterrupt); |
kandangath | 0:ca5a0fee9f52 | 15 | } |
kandangath | 0:ca5a0fee9f52 | 16 | fDebounce_us = 1000*debounce_ms; |
kandangath | 0:ca5a0fee9f52 | 17 | } |
kandangath | 0:ca5a0fee9f52 | 18 | |
kandangath | 0:ca5a0fee9f52 | 19 | DebounceInterrupts::~DebounceInterrupts() |
kandangath | 0:ca5a0fee9f52 | 20 | { |
kandangath | 0:ca5a0fee9f52 | 21 | } |
kandangath | 0:ca5a0fee9f52 | 22 | |
kandangath | 0:ca5a0fee9f52 | 23 | void DebounceInterrupts::onInterrupt() |
kandangath | 0:ca5a0fee9f52 | 24 | { |
kandangath | 0:ca5a0fee9f52 | 25 | timeout.detach(); |
kandangath | 0:ca5a0fee9f52 | 26 | timeout.attach_us(fCallback,fDebounce_us); |
kandangath | 0:ca5a0fee9f52 | 27 | } |