jamie wubben
/
PGO6_VoteController_template
jamie
Fork of PGO6_VoteController_template by
Revision 3:c77bed3482d3, committed 2018-10-11
- Comitter:
- jamie_wubben
- Date:
- Thu Oct 11 15:51:58 2018 +0000
- Parent:
- 2:5b7d055dbc91
- Commit message:
- Jamie
Changed in this revision
diff -r 5b7d055dbc91 -r c77bed3482d3 debounce_button.cpp --- a/debounce_button.cpp Tue Oct 31 09:01:56 2017 +0000 +++ b/debounce_button.cpp Thu Oct 11 15:51:58 2018 +0000 @@ -1,5 +1,13 @@ #include "debounce_button.h" +DigitalIn button(USER_BUTTON); +DigitalOut led1(LED1); +DigitalOut led2(LED2); +volatile bool button1_enabled = true; +volatile bool button1_buzy = false; +Timeout someTimeout; +Timeout timer; +int counter = 0; /** Some tips and tricks: - To use the built-in LED: @@ -25,18 +33,23 @@ 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 replay(){ + //replay the multiclick + for(int i=0;i<counter;i++){ + led2 = 1; + wait(0.2); + led2 =0; + wait(0.2); + } } -/** - TODO - ---- - This function enables the button again, such that unwanted clicks of the bouncing button get ignored. -*/ +// This function enables the button again, such that unwanted clicks of the bouncing button get ignored. void button1_enabled_cb(void) { - + button1_enabled = true; + led1 = 0; + replay(); } /** @@ -52,7 +65,27 @@ - informs the main loop that the user is clicking the button. Therefore, this main loop cannot continue its procedure until the clicks within 1 second have been counted. */ + +void button1_busy_enable(){ + button1_buzy = false; +} void button1_onpressed_cb(void) { + //button is pressed first time, turn led on + if(button1_enabled){ + counter = 0; + // turns the built-in LED on, so the user gets informed that the program has started with counting clicks + led1 = 1; + //disables the button such that the debouncer is active + button1_enabled = false; + button1_buzy = true; + timer.attach(callback(&button1_busy_enable),0.1); + // enables the button again after a certain amount of time (use interrupts with "button1_enabled_cb()" as callback. + someTimeout.attach(callback(&button1_enabled_cb), 5); + }else if(!button1_buzy){ + counter++; + button1_buzy = true; + timer.attach(callback(&button1_busy_enable),0.1); + } } \ No newline at end of file
diff -r 5b7d055dbc91 -r c77bed3482d3 debounce_button.h --- a/debounce_button.h Tue Oct 31 09:01:56 2017 +0000 +++ b/debounce_button.h Thu Oct 11 15:51:58 2018 +0000 @@ -9,6 +9,7 @@ */ + 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 @@ -16,4 +17,6 @@ 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_onpressed_cb(void); // Callback which is called when the user presses the button +void count(void); +void button1_busy_enable(void);
diff -r 5b7d055dbc91 -r c77bed3482d3 main.cpp --- a/main.cpp Tue Oct 31 09:01:56 2017 +0000 +++ b/main.cpp Thu Oct 11 15:51:58 2018 +0000 @@ -40,9 +40,22 @@ - To have a uniform message sending procedure, use the following function usage: sendMessage(&client, topic, buf, qos, retained, duplicate) */ - int main(int argc, char* argv[]) { - + DigitalOut led1(LED1); + for(int i=0;i<5;i++){ + led1 = 1; + wait(0.5); + led1 = 0; + wait(0.5); + } + + InterruptIn button(USER_BUTTON); + while(1){ + button.fall(callback(button1_onpressed_cb)); + } return 0; } + + + \ No newline at end of file