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