Skip to content
Permalink
Browse files
Event queue example 2
  • Loading branch information
aa6164 committed Nov 2, 2022
1 parent adb1bce commit 07a30c6acdc4aa958ffd0547cb9569cdb1a06fa8
Showing 1 changed file with 9 additions and 6 deletions.
@@ -3,31 +3,34 @@
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed.h"
#include "mbed_events.h"

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


void rise_handler(void)
{
queue.call(printf, "rise_handler in context %p\n", ThisThread::get_id());
// Toggle LED
led1 = !led1;
}

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

int main()
{
// Request the shared queue
EventQueue *queue = mbed_event_queue();
// Start the event queue
t.start(callback(&queue, &EventQueue::dispatch_forever));
printf("Starting in context %p\r\n", ThisThread::get_id());
// The 'rise' handler will execute in IRQ context
sw.rise(rise_handler);
// The 'fall' handler will execute in the context of the shared queue thread
sw.fall(queue->event(fall_handler));
// The 'fall' handler will execute in the context of thread 't'
sw.fall(queue.event(fall_handler));
}

0 comments on commit 07a30c6

Please sign in to comment.