2bf2cef134c8faf1eeb4dbd83add2c2e7b83448b
9 #include "mTaskSymbols.h"
12 #define MAXTASKSIZE 1024
14 #define STACKSIZE 1024
18 #define debug(s, ...) printf(s, ##__VA_ARGS__);
20 #define debug(s, ...) ;
23 #define pdie(s) {perror(s); exit(1);}
24 #define die(s, ...) {fprintf(stderr, s, ##__VA_ARGS__); exit(1);}
26 char tasks
[MAXTASKS
][MAXTASKSIZE
] = {0};
27 bool tasks_used
[MAXTASKS
] = {0};
29 void killHandler(int i
)
31 printf("%s caught, Bye...\n", strsignal(i
));
35 bool input_available(int fd
){
42 if(select(fd
+1, &fds
, NULL
, NULL
, &tv
) == -1){
45 return FD_ISSET(fd
, &fds
);
48 void read_message(int fd_in
, int fd_out
)
55 for(ct
= 0; ct
<MAXTASKS
; ct
++)
59 die("Trying to add too much tasks...\n");
61 debug("Receiving input for task %d\n", ct
);
64 debug("Receiving an sds\n");
65 } else if((char) c
== 't'){
70 if(tasklen
> MAXTASKSIZE
)
71 die("Task is too long: %d\n", tasklen
);
72 for(int i
= 0; i
<tasklen
; i
++){
73 debug("Read %d\n", i
);
74 read(fd_in
, tasks
[ct
]+i
, 1);
75 debug("t[][%i]: %d\n", i
,
78 //Return the task number for later removal
79 write(fd_out
, (void *)&ct
, 1);
80 debug("Received a task of length %d\n", tasklen
);
81 tasks_used
[ct
] = true;
83 die("Unknown message: %c?\n", c
);
87 void run_task(char *program
)
98 fd_in
= fileno(stdin
);
99 fd_out
= fileno(stdout
);
101 //Register signal handler
102 if(signal(SIGINT
, killHandler
) == SIG_ERR
){
103 die("Couldn't register signal handler...\n");
105 if(signal(SIGTERM
, killHandler
) == SIG_ERR
){
106 die("Couldn't register signal handler...\n");
110 //Check for new tasks
111 if(input_available(fd_in
)){
112 read_message(fd_in
, fd_out
);
116 for(ct
= 0; ct
<MAXTASKS
; ct
++){
118 debug("Task %d not implemented\n", ct
);
121 debug("Going to execute task %d\n", ct
);
129 while ((c = getchar()) != EOF && pc < PROGRAMSIZE)
131 if (pc >= PROGRAMSIZE)
132 die("Max program size: %d\n", PROGRAMSIZE);
134 debug("Done reading, program length: %d\n", pl);
137 //Reset program counter and stack counter
141 switch(program[pc++]){
145 stack[sp++] = program[pc++];
151 stack[sp] = stack[sp] > 0 ? 0 : 1;
154 stack[sp-1] = stack[sp] + stack[sp-1];
158 stack[sp-1] = stack[sp] - stack[sp-1];
162 stack[sp-1] = stack[sp] * stack[sp-1];
166 stack[sp-1] = stack[sp] / stack[sp-1];
170 stack[sp-1] = stack[sp] && stack[sp-1];
174 stack[sp-1] = stack[sp] || stack[sp-1];
178 stack[sp-1] = stack[sp] == stack[sp-1];
182 stack[sp-1] = stack[sp] != stack[sp-1];
185 case BCLES: stack[sp-1] = stack[sp] < stack[sp-1];
189 stack[sp-1] = stack[sp] > stack[sp-1];
193 stack[sp-1] = stack[sp] <= stack[sp-1];
197 stack[sp-1] = stack[sp] >= stack[sp-1];
201 pc = pc + program[pc];
204 pc += stack[sp] ? program[pc] : 1;
207 pc += stack[sp] ? 1 : program[pc];
210 printf("SerialAvailable()\n");
213 printf("SerialPrint()\n");
215 case BCSERIALPRINTLN:
216 printf("SerialPrintln()\n");
219 printf("SerialRead()\n");
221 case BCSERIALPARSEINT:
222 printf("SerialParseInt()\n");
225 printf("AnalogRead()\n");
228 printf("AnalogWrite()\n");
230 die("Unrecognized command: %X\n", program[--pc]);