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