Skip to content

Instantly share code, notes, and snippets.

@lonesometraveler
Created March 31, 2020 11:21
Show Gist options
  • Save lonesometraveler/823a91ceed53f6783be1eaae97a36858 to your computer and use it in GitHub Desktop.
Save lonesometraveler/823a91ceed53f6783be1eaae97a36858 to your computer and use it in GitHub Desktop.
Mbed EventQueue example 1: Ticker Callback and deferred printf()
// Execution of certain function in an ISR context is not safe.
// For example, printf() in an interrupt context causes a Mutex error.
// Using Mbed's EventQueue, you can defer execution of code from an interrupt context to a user context.
// More about EventQueue: https://os.mbed.com/docs/mbed-os/v5.15/tutorials/the-eventqueue-api.html
#include "mbed.h"
Ticker ticker;
EventQueue *queue = mbed_event_queue(); // event queue
int counter = 0;
void processInUserContext() {
printf("printing in a user context. counter = %d\r\n", counter);
}
void tickerCallback() {
counter += 1;
queue->call(&processInUserContext); // defer the execution to a different context
}
int main() {
ticker.attach(&tickerCallback, 1.0);
queue->dispatch_forever();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment