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.h@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 | #ifndef Hotboards_buttons_h |
Hotboards | 0:810519ce94bf | 9 | #define Hotboards_buttons_h |
Hotboards | 0:810519ce94bf | 10 | |
Hotboards | 0:810519ce94bf | 11 | #include "mbed.h" |
Hotboards | 0:810519ce94bf | 12 | |
Hotboards | 0:810519ce94bf | 13 | /** Hotboards_buttons class. |
Hotboards | 0:810519ce94bf | 14 | * Used to read mechanical buttons |
Hotboards | 0:810519ce94bf | 15 | * |
Hotboards | 0:810519ce94bf | 16 | * Example: |
Hotboards | 0:810519ce94bf | 17 | * @code |
Hotboards | 0:810519ce94bf | 18 | * #include "Hotboards_buttons.h" |
Hotboards | 0:810519ce94bf | 19 | * |
Hotboards | 0:810519ce94bf | 20 | * Hotboards_buttons btn( 5 ); |
Hotboards | 0:810519ce94bf | 21 | * |
Hotboards | 0:810519ce94bf | 22 | * int main( void ) |
Hotboards | 0:810519ce94bf | 23 | * { |
Hotboards | 0:810519ce94bf | 24 | * for(;;){ |
Hotboards | 0:810519ce94bf | 25 | * if( btn.isPressed( ) ) |
Hotboards | 0:810519ce94bf | 26 | * // do something |
Hotboards | 0:810519ce94bf | 27 | * } |
Hotboards | 0:810519ce94bf | 28 | * } |
Hotboards | 0:810519ce94bf | 29 | * @endcode |
Hotboards | 0:810519ce94bf | 30 | */ |
Hotboards | 0:810519ce94bf | 31 | class Hotboards_buttons |
Hotboards | 0:810519ce94bf | 32 | { |
Hotboards | 0:810519ce94bf | 33 | public : |
Hotboards | 0:810519ce94bf | 34 | /** Create Hotboards_buttons instance |
Hotboards | 0:810519ce94bf | 35 | * @param pin pin where the button will be read it |
Hotboards | 0:810519ce94bf | 36 | * @param active logic level that gives you when the button is pressed |
Hotboards | 0:810519ce94bf | 37 | * |
Hotboards | 0:810519ce94bf | 38 | * Example: |
Hotboards | 0:810519ce94bf | 39 | * @code |
Hotboards | 0:810519ce94bf | 40 | * // instance one button on pin 5 (with pull-ups) |
Hotboards | 0:810519ce94bf | 41 | * Hotboards_buttons btn( 5 ); |
Hotboards | 0:810519ce94bf | 42 | * // instance one button on pin 6 (with pull-downs) |
Hotboards | 0:810519ce94bf | 43 | * Hotboards_buttons btn( 6, 1 ); |
Hotboards | 0:810519ce94bf | 44 | * @endcode |
Hotboards | 0:810519ce94bf | 45 | */ |
Hotboards | 0:810519ce94bf | 46 | Hotboards_buttons( PinName pin, bool active=0 ); |
Hotboards | 0:810519ce94bf | 47 | |
Hotboards | 0:810519ce94bf | 48 | /** Tells you if a button has been pressed (only once) |
Hotboards | 0:810519ce94bf | 49 | * @return '1' if the buttons has been pressed |
Hotboards | 0:810519ce94bf | 50 | * |
Hotboards | 0:810519ce94bf | 51 | * Example: |
Hotboards | 0:810519ce94bf | 52 | * @code |
Hotboards | 0:810519ce94bf | 53 | * // instance one button on pin 5 |
Hotboards | 0:810519ce94bf | 54 | * Hotboards_buttons btn( 5 ); |
Hotboards | 0:810519ce94bf | 55 | * if( btn.isPressed( ) ) |
Hotboards | 0:810519ce94bf | 56 | * // do something |
Hotboards | 0:810519ce94bf | 57 | * @endcode |
Hotboards | 0:810519ce94bf | 58 | */ |
Hotboards | 0:810519ce94bf | 59 | bool isPressed( void ); |
Hotboards | 0:810519ce94bf | 60 | |
Hotboards | 0:810519ce94bf | 61 | /** Tells you if a button has been Released (only once) |
Hotboards | 0:810519ce94bf | 62 | * @return '1' if the buttons has been Released |
Hotboards | 0:810519ce94bf | 63 | * |
Hotboards | 0:810519ce94bf | 64 | * Example: |
Hotboards | 0:810519ce94bf | 65 | * @code |
Hotboards | 0:810519ce94bf | 66 | * // instance one button on pin 5 |
Hotboards | 0:810519ce94bf | 67 | * Hotboards_buttons btn( 5 ); |
Hotboards | 0:810519ce94bf | 68 | * if( btn.isReleased( ) ) |
Hotboards | 0:810519ce94bf | 69 | * // do something |
Hotboards | 0:810519ce94bf | 70 | * @endcode |
Hotboards | 0:810519ce94bf | 71 | */ |
Hotboards | 0:810519ce94bf | 72 | bool isReleased( void ); |
Hotboards | 0:810519ce94bf | 73 | |
Hotboards | 0:810519ce94bf | 74 | /** Tells you the button state |
Hotboards | 0:810519ce94bf | 75 | * @return '1' if the buttons is pressed |
Hotboards | 0:810519ce94bf | 76 | * |
Hotboards | 0:810519ce94bf | 77 | * Example: |
Hotboards | 0:810519ce94bf | 78 | * @code |
Hotboards | 0:810519ce94bf | 79 | * // instance one button on pin 5 |
Hotboards | 0:810519ce94bf | 80 | * Hotboards_buttons btn( 5 ); |
Hotboards | 0:810519ce94bf | 81 | * if( btn.istatus( ) ) |
Hotboards | 0:810519ce94bf | 82 | * // button pressed |
Hotboards | 0:810519ce94bf | 83 | * else |
Hotboards | 0:810519ce94bf | 84 | * // button not pressed |
Hotboards | 0:810519ce94bf | 85 | * @endcode |
Hotboards | 0:810519ce94bf | 86 | */ |
Hotboards | 0:810519ce94bf | 87 | bool status( void ); |
Hotboards | 0:810519ce94bf | 88 | |
Hotboards | 0:810519ce94bf | 89 | private : |
Hotboards | 0:810519ce94bf | 90 | void poll( void ); |
Hotboards | 0:810519ce94bf | 91 | DigitalIn _pin; |
Hotboards | 0:810519ce94bf | 92 | bool _pressed; |
Hotboards | 0:810519ce94bf | 93 | bool _lastButtonState; |
Hotboards | 0:810519ce94bf | 94 | bool _isPressed; |
Hotboards | 0:810519ce94bf | 95 | bool _isReleased; |
Hotboards | 0:810519ce94bf | 96 | }; |
Hotboards | 0:810519ce94bf | 97 | |
Hotboards | 0:810519ce94bf | 98 | #endif |