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