Create an Mbed program that - Outputs a string every time the button is pressed - Blinks LED1 if the button is pressed twice within two seconds - Use time_t seconds = time(NULL) to get current time
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 time_t pressed_seconds = time(NULL); 00008 00009 void blink_led(){ 00010 // Blink the led two times if button pressed two times in two seconds 00011 led1 = !led1; 00012 wait(0.5); 00013 led1 = !led1; 00014 wait(0.5); 00015 led1 = !led1; 00016 wait(0.5); 00017 led1 = !led1; 00018 } 00019 void rise_handler_thread_context(void) { 00020 if(pressed_seconds - time(NULL) > 2){ 00021 pressed_seconds = time(NULL); 00022 } else { 00023 blink_led(); 00024 pressed_seconds = time(NULL); 00025 } 00026 } 00027 00028 void rise_handler_iterrupt_context(void) { 00029 // Execute the time critical part first 00030 // The rest can execute later in user context (and can contain code that's not interrupt safe) 00031 // We use the 'queue.call' function to add an event (the call to 'rise_handler_user_context') to the queue 00032 queue.call(rise_handler_thread_context); 00033 } 00034 00035 void fall_handler(void) { 00036 printf("I am a string\n"); 00037 } 00038 00039 int main() { 00040 // Start the event queue 00041 t.start(callback(&queue, &EventQueue::dispatch_forever)); 00042 printf("Starting in context %p\r\n", Thread::gettid()); 00043 // The 'rise' handler will execute in IRQ context 00044 button.rise(rise_handler_iterrupt_context); 00045 // The 'fall' handler will execute in the context of thread 't' 00046 button.fall(queue.event(fall_handler)); 00047 }
Generated on Mon Aug 1 2022 21:57:44 by
1.7.2