Jim Flynn
/
button_class_and_test
Implementation of a button class and test program
Revision 1:c668e9604e63, committed 2018-09-07
- Comitter:
- JMF
- Date:
- Fri Sep 07 20:54:25 2018 +0000
- Parent:
- 0:377a50b631cf
- Commit message:
- Added a destructor to the class.
Changed in this revision
Button.hpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 377a50b631cf -r c668e9604e63 Button.hpp --- a/Button.hpp Thu Aug 30 00:35:30 2018 +0000 +++ b/Button.hpp Fri Sep 07 20:54:25 2018 +0000 @@ -1,20 +1,32 @@ -// Licensed under the MIT license. See LICENSE file in the project root for full license information. +/** +* copyright (c) 2018, James Flynn +* SPDX-License-Identifier: MIT +*/ + +/** +* @file button.hpp +* @brief A small BUTTON class for detecting & debouncing button preesses +* +* @author James Flynn +* +* @date 24-Aug-2018 +*/ #include "mbed.h" -#define BUTTON_DEBOUNCE 20 +#define BUTTON_DEBOUNCE 20 //specify the number of msec to debounce class Button { protected: InterruptIn user_button; - void (*br_cb)(int); - void (*bp_cb)(void); + void (*br_cb)(int); //button release callback + void (*bp_cb)(void); //button press callback - Thread button_thread; + Thread *button_thread; void button_monitor_task(void); EventQueue button_queue; - uint64_t bp_time, bp_duration; - int button_pressed; + uint64_t bp_time, bp_duration; //button press start time and button press duration + int button_pressed; //counts the number of times the button has been pressed void button_press_handler(void) { if( (rtos::Kernel::get_ms_count() - bp_time) < BUTTON_DEBOUNCE) @@ -45,9 +57,10 @@ bp_duration(0), button_pressed(0) { - // The user button is active low so setup the falling edge to generate - // an interrupt. Catch the release with an event queue event - button_thread.start(callback(&button_queue, &EventQueue::dispatch_forever)); + // The user button is setup for the edge to generate an interrupt. + // The release is caught an event queue callback + button_thread=new Thread(osPriorityNormal,256,NULL,"button_thread"); + button_thread->start(callback(&button_queue, &EventQueue::dispatch_forever)); if( s == ActiveHigh ) { user_button.rise( Callback<void()>(this, &Button::button_press_handler) ); user_button.fall( button_queue.event( Callback<void()>(this, &Button::button_release_handler))); @@ -58,6 +71,13 @@ } } + ~Button() { + button_thread->terminate(); + delete button_thread; + } + + // will return the number of times the button has been pressed (if it was pressed > 1 time before checked) + // and returns the duration of the last button press in duration int chkButton_press(int *duration) { int bp = button_pressed; @@ -69,7 +89,9 @@ return bp; } + //allows the user to set a callback for a button press in void setButton_press_cb( void (*buttonpresscb)(void) ) { bp_cb = buttonpresscb; } }; +