i should make literal work
[mTask.git] / int / int.c
index 6ef8cc4..fd54153 100644 (file)
--- a/int/int.c
+++ b/int/int.c
@@ -24,6 +24,7 @@
 #define die(s, ...) {fprintf(stderr, s, ##__VA_ARGS__); exit(1);}
 
 char tasks[MAXTASKS][MAXTASKSIZE] = {0};
+bool tasks_used[MAXTASKS] = {0};
 
 void killHandler(int i)
 {
@@ -38,75 +39,58 @@ bool input_available(int fd){
        tv.tv_usec = 0;
        FD_ZERO(&fds);
        FD_SET(fd, &fds);
-       if(select(fd+1, &fds, NULL, NULL, &tv) == -1){
+       if (select(fd+1, &fds, NULL, NULL, &tv) == -1)
                pdie("select");
-       }
        return FD_ISSET(fd, &fds);
 }
 
-int main(void)
+void read_message(int fd_in, int fd_out)
 {
-       uint8_t c;
-//     char taskindex = 0;
-//     int pl, sp, pc;
-//
-       int fd_in = 0;
-       int next_free_new_task = 0;
-       uint16_t tasklen = 0;
+       //Find next task
+       uint8_t c, ct;
+       uint16_t tasklen;
 
-       //Register signal handler
-       if(signal(SIGINT, killHandler) == SIG_ERR){
-               die("Couldn't register signal handler...\n");
-       }
-       if(signal(SIGTERM, killHandler) == SIG_ERR){
-               die("Couldn't register signal handler...\n");
-       }
+       for(ct = 0; ct<MAXTASKS; ct++)
+               if(!tasks_used[ct])
+                       break;
+       if(ct == MAXTASKS)
+               die("Trying to add too much tasks...\n");
 
-       while(true){
-               if(input_available(fd_in)){
-                       printf("Receiving input\n");
-                       read(fd_in, &c, 1);
-                       if((char) c == 's'){
-                               debug("Receiving an sds\n");
-                       } else if((char) c == 't'){
-                               read(fd_in, &c, 1);
-                               tasklen = 256*c;
-                               read(fd_in, &c, 1);
-                               tasklen += c;
-                               if(tasklen > MAXTASKSIZE){
-                                       die("Task is too long: %d\n", tasklen);
-                               }
-                               for(int i = 0; i<tasklen; i++){
-                                       debug("Read %d\n", i);
-                                       read(fd_in, tasks[next_free_new_task]+i, 1);
-                                       read(fd_in, tasks[next_free_new_task]+i, 1);
-                                       debug("t[][%i]: %d\n", i, tasks[next_free_new_task][i]);
-                               }
-                               debug("Receiving a task of length %d\n", tasklen);
-                       } else {
-                               die("Unknown message: %c?\n", c);
-                       }
-                       exit(1);
+       debug("Receiving input for task %d\n", ct);
+       read(fd_in, &c, 1);
+       if(c == 's') {
+               debug("Receiving an sds\n");
+       } else if(c == 't'){
+               read(fd_in, &c, 1);
+               tasklen = 256*c;
+               read(fd_in, &c, 1);
+               tasklen += c;
+               if(tasklen > MAXTASKSIZE)
+                       die("Task is too long: %d\n", tasklen);
+               for(int i = 0; i<tasklen; i++){
+                       debug("Read %d\n", i);
+                       read(fd_in, tasks[ct]+i, 1);
+                       debug("t[][%i]: %d\n", i,
+                               tasks[ct][i]);
                }
-               usleep(1);
+               //Return the task number for later removal
+               write(fd_out, (void *)&ct, 1);
+               debug("Received a task of length %d\n", tasklen);
+               tasks_used[ct] = true;
+       } else {
+               debug("Unknown message: %X?\n", c);
        }
+}
 
-/*     //Read program
-       pc = 0;
-       while ((c = getchar()) != EOF && pc < PROGRAMSIZE)
-               program[pc++] = c;
-       if (pc >= PROGRAMSIZE)
-               die("Max program size: %d\n", PROGRAMSIZE);
-       pl = pc;
-       debug("Done reading, program length: %d\n", pl);
-
-       //Evaluate program
-       //Reset program counter and stack counter
-       pc = 0;
-       sp = 0;
-       while(pc != pl){
+void run_task(char *program)
+{
+       int pc = 0;
+       int sp = 0;
+       int plen = strlen(program);
+       char stack[1024] = {0};
+       while(pc != plen){
                switch(program[pc++]){
-               case BCNOP:;
+               case BCNOP:
                        break;
                case BCPUSH:
                        stack[sp++] = program[pc++];
@@ -189,13 +173,72 @@ int main(void)
                        printf("SerialParseInt()\n");
                        break;
                case BCANALOGREAD:
-                       printf("AnalogRead()\n");
+                       printf("AnalogRead(%d)\n", program[pc++]);
                        break;
                case BCANALOGWRITE:
-                       printf("AnalogWrite()\n");
+                       printf("AnalogWrite(%d)\n", program[pc++]);
+                       break;
+               case BCDIGITALREAD:
+                       printf("DigitalRead(%d)\n", program[pc++]);
+                       break;
+               case BCDIGITALWRITE:
+                       printf("DigitalWrite(%d)\n", program[pc++]);
+                       break;
                default:
-                       die("Unrecognized command: %X\n", program[--pc]);
+                       die("Unrecognized command: %d\n", program[--pc]);
+               }
+       }
+}
+
+int main(void)
+{
+       int fd_in, fd_out;
+       int ct;
+
+       fd_in = fileno(stdin);
+       fd_out = fileno(stdout);
+
+       //Register signal handler
+       if(signal(SIGINT, killHandler) == SIG_ERR){
+               die("Couldn't register signal handler...\n");
+       }
+       if(signal(SIGTERM, killHandler) == SIG_ERR){
+               die("Couldn't register signal handler...\n");
+       }
+
+       while(true){
+               //Check for new tasks
+               if(input_available(fd_in)){
+                       read_message(fd_in, fd_out);
                }
+               //Run tasks
+               for(ct = 0; ct<MAXTASKS; ct++){
+                       if(!tasks_used[ct]){
+                               debug("Task %d not implemented\n", ct);
+                               continue;
+                       }
+                       debug("Going to execute task %d\n", ct);
+                       run_task(tasks[ct]);
+                       exit(1);
+               }
+//             exit(1);
+               usleep(10);
+       }
+
+/*     //Read program
+       pc = 0;
+       while ((c = getchar()) != EOF && pc < PROGRAMSIZE)
+               program[pc++] = c;
+       if (pc >= PROGRAMSIZE)
+               die("Max program size: %d\n", PROGRAMSIZE);
+       pl = pc;
+       debug("Done reading, program length: %d\n", pl);
+
+       //Evaluate program
+       //Reset program counter and stack counter
+       pc = 0;
+       sp = 0;
+       while(pc != pl){
        }
 */
        return 0;