Skip to content

Commit 25e3047

Browse files
committed
refactored coroutine to separate declaration and implementation
commented out GPRS/AT code
1 parent 245c37f commit 25e3047

File tree

8 files changed

+534
-387
lines changed

8 files changed

+534
-387
lines changed

Coroutine/Coroutine.cpp

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

Coroutine/Coroutine.h

+8-38
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef __COROUTINE__
22
#define __COROUTINE__
33

4-
unsigned long millis();
4+
#include <Arduino.h>
55

66
#define TIMEOUT_MS 500
77

@@ -95,46 +95,16 @@ while (_subtask->live()) { \
9595

9696
#define HAS_TIMEOUT() _subtask == nullptr ? false : _subtask->has_timeout()
9797

98-
//////////// EXAMPLE OF MAIN SCHEDULING LOOP implementation ////////////
99-
/*
100-
* It can be the only thing you have the call from the arduino loop() function
101-
*/
102-
103-
typedef void (*coro_callback)(uint8_t idx, const ICoroutine*);
104-
105-
void schedule_coro(ICoroutine* coroutines[], uint8_t size, coro_callback clbk) {
106-
bool has_coro = true;
107-
while (has_coro) {
108-
has_coro = false;
109-
for (uint8_t idx = 0; idx < size; ++idx) {
110-
ICoroutine* coro = coroutines[idx];
111-
if (coro == nullptr)
112-
continue;
113-
has_coro = true;
114-
if (coro->live()) {
115-
coro->run();
116-
} else {
117-
clbk(idx, coro);
118-
delete coro;
119-
coroutines[idx] = nullptr;
120-
}
121-
}
122-
}
123-
}
12498

12599
//////////// EXAMPLE OF ASYNC Delay implementation ////////////
100+
// Implementation in the corresponding .cpp file //
101+
COROUTINE(int, Delay,) // NOTE the ', ' because there is no argument or variable
126102

127-
/*
128-
* This coroutine will simply exit when timing out
129-
*/
103+
//////////// EXAMPLE OF MAIN SCHEDULING LOOP implementation ////////////
104+
// Implementation in the corresponding .cpp file //
105+
106+
typedef void (*coro_callback)(uint8_t idx, const ICoroutine*);
107+
void schedule_coro(ICoroutine* coroutines[], uint8_t size, coro_callback clbk);
130108

131-
COROUTINE(int, Delay,) // NOTE the ', ' because there is no argument or variable
132-
CORO_START(Delay)
133-
{
134-
while (true)
135-
YIELD()
136-
}
137-
CORO_RETURN(0)
138-
CORO_END()
139109

140110
#endif

0 commit comments

Comments
 (0)