Merge branch 'master' of gitlab.science.ru.nl:mlubbers/mTask
[mTask.git] / client / client.c
1 #include <stdbool.h>
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #ifdef LINUX
7 #include <stdio.h>
8 #endif
9
10 #include "interpret.h"
11 #include "mTaskSymbols.h"
12 #include "sds.h"
13 #include "spec.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 #define MSG_SPEC 'c'
23
24 #define LOOPDELAY 100
25
26 void read_message(void)
27 {
28 //Find next task
29 if(input_available()){
30 uint8_t c = read_byte();
31 debug("Receiving input: %c %02x\n", c, c);
32 switch(c){
33 case MSG_SDS_SPEC:
34 debug("Receiving an sds");
35 sds_register();
36 break;
37 case MSG_SDS_UPD:
38 debug("Receiving an sds update");
39 //TODO do something with the return value
40 sds_update();
41 break;
42 case MSG_SDS_DEL:
43 debug("Receiving a delete SDS request");
44 sds_delete();
45 break;
46 case MSG_DEL_TASK:
47 debug("Receiving a delete task request");
48 task_delete();
49 break;
50 case MSG_GET_TASK:
51 debug("Receiving a task");
52 task_register();
53 break;
54 case MSG_SPEC:
55 debug("Receiving a spec request");
56 spec_send();
57 break;
58 case '\0':
59 debug("Server closed connection");
60 break;
61 case '\n':
62 break;
63 default:
64 debug("Unknown message: %X", c);
65 }
66 } else {
67 // debug("No input");
68 }
69 }
70
71 unsigned long loopmillis = 0;
72 void loop(void)
73 {
74 #ifdef ARDUINO_ESP8266_NODEMCU
75 if(getmillis()-loopmillis < LOOPDELAY){
76 return;
77 }
78 loopmillis = getmillis();
79 #endif
80 debug("Loop");
81
82 struct task *t;
83 long cyclestart;
84
85 read_message();
86
87 //Run tasks
88 cyclestart = getmillis();
89 for(struct task *t = task_head(); t != NULL; t = task_next(t)){
90 //interrupt task
91 if(is_interrupt_task(t) && had_interrupt(t)){
92 debug("Interrupt task %d not implemented", t->taskid);
93 run_task(t);
94 //Interval task, and interval passed
95 } else if(cyclestart-t->lastrun > t->interval){
96 debug("Running interval task: %d", t->taskid);
97 run_task(t);
98
99 //Oneshot task, thus disable
100 // if(t->interval == 0){
101 // curtask->used = false;
102 // }
103 t->lastrun = cyclestart;
104 }
105 }
106 }
107
108 #ifdef STM
109 int main(void){
110 #elif defined ARDUINO_ESP8266_NODEMCU
111 void setup(){
112 #elif defined LINUX
113 int main(int argc, char *argv[]){
114 gargc = argc;
115 gargv = argv;
116 #endif
117
118 //Initialize systems
119 real_setup();
120 sds_init();
121 task_init();
122 debug("sending device spec");
123
124 #ifndef ARDUINO_ESP8266_NODEMCU
125 while(true){
126 //Check for newtasks
127 loop();
128 msdelay(LOOPDELAY);
129 }
130
131 return 0;
132 #endif
133 }