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