
PGO6_VoteController (Astrid Vanneste)
Fork of PGO6_VoteController_template by
Revision 3:376ac6744373, committed 2018-10-08
- Comitter:
- AstridVanneste
- Date:
- Mon Oct 08 12:56:29 2018 +0000
- Parent:
- 2:5b7d055dbc91
- Child:
- 4:08da93eb6014
- Commit message:
- initial
Changed in this revision
--- a/debounce_button.cpp Tue Oct 31 09:01:56 2017 +0000 +++ b/debounce_button.cpp Mon Oct 08 12:56:29 2018 +0000 @@ -1,5 +1,11 @@ #include "debounce_button.h" +volatile bool button1_pressed; // Used in the main loop +volatile bool button1_enabled; // Used for debouncing +volatile int multiclick_state; // Counts how many clicks occured in the time slot, used in main loop +volatile bool button1_busy; // Informs the mainloop that the user is clicking the button => busy multiclick +volatile int internal_click_count; // Counts how many clicks occured in the time slot + /** Some tips and tricks: - To use the built-in LED: @@ -25,8 +31,11 @@ such that it can proceed its program. - turns the built-in LED off. Therefore, the user gets informed that the program stopped counting the clicks. */ -void button1_multiclick_reset_cb(void) { - +void button1_multiclick_reset_cb(void) +{ + button1_busy = false; + multiclick_state = internal_click_count; + internal_click_count = 0; } /** @@ -54,5 +63,35 @@ */ void button1_onpressed_cb(void) { + DigitalOut led1(LED1); + led1 = 1; + + if(button1_enabled) + { + button1_enabled = false; // button is not enabled => debouncing + + Timeout debounce_time; + debounce_time.attach(callback(button1_enabled_cb), 0.5); + + internal_click_count++; + + if(!button1_busy) + { + button1_pressed = true; + button1_busy = true; + + Timeout multiclick_time; + multiclick_time.attach(callback(button1_multiclick_reset_cb), 1); + } + } +} + +void init_debouncer() +{ + button1_pressed = false; + button1_enabled = true; + button1_busy = false; + multiclick_state = 0; + internal_click_count = 0; } \ No newline at end of file
--- a/debounce_button.h Tue Oct 31 09:01:56 2017 +0000 +++ b/debounce_button.h Mon Oct 08 12:56:29 2018 +0000 @@ -9,11 +9,13 @@ */ -extern volatile bool button1_pressed; // Used in the main loop -extern volatile bool button1_enabled; // Used for debouncing -extern volatile int multiclick_state; // Counts how many clicks occured in the time slot, used in main loop -extern volatile bool button1_busy; // Informs the mainloop that the user is clicking the button +extern volatile bool button1_pressed; // Used in the main loop +extern volatile bool button1_enabled; // Used for debouncing +extern volatile int multiclick_state; // Counts how many clicks occured in the time slot, used in main loop +extern volatile bool button1_busy; // Informs the mainloop that the user is clicking the button => busy multiclick +extern volatile int internal_click_count; // Counts how many clicks occured in the time slot -void button1_multiclick_reset_cb(void); // Resets the amount of clicks, but stores this value for the usage in the main loop -void button1_enabled_cb(void); // Enables the button again after a timeout, used for debouncing the button -void button1_onpressed_cb(void); // Callback which is called when the user presses the button \ No newline at end of file +void button1_multiclick_reset_cb(void); // Resets the amount of clicks, but stores this value for the usage in the main loop +void button1_enabled_cb(void); // Enables the button again after a timeout, used for debouncing the button +void button1_onpressed_cb(void); // Callback which is called when the user presses the button +void init_debouncer(); \ No newline at end of file
--- a/main.cpp Tue Oct 31 09:01:56 2017 +0000 +++ b/main.cpp Mon Oct 08 12:56:29 2018 +0000 @@ -43,6 +43,9 @@ int main(int argc, char* argv[]) { + InterruptIn button(USER_BUTTON); + button.fall(callback(button1_onpressed_cb)); + init_debouncer(); return 0; }