0218fb7a4c7e438477fb100eecb7b12d8e959027
[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 }
67 }
68
69 unsigned long loopmillis = 0;
70 void loop(void)
71 {
72 #ifdef ARDUINO_ESP8266_NODEMCU
73 if(getmillis()-loopmillis < LOOPDELAY){
74 return;
75 }
76 #endif
77
78 int ct;
79 long cyclestart;
80 struct task *curtask;
81
82 read_message();
83
84 //Run tasks
85 cyclestart = getmillis();
86 for(ct = 0; ct<MAXTASKS; ct++){
87 //See whether the task is even in use
88 if((curtask = task_get(ct)) == NULL){
89 // debug("Task %d not implemented\n", ct);
90 continue;
91 }
92 //interrupt task
93 if(is_interrupt_task(curtask) && had_interrupt(curtask)){
94 debug("Interrupt task %d not implemented", ct);
95 run_task(curtask);
96 //Interval task, and interval passed
97 } else if(cyclestart-curtask->lastrun > curtask->interval){
98 debug("Running interval task: %d", ct);
99 run_task(curtask);
100
101 //Oneshot task, thus disable
102 if(curtask->interval == 0){
103 curtask->used = false;
104 }
105 curtask->lastrun = cyclestart;
106 }
107 }
108 }
109
110 #ifdef STM
111 int main(void){
112 #elif defined ARDUINO_ESP8266_NODEMCU
113 void setup(){
114 #elif defined LINUX
115 int main(int argc, char *argv[]){
116 gargc = argc;
117 gargv = argv;
118 #endif
119
120 //Initialize systems
121 real_setup();
122 sds_init();
123 task_init();
124 debug("sending device spec");
125
126 #ifndef ARDUINO_ESP8266_NODEMCU
127 while(true){
128 //Check for newtasks
129 loop();
130 msdelay(LOOPDELAY);
131 }
132
133 return 0;
134 #endif
135 }