18182389236e0ba2a4757b3b710c0d20324d6a03
[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 //interrupt task
78 if(is_interrupt_task(curtask) && had_interrupt(curtask)){
79 debug("Interrupt task %d not implemented", ct);
80 run_task(curtask);
81 //Interval task, and interval passed
82 } else if(cyclestart-curtask->lastrun > curtask->interval){
83 debug("Running interval task: %d", ct);
84 run_task(curtask);
85
86 //Oneshot task, thus disable
87 if(curtask->interval == 0){
88 curtask->used = false;
89 }
90 curtask->lastrun = cyclestart;
91 }
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 loop();
111 delay(50);
112 }
113 return 0;
114 }