you may want to take a look at this one
an event loop developed on maple mini
https://github.com/ag88/stm32duino-eventloop
but the codes are not specific to the mcu
note that this isn't the only one and neither is it a good let alone best one, there are plenty more implementations and examples which are probably better
https://www.google.com/search?q=event+loop+arduino
an event loop is a 'glorified' version of this,
Code: Select all
loop() {
task1();
task2();
task3();
}
or more correctly 'co-operative multi-tasking', so each task() should not hog and wait for things, instead use global variables to keep state and let go the time slice and return from the function.
the difference between an event loop vs the above simple 'scheduler' is it works more like a node-js event loop
https://www.tutorialspoint.com/nodejs/n ... t_loop.htm
i place events in a queue and send them to tasks so each task has to evaluate if it should process an event.
does it work? well the whole 'windowing' system world works nearly based on this concept, so do node js, javascript etc.
why event loop? for RTOS, it needs to allocate memory to each vtask, hence, this fragments the already very limited memory and you would soon run out of memory. using an event loop, lets you do 'co-operative' multitasking and you can 'send events' between tasks (e.g. task1 check button presses, post the event, another task check the queue and process the event (if needed). 'dynamic' memory is basically your local variables (placed on the stack) and that gets unwound when each function (task) returns.
- not real time, but the event loop has a hidden surprising result of keeping everything in a queue (actually a circular buffer), so your events happen one after another in sequence. it got rid of a lot of synchronization pains, if you put every thing on a queue, there is no need to synchronize, every thing happen one after another as the event loop dispatches events to the tasks one after another.
what about real time? the interrupt request handlers take care of that, anything that needs 'real time' goes into the isr or hardware
the other benefit of an event loop is each task() is a regular c function or c++ method. so you can call it from anywhere with little concerns.