not working again.
[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 update");
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 // write_dpin(0, true);
44 task_register();
45 // write_dpin(0, false);
46 break;
47 case '\0':
48 break;
49 case '\n':
50 break;
51 default:
52 debug("Unknown message: %X", c);
53 debugi(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 write_byte('\n');
83 }
84 }
85
86 #ifdef STM
87 int main(void){
88 #else
89 int main(int argc, char *argv[]){
90 gargc = argc;
91 gargv = argv;
92 #endif
93
94 //Initialize systems
95 setup();
96 sds_init();
97 task_init();
98
99 debug("booting up");
100 while(true){
101 //Check for newetasks
102 // debug("loop");
103 loop();
104 delay(100);
105 }
106 return 0;
107 }