update spec to support normal io pins
[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 break;
71 case '\0':
72 debug("Server closed connection");
73 break;
74 case '\n':
75 break;
76 default:
77 debug("Unknown message: %X", c);
78 }
79 } else {
80 // debug("No input");
81 }
82 }
83
84 unsigned long loopmillis = 0;
85 void loop(void)
86 {
87 #if defined ARDUINO_ESP8266_NODEMCU || defined ARDUINO_AVR_UNO
88 if (getmillis() - loopmillis < LOOPDELAY) {
89 return;
90 }
91 loopmillis = getmillis();
92 #endif
93 read_message();
94
95 //Run tasks
96 unsigned long cyclestart = getmillis();
97 for (struct task *t = task_head(); t != NULL; t = task_next(t)) {
98 //interrupt task
99 if (is_interrupt_task(t) && had_interrupt(t)) {
100 debug("Interrupt task %d not implemented", t->taskid);
101 run_task(t);
102 //Interval task, and interval passed
103 } else if (cyclestart - t->lastrun > t->interval) {
104 debug("Running interval task: %d", t->taskid);
105 run_task(t);
106
107 //Oneshot task, thus disable
108 if (t->interval == 0) {
109 task_delete(t->taskid);
110 }
111 t->lastrun = cyclestart;
112 }
113 }
114 }
115
116 #ifdef STM
117 int main(void) {
118 #elif defined ARDUINO_ESP8266_NODEMCU || defined ARDUINO_AVR_UNO
119 void setup() {
120 #elif defined LINUX
121 int main(int argc, char *argv[]) {
122 gargc = argc;
123 gargv = argv;
124 #endif
125
126 //Initialize device independant functionality
127 real_setup();
128
129 #if !defined(ARDUINO_ESP8266_NODEMCU) && !defined(ARDUINO_AVR_UNO)
130 while (true) {
131 //Check for newtasks
132 loop();
133 msdelay(LOOPDELAY);
134 }
135
136 return 0;
137 #endif
138 }