IoT werksessies embedded

Dependencies:   MQTT

Fork of PGO6_VoteController_template by Jens de hoog

Committer:
stijndirickx
Date:
Mon Oct 15 15:47:44 2018 +0000
Revision:
3:6793060aacc6
Parent:
2:5b7d055dbc91
Eerste werksessie;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jensdehoog 0:fd29cd85e75e 1 #include "debounce_button.h"
jensdehoog 0:fd29cd85e75e 2
stijndirickx 3:6793060aacc6 3 int amount_of_clicks = 0;
stijndirickx 3:6793060aacc6 4 DigitalOut led1(LED1);
stijndirickx 3:6793060aacc6 5 DigitalOut led2(LED2);
stijndirickx 3:6793060aacc6 6 DigitalOut led3(LED3);
stijndirickx 3:6793060aacc6 7 Timeout someTimeout1;
stijndirickx 3:6793060aacc6 8 Timeout someTimeout2;
stijndirickx 3:6793060aacc6 9
stijndirickx 3:6793060aacc6 10 volatile bool button1_pressed; // Used in the main loop
stijndirickx 3:6793060aacc6 11 volatile bool button1_enabled; // Used for debouncing
stijndirickx 3:6793060aacc6 12 volatile int multiclick_state; // Counts how many clicks occured in the time slot, used in main loop
stijndirickx 3:6793060aacc6 13 volatile bool button1_busy; // Informs the mainloop that the user is clicking the button
stijndirickx 3:6793060aacc6 14
stijndirickx 3:6793060aacc6 15 /*
stijndirickx 3:6793060aacc6 16 Delay;
stijndirickx 3:6793060aacc6 17 Timeout someTimeout;
stijndirickx 3:6793060aacc6 18 someTimeout.attach(callback(&someFunction), 0.5)
jensdehoog 0:fd29cd85e75e 19 */
jensdehoog 0:fd29cd85e75e 20
stijndirickx 3:6793060aacc6 21
jensdehoog 2:5b7d055dbc91 22 void button1_multiclick_reset_cb(void) {
jensdehoog 2:5b7d055dbc91 23
stijndirickx 3:6793060aacc6 24 //stores the amount of clicks in a variable which is read by the main loop.
stijndirickx 3:6793060aacc6 25 multiclick_state = amount_of_clicks;
stijndirickx 3:6793060aacc6 26
stijndirickx 3:6793060aacc6 27 //resets the click counter which is used inside this file.
stijndirickx 3:6793060aacc6 28 amount_of_clicks = 0;
stijndirickx 3:6793060aacc6 29
stijndirickx 3:6793060aacc6 30 //lowers a flag which tells the main loop that the user stopped pressing the button such that it can proceed its program.
stijndirickx 3:6793060aacc6 31 button1_pressed = false;
stijndirickx 3:6793060aacc6 32
stijndirickx 3:6793060aacc6 33 //turns the built-in LED off. Therefore, the user gets informed that the program stopped counting the clicks.
stijndirickx 3:6793060aacc6 34 led1 = 0;
jensdehoog 2:5b7d055dbc91 35 }
jensdehoog 2:5b7d055dbc91 36
jensdehoog 2:5b7d055dbc91 37 void button1_enabled_cb(void)
jensdehoog 2:5b7d055dbc91 38 {
stijndirickx 3:6793060aacc6 39 //This function enables the button again, such that unwanted clicks of the bouncing button get ignored.
stijndirickx 3:6793060aacc6 40 button1_enabled = true;
jensdehoog 2:5b7d055dbc91 41 }
jensdehoog 0:fd29cd85e75e 42
jensdehoog 0:fd29cd85e75e 43 void button1_onpressed_cb(void)
jensdehoog 0:fd29cd85e75e 44 {
stijndirickx 3:6793060aacc6 45 //turns the built-in LED on, so the user gets informed that the program has started with counting clicks
stijndirickx 3:6793060aacc6 46 led1 = 1;
jensdehoog 0:fd29cd85e75e 47
stijndirickx 3:6793060aacc6 48 //disables the button such that the debouncer is active
stijndirickx 3:6793060aacc6 49 button1_enabled = false;
stijndirickx 3:6793060aacc6 50
stijndirickx 3:6793060aacc6 51
stijndirickx 3:6793060aacc6 52 //enables the button again after a certain amount of time
stijndirickx 3:6793060aacc6 53 //(use interrupts with "button1_enabled_cb()" as callback.
stijndirickx 3:6793060aacc6 54 someTimeout1.attach(callback(button1_enabled_cb), 0.1);
stijndirickx 3:6793060aacc6 55
stijndirickx 3:6793060aacc6 56
stijndirickx 3:6793060aacc6 57 //TODO revisit debouncing flag
stijndirickx 3:6793060aacc6 58 amount_of_clicks += 1; //clicks too much
stijndirickx 3:6793060aacc6 59
stijndirickx 3:6793060aacc6 60
stijndirickx 3:6793060aacc6 61 //counts the amount of clicks within a period of 1 second
stijndirickx 3:6793060aacc6 62 someTimeout2.attach(callback(button1_multiclick_reset_cb), 1);
stijndirickx 3:6793060aacc6 63
stijndirickx 3:6793060aacc6 64
stijndirickx 3:6793060aacc6 65 //informs the main loop that the button has been pressed
stijndirickx 3:6793060aacc6 66 button1_pressed = true;
stijndirickx 3:6793060aacc6 67
stijndirickx 3:6793060aacc6 68 //informs the main loop that the user is clicking the button.
stijndirickx 3:6793060aacc6 69 button1_busy = true;
stijndirickx 3:6793060aacc6 70
stijndirickx 3:6793060aacc6 71
stijndirickx 3:6793060aacc6 72 //Therefore, this main loop cannot continue its procedure until the clicks within 1 second have been counted.
jensdehoog 0:fd29cd85e75e 73 }