remove traces
[mTask.git] / int / main.c
1 #include <stdbool.h>
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #ifdef STM
7 #else
8 #include <stdio.h>
9 #endif
10
11 #include "interpret.h"
12 #include "mTaskSymbols.h"
13 #include "sds.h"
14 #include "task.h"
15 #include "interface.h"
16
17 #define MSG_GET_TASK 't'
18 #define MSG_DEL_TASK 'd'
19 #define MSG_SDS_SPEC 's'
20 #define MSG_SDS_UPD 'u'
21
22 void read_message(void)
23 {
24 //Find next task
25 if(input_available()){
26 uint8_t c = read_byte();
27 debug("Receiving input: %c %02x\n", c, c);
28 switch(c){
29 case MSG_SDS_SPEC:
30 debug("Receiving an sds");
31 sds_register();
32 break;
33 case MSG_SDS_UPD:
34 debug("Receiving an sds update");
35 //TODO do something with the return value
36 sds_update();
37 break;
38 case MSG_DEL_TASK:
39 debug("Receiving a delete task request");
40 task_delete();
41 break;
42 case MSG_GET_TASK:
43 debug("Receiving a task");
44 // write_dpin(0, true);
45 task_register();
46 // write_dpin(0, false);
47 break;
48 case '\0':
49 break;
50 case '\n':
51 break;
52 default:
53 debug("Unknown message: %X", c);
54 }
55 }
56 }
57
58 void loop(void)
59 {
60 int ct;
61 long cyclestart;
62 struct task *curtask;
63
64 read_message();
65
66 //Run tasks
67 cyclestart = millis();
68 for(ct = 0; ct<MAXTASKS; ct++){
69 //See whether the task is even in use
70 if((curtask = task_get(ct)) == NULL){
71 // debug("Task %d not implemented\n", ct);
72 continue;
73 }
74 //See whether the task interval has passed
75 if(cyclestart-curtask->lastrun < curtask->interval){
76 // debug("Task %d not scheduled\n", ct);
77 continue;
78 }
79 debug("Current task to run: %d", ct);
80 run_task(curtask);
81 curtask->lastrun = cyclestart;
82 }
83 }
84
85 #ifdef STM
86 int main(void){
87 #else
88 int main(int argc, char *argv[]){
89 gargc = argc;
90 gargv = argv;
91 #endif
92
93 //Initialize systems
94 setup();
95 sds_init();
96 task_init();
97 //debug("booting up");
98 while(true){
99 //Check for newetasks
100 // debug("loop");
101 loop();
102 delay(50);
103 }
104 return 0;
105 }