Started with nodeMCU support
[mTask.git] / client / main.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 #elif defined NODEMCU
9 #include "ets_sys.h"
10 #include "osapi.h"
11 #include "gpio.h"
12 #include "os_type.h"
13 #endif
14
15 #include "interpret.h"
16 #include "mTaskSymbols.h"
17 #include "sds.h"
18 #include "spec.h"
19 #include "task.h"
20 #include "interface.h"
21
22 #define MSG_GET_TASK 't'
23 #define MSG_DEL_TASK 'd'
24 #define MSG_SDS_SPEC 's'
25 #define MSG_SDS_DEL 'a'
26 #define MSG_SDS_UPD 'u'
27 #define MSG_SPEC 'c'
28
29 void read_message(void)
30 {
31 //Find next task
32 if(input_available()){
33 uint8_t c = read_byte();
34 debug("Receiving input: %c %02x\n", c, c);
35 switch(c){
36 case MSG_SDS_SPEC:
37 debug("Receiving an sds");
38 sds_register();
39 break;
40 case MSG_SDS_UPD:
41 debug("Receiving an sds update");
42 //TODO do something with the return value
43 sds_update();
44 break;
45 case MSG_SDS_DEL:
46 debug("Receiving a delete SDS request");
47 sds_delete();
48 break;
49 case MSG_DEL_TASK:
50 debug("Receiving a delete task request");
51 task_delete();
52 break;
53 case MSG_GET_TASK:
54 debug("Receiving a task");
55 task_register();
56 break;
57 case MSG_SPEC:
58 debug("Receiving a spec request");
59 spec_send();
60 break;
61 case '\0':
62 debug("Server closed connection");
63 break;
64 case '\n':
65 break;
66 default:
67 debug("Unknown message: %X", c);
68 }
69 }
70 }
71
72 void loop(void)
73 {
74 int ct;
75 long cyclestart;
76 struct task *curtask;
77
78 read_message();
79
80 //Run tasks
81 cyclestart = millis();
82 for(ct = 0; ct<MAXTASKS; ct++){
83 //See whether the task is even in use
84 if((curtask = task_get(ct)) == NULL){
85 // debug("Task %d not implemented\n", ct);
86 continue;
87 }
88 //interrupt task
89 if(is_interrupt_task(curtask) && had_interrupt(curtask)){
90 debug("Interrupt task %d not implemented", ct);
91 run_task(curtask);
92 //Interval task, and interval passed
93 } else if(cyclestart-curtask->lastrun > curtask->interval){
94 debug("Running interval task: %d", ct);
95 run_task(curtask);
96
97 //Oneshot task, thus disable
98 if(curtask->interval == 0){
99 curtask->used = false;
100 }
101 curtask->lastrun = cyclestart;
102 }
103 }
104 }
105
106 #ifdef STM
107 int main(void){
108 #elif defined NODEMCU
109 void ICACHE_FLASH_ATTR user_init(){
110 #elif defined LINUX
111 int main(int argc, char *argv[]){
112 gargc = argc;
113 gargv = argv;
114 #endif
115
116 //Initialize systems
117 setup();
118 sds_init();
119 task_init();
120 debug("sending device spec");
121 while(true){
122 //Check for newtasks
123 loop();
124 delay(100);
125 }
126
127 #ifndef NODEMCU
128 return 0;
129 #endif
130 }