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