f86beff79013463844d7bf19323c6c676559f389
[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 //Onshot task
78 if(curtask->interval == 0){
79 run_task(curtask);
80 curtask->used = false;
81 //Interrupt task
82 } else if(curtask->interval & 32768){
83 debug("Interrupt task %d not implemented", ct);
84 //Interval task, check if interval is passed
85 } else if(cyclestart-curtask->lastrun > curtask->interval){
86 debug("Running interval task: %d", ct);
87 run_task(curtask);
88 curtask->lastrun = cyclestart;
89 }
90 }
91 }
92
93 #ifdef STM
94 int main(void){
95 #else
96 int main(int argc, char *argv[]){
97 gargc = argc;
98 gargv = argv;
99 #endif
100
101 //Initialize systems
102 setup();
103 sds_init();
104 task_init();
105 debug("booting up");
106 while(true){
107 //Check for newetasks
108 loop();
109 delay(50);
110 }
111 return 0;
112 }