A debouncing InterruptIn wrapper
ButtonIn.cpp@2:c353357a97e4, 2012-02-07 (annotated)
- Committer:
- evwijk
- Date:
- Tue Feb 07 10:35:38 2012 +0000
- Revision:
- 2:c353357a97e4
- Parent:
- 1:e806603f0088
First public release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
evwijk | 2:c353357a97e4 | 1 | /* |
evwijk | 2:c353357a97e4 | 2 | ButtonIn is a debouncing InterruptIn class for mbed (http://mbed.org). |
evwijk | 2:c353357a97e4 | 3 | |
evwijk | 2:c353357a97e4 | 4 | Copyright (C) 2012 Erik van Wijk (http://mbed.org/users/evwijk/) |
evwijk | 2:c353357a97e4 | 5 | |
evwijk | 2:c353357a97e4 | 6 | This program is free software: you can redistribute it and/or modify |
evwijk | 2:c353357a97e4 | 7 | it under the terms of the GNU General Public License as published by |
evwijk | 2:c353357a97e4 | 8 | the Free Software Foundation, either version 3 of the License, or |
evwijk | 2:c353357a97e4 | 9 | (at your option) any later version. |
evwijk | 2:c353357a97e4 | 10 | |
evwijk | 2:c353357a97e4 | 11 | This library is distributed in the hope that it will be useful, |
evwijk | 2:c353357a97e4 | 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
evwijk | 2:c353357a97e4 | 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
evwijk | 2:c353357a97e4 | 14 | GNU General Public License for more details. |
evwijk | 2:c353357a97e4 | 15 | |
evwijk | 2:c353357a97e4 | 16 | You should have received a copy of the GNU General Public License |
evwijk | 2:c353357a97e4 | 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
evwijk | 2:c353357a97e4 | 18 | */ |
evwijk | 2:c353357a97e4 | 19 | |
evwijk | 1:e806603f0088 | 20 | #include "ButtonIn.h" |
evwijk | 1:e806603f0088 | 21 | |
evwijk | 1:e806603f0088 | 22 | ButtonIn::ButtonIn(PinName buttonPin) : |
evwijk | 1:e806603f0088 | 23 | _button(buttonPin) { |
evwijk | 1:e806603f0088 | 24 | _buttonCanPress = true; |
evwijk | 1:e806603f0088 | 25 | _callback = NULL; |
evwijk | 1:e806603f0088 | 26 | _callbackInstance = NULL; |
evwijk | 1:e806603f0088 | 27 | _callbackMethod = NULL; |
evwijk | 2:c353357a97e4 | 28 | timeout = 200; |
evwijk | 1:e806603f0088 | 29 | _button.rise(this, &ButtonIn::click); |
evwijk | 1:e806603f0088 | 30 | } |
evwijk | 1:e806603f0088 | 31 | |
evwijk | 1:e806603f0088 | 32 | void ButtonIn::attach(void (*method)(void)) { |
evwijk | 1:e806603f0088 | 33 | _callback = method; |
evwijk | 1:e806603f0088 | 34 | } |
evwijk | 1:e806603f0088 | 35 | |
evwijk | 1:e806603f0088 | 36 | template<class T> |
evwijk | 1:e806603f0088 | 37 | void attach(T* instance, void (T::*method)(void)) { |
evwijk | 1:e806603f0088 | 38 | _callbackInstance = (ButtonInCallbackInstance *)instance; |
evwijk | 1:e806603f0088 | 39 | _callbackMethod = (void (ButtonInCallbackInstance::*)(void))method; |
evwijk | 1:e806603f0088 | 40 | } |
evwijk | 1:e806603f0088 | 41 | |
evwijk | 1:e806603f0088 | 42 | void ButtonIn::click() { |
evwijk | 1:e806603f0088 | 43 | if (_buttonCanPress) { |
evwijk | 1:e806603f0088 | 44 | _buttonCanPress = false; |
evwijk | 2:c353357a97e4 | 45 | _buttonDownTimeout.attach_us(this, &ButtonIn::reset, timeout * 1000); |
evwijk | 1:e806603f0088 | 46 | call(); |
evwijk | 1:e806603f0088 | 47 | } |
evwijk | 1:e806603f0088 | 48 | } |
evwijk | 1:e806603f0088 | 49 | |
evwijk | 1:e806603f0088 | 50 | void ButtonIn::call() { |
evwijk | 1:e806603f0088 | 51 | if (_callback != NULL) |
evwijk | 1:e806603f0088 | 52 | (*_callback)(); |
evwijk | 1:e806603f0088 | 53 | else |
evwijk | 1:e806603f0088 | 54 | (_callbackInstance->*_callbackMethod)(); |
evwijk | 1:e806603f0088 | 55 | } |
evwijk | 1:e806603f0088 | 56 | |
evwijk | 1:e806603f0088 | 57 | void ButtonIn::reset() { |
evwijk | 1:e806603f0088 | 58 | _buttonCanPress = true; |
evwijk | 0:2e999fcd4c7b | 59 | } |