assignment

Committer:
frunzl
Date:
Tue Nov 27 21:51:53 2018 +0000
Revision:
0:6b65e6a1f3ae
assignment;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
frunzl 0:6b65e6a1f3ae 1 #include "mbed.h"
frunzl 0:6b65e6a1f3ae 2
frunzl 0:6b65e6a1f3ae 3 DigitalOut led1(LED1);
frunzl 0:6b65e6a1f3ae 4 InterruptIn button(USER_BUTTON);
frunzl 0:6b65e6a1f3ae 5 EventQueue queue(32 * EVENTS_EVENT_SIZE);
frunzl 0:6b65e6a1f3ae 6 Thread t;
frunzl 0:6b65e6a1f3ae 7
frunzl 0:6b65e6a1f3ae 8 volatile time_t first_seconds = 0;
frunzl 0:6b65e6a1f3ae 9
frunzl 0:6b65e6a1f3ae 10 void fall_handler_thread_context(void) {
frunzl 0:6b65e6a1f3ae 11 printf("rise_handler_thread_context in context %p\r\n", Thread::gettid());
frunzl 0:6b65e6a1f3ae 12 printf("firt_seconds: %d\n", first_seconds);
frunzl 0:6b65e6a1f3ae 13 time_t seconds = time(NULL);
frunzl 0:6b65e6a1f3ae 14 if (first_seconds != 0 && ((seconds - first_seconds) < 2)) {
frunzl 0:6b65e6a1f3ae 15 led1 = !led1;
frunzl 0:6b65e6a1f3ae 16 }
frunzl 0:6b65e6a1f3ae 17 printf("seconds: %d\n", seconds);
frunzl 0:6b65e6a1f3ae 18 first_seconds = seconds;
frunzl 0:6b65e6a1f3ae 19 }
frunzl 0:6b65e6a1f3ae 20
frunzl 0:6b65e6a1f3ae 21 void fall_handler_iterrupt_context(void) {
frunzl 0:6b65e6a1f3ae 22 // Execute the time critical part first
frunzl 0:6b65e6a1f3ae 23
frunzl 0:6b65e6a1f3ae 24 // The rest can execute later in user context (and can contain code that's not interrupt safe)
frunzl 0:6b65e6a1f3ae 25 // We use the 'queue.call' function to add an event (the call to 'rise_handler_user_context') to the queue
frunzl 0:6b65e6a1f3ae 26 queue.call(fall_handler_thread_context);
frunzl 0:6b65e6a1f3ae 27 }
frunzl 0:6b65e6a1f3ae 28
frunzl 0:6b65e6a1f3ae 29
frunzl 0:6b65e6a1f3ae 30 int main() {
frunzl 0:6b65e6a1f3ae 31 // Start the event queue
frunzl 0:6b65e6a1f3ae 32 t.start(callback(&queue, &EventQueue::dispatch_forever));
frunzl 0:6b65e6a1f3ae 33 printf("Starting in context %p\r\n", Thread::gettid());
frunzl 0:6b65e6a1f3ae 34 // The 'rise' handler will execute in IRQ context
frunzl 0:6b65e6a1f3ae 35 button.fall(fall_handler_iterrupt_context);
frunzl 0:6b65e6a1f3ae 36 // button.rise(queue.event(rise_handler));
frunzl 0:6b65e6a1f3ae 37 // The 'fall' handler will execute in the context of thread 't'
frunzl 0:6b65e6a1f3ae 38 }