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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Hotboards_buttons.h Source File

Hotboards_buttons.h

00001 /*
00002   Hotboards_buttons.h - Driver to control buttons (with hardware debounce)
00003   this driver considered external pull-ups (or pull-downs) are in use
00004   Hotboards buttons board (http://hotboards.org)
00005   Created by Diego Perez, January 16, 2016.
00006   Released into the public domain.
00007 */
00008 #ifndef Hotboards_buttons_h
00009 #define Hotboards_buttons_h
00010 
00011 #include "mbed.h"
00012 
00013 /** Hotboards_buttons class.
00014  *  Used to read mechanical buttons
00015  *
00016  * Example:
00017  * @code
00018  * #include "Hotboards_buttons.h"
00019  *
00020  * Hotboards_buttons btn( 5 );
00021  *
00022  * int main( void )
00023  * {
00024  *     for(;;){
00025  *         if( btn.isPressed( ) )
00026  *             // do something
00027  *     }
00028  * }
00029  * @endcode
00030  */
00031 class Hotboards_buttons
00032 {
00033     public :
00034         /** Create Hotboards_buttons instance
00035           * @param pin pin where the button will be read it
00036           * @param active logic level that gives you when the button is pressed
00037           *
00038           * Example:
00039           * @code
00040           *   // instance one button on pin 5 (with pull-ups)
00041           *   Hotboards_buttons btn( 5 );
00042           *   // instance one button on pin 6 (with pull-downs)
00043           *   Hotboards_buttons btn( 6, 1 );
00044           * @endcode
00045           */
00046         Hotboards_buttons( PinName pin, bool active=0 );
00047 
00048         /** Tells you if a button has been pressed (only once)
00049           * @return '1' if the buttons has been pressed
00050           *
00051           * Example:
00052           * @code
00053           *   // instance one button on pin 5
00054           *   Hotboards_buttons btn( 5 );
00055           *   if( btn.isPressed( ) )
00056           *       // do something
00057           * @endcode
00058           */
00059         bool isPressed( void );
00060 
00061         /** Tells you if a button has been Released (only once)
00062           * @return '1' if the buttons has been Released
00063           *
00064           * Example:
00065           * @code
00066           *   // instance one button on pin 5
00067           *   Hotboards_buttons btn( 5 );
00068           *   if( btn.isReleased( ) )
00069           *       // do something
00070           * @endcode
00071           */
00072         bool isReleased( void );
00073 
00074         /** Tells you the button state
00075           * @return '1' if the buttons is pressed
00076           *
00077           * Example:
00078           * @code
00079           *   // instance one button on pin 5
00080           *   Hotboards_buttons btn( 5 );
00081           *   if( btn.istatus( ) )
00082           *       // button pressed
00083           *   else
00084           *       // button not pressed
00085           * @endcode
00086           */
00087         bool status( void );
00088 
00089     private :
00090         void poll( void );
00091         DigitalIn _pin;
00092         bool _pressed;
00093         bool _lastButtonState;
00094         bool _isPressed;
00095         bool _isReleased;
00096 };
00097 
00098 #endif