forked from ticapix/arduino-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCoroutine.cpp
executable file
·58 lines (49 loc) · 1.33 KB
/
Coroutine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Coroutines.cpp : Defines the entry point for the console application.
//
#include <list>
#include <stdio.h>
#include "Coroutine.h"
#include "Arduino.h"
//////////// EXAMPLE OF ASYNC Delay implementation ////////////
/*
* This coroutine will simply exit when timing out
*/
CORO_START(Delay)
{
while (true)
YIELD()
}
CORO_RETURN(0)
CORO_END()
//////////// EXAMPLE OF MAIN SCHEDULING LOOP implementation ////////////
/*
* It can be the only thing you have the call from the arduino loop() function
*/
void schedule_coro(ICoroutine* coroutines[], uint8_t size, coro_callback clbk) {
bool has_coro = true;
while (has_coro) {
has_coro = false;
for (uint8_t idx = 0; idx < size; ++idx) {
ICoroutine* coro = coroutines[idx];
if (coro == nullptr)
continue;
has_coro = true;
if (coro->live()) {
coro->run();
} else {
clbk(idx, coro);
delete coro;
coroutines[idx] = nullptr;
}
}
}
}
///////////////////////////////////////////////////////////////////////
//http://en.cppreference.com/w/cpp/memory/new/operator_delete
/*
* TODO: overload new.
* if free_ram() too small, redirect to default TIMEOUT error result
* overload delete
* if default TIMEOUT error, do not free
*/
///////////////////////////////////////////////////////////////////////