Initial commit

Dependencies:   MQTT

Fork of PGO6_VoteController_template by Jens de hoog

Committer:
Rennert
Date:
Thu Oct 26 14:25:29 2017 +0000
Revision:
2:f283e5274a0f
Parent:
1:e7a311bd038e
Commented;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jensdehoog 0:fd29cd85e75e 1 #include "debounce_button.h"
Rennert 1:e7a311bd038e 2 volatile bool button1_pressed; // Used in the main loop
Rennert 1:e7a311bd038e 3 volatile bool button1_enabled=true; // Used for debouncing
Rennert 1:e7a311bd038e 4 volatile int multiclick_state; // Counts how many clicks occured in the time slot, used in main loop
Rennert 1:e7a311bd038e 5 volatile bool button1_busy=false;
Rennert 1:e7a311bd038e 6 Timeout enableTimeout;
Rennert 1:e7a311bd038e 7 Timeout resetTimeout;
Rennert 1:e7a311bd038e 8 DigitalOut myled(LED1);
Rennert 1:e7a311bd038e 9 int counter=0;
jensdehoog 0:fd29cd85e75e 10
jensdehoog 0:fd29cd85e75e 11 /**
jensdehoog 0:fd29cd85e75e 12 Some tips and tricks:
jensdehoog 0:fd29cd85e75e 13 - To use the built-in LED:
jensdehoog 0:fd29cd85e75e 14 DigitalOut led1(LED1);
jensdehoog 0:fd29cd85e75e 15 ...
jensdehoog 0:fd29cd85e75e 16 led1 = 1;
jensdehoog 0:fd29cd85e75e 17 - To delay the call of a function:
jensdehoog 0:fd29cd85e75e 18 Timeout someTimeout;
jensdehoog 0:fd29cd85e75e 19 ...
jensdehoog 0:fd29cd85e75e 20 someTimeout.attach(callback(&someFunction), 0.5) with 0.5 as 500 milliseconds
jensdehoog 0:fd29cd85e75e 21 - The variables that are used in interrupt callbacks have to be volatile,
jensdehoog 0:fd29cd85e75e 22 because these variables can change at any time. Therefore, the compiler is not
jensdehoog 0:fd29cd85e75e 23 going to make optimisations.
jensdehoog 0:fd29cd85e75e 24 */
jensdehoog 0:fd29cd85e75e 25
jensdehoog 0:fd29cd85e75e 26 /**
jensdehoog 0:fd29cd85e75e 27 TODO
jensdehoog 0:fd29cd85e75e 28 ----
jensdehoog 0:fd29cd85e75e 29 This function:
jensdehoog 0:fd29cd85e75e 30 - stores the amount of clicks in a variable which is read by the main loop.
jensdehoog 0:fd29cd85e75e 31 - resets the click counter which is used inside this file.
jensdehoog 0:fd29cd85e75e 32 - lowers a flag which tells the main loop that the user stopped pressing the button
jensdehoog 0:fd29cd85e75e 33 such that it can proceed its program.
jensdehoog 0:fd29cd85e75e 34 - turns the built-in LED off. Therefore, the user gets informed that the program stopped counting the clicks.
jensdehoog 0:fd29cd85e75e 35 */
jensdehoog 0:fd29cd85e75e 36 void button1_multiclick_reset_cb(void) {
Rennert 1:e7a311bd038e 37 myled=0;
Rennert 1:e7a311bd038e 38 multiclick_state=counter;
Rennert 1:e7a311bd038e 39 counter=0;
Rennert 1:e7a311bd038e 40 button1_busy=false;
jensdehoog 0:fd29cd85e75e 41 }
jensdehoog 0:fd29cd85e75e 42
jensdehoog 0:fd29cd85e75e 43 /**
Rennert 1:e7a311bd038e 44 TODO:FINIDO
jensdehoog 0:fd29cd85e75e 45 ----
jensdehoog 0:fd29cd85e75e 46 This function enables the button again, such that unwanted clicks of the bouncing button get ignored.
jensdehoog 0:fd29cd85e75e 47 */
jensdehoog 0:fd29cd85e75e 48 void button1_enabled_cb(void)
jensdehoog 0:fd29cd85e75e 49 {
Rennert 1:e7a311bd038e 50 button1_enabled=true;
jensdehoog 0:fd29cd85e75e 51 }
jensdehoog 0:fd29cd85e75e 52
Rennert 1:e7a311bd038e 53
jensdehoog 0:fd29cd85e75e 54 /**
jensdehoog 0:fd29cd85e75e 55 TODO
jensdehoog 0:fd29cd85e75e 56 ----
jensdehoog 0:fd29cd85e75e 57 This function:
Rennert 1:e7a311bd038e 58 X- turns the built-in LED on, so the user gets informed that the program has started with counting clicks
Rennert 1:e7a311bd038e 59 X- disables the button such that the debouncer is active
Rennert 1:e7a311bd038e 60 X- enables the button again after a certain amount of time
jensdehoog 0:fd29cd85e75e 61 (use interrupts with "button1_enabled_cb()" as callback.
jensdehoog 0:fd29cd85e75e 62 - counts the amount of clicks within a period of 1 second
Rennert 1:e7a311bd038e 63 X- informs the main loop that the button has been pressed
jensdehoog 0:fd29cd85e75e 64 - informs the main loop that the user is clicking the button.
jensdehoog 0:fd29cd85e75e 65 Therefore, this main loop cannot continue its procedure until the clicks within 1 second have been counted.
jensdehoog 0:fd29cd85e75e 66 */
Rennert 2:f283e5274a0f 67
Rennert 2:f283e5274a0f 68 //Processes button interrupts
jensdehoog 0:fd29cd85e75e 69 void button1_onpressed_cb(void)
jensdehoog 0:fd29cd85e75e 70 {
Rennert 2:f283e5274a0f 71 //If not busy, start measuring presses
Rennert 1:e7a311bd038e 72 if(!button1_busy){
Rennert 1:e7a311bd038e 73 button1_busy=true;
Rennert 1:e7a311bd038e 74 button1_enabled=false;
Rennert 1:e7a311bd038e 75 myled=1;
Rennert 1:e7a311bd038e 76 resetTimeout.attach(callback(&button1_multiclick_reset_cb), 1.0);
Rennert 1:e7a311bd038e 77 enableTimeout.attach(callback(&button1_enabled_cb), 0.1);
Rennert 1:e7a311bd038e 78
Rennert 1:e7a311bd038e 79 }
Rennert 2:f283e5274a0f 80 //If busy, measure presses
Rennert 1:e7a311bd038e 81 if(button1_busy && button1_enabled){
Rennert 1:e7a311bd038e 82 button1_enabled=false;
Rennert 1:e7a311bd038e 83 enableTimeout.attach(callback(&button1_enabled_cb), 0.1);
Rennert 1:e7a311bd038e 84 counter++;
Rennert 1:e7a311bd038e 85 }
Rennert 1:e7a311bd038e 86 }