A debouncing InterruptIn wrapper
Revision 0:2e999fcd4c7b, committed 2012-02-06
- Comitter:
- evwijk
- Date:
- Mon Feb 06 19:53:40 2012 +0000
- Child:
- 1:e806603f0088
- Commit message:
Changed in this revision
ButtonIn.cpp | Show annotated file Show diff for this revision Revisions of this file |
ButtonIn.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ButtonIn.cpp Mon Feb 06 19:53:40 2012 +0000 @@ -0,0 +1,38 @@ +#include "ButtonIn.h" + +ButtonIn::ButtonIn(PinName buttonPin) : + _button(buttonPin) { + _buttonCanPress = true; + _callback = NULL; + _callbackInstance = NULL; + _callbackMethod = NULL; + _button.rise(this, &ButtonIn::click); +} + +void ButtonIn::attach(void (*method)(void)) { + _callback = method; +} + +template<class T> +void attach(T* instance, void (T::*method)(void)) { + _callbackInstance = (ButtonInCallbackInstance *)instance; + _callbackMethod = (void (ButtonInCallbackInstance::*)(void))method; +} + +void ButtonIn::click() { + if (_buttonCanPress) { + _buttonCanPress = false; + _buttonDownTimeout.attach(this, &ButtonIn::reset, 0.5); + } +} + +void ButtonIn::call() { + if (_callback != NULL) + (*_callback)(); + else + (_callbackInstance->*_callbackMethod)(); +} + +void ButtonIn::reset() { + _buttonCanPress = true; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ButtonIn.h Mon Feb 06 19:53:40 2012 +0000 @@ -0,0 +1,37 @@ +#include "mbed.h" + +#ifndef _ButtonIn_ +#define _ButtonIn_ + + +class ButtonInCallbackInstance; + + +class ButtonIn { + +private: + InterruptIn _button; + Timeout _buttonDownTimeout; + + bool _buttonCanPress; + void (*_callback)(void); + ButtonInCallbackInstance *_callbackInstance; + void (ButtonInCallbackInstance::*_callbackMethod)(void); + + void click(); + void call(); + void reset(); + + + +public: + ButtonIn(PinName buttonPin); + + + void attach(void (*method)(void) = 0); + + template<class T> + void attach(T* instance, void (T::*method)(void)); +}; + +#endif \ No newline at end of file