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