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