last version

Dependencies:   MQTT

Committer:
fatihkilic
Date:
Fri Nov 22 17:55:41 2019 +0000
Revision:
3:e75fe1b54623
Parent:
2:5b7d055dbc91
last version;

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
jensdehoog 0:fd29cd85e75e 18 /**
jensdehoog 2:5b7d055dbc91 19 TODO
jensdehoog 2:5b7d055dbc91 20 ----
jensdehoog 2:5b7d055dbc91 21 This function:
jensdehoog 2:5b7d055dbc91 22 - stores the amount of clicks in a variable which is read by the main loop.
jensdehoog 2:5b7d055dbc91 23 - resets the click counter which is used inside this file.
jensdehoog 2:5b7d055dbc91 24 - lowers a flag which tells the main loop that the user stopped pressing the button
jensdehoog 2:5b7d055dbc91 25 such that it can proceed its program.
jensdehoog 2:5b7d055dbc91 26 - turns the built-in LED off. Therefore, the user gets informed that the program stopped counting the clicks.
jensdehoog 2:5b7d055dbc91 27 */
fatihkilic 3:e75fe1b54623 28
fatihkilic 3:e75fe1b54623 29 InterruptIn button(USER_BUTTON);
fatihkilic 3:e75fe1b54623 30 DigitalOut led1(LED1);
fatihkilic 3:e75fe1b54623 31 DigitalIn enable(USER_BUTTON);
fatihkilic 3:e75fe1b54623 32 volatile bool button1_pressed = false;
fatihkilic 3:e75fe1b54623 33 volatile bool button1_enabled = false;
fatihkilic 3:e75fe1b54623 34 volatile int multiclick_state= 0;
fatihkilic 3:e75fe1b54623 35 volatile bool button1_busy = false;
fatihkilic 3:e75fe1b54623 36 volatile int total = 0;
fatihkilic 3:e75fe1b54623 37 Timeout timeout;
fatihkilic 3:e75fe1b54623 38
jensdehoog 2:5b7d055dbc91 39 void button1_multiclick_reset_cb(void) {
fatihkilic 3:e75fe1b54623 40 total = multiclick_state;
fatihkilic 3:e75fe1b54623 41 multiclick_state = 0;
fatihkilic 3:e75fe1b54623 42 led1 = 0;
fatihkilic 3:e75fe1b54623 43 button1_pressed = false;
fatihkilic 3:e75fe1b54623 44 button1_busy = false;
fatihkilic 3:e75fe1b54623 45 button1_enabled_cb();
jensdehoog 2:5b7d055dbc91 46
jensdehoog 2:5b7d055dbc91 47 }
jensdehoog 2:5b7d055dbc91 48
jensdehoog 2:5b7d055dbc91 49 /**
jensdehoog 2:5b7d055dbc91 50 TODO
jensdehoog 2:5b7d055dbc91 51 ----
jensdehoog 2:5b7d055dbc91 52 This function enables the button again, such that unwanted clicks of the bouncing button get ignored.
jensdehoog 0:fd29cd85e75e 53 */
fatihkilic 3:e75fe1b54623 54
fatihkilic 3:e75fe1b54623 55
fatihkilic 3:e75fe1b54623 56
fatihkilic 3:e75fe1b54623 57
jensdehoog 2:5b7d055dbc91 58 void button1_enabled_cb(void)
jensdehoog 2:5b7d055dbc91 59 {
fatihkilic 3:e75fe1b54623 60 // button.rise(callback(button1_onpressed_cb));
fatihkilic 3:e75fe1b54623 61
fatihkilic 3:e75fe1b54623 62 button1_enabled = true;
jensdehoog 2:5b7d055dbc91 63 }
jensdehoog 0:fd29cd85e75e 64
jensdehoog 2:5b7d055dbc91 65 /**
jensdehoog 2:5b7d055dbc91 66 TODO
jensdehoog 2:5b7d055dbc91 67 ----
jensdehoog 2:5b7d055dbc91 68 This function:
jensdehoog 2:5b7d055dbc91 69 - turns the built-in LED on, so the user gets informed that the program has started with counting clicks
jensdehoog 2:5b7d055dbc91 70 - disables the button such that the debouncer is active
jensdehoog 2:5b7d055dbc91 71 - enables the button again after a certain amount of time
jensdehoog 2:5b7d055dbc91 72 (use interrupts with "button1_enabled_cb()" as callback.
jensdehoog 2:5b7d055dbc91 73 - counts the amount of clicks within a period of 1 second
jensdehoog 2:5b7d055dbc91 74 - informs the main loop that the button has been pressed
jensdehoog 2:5b7d055dbc91 75 - informs the main loop that the user is clicking the button.
jensdehoog 2:5b7d055dbc91 76 Therefore, this main loop cannot continue its procedure until the clicks within 1 second have been counted.
jensdehoog 2:5b7d055dbc91 77 */
jensdehoog 0:fd29cd85e75e 78 void button1_onpressed_cb(void)
jensdehoog 0:fd29cd85e75e 79 {
fatihkilic 3:e75fe1b54623 80 if(button1_enabled && enable){
fatihkilic 3:e75fe1b54623 81 led1 = 1;
fatihkilic 3:e75fe1b54623 82 button1_pressed = true;
fatihkilic 3:e75fe1b54623 83 button1_enabled = false;
fatihkilic 3:e75fe1b54623 84 button1_busy = true;
fatihkilic 3:e75fe1b54623 85 timeout.attach(callback(&button1_multiclick_reset_cb),1);
fatihkilic 3:e75fe1b54623 86 }
fatihkilic 3:e75fe1b54623 87 if(enable && button1_pressed){
fatihkilic 3:e75fe1b54623 88 button1_pressed = false;
fatihkilic 3:e75fe1b54623 89 multiclick_state++;
fatihkilic 3:e75fe1b54623 90 }
fatihkilic 3:e75fe1b54623 91 if(!enable){
fatihkilic 3:e75fe1b54623 92 button1_pressed = true;
fatihkilic 3:e75fe1b54623 93 }
jensdehoog 0:fd29cd85e75e 94
fatihkilic 3:e75fe1b54623 95
fatihkilic 3:e75fe1b54623 96 }
fatihkilic 3:e75fe1b54623 97
fatihkilic 3:e75fe1b54623 98 void button1_onrelease_cb(void){
fatihkilic 3:e75fe1b54623 99 button1_pressed = false;
jensdehoog 0:fd29cd85e75e 100 }