update message spec
[mTask.git] / client / 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_DEL 'a'
21 #define MSG_SDS_UPD 'u'
22
23 void read_message(void)
24 {
25 //Find next task
26 if(input_available()){
27 uint8_t c = read_byte();
28 debug("Receiving input: %c %02x\n", c, c);
29 switch(c){
30 case MSG_SDS_SPEC:
31 debug("Receiving an sds");
32 sds_register();
33 break;
34 case MSG_SDS_UPD:
35 debug("Receiving an sds update");
36 //TODO do something with the return value
37 sds_update();
38 break;
39 case MSG_SDS_DEL:
40 debug("Receiving a delete SDS request");
41 sds_delete();
42 break;
43 case MSG_DEL_TASK:
44 debug("Receiving a delete task request");
45 task_delete();
46 break;
47 case MSG_GET_TASK:
48 debug("Receiving a task");
49 task_register();
50 break;
51 case '\0':
52 break;
53 case '\n':
54 break;
55 default:
56 debug("Unknown message: %X", c);
57 }
58 }
59 }
60
61 void loop(void)
62 {
63 int ct;
64 long cyclestart;
65 struct task *curtask;
66
67 read_message();
68
69 //Run tasks
70 cyclestart = millis();
71 for(ct = 0; ct<MAXTASKS; ct++){
72 //See whether the task is even in use
73 if((curtask = task_get(ct)) == NULL){
74 // debug("Task %d not implemented\n", ct);
75 continue;
76 }
77 //See whether the task interval has passed
78 if(cyclestart-curtask->lastrun < curtask->interval){
79 // debug("Task %d not scheduled\n", ct);
80 continue;
81 }
82 debug("Current task to run: %d", ct);
83 run_task(curtask);
84 curtask->lastrun = cyclestart;
85 if(curtask->interval == 0){
86 curtask->used = false;
87 write_byte('m');
88 write_byte('d');
89 write_byte('\n');
90 }
91 write_byte('\n');
92 }
93 }
94
95 #ifdef STM
96 int main(void){
97 #else
98 int main(int argc, char *argv[]){
99 gargc = argc;
100 gargv = argv;
101 #endif
102
103 //Initialize systems
104 setup();
105 sds_init();
106 task_init();
107 //debug("booting up");
108 while(true){
109 //Check for newetasks
110 // debug("loop");
111 loop();
112 delay(50);
113 }
114 return 0;
115 }