added sds operators, timing and task deletion
authorMart Lubbers <mart@martlubbers.net>
Wed, 14 Dec 2016 14:31:19 +0000 (15:31 +0100)
committerMart Lubbers <mart@martlubbers.net>
Wed, 14 Dec 2016 14:31:19 +0000 (15:31 +0100)
int/int.c
int/mTaskSymbols.h
mTaskInterpret.dcl
mTaskInterpret.icl

index 37859cc..c89a6a8 100644 (file)
--- a/int/int.c
+++ b/int/int.c
@@ -8,6 +8,7 @@
 #include <string.h>
 #include <sys/socket.h>
 #include <sys/types.h>
+#include <sys/time.h>
 #include <unistd.h>
 
 #include "mTaskSymbols.h"
@@ -18,6 +19,7 @@
 #define STACKSIZE 1024
 
 #define MSG_TASK 't'
+#define MSG_DELTASK 'd'
 #define MSG_SDS 's'
 
 #define DEBUG
 struct task {
        uint8_t bc[MAXTASKSIZE];
        uint16_t tlen;
+       uint16_t interval;
+       long lastrun;
        bool used;
 };
 
+struct timeval tv1;
 struct task tasks[MAXTASKS] = {0};
 int sock_fd = -1;
 int fd = -1;
@@ -62,6 +67,12 @@ bool input_available(int fd){
        return FD_ISSET(fd, &fds);
 }
 
+long millis() {
+       if (gettimeofday(&tv1, NULL) == -1)
+               pdie("gettimeofday");
+       return tv1.tv_sec*1000 + tv1.tv_usec/1000;
+}
+
 void read_message(int fd_in, int fd_out)
 {
        //Find next task
@@ -79,13 +90,28 @@ void read_message(int fd_in, int fd_out)
        case MSG_SDS:
                debug("Receiving an sds\n");
                break;
+       case MSG_DELTASK:
+               debug("Receiving a delete task request\n");
+               //Read task number and set unused
+               read(fd_in, &c, 1);
+               tasks[ct].used = false;
+               break;
        case MSG_TASK:
+               debug("Receiving a task\n");
+               bzero(&tasks[ct], sizeof(struct task));
+               //Read interval
+               read(fd_in, &c, 1);
+               tasks[ct].interval = 256*c;
+               read(fd_in, &c, 1);
+               tasks[ct].interval += c;
+               //Read tasklength
                read(fd_in, &c, 1);
                tasks[ct].tlen = 256*c;
                read(fd_in, &c, 1);
                tasks[ct].tlen += c;
                if(tasks[ct].tlen > MAXTASKSIZE)
                        die("Task is too long: %d\n", tasks[ct].tlen);
+               //Read task bytecode
                for(int i = 0; i<tasks[ct].tlen; i++){
                        debug("Read %d\n", i);
                        read(fd_in, tasks[ct].bc+i, 1);
@@ -113,122 +139,100 @@ void run_task(int tasknum)
        while(pc != plen){
                printf("program: %x\n", program[pc]);
                switch(program[pc++]){
-               case BCNOP:
-                       trace("nop\n");
+               case BCNOP: trace("nop\n");
                        break;
-               case BCPUSH:
-                       trace("push %d\n", program[pc]);
+               case BCPUSH: trace("push %d\n", program[pc]);
                        stack[sp++] = program[pc++];
                        break;
-               case BCPOP:
-                       trace("pop\n");
+               case BCPOP: trace("pop\n");
                        sp--;
                        break;
-               case BCNOT:
-                       trace("not\n");
+               case BCSDSSTORE: trace("sds store\n");
+                       break;
+               case BCSDSFETCH: trace("sds fetch\n");
+                       break;
+               case BCSDSPUBLISH: trace("sds publish\n");
+                       break;
+               case BCNOT: trace("not\n");
                        stack[sp] = stack[sp] > 0 ? 0 : 1;
                        break;
-               case BCADD:
-                       trace("add\n");
+               case BCADD: trace("add\n");
                        stack[sp-1] = stack[sp] + stack[sp-1];
                        sp -= 1;
                        break;
-               case BCSUB:
-                       trace("sub\n");
+               case BCSUB: trace("sub\n");
                        stack[sp-1] = stack[sp] - stack[sp-1];
                        sp -= 1;
                        break;
-               case BCMUL:
-                       trace("mul\n");
+               case BCMUL: trace("mul\n");
                        stack[sp-1] = stack[sp] * stack[sp-1];
                        sp -= 1;
                        break;
-               case BCDIV:
-                       trace("div\n");
+               case BCDIV: trace("div\n");
                        stack[sp-1] = stack[sp] / stack[sp-1];
                        sp -= 1;
                        break;
-               case BCAND:
-                       trace("and\n");
+               case BCAND: trace("and\n");
                        stack[sp-1] = stack[sp] && stack[sp-1];
                        sp -= 1;
                        break;
-               case BCOR:
-                       trace("or\n");
+               case BCOR: trace("or\n");
                        stack[sp-1] = stack[sp] || stack[sp-1];
                        sp -= 1;
                        break;
-               case BCEQ:
-                       trace("eq\n");
+               case BCEQ: trace("eq\n");
                        stack[sp-1] = stack[sp] == stack[sp-1];
                        sp -= 1;
                        break;
-               case BCNEQ:
-                       trace("neq\n");
+               case BCNEQ: trace("neq\n");
                        stack[sp-1] = stack[sp] != stack[sp-1];
                        sp -= 1;
                        break;
-               case BCLES:
-                       trace("les\n");
+               case BCLES: trace("les\n");
                        stack[sp-1] = stack[sp] < stack[sp-1];
                        sp -= 1;
                        break;
-               case BCGRE:
-                       trace("gre\n");
+               case BCGRE: trace("gre\n");
                        stack[sp-1] = stack[sp] > stack[sp-1];
                        sp -= 1;
                        break;
-               case BCLEQ:
-                       trace("leq\n");
+               case BCLEQ: trace("leq\n");
                        stack[sp-1] = stack[sp] <= stack[sp-1];
                        sp -= 1;
                        break;
-               case BCGEQ:
-                       trace("geq\n");
+               case BCGEQ: trace("geq\n");
                        stack[sp-1] = stack[sp] >= stack[sp-1];
                        sp -= 1;
                        break;
-               case BCJMP:
-                       trace("jmp to %d\n", program[pc]);
+               case BCJMP: trace("jmp to %d\n", program[pc]);
                        pc = pc + program[pc];
                        break;
-               case BCJMPT:
-                       trace("jmpt to %d\n", program[pc]);
+               case BCJMPT: trace("jmpt to %d\n", program[pc]);
                        pc += stack[sp] ? program[pc] : 1;
                        break;
-               case BCJMPF:
-                       trace("jmpf to %d\n", program[pc]);
+               case BCJMPF: trace("jmpf to %d\n", program[pc]);
                        pc += stack[sp] ? 1 : program[pc];
                        break;
-               case BCSERIALAVAIL:
-                       trace("SerialAvailable()\n");
+               case BCSERIALAVAIL: trace("SerialAvailable()\n");
                        break;
-               case BCSERIALPRINT:
-                       trace("SerialPrint()\n");
+               case BCSERIALPRINT: trace("SerialPrint()\n");
                        break;
-               case BCSERIALPRINTLN:
-                       trace("SerialPrintln()\n");
+               case BCSERIALPRINTLN: trace("SerialPrintln()\n");
                        break;
-               case BCSERIALREAD:
-                       trace("SerialRead()\n");
+               case BCSERIALREAD: trace("SerialRead()\n");
                        break;
-               case BCSERIALPARSEINT:
-                       trace("SerialParseInt()\n");
+               case BCSERIALPARSEINT: trace("SerialParseInt()\n");
                        break;
-               case BCANALOGREAD:
-                       trace("AnalogRead(%d)\n", program[pc]);
+               case BCANALOGREAD: trace("AnalogRead(%d)\n", program[pc]);
                        pc++;
                        break;
-               case BCANALOGWRITE:
-                       trace("AnalogWrite(%d)\n", program[pc]);
+               case BCANALOGWRITE: trace("AnalogWrite(%d)\n", program[pc]);
                        pc++;
                        break;
-               case BCDIGITALREAD:
-                       trace("DigitalRead(%d)\n", program[pc]);
+               case BCDIGITALREAD: trace("DigitalRead(%d)\n", program[pc]);
                        pc++;
                        break;
-               case BCDIGITALWRITE:
-                       trace("DigitalWrite(%d)\n", program[pc]);
+               case BCDIGITALWRITE: trace("DigitalWrite(%d)\n", program[pc]);
                        pc++;
                        break;
                default:
@@ -302,16 +306,24 @@ int main(int argc, char *argv[])
 
        open_filedescriptors();
 
+       long cyclestart;
        while(true){
                //Check for new tasks
                if(input_available(fd))
                        read_message(fd, fd);
                //Run tasks
+               cyclestart = millis();
                for(ct = 0; ct<MAXTASKS; ct++){
+                       //See whether the task is even in use
                        if(!tasks[ct].used){
                                debug("Task %d not implemented\n", ct);
                                continue;
                        }
+                       //See whether the task interval has passed
+                       if(cyclestart-tasks[ct].lastrun < tasks[ct].interval){
+                               debug("Task %d not scheduled\n", ct);
+                               continue;
+                       }
 #ifdef DEBUG
                        printf("Current task to run: %d\n", ct);
                        printf("Enter to continue, s to step\n");
index c16a8bb..90e4b66 100644 (file)
@@ -3,31 +3,33 @@
 #define BCNOP 1
 #define BCPUSH 2
 #define BCPOP 3
-#define BCSDS 4
-#define BCNOT 5
-#define BCADD 6
-#define BCSUB 7
-#define BCMUL 8
-#define BCDIV 9
-#define BCAND 10
-#define BCOR 11
-#define BCEQ 12
-#define BCNEQ 13
-#define BCLES 14
-#define BCGRE 15
-#define BCLEQ 16
-#define BCGEQ 17
-#define BCJMP 18
-#define BCJMPT 19
-#define BCJMPF 20
-#define BCSERIALAVAIL 21
-#define BCSERIALPRINT 22
-#define BCSERIALPRINTLN 23
-#define BCSERIALREAD 24
-#define BCSERIALPARSEINT 25
-#define BCANALOGREAD 26
-#define BCANALOGWRITE 27
-#define BCDIGITALREAD 28
-#define BCDIGITALWRITE 29
-#define BCTEST 30
+#define BCSDSSTORE 4
+#define BCSDSFETCH 5
+#define BCSDSPUBLISH 6
+#define BCNOT 7
+#define BCADD 8
+#define BCSUB 9
+#define BCMUL 10
+#define BCDIV 11
+#define BCAND 12
+#define BCOR 13
+#define BCEQ 14
+#define BCNEQ 15
+#define BCLES 16
+#define BCGRE 17
+#define BCLEQ 18
+#define BCGEQ 19
+#define BCJMP 20
+#define BCJMPT 21
+#define BCJMPF 22
+#define BCSERIALAVAIL 23
+#define BCSERIALPRINT 24
+#define BCSERIALPRINTLN 25
+#define BCSERIALREAD 26
+#define BCSERIALPARSEINT 27
+#define BCANALOGREAD 28
+#define BCANALOGWRITE 29
+#define BCDIGITALREAD 30
+#define BCDIGITALWRITE 31
+#define BCTEST 32
 #endif
index a7b463d..0939ab4 100644 (file)
@@ -8,7 +8,10 @@ import mTask
        = BCNop
        | BCPush Int
        | BCPop
-       | BCSds Int
+       //SDS functions
+       | BCSdsStore Int
+       | BCSdsFetch Int
+       | BCSdsPublish Int
        //Unary ops
        | BCNot
        //Binary Int ops
index 275e8cc..fff57e4 100644 (file)
@@ -91,11 +91,11 @@ BCIfStmt b t e = b <+-> [BCJmpF $ length <$> t + 1] <++> t
 instance noOp ByteCode where noOp = BC []
 
 instance sds ByteCode where
-       sds f = {main = 
+       sds f = undef/*{main = 
                        let var = 42
                            (v In body) = f var
                        in unMain body
-               }
+               }*/
        con f = undef
 
 instance serial ByteCode where