Lucas Pennati / Mbed OS BlinkButton2Sec
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 DigitalOut led1(LED1);
00004 InterruptIn button(USER_BUTTON);
00005 EventQueue queue(32 * EVENTS_EVENT_SIZE);
00006 Thread t;
00007 
00008 time_t last_click;
00009 
00010 void fall_handler(void) {
00011     printf("Button pressed\r\n");
00012     
00013     time_t current = time(NULL);
00014     
00015     if (current - last_click <= 2) {
00016         // Turn on the led, wait one sec, and then turn it off
00017         led1 = !led1;
00018         wait(0.5);
00019         led1 = !led1;
00020     }
00021     
00022     // Update the last_click time
00023     last_click = current;
00024 }
00025 
00026 int main() {
00027     // We start by setting a last click bogus time
00028     last_click = time(NULL);
00029     
00030     // Start the event queue
00031     t.start(callback(&queue, &EventQueue::dispatch_forever));
00032     button.fall(queue.event(fall_handler));
00033     
00034     printf("Setup complete\n");
00035 }