Driver to control buttons (with hardware debounce). This driver considered external pull-ups (or pull-downs) are in use
Dependents: buttons is_released is_pressed button_status ... more
Hotboards_buttons.cpp@0:810519ce94bf, 2016-02-29 (annotated)
- Committer:
- Hotboards
- Date:
- Mon Feb 29 02:30:13 2016 +0000
- Revision:
- 0:810519ce94bf
Driver to control buttons (with hardware debounce), this driver considered external pull-ups (or pull-downs) are in use
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Hotboards | 0:810519ce94bf | 1 | /* |
Hotboards | 0:810519ce94bf | 2 | Hotboards_buttons.h - Driver to control buttons (with hardware debounce) |
Hotboards | 0:810519ce94bf | 3 | this driver considered external pull-ups (or pull-downs) are in use |
Hotboards | 0:810519ce94bf | 4 | Hotboards buttons board (http://hotboards.org) |
Hotboards | 0:810519ce94bf | 5 | Created by Diego Perez, January 16, 2016. |
Hotboards | 0:810519ce94bf | 6 | Released into the public domain. |
Hotboards | 0:810519ce94bf | 7 | */ |
Hotboards | 0:810519ce94bf | 8 | #include "Hotboards_buttons.h" |
Hotboards | 0:810519ce94bf | 9 | |
Hotboards | 0:810519ce94bf | 10 | Hotboards_buttons::Hotboards_buttons( PinName pin, bool active ) : |
Hotboards | 0:810519ce94bf | 11 | _pin( pin ) |
Hotboards | 0:810519ce94bf | 12 | { |
Hotboards | 0:810519ce94bf | 13 | _lastButtonState = 0; |
Hotboards | 0:810519ce94bf | 14 | _pressed = active; |
Hotboards | 0:810519ce94bf | 15 | _isPressed = 0; |
Hotboards | 0:810519ce94bf | 16 | _isReleased = 0; |
Hotboards | 0:810519ce94bf | 17 | } |
Hotboards | 0:810519ce94bf | 18 | |
Hotboards | 0:810519ce94bf | 19 | bool Hotboards_buttons::isPressed( void ) |
Hotboards | 0:810519ce94bf | 20 | { |
Hotboards | 0:810519ce94bf | 21 | bool isPressed; |
Hotboards | 0:810519ce94bf | 22 | poll( ); |
Hotboards | 0:810519ce94bf | 23 | isPressed = _isPressed; |
Hotboards | 0:810519ce94bf | 24 | _isPressed = 0; |
Hotboards | 0:810519ce94bf | 25 | return isPressed; |
Hotboards | 0:810519ce94bf | 26 | } |
Hotboards | 0:810519ce94bf | 27 | |
Hotboards | 0:810519ce94bf | 28 | bool Hotboards_buttons::isReleased( void ) |
Hotboards | 0:810519ce94bf | 29 | { |
Hotboards | 0:810519ce94bf | 30 | bool isReleased; |
Hotboards | 0:810519ce94bf | 31 | poll( ); |
Hotboards | 0:810519ce94bf | 32 | isReleased = _isReleased; |
Hotboards | 0:810519ce94bf | 33 | _isReleased = 0; |
Hotboards | 0:810519ce94bf | 34 | return isReleased; |
Hotboards | 0:810519ce94bf | 35 | } |
Hotboards | 0:810519ce94bf | 36 | |
Hotboards | 0:810519ce94bf | 37 | bool Hotboards_buttons::status( void ) |
Hotboards | 0:810519ce94bf | 38 | { |
Hotboards | 0:810519ce94bf | 39 | if( _pin == _pressed ) return 1; |
Hotboards | 0:810519ce94bf | 40 | else return 0; |
Hotboards | 0:810519ce94bf | 41 | } |
Hotboards | 0:810519ce94bf | 42 | |
Hotboards | 0:810519ce94bf | 43 | void Hotboards_buttons::poll( void ) |
Hotboards | 0:810519ce94bf | 44 | { |
Hotboards | 0:810519ce94bf | 45 | bool buttonState; |
Hotboards | 0:810519ce94bf | 46 | // leemos el boton de la tarjeta |
Hotboards | 0:810519ce94bf | 47 | buttonState = status( ); |
Hotboards | 0:810519ce94bf | 48 | |
Hotboards | 0:810519ce94bf | 49 | // comparemos el estado actual con la anterior lectura |
Hotboards | 0:810519ce94bf | 50 | if( buttonState != _lastButtonState ) |
Hotboards | 0:810519ce94bf | 51 | { |
Hotboards | 0:810519ce94bf | 52 | // si el estado cambio esque recien se presiono |
Hotboards | 0:810519ce94bf | 53 | if( buttonState ) |
Hotboards | 0:810519ce94bf | 54 | { |
Hotboards | 0:810519ce94bf | 55 | // regresamos un uno si el boton cambio a presionado |
Hotboards | 0:810519ce94bf | 56 | _isPressed = 1; |
Hotboards | 0:810519ce94bf | 57 | } |
Hotboards | 0:810519ce94bf | 58 | else |
Hotboards | 0:810519ce94bf | 59 | { |
Hotboards | 0:810519ce94bf | 60 | // si no es que fue soltado |
Hotboards | 0:810519ce94bf | 61 | _isReleased = 1; |
Hotboards | 0:810519ce94bf | 62 | } |
Hotboards | 0:810519ce94bf | 63 | } |
Hotboards | 0:810519ce94bf | 64 | // respaldamos el esatdo actual para compararlo la sigentoie |
Hotboards | 0:810519ce94bf | 65 | // vez que preguntemos |
Hotboards | 0:810519ce94bf | 66 | _lastButtonState = buttonState; |
Hotboards | 0:810519ce94bf | 67 | } |