Luigi Frunzio / 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 volatile time_t first_seconds = 0;
00009 
00010 void fall_handler_thread_context(void) {
00011     printf("rise_handler_thread_context in context %p\r\n", Thread::gettid());
00012     printf("firt_seconds: %d\n", first_seconds);
00013     time_t seconds = time(NULL);
00014     if (first_seconds != 0 && ((seconds - first_seconds) < 2)) {
00015             led1 = !led1;
00016     }
00017     printf("seconds: %d\n", seconds);
00018     first_seconds = seconds; 
00019 }
00020 
00021 void fall_handler_iterrupt_context(void) {
00022     // Execute the time critical part first
00023     
00024     // The rest can execute later in user context (and can contain code that's not interrupt safe)
00025     // We use the 'queue.call' function to add an event (the call to 'rise_handler_user_context') to the queue
00026     queue.call(fall_handler_thread_context);
00027 }
00028 
00029 
00030 int main() {
00031     // Start the event queue
00032     t.start(callback(&queue, &EventQueue::dispatch_forever));
00033     printf("Starting in context %p\r\n", Thread::gettid());
00034     // The 'rise' handler will execute in IRQ context
00035     button.fall(fall_handler_iterrupt_context);
00036     // button.rise(queue.event(rise_handler));
00037     // The 'fall' handler will execute in the context of thread 't'
00038 }