A debouncing InterruptIn wrapper
ButtonIn.h@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 "mbed.h" |
evwijk | 1:e806603f0088 | 21 | |
evwijk | 1:e806603f0088 | 22 | #ifndef _ButtonIn_ |
evwijk | 1:e806603f0088 | 23 | #define _ButtonIn_ |
evwijk | 1:e806603f0088 | 24 | |
evwijk | 1:e806603f0088 | 25 | class ButtonInCallbackInstance; |
evwijk | 1:e806603f0088 | 26 | |
evwijk | 1:e806603f0088 | 27 | class ButtonIn { |
evwijk | 1:e806603f0088 | 28 | |
evwijk | 1:e806603f0088 | 29 | private: |
evwijk | 1:e806603f0088 | 30 | InterruptIn _button; |
evwijk | 1:e806603f0088 | 31 | Timeout _buttonDownTimeout; |
evwijk | 1:e806603f0088 | 32 | |
evwijk | 1:e806603f0088 | 33 | volatile bool _buttonCanPress; |
evwijk | 1:e806603f0088 | 34 | void (*_callback)(void); |
evwijk | 1:e806603f0088 | 35 | ButtonInCallbackInstance *_callbackInstance; |
evwijk | 1:e806603f0088 | 36 | void (ButtonInCallbackInstance::*_callbackMethod)(void); |
evwijk | 1:e806603f0088 | 37 | |
evwijk | 1:e806603f0088 | 38 | void click(); |
evwijk | 1:e806603f0088 | 39 | void call(); |
evwijk | 1:e806603f0088 | 40 | void reset(); |
evwijk | 1:e806603f0088 | 41 | |
evwijk | 1:e806603f0088 | 42 | |
evwijk | 1:e806603f0088 | 43 | public: |
evwijk | 2:c353357a97e4 | 44 | // ******************************************************************************** |
evwijk | 2:c353357a97e4 | 45 | // * Constructor |
evwijk | 2:c353357a97e4 | 46 | // * |
evwijk | 2:c353357a97e4 | 47 | // * @param buttonPin The pin which is connected to the button. |
evwijk | 2:c353357a97e4 | 48 | // ******************************************************************************** |
evwijk | 1:e806603f0088 | 49 | ButtonIn(PinName buttonPin); |
evwijk | 1:e806603f0088 | 50 | |
evwijk | 2:c353357a97e4 | 51 | // ******************************************************************************** |
evwijk | 2:c353357a97e4 | 52 | // * Attaches the method to be called when the button is pressed. |
evwijk | 2:c353357a97e4 | 53 | // * |
evwijk | 2:c353357a97e4 | 54 | // * @param method A reference to the method to be called. |
evwijk | 2:c353357a97e4 | 55 | // ******************************************************************************** |
evwijk | 1:e806603f0088 | 56 | void attach(void (*method)(void) = 0); |
evwijk | 1:e806603f0088 | 57 | |
evwijk | 2:c353357a97e4 | 58 | // ******************************************************************************** |
evwijk | 2:c353357a97e4 | 59 | // * Attaches the method to be called when the button is pressed. |
evwijk | 2:c353357a97e4 | 60 | // * |
evwijk | 2:c353357a97e4 | 61 | // * @param instance A reference to the instance of the class containing the |
evwijk | 2:c353357a97e4 | 62 | // * method to be called. |
evwijk | 2:c353357a97e4 | 63 | // * @param method A reference to the method to be called. |
evwijk | 2:c353357a97e4 | 64 | // ******************************************************************************** |
evwijk | 1:e806603f0088 | 65 | template<class T> |
evwijk | 1:e806603f0088 | 66 | void attach(T* instance, void (T::*method)(void)); |
evwijk | 2:c353357a97e4 | 67 | |
evwijk | 2:c353357a97e4 | 68 | |
evwijk | 2:c353357a97e4 | 69 | // ******************************************************************************** |
evwijk | 2:c353357a97e4 | 70 | // * The time in milliseconds in which the button can not be pressed again. |
evwijk | 2:c353357a97e4 | 71 | // * |
evwijk | 2:c353357a97e4 | 72 | // * @default 20ms |
evwijk | 2:c353357a97e4 | 73 | // ******************************************************************************** |
evwijk | 2:c353357a97e4 | 74 | int timeout; |
evwijk | 1:e806603f0088 | 75 | }; |
evwijk | 1:e806603f0088 | 76 | |
evwijk | 0:2e999fcd4c7b | 77 | #endif |