VoteController

Dependencies:   MQTT

Fork of PGO6_VoteController_template by Jens de hoog

Committer:
Dieter97
Date:
Thu Oct 11 16:09:27 2018 +0000
Revision:
4:74736eccf6bb
Parent:
3:6b6cab4ad185
fixed sending to wrong topic

Who changed what in which revision?

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