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
Revision 0:810519ce94bf, committed 2016-02-29
- Comitter:
- Hotboards
- Date:
- Mon Feb 29 02:30:13 2016 +0000
- Commit message:
- Driver to control buttons (with hardware debounce), this driver considered external pull-ups (or pull-downs) are in use
Changed in this revision
Hotboards_buttons.cpp | Show annotated file Show diff for this revision Revisions of this file |
Hotboards_buttons.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 810519ce94bf Hotboards_buttons.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Hotboards_buttons.cpp Mon Feb 29 02:30:13 2016 +0000 @@ -0,0 +1,67 @@ +/* + Hotboards_buttons.h - Driver to control buttons (with hardware debounce) + this driver considered external pull-ups (or pull-downs) are in use + Hotboards buttons board (http://hotboards.org) + Created by Diego Perez, January 16, 2016. + Released into the public domain. +*/ +#include "Hotboards_buttons.h" + +Hotboards_buttons::Hotboards_buttons( PinName pin, bool active ) : + _pin( pin ) +{ + _lastButtonState = 0; + _pressed = active; + _isPressed = 0; + _isReleased = 0; +} + +bool Hotboards_buttons::isPressed( void ) +{ + bool isPressed; + poll( ); + isPressed = _isPressed; + _isPressed = 0; + return isPressed; +} + +bool Hotboards_buttons::isReleased( void ) +{ + bool isReleased; + poll( ); + isReleased = _isReleased; + _isReleased = 0; + return isReleased; +} + +bool Hotboards_buttons::status( void ) +{ + if( _pin == _pressed ) return 1; + else return 0; +} + +void Hotboards_buttons::poll( void ) +{ + bool buttonState; + // leemos el boton de la tarjeta + buttonState = status( ); + + // comparemos el estado actual con la anterior lectura + if( buttonState != _lastButtonState ) + { + // si el estado cambio esque recien se presiono + if( buttonState ) + { + // regresamos un uno si el boton cambio a presionado + _isPressed = 1; + } + else + { + // si no es que fue soltado + _isReleased = 1; + } + } + // respaldamos el esatdo actual para compararlo la sigentoie + // vez que preguntemos + _lastButtonState = buttonState; +}
diff -r 000000000000 -r 810519ce94bf Hotboards_buttons.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Hotboards_buttons.h Mon Feb 29 02:30:13 2016 +0000 @@ -0,0 +1,98 @@ +/* + Hotboards_buttons.h - Driver to control buttons (with hardware debounce) + this driver considered external pull-ups (or pull-downs) are in use + Hotboards buttons board (http://hotboards.org) + Created by Diego Perez, January 16, 2016. + Released into the public domain. +*/ +#ifndef Hotboards_buttons_h +#define Hotboards_buttons_h + +#include "mbed.h" + +/** Hotboards_buttons class. + * Used to read mechanical buttons + * + * Example: + * @code + * #include "Hotboards_buttons.h" + * + * Hotboards_buttons btn( 5 ); + * + * int main( void ) + * { + * for(;;){ + * if( btn.isPressed( ) ) + * // do something + * } + * } + * @endcode + */ +class Hotboards_buttons +{ + public : + /** Create Hotboards_buttons instance + * @param pin pin where the button will be read it + * @param active logic level that gives you when the button is pressed + * + * Example: + * @code + * // instance one button on pin 5 (with pull-ups) + * Hotboards_buttons btn( 5 ); + * // instance one button on pin 6 (with pull-downs) + * Hotboards_buttons btn( 6, 1 ); + * @endcode + */ + Hotboards_buttons( PinName pin, bool active=0 ); + + /** Tells you if a button has been pressed (only once) + * @return '1' if the buttons has been pressed + * + * Example: + * @code + * // instance one button on pin 5 + * Hotboards_buttons btn( 5 ); + * if( btn.isPressed( ) ) + * // do something + * @endcode + */ + bool isPressed( void ); + + /** Tells you if a button has been Released (only once) + * @return '1' if the buttons has been Released + * + * Example: + * @code + * // instance one button on pin 5 + * Hotboards_buttons btn( 5 ); + * if( btn.isReleased( ) ) + * // do something + * @endcode + */ + bool isReleased( void ); + + /** Tells you the button state + * @return '1' if the buttons is pressed + * + * Example: + * @code + * // instance one button on pin 5 + * Hotboards_buttons btn( 5 ); + * if( btn.istatus( ) ) + * // button pressed + * else + * // button not pressed + * @endcode + */ + bool status( void ); + + private : + void poll( void ); + DigitalIn _pin; + bool _pressed; + bool _lastButtonState; + bool _isPressed; + bool _isReleased; +}; + +#endif