From 07a30c6acdc4aa958ffd0547cb9569cdb1a06fa8 Mon Sep 17 00:00:00 2001 From: "aa6164@coventry.ac.uk" Date: Wed, 2 Nov 2022 16:34:21 +0000 Subject: [PATCH] Event queue example 2 --- src/main.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 4597f49..db93b21 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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)); }