Lucas Pennati / Mbed OS ProducerConsumer

main.cpp

Committer:
lmottola
Date:
2018-11-12
Revision:
0:6ab5b8697bfb
Child:
1:cc715a7c24a5

File content as of revision 0:6ab5b8697bfb:

#include "mbed.h"

DigitalOut led1(LED1);
InterruptIn button(USER_BUTTON);
EventQueue queue(32 * EVENTS_EVENT_SIZE);
Thread t;

void rise_handler_thread_context(void) {
    printf("rise_handler_thread_context in context %p\r\n", Thread::gettid());
}

void rise_handler_iterrupt_context(void) {
    // Execute the time critical part first
    led1 = !led1;
    // The rest can execute later in user context (and can contain code that's not interrupt safe)
    // We use the 'queue.call' function to add an event (the call to 'rise_handler_user_context') to the queue
    queue.call(rise_handler_thread_context);
}

void fall_handler(void) {
    printf("fall_handler in context %p\r\n", Thread::gettid());
    // Toggle LED
    led1 = !led1;
}

int main() {
    // Start the event queue
    t.start(callback(&queue, &EventQueue::dispatch_forever));
    printf("Starting in context %p\r\n", Thread::gettid());
    // The 'rise' handler will execute in IRQ context
    button.rise(rise_handler_iterrupt_context);
    // The 'fall' handler will execute in the context of thread 't'
    button.fall(queue.event(fall_handler));
}