-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
164 lines (137 loc) · 4.42 KB
/
main.c
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* Title: library manager
Date: Mon, 11 Jun 2021
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include "coroutines_mgr_v2.h"
#include "../thread_task_mgr/thread_task_mgr.h"
#include "../modules/modules.h"
#include "../glcd/glcd.h"
void coroutine1()
{
printf("\ncoroutine1 -------------------------------\n");
static PfnCoroutine funcs[] = { function1, function2 };
RunCoroutineParallel(2, funcs);
}
void coroutine2()
{
printf("\ncoroutine2 -------------------------------\n");
static PfnCoroutine funcs[] = { function3, function4 };
RunCoroutineParallel(2, funcs);
}
////////////////////////////////////////////////////////////////////////////////////////////
// functions declarations
////////////////////////////////////////////////////////////////////////////////////////////
// global variables
////////////////////////////////////////////////////////////////////////////////////////////
// local continuation definitions
#define LC_INIT(s) s = 0;
#define LC_RESUME(s) switch(s) { case __LINE__:
#define LC_SET(s) s = __LINE__;
#define LC_END(s) s = 0; }
///////////////////////////////////////////////////////////
// coroutine function versions to work in the main function
///////////////////////////////////////////////////////////
// #1. coroutine version to work with bool locks
int cr_flag_state = 0;
bool coroutine_lock(void){
static int i = 0;
switch(cr_flag_state){
case 0:
do{
if(i < 2000){
cr_flag_state = __LINE__ + 3;
printf("return 0; %d cr_flag_state %d\n",i, cr_flag_state);
return 0;
case __LINE__:
i++;
}
}while(i < 1000);
}
printf("return 1;\n");
return 1;
}
// #2. coroutine version to work with timer delay counter checks
int cr_tmr_state = 0;
bool coroutine_timer(void){
static int i;
switch(cr_tmr_state){
case 0:
for(i = 0; i < 1000; i++){
cr_tmr_state = __LINE__ + 3;
printf("return 0; %d cr_tmr_state %d\n",i, cr_tmr_state);
return 0;
case __LINE__:
;
}
}
printf("return 1;\n");
return 1;
}
static volatile uint8_t bit0;
uint8_t task1(void){
TaskStart;
TaskFunctionSet;
bit0 = i2c_write();
printf("bit0 %d line %d\n", bit0, __LINE__);
CheckTask(bit0, TaskBusy);
printf("data 1 sent ///////////////\n");
TaskFunctionSet;
bit0 = i2c_write();
printf("bit0 %d line %d\n", bit0, __LINE__);
CheckTask(bit0, TaskBusy);
printf("data 2 sent ///////////////\n");
TaskEnd;
}
int main(){
////////////////////////////////////////////////////////////
// First version of coroutines implementation:
// * sequential coroutines control flow
// * tasks run in parallel for each coroutine
// coroutine1();
// coroutine2();
////////////////////////////////////////////////////////////
// Second version of coroutines implementation:
// * parallel coroutines control flow
// * tasks run in sequence for each coroutine
// main thread
bool thread;
uint8_t task1_cnts = 2, task2_cnts = 2;
uint8_t task1_cntr = 0, task2_cntr = 0;
bool task1_done, task1_state[task1_cnts];
bool task2_done, task2_state[task2_cnts];
uint8_t task1_flag;
task1_done = false;
thread = false;
task1_flag = false;
bit0 = false;
start_time();
do{
do{
task1_flag = task1();
printf("task1_flag %d\n", task1_flag);
//task1_flag = i2c_write();
//i2c_bit_set_clear_routine();
}while(task1_flag != TaskFinished);
if(task1_flag == TaskFinished){thread = true;}
if(thread){printf("thread is done\n");}
}while(!thread);
// a test to prove how coroutines work
// so as coroutines need to be inside a function outside main
//bool bit1;
// do{
// BeginCoroutine;
// printf("\n1 part1 %d\n", __LINE__ + 1);
// CoroutineDelay(200);
// printf("\n1 part2 %d\n", __LINE__ + 1);
// CoroutineDelay(30);
// printf("\n1 part3 %d\n", __LINE__ + 1);
// CoroutineDelay(50);
// printf("\n1 part4 %d\n", __LINE__ + 1);
// CoroutineDelay(10);
// EndCoroutine;
// }while(!bit1);
return 0;
}