try something
[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 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 }
54 }
55 }
56
57 void loop(void)
58 {
59 int ct;
60 long cyclestart;
61 struct task *curtask;
62
63 read_message();
64
65 //Run tasks
66 cyclestart = millis();
67 for(ct = 0; ct<MAXTASKS; ct++){
68 //See whether the task is even in use
69 if((curtask = task_get(ct)) == NULL){
70 // debug("Task %d not implemented\n", ct);
71 continue;
72 }
73 //See whether the task interval has passed
74 if(cyclestart-curtask->lastrun < curtask->interval){
75 // debug("Task %d not scheduled\n", ct);
76 continue;
77 }
78 debug("Current task to run: %d", ct);
79 run_task(curtask);
80 curtask->lastrun = cyclestart;
81 write_byte('\n');
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
98 debug("booting up");
99 while(true){
100 //Check for newetasks
101 debug("loop");
102 loop();
103 delay(100);
104 }
105 return 0;
106 }