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