Leonardo Iandiorio / 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 led3(LED3);
00004 InterruptIn button(USER_BUTTON);
00005 EventQueue queue(32 * EVENTS_EVENT_SIZE);
00006 Thread t;
00007 int counter = 0;
00008 volatile time_t seconds_now;
00009 
00010 void rise_handler_thread_context(void) {
00011     counter++;
00012     if(counter == 2) {
00013         counter = 0;
00014         if((time(NULL) - seconds_now) < 2) {
00015                 led3 = !led3;
00016                 wait(0.8);
00017                 led3 = !led3;
00018             }
00019     } else {
00020         seconds_now = time(NULL);
00021     }
00022 }
00023 
00024 void rise_handler_iterrupt_context(void) {
00025     // Execute the time critical part first
00026     // The rest can execute later in user context (and can contain code that's not interrupt safe)
00027     // We use the 'queue.call' function to add an event (the call to 'rise_handler_user_context') to the queue
00028     queue.call(rise_handler_thread_context);
00029 }
00030 
00031 void fall_handler(void) {
00032     printf("Output a String\n");
00033 }
00034 
00035 int main() {
00036     // Start the event queue
00037     t.start(callback(&queue, &EventQueue::dispatch_forever));
00038     printf("Starting in context %p\r\n", Thread::gettid());
00039     // The 'rise' handler will execute in IRQ context
00040     button.rise(rise_handler_iterrupt_context);
00041     // The 'fall' handler will execute in the context of thread 't'
00042     button.fall(queue.event(fall_handler));
00043 }