A debouncing InterruptIn wrapper
Revision 2:c353357a97e4, committed 2012-02-07
- Comitter:
- evwijk
- Date:
- Tue Feb 07 10:35:38 2012 +0000
- Parent:
- 1:e806603f0088
- Commit message:
- First public release
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 |
diff -r e806603f0088 -r c353357a97e4 ButtonIn.cpp --- a/ButtonIn.cpp Tue Feb 07 09:31:26 2012 +0000 +++ b/ButtonIn.cpp Tue Feb 07 10:35:38 2012 +0000 @@ -1,3 +1,22 @@ +/* +ButtonIn is a debouncing InterruptIn class for mbed (http://mbed.org). + +Copyright (C) 2012 Erik van Wijk (http://mbed.org/users/evwijk/) + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + #include "ButtonIn.h" ButtonIn::ButtonIn(PinName buttonPin) : @@ -6,6 +25,7 @@ _callback = NULL; _callbackInstance = NULL; _callbackMethod = NULL; + timeout = 200; _button.rise(this, &ButtonIn::click); } @@ -20,18 +40,14 @@ } void ButtonIn::click() { - printf("click\r\n"); if (_buttonCanPress) { _buttonCanPress = false; - printf("press\r\n"); - //_buttonDownTimer.reset(); - _buttonDownTimeout.attach(this, &ButtonIn::reset, 5); + _buttonDownTimeout.attach_us(this, &ButtonIn::reset, timeout * 1000); call(); } } void ButtonIn::call() { - printf("call\r\n"); if (_callback != NULL) (*_callback)(); else
diff -r e806603f0088 -r c353357a97e4 ButtonIn.h --- a/ButtonIn.h Tue Feb 07 09:31:26 2012 +0000 +++ b/ButtonIn.h Tue Feb 07 10:35:38 2012 +0000 @@ -1,12 +1,29 @@ +/* +ButtonIn is a debouncing InterruptIn class for mbed (http://mbed.org). + +Copyright (C) 2012 Erik van Wijk (http://mbed.org/users/evwijk/) + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + #include "mbed.h" #ifndef _ButtonIn_ #define _ButtonIn_ - class ButtonInCallbackInstance; - class ButtonIn { private: @@ -23,15 +40,38 @@ void reset(); - public: + // ******************************************************************************** + // * Constructor + // * + // * @param buttonPin The pin which is connected to the button. + // ******************************************************************************** ButtonIn(PinName buttonPin); - + // ******************************************************************************** + // * Attaches the method to be called when the button is pressed. + // * + // * @param method A reference to the method to be called. + // ******************************************************************************** void attach(void (*method)(void) = 0); + // ******************************************************************************** + // * Attaches the method to be called when the button is pressed. + // * + // * @param instance A reference to the instance of the class containing the + // * method to be called. + // * @param method A reference to the method to be called. + // ******************************************************************************** template<class T> void attach(T* instance, void (T::*method)(void)); + + + // ******************************************************************************** + // * The time in milliseconds in which the button can not be pressed again. + // * + // * @default 20ms + // ******************************************************************************** + int timeout; }; #endif \ No newline at end of file