forked from Jumperr-labs/STM32_Button_Debounce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
43 lines (35 loc) · 1.15 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "mbed.h"
DigitalOut led1(LED1);
InterruptIn button1(USER_BUTTON);
volatile bool button1_pressed = false; // Used in the main loop 1
volatile bool button1_enabled = true; // Used for debouncing
Timeout button1_timeout; // Used for debouncing
// Enables button when bouncing is over
void button1_enabled_cb(void)
{
button1_enabled = true;
}
// ISR handling button pressed event
void button1_onpressed_cb(void)
{
if (button1_enabled) { // Disabled while the button is bouncing
button1_enabled = false;
button1_pressed = true; // To be read by the main loop
button1_timeout.attach(callback(button1_enabled_cb), 0.3); // Debounce time 300 ms
}
}
int main()
{
printf("mBed boot done\n");
printf("Press user button to toggle LED\n");
//button1.mode(PullUp); // Activate pull-up
button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event
int idx = 0; // Just for printf below
while(1) {
if (button1_pressed) { // Set when button is pressed
button1_pressed = false;
printf("Button pressed %d\n", idx++);
led1 = !led1;
}
}
}