A debouncing InterruptIn wrapper
Embed:
(wiki syntax)
Show/hide line numbers
ButtonIn.cpp
00001 /* 00002 ButtonIn is a debouncing InterruptIn class for mbed (http://mbed.org). 00003 00004 Copyright (C) 2012 Erik van Wijk (http://mbed.org/users/evwijk/) 00005 00006 This program is free software: you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation, either version 3 of the License, or 00009 (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program. If not, see <http://www.gnu.org/licenses/>. 00018 */ 00019 00020 #include "ButtonIn.h" 00021 00022 ButtonIn::ButtonIn(PinName buttonPin) : 00023 _button(buttonPin) { 00024 _buttonCanPress = true; 00025 _callback = NULL; 00026 _callbackInstance = NULL; 00027 _callbackMethod = NULL; 00028 timeout = 200; 00029 _button.rise(this, &ButtonIn::click); 00030 } 00031 00032 void ButtonIn::attach(void (*method)(void)) { 00033 _callback = method; 00034 } 00035 00036 template<class T> 00037 void attach(T* instance, void (T::*method)(void)) { 00038 _callbackInstance = (ButtonInCallbackInstance *)instance; 00039 _callbackMethod = (void (ButtonInCallbackInstance::*)(void))method; 00040 } 00041 00042 void ButtonIn::click() { 00043 if (_buttonCanPress) { 00044 _buttonCanPress = false; 00045 _buttonDownTimeout.attach_us(this, &ButtonIn::reset, timeout * 1000); 00046 call(); 00047 } 00048 } 00049 00050 void ButtonIn::call() { 00051 if (_callback != NULL) 00052 (*_callback)(); 00053 else 00054 (_callbackInstance->*_callbackMethod)(); 00055 } 00056 00057 void ButtonIn::reset() { 00058 _buttonCanPress = true; 00059 }
Generated on Wed Jul 13 2022 17:31:24 by 1.7.2