From: Mart Lubbers Date: Thu, 12 Jan 2017 11:28:46 +0000 (+0100) Subject: add clean-platform X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=ddfd196fa4999b117701121a2f4cb1fe378f902d;p=mTask.git add clean-platform --- diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..98b81d8 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,4 @@ +[submodule "clean-platform"] + path = clean-platform + url = https://gitlab.science.ru.nl/clean-and-itasks/clean-platform.git + branch = patch-JSON diff --git a/Makefile b/Makefile index 915f396..6be8eb5 100644 --- a/Makefile +++ b/Makefile @@ -7,11 +7,11 @@ CLMLIBS:=\ -I $(CLEAN_HOME)/lib/iTasks-SDK/Patches/StdEnv\ -I $(CLEAN_HOME)/lib/iTasks-SDK/Patches/TCPIP\ -I $(CLEAN_HOME)/lib/iTasks-SDK/Server\ - -I $(CLEAN_HOME)/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent\ - -I $(CLEAN_HOME)/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent/Deprecated/StdLib\ - -I $(CLEAN_HOME)/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Posix\ - -I $(CLEAN_HOME)/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Linux\ - -I $(CLEAN_HOME)/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Linux-64\ + -I ./clean-platform/src/libraries/OS-Independent\ + -I ./clean-platform/src/libraries/OS-Independent/Deprecated/StdLib\ + -I ./clean-platform/src/libraries/OS-Posix\ + -I ./clean-platform/src/libraries/OS-Linux\ + -I ./clean-platform/src/libraries/OS-Linux-64\ -I $(CLEAN_HOME)/lib/iTasks-SDK/Dependencies/graph_copy/linux64\ -I $(CLEAN_HOME)/lib/iTasks-SDK/Dependencies/graph_copy/common\ -I $(CLEAN_HOME)/lib/iTasks-SDK/Dependencies/SAPL\ diff --git a/clean-platform b/clean-platform new file mode 160000 index 0000000..e79aa3e --- /dev/null +++ b/clean-platform @@ -0,0 +1 @@ +Subproject commit e79aa3e3f1704c704efc67655e6588a185578de1 diff --git a/int/mTaskSymbols.h b/int/mTaskSymbols.h new file mode 100644 index 0000000..35b211a --- /dev/null +++ b/int/mTaskSymbols.h @@ -0,0 +1,36 @@ +#ifndef MTASK_H +#define MTASK_H +#define BCNOP 1 +#define BCLAB 2 +#define BCPUSH 3 +#define BCPOP 4 +#define BCSDSSTORE 5 +#define BCSDSFETCH 6 +#define BCSDSPUBLISH 7 +#define BCNOT 8 +#define BCADD 9 +#define BCSUB 10 +#define BCMUL 11 +#define BCDIV 12 +#define BCAND 13 +#define BCOR 14 +#define BCEQ 15 +#define BCNEQ 16 +#define BCLES 17 +#define BCGRE 18 +#define BCLEQ 19 +#define BCGEQ 20 +#define BCJMP 21 +#define BCJMPT 22 +#define BCJMPF 23 +#define BCSERIALAVAIL 24 +#define BCSERIALPRINT 25 +#define BCSERIALPRINTLN 26 +#define BCSERIALREAD 27 +#define BCSERIALPARSEINT 28 +#define BCANALOGREAD 29 +#define BCANALOGWRITE 30 +#define BCDIGITALREAD 31 +#define BCDIGITALWRITE 32 +#define BCTEST 33 +#endif diff --git a/int/nucleo-f767-blinky/src/Makefile b/int/nucleo-f767-blinky/src/Makefile index 6ebad9d..72506e8 100644 --- a/int/nucleo-f767-blinky/src/Makefile +++ b/int/nucleo-f767-blinky/src/Makefile @@ -1,6 +1,6 @@ CFLAGS:=-g -Wall -Wextra -Werror -DDEBUG PROG:=main -OBJS:=interpret.o sds.o task.o main.o +OBJS:=interpret.o sds.o task.o main.o interface.o all: mTaskSymbols.h $(PROG) diff --git a/int/nucleo-f767-blinky/src/interface.c b/int/nucleo-f767-blinky/src/interface.c new file mode 100644 index 0000000..2f7fb57 --- /dev/null +++ b/int/nucleo-f767-blinky/src/interface.c @@ -0,0 +1,172 @@ +#include +#include +#include +#include + +#ifdef STM32F767xx +#include "stm32f7xx_hal.h" +#include "gpio.h" +#include "usart.h" +#else +#include +#include +#include +#include +#include +#include +#include +#include +#endif + +#include "interface.h" + +#define SET_LED_RED GPIOB->BSRR = GPIO_PIN_14 +#define RESET_LED_RED GPIOB->BSRR = GPIO_PIN_14 << 16 + +#define SET_LED_BLUE GPIOB->BSRR = GPIO_PIN_7 +#define RESET_LED_BLUE GPIOB->BSRR = GPIO_PIN_7 << 16 + +#define SET_LED_GREEN GPIOB->BSRR = GPIO_PIN_0 +#define RESET_LED_GREEN GPIOB->BSRR = GPIO_PIN_0 << 16 + +//Globals +#ifdef STM32F767xx +volatile char uartf = 0; +#else +struct timeval tv1; +int sock_fd = -1; +int fd = -1; +int gargc; +char **gargv; +#endif +uint8_t bt; + +//Specifics +#ifdef STM32F767xx +void _exit(int i){ + while(1); + (void)i; +} + +void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle) +{ + uartf=1; +} +#else +void killHandler(int i) +{ + printf("%i caught, Bye...\n", i); + exit(1); +} + +void usage(FILE *o, char *arg0){ + fprintf(o, "Usage: %s [opts]\n\nOptions\n" + "-p PORT Custom port number, default: 8123\n" , arg0); +} +#endif + +//Interface things +long millis() { +#ifdef STM32F767xx + return HAL_GetTick(); +#else + if (gettimeofday(&tv1, NULL) == -1) + pdie("gettimeofday"); + return tv1.tv_sec*1000 + tv1.tv_usec/1000; +#endif +} + +bool input_available(){ +#ifdef STM32F767xx + return false; +#else + struct timeval tv; + fd_set fds; + tv.tv_sec = 0; + tv.tv_usec = 0; + FD_ZERO(&fds); + FD_SET(fd, &fds); + if (select(fd+1, &fds, NULL, NULL, &tv) == -1) + pdie("select"); + return FD_ISSET(fd, &fds); +#endif +} + +uint8_t read_byte() +{ +#ifdef STM32F767xx + HAL_UART_Receive(&huart3, &bt, 1, 1000); + return 0; +#else + read(fd, &bt, 1); + return bt; +#endif +} + +void write_byte(uint8_t b) +{ +#ifdef STM32F767xx + HAL_UART_Transmit_DMA(&huart3, &b, 1); +#else + write(fd, &b, 1); +#endif +} + +void delay(long ms) +{ +#ifdef STM32F767xx + HAL_Delay(ms); +#else + usleep(ms*1000); +#endif +} + +void setup() +{ +#ifdef STM32F767xx +#else + int port = 8123, opti = 1; + //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"); + } + //Command line arguments + while(opti < gargc){ + if(strcmp((*gargv)+opti, "-h") == 0){ + usage(stdout, gargv[0]); + exit(EXIT_SUCCESS); + } else if(strcmp(gargv[opti], "-p") == 0 && opti+1 1\n"); + } else { + usage(stderr, gargv[0]); + exit(EXIT_FAILURE); + } + opti++; + } + + //Open file descriptors + struct sockaddr_in sa; + + memset(&sa, 0, sizeof(sa)); + sa.sin_family = AF_INET; + sa.sin_addr.s_addr = INADDR_ANY; + sa.sin_port = htons(port); + + if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) + pdie("socket"); + if(bind(sock_fd, (struct sockaddr*)&sa, sizeof(sa)) == -1) + pdie("bind"); + if(listen(sock_fd, 10) == -1) + pdie("listen"); + + printf("Listening on %d\n", port); + fflush(stdout); + if((fd = accept(sock_fd, (struct sockaddr*)NULL, NULL)) == -1) + pdie("accept"); +#endif +} diff --git a/int/nucleo-f767-blinky/src/interface.h b/int/nucleo-f767-blinky/src/interface.h new file mode 100644 index 0000000..b9c223a --- /dev/null +++ b/int/nucleo-f767-blinky/src/interface.h @@ -0,0 +1,44 @@ +#ifndef MAIN_H +#define MAIN_H + +#include +#include + +#ifndef STM32F767xx +extern int gargc; +extern char **gargv; +#endif + +uint8_t read_byte(); +void write_byte(uint8_t b); + +void write_dpin(uint8_t i, bool b); +bool read_dpin(uint8_t i); + +void write_apin(uint8_t i, uint8_t a); +uint8_t read_apin(uint8_t i); + +long millis(); +bool input_available(); +void delay(long ms); + +void setup(); + +#define read16() 256*read_byte() + read_byte() +#ifdef STM32F767xx +#define debug(s, ...) ; +#define pdie(s) ; +#define die(s, ...) ; +#else + +#ifdef DEBUG +#define debug(s, ...) printf(s, ##__VA_ARGS__); +#else +#define debug(s, ...) ; +#endif + +#define pdie(s) {perror(s); exit(1);} +#define die(s, ...) {fprintf(stderr, s, ##__VA_ARGS__); exit(1);} +#endif + +#endif diff --git a/int/nucleo-f767-blinky/src/interpret.c b/int/nucleo-f767-blinky/src/interpret.c index 4238f44..bbfa351 100644 --- a/int/nucleo-f767-blinky/src/interpret.c +++ b/int/nucleo-f767-blinky/src/interpret.c @@ -7,10 +7,11 @@ #include "task.h" #include "sds.h" -#ifdef ARDUINO +#ifdef STM32F767xx #define trace(op, ...) ; #else -#define trace(op, ...) printf("pc: %d, sp: %d, op: " op, pc, sp, ##__VA_ARGS__); +#define trace(op, ...) ; +//printf("pc: %d, sp: %d, op: " op, pc, sp, ##__VA_ARGS__); #endif void run_task(struct task *t) @@ -22,10 +23,10 @@ void run_task(struct task *t) char stack[STACKSIZE] = {0}; printf("Running task with length: %d\n", plen); while(pc != plen){ - printf("program: %d\n", program[pc]); - printf("stack: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", - stack[0], stack[1], stack[2], stack[3], stack[4], - stack[5], stack[6], stack[7], stack[8], stack[9]); + //printf("program: %d\n", program[pc]); + //printf("stack: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", + // stack[0], stack[1], stack[2], stack[3], stack[4], + // stack[5], stack[6], stack[7], stack[8], stack[9]); switch(program[pc++]){ case BCNOP: trace("nop\n"); diff --git a/int/nucleo-f767-blinky/src/main.c b/int/nucleo-f767-blinky/src/main.c index 5ebac62..1a704b4 100644 --- a/int/nucleo-f767-blinky/src/main.c +++ b/int/nucleo-f767-blinky/src/main.c @@ -22,109 +22,13 @@ #include "mTaskSymbols.h" #include "sds.h" #include "task.h" -#include "misc.h" +#include "interface.h" #define MSG_GET_TASK 't' #define MSG_DEL_TASK 'd' #define MSG_SDS_SPEC 's' #define MSG_SDS_UPD 'u' -void _exit(int i){ - while(1); - (void)i; -} - -//Globals -#ifdef STM32F767xx -volatile char uartf = 0; -#else -struct timeval tv1; -int sock_fd = -1; -int fd = -1; -int *argc; -char **argv; -#endif -uint8_t bt; - -#define SET_LED_RED GPIOB->BSRR = GPIO_PIN_14 -#define RESET_LED_RED GPIOB->BSRR = GPIO_PIN_14 << 16 - -#define SET_LED_BLUE GPIOB->BSRR = GPIO_PIN_7 -#define RESET_LED_BLUE GPIOB->BSRR = GPIO_PIN_7 << 16 - -#define SET_LED_GREEN GPIOB->BSRR = GPIO_PIN_0 -#define RESET_LED_GREEN GPIOB->BSRR = GPIO_PIN_0 << 16 - -long millis() { -#ifdef STM32F767xx - return HAL_GetTick(); -#else - if (gettimeofday(&tv1, NULL) == -1) - pdie("gettimeofday"); - return tv1.tv_sec*1000 + tv1.tv_usec/1000; -#endif -} - -bool input_available(){ -#ifdef STM32F767xx - return false; -#else - struct timeval tv; - fd_set fds; - tv.tv_sec = 0; - tv.tv_usec = 0; - FD_ZERO(&fds); - FD_SET(fd, &fds); - if (select(fd+1, &fds, NULL, NULL, &tv) == -1) - pdie("select"); - return FD_ISSET(fd, &fds); -#endif -} - -uint8_t read_byte() -{ -#ifdef STM32F767xx - HAL_UART_Receive(&huart3, &bt, 1, 1000); - return 0; -#else - read(fd, &bt, 1); - return bt; -#endif -} - -void write_byte(uint8_t b) -{ -#ifdef STM32F767xx - HAL_UART_Transmit_DMA(&huart3, &b, 1); -#else - write(fd, &b, 1); -#endif -} - -void delay(int ms) -{ -#ifdef STM32F767xx - HAL_Delay(ms); -#else - usleep(ms*1000); -#endif -} - -#ifndef STM32F767xx -void killHandler(int i) -{ - printf("%i caught, Bye...\n", i); - exit(1); -} -#endif - -#ifdef STM32F767xx -void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle) -{ - uartf=1; -} -#endif - void read_message() { //Find next task @@ -155,72 +59,15 @@ void read_message() } } -void usage(FILE *o, char *arg0){ - fprintf(o, "Usage: %s [opts]\n\nOptions\n" - "-p PORT Custom port number, default: 8123\n" , arg0); -} - -void setup() -{ -#ifdef STM32F767xx -#else - int port = 8123, opti = 1; - //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"); - } - //Command line arguments - while(opti < *argc){ - if(strcmp((*argv)+opti, "-h") == 0){ - usage(stdout, argv[0]); - exit(EXIT_SUCCESS); - } else if(strcmp(argv[opti], "-p") == 0 && opti+1<*argc){ - port = atoi(argv[++opti]); - if(port < 1) - die("Port numbers are > 1\n"); - } else { - usage(stderr, argv[0]); - exit(EXIT_FAILURE); - } - opti++; - } - - //Open file descriptors - struct sockaddr_in sa; - - memset(&sa, 0, sizeof(sa)); - sa.sin_family = AF_INET; - sa.sin_addr.s_addr = INADDR_ANY; - sa.sin_port = htons(port); - - if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) - pdie("socket"); - if(bind(sock_fd, (struct sockaddr*)&sa, sizeof(sa)) == -1) - pdie("bind"); - if(listen(sock_fd, 10) == -1) - pdie("listen"); - - printf("Listening on %d\n", port); - fflush(stdout); - if((fd = accept(sock_fd, (struct sockaddr*)NULL, NULL)) == -1) - pdie("accept"); -#endif - - //Initialize systems - sds_init(); - task_init(); -} - void loop() { int ct; long cyclestart; struct task *curtask; + if(input_available()) read_message(); + //Run tasks cyclestart = millis(); for(ct = 0; ctlastrun = cyclestart; } write_byte('\n'); } #ifdef STM32F767xx -int main1(void) +int main1(void){ #else -int main(int ac, char *av[]) -#endif -{ -#ifndef STM32F767xx - argc = ∾ - argv = av; +int main(int argc, char *argv[]){ + gargc = argc; + gargv = argv; #endif + //Initialize systems setup(); + sds_init(); + task_init(); write_byte('\n'); while(true){ //Check for new tasks loop(); - debug("Waiting for 500ms\n"); - delay(500); - debug("done waiting\n"); - delay(500); + delay(10); } return 0; } diff --git a/int/nucleo-f767-blinky/src/main.h b/int/nucleo-f767-blinky/src/main.h deleted file mode 100644 index 02c9b26..0000000 --- a/int/nucleo-f767-blinky/src/main.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef MAIN_H -#define MAIN_H - -#include - -uint8_t read_byte(); -void write_byte(uint8_t b); -#endif diff --git a/int/nucleo-f767-blinky/src/misc.h b/int/nucleo-f767-blinky/src/misc.h index 5f188f4..198dcc4 100644 --- a/int/nucleo-f767-blinky/src/misc.h +++ b/int/nucleo-f767-blinky/src/misc.h @@ -1,6 +1,6 @@ #ifndef MISC_H #define MISC_H -#include "main.h" +#include "interface.h" #define read16() 256*read_byte() + read_byte() #ifdef STM32F767xx diff --git a/int/nucleo-f767-blinky/src/sds.c b/int/nucleo-f767-blinky/src/sds.c index 4751284..8e3d2f6 100644 --- a/int/nucleo-f767-blinky/src/sds.c +++ b/int/nucleo-f767-blinky/src/sds.c @@ -6,7 +6,7 @@ #include #endif -#include "main.h" +#include "interface.h" #include "interpret.h" #include "misc.h" #include "sds.h" @@ -43,7 +43,7 @@ bool sds_update() { uint8_t cs, id; //Read identifier - id = read_byte(); + id = read16(); for(cs = 0; cs String -decode :: String -> MTaskMessage +instance toString MTaskMSGRecv +instance toString MTaskMSGSend +encode :: MTaskMSGSend -> String +decode :: String -> MTaskMSGRecv :: BC = BCNop @@ -82,10 +86,10 @@ instance arith ByteCode instance boolExpr ByteCode instance analogIO ByteCode instance digitalIO ByteCode -instance If ByteCode Stmt Stmt Stmt -instance If ByteCode e Stmt Stmt -instance If ByteCode Stmt e Stmt -instance If ByteCode x y Expr +//instance If ByteCode Stmt Stmt Stmt +//instance If ByteCode e Stmt Stmt +//instance If ByteCode Stmt e Stmt +instance If ByteCode x y Stmt instance IF ByteCode instance noOp ByteCode @@ -96,7 +100,8 @@ instance serial ByteCode //pub :: (ByteCode a b) -> ByteCode a b -toMessages :: Int (String, BCState) -> [MTaskMessage] +toMessages :: Int (String, BCState) -> ([MTaskMSGSend], BCState) +toSDSUpdate :: Int Int -> [MTaskMSGSend] toByteVal :: BC -> [Char] toReadableByteCode :: (ByteCode a b) -> (String, BCState) diff --git a/mTaskInterpret.icl b/mTaskInterpret.icl index 5876047..9685913 100644 --- a/mTaskInterpret.icl +++ b/mTaskInterpret.icl @@ -22,33 +22,33 @@ from Text import class Text(concat,join,toUpperCase), instance Text String import qualified Data.Map as DM import Text.Encodings.Base64 -encode :: MTaskMessage -> String +encode :: MTaskMSGSend -> String encode (MTSds i v) = "s" +++ to16bit i +++ v +++ "\n" encode (MTTask to data) = "t" +++ to16bit to +++ to16bit (size data) +++ data +++ "\n" -encode (MTPub i v) = "u" +++ to16bit i +++ v +++ "\n" encode (MTUpd i v) = "u" +++ to16bit i +++ v +++ "\n" -encode MTEmpty = "" -decode :: String -> MTaskMessage + +decode :: String -> MTaskMSGRecv decode x | size x == 0 = MTEmpty = case x.[0] of '\0' = MTEmpty - 'u' = MTUpd (from16bit (x % (1,3))) (x % (3,5)) + 'u' = MTPub (from16bit (x % (1,3))) (x % (3,5)) _ = abort ("Didn't understand message: " +++ join " " [toString (toInt c)\\c<-: x] +++ "\n") safePrint :== toString o toJSON -derive gPrint MTaskMessage -instance toString MTaskMessage where +instance toString MTaskMSGSend where toString (MTSds i v) = "Sds id: " +++ toString i +++ " value " +++ safePrint v toString (MTTask to data) = "Task timeout: " +++ toString to +++ " data " +++ safePrint data - toString (MTPub i v) = "Publish id: " +++ toString i - +++ " value " +++ safePrint v toString (MTUpd i v) = "Update id: " +++ toString i +++ " value " +++ safePrint v + +instance toString MTaskMSGRecv where + toString (MTPub i v) = "Publish id: " +++ toString i + +++ " value " +++ safePrint v toString MTEmpty = "Empty message" bclength :: BC -> Int @@ -68,7 +68,7 @@ bclength _ = 1 toByteVal :: BC -> [Char] toByteVal b -# bt = toChar $ consIndex{|*|} b +# bt = toChar $ consIndex{|*|} b + 1 = [bt:case b of (BCPush i) = i (BCLab i) = [toChar i] @@ -146,10 +146,10 @@ instance digitalIO ByteCode where digitalRead p = retrn [BCDigitalRead $ pin p] digitalWrite p b = b <+-> [BCDigitalWrite $ pin p] -instance If ByteCode Stmt Stmt Stmt where If b t e = BCIfStmt b t e -instance If ByteCode e Stmt Stmt where If b t e = BCIfStmt b t e -instance If ByteCode Stmt e Stmt where If b t e = BCIfStmt b t e -instance If ByteCode x y Expr where If b t e = BCIfStmt b t e +//instance If ByteCode Stmt Stmt Stmt where If b t e = BCIfStmt b t e +//instance If ByteCode e Stmt Stmt where If b t e = BCIfStmt b t e +//instance If ByteCode Stmt e Stmt where If b t e = BCIfStmt b t e +instance If ByteCode x y Stmt where If b t e = BCIfStmt b t e instance IF ByteCode where IF b t e = BCIfStmt b t e (?) b t = BCIfStmt b t $ retrn [] @@ -243,8 +243,11 @@ toReadableByteCode x // where // bc :: ByteCode Int Expr // bc = (lit 36 +. lit 42) +. lit 44 -toMessages :: Int (String, BCState) -> [MTaskMessage] -toMessages interval (bytes, {sdss}) = [MTSds i (toString b)\\(i,b)<-sdss] ++ [MTTask interval bytes] +toMessages :: Int (String, BCState) -> ([MTaskMSGSend], BCState) +toMessages interval (bytes, st=:{sdss}) = ([MTSds i (toString b)\\(i,b)<-sdss] ++ [MTTask interval bytes], st) + +toSDSUpdate :: Int Int -> [MTaskMSGSend] +toSDSUpdate i v = [MTUpd i (to16bit v)] Start = toMessages 500 $ toRealByteCode (unMain bc) //Start = fst $ toReadableByteCode $ unMain bc diff --git a/miTask.icl b/miTask.icl index 62b6ce1..e5483cc 100644 --- a/miTask.icl +++ b/miTask.icl @@ -7,44 +7,106 @@ from Text import class Text(concat,join,split), instance Text String import iTasks import mTask -derive class iTask MTaskMessage +derive class iTask MTaskMSGRecv, MTaskMSGSend Start :: *World -> *World Start world = startEngine ( enterInformation "Port Number?" [] >>= \port->withShared ([], False, [], False) (mTaskTask port) ) world -Start world = startEngine mTaskTask world +//Start world = startEngine mTaskTask world -mTaskTask :: Int (Shared ([MTaskMessage],Bool,[MTaskMessage],Bool)) -> Task () +mTaskTask :: Int (Shared ([MTaskMSGRecv],Bool,[MTaskMSGSend],Bool)) -> Task () mTaskTask port ch = - syncNetworkChannel "localhost" port "\n" decode encode ch ||- + syncNetworkChannel "localhost" port "\n" decode encode` ch ||- + sendMsg msgs ch ||- ( - sendMsg msgs ch >>= \_-> - viewSharedInformation "channels" [ViewWith lens] ch @! () - ) >>* [OnAction ActionFinish (always shutDown)] + ( + consumeNetworkStream (processSDSs sdsShares messageShare) ch ||- + viewSharedInformation "channels" [ViewWith lens] ch ||- + viewSh sdsShares ch + ) >>* [OnAction ActionFinish (always shutDown)] + ) where - lens :: ([MTaskMessage],Bool,[MTaskMessage],Bool) -> ([String], [String]) - lens (r,_,s,_) = (f r, f s) + encode` m + | not (trace_tn (toString (toJSON m))) = undef + = encode m + + messageShare :: Shared [String] + messageShare = sharedStore "mTaskMessagesRecv" [] + + processSDSs :: [(Int, Shared Int)] (Shared [String]) [MTaskMSGRecv] -> Task () + processSDSs _ _ [] = return () + processSDSs s y [x:xs] = updateSDSs s y x >>= \_->processSDSs s y xs + + updateSDSs :: [(Int, Shared Int)] (Shared [String]) MTaskMSGRecv -> Task () + updateSDSs _ m (MTMessage s) = upd (\l->[s:l]) m @! () + updateSDSs _ _ MTEmpty = return () + updateSDSs [(id, sh):xs] m n=:(MTPub i d) + | id == i = set ((toInt d.[0])*265 + toInt d.[1]) sh @! () + = updateSDSs xs m n + + lens :: ([MTaskMSGRecv],Bool,[MTaskMSGSend],Bool) -> ([String], [String]) + lens (r,_,s,_) = (f r, map toString s) where f [] = [] f [MTEmpty:xs] = f xs f [x:xs] = [toString x:f xs] - msgs - | not (trace_tn (fst (toReadableByteCode (unMain bc)))) = undef - = toMessages 500 (toRealByteCode (unMain bc)) + viewSh :: [(Int, Shared Int)] (Shared ([MTaskMSGRecv],Bool,[MTaskMSGSend],Bool)) -> Task () + viewSh [] ch = return () + viewSh [(i, sh):xs] ch + # sharename = "SDS-" +++ toString i + = ( + viewSharedInformation ("SDS-" +++ toString i) [] sh ||- + forever ( + enterInformation sharename [] + >>* [OnAction ActionOk + (ifValue (\j->j>=1 && j <= 3) + (\c->set c sh + >>= \_->sendMsg (toSDSUpdate i c) ch + @! () + ) + )] + ) + ) ||- viewSh xs ch + + sdsShares = makeShares st - bc :: Main (ByteCode Int Stmt) - bc = sds \x=1 In {main = - If (x ==. lit 3) - (x =. lit 1) - (x =. x +. lit 1) :. pub x} + (msgs, st) = toMessages 500 (toRealByteCode (unMain bc)) -sendMsg :: [MTaskMessage] (Shared ([MTaskMessage],Bool,[MTaskMessage],Bool)) -> Task () -sendMsg m ch -| not (trace_tn (join "\n" (map (toString o toJSON) m))) = undef -= upd (\(r,rs,s,ss)->(r,rs,s ++ m,ss)) ch @! () + bc :: Main (ByteCode () Stmt) + bc = sds \x=1 In sds \pinnetje=1 In {main = + x =. x +. lit 1 :. + pub x :. + IF (pinnetje ==. lit 1) ( + analogWrite A0 (lit 1) :. + analogWrite A1 (lit 0) :. + analogWrite A2 (lit 0) + ) ( + IF (pinnetje ==. lit 2) ( + analogWrite A0 (lit 0) :. + analogWrite A1 (lit 1) :. + analogWrite A2 (lit 0) + ) ( + analogWrite A0 (lit 0):. + analogWrite A1 (lit 0):. + analogWrite A2 (lit 1) + ) + )} +// bc :: Main (ByteCode Int Stmt) +// bc = sds \x=1 In {main = +// If (x ==. lit 3) +// (x =. lit 1) +// (x =. x +. lit 1) :. pub x} + +makeShares :: BCState -> [(Int, Shared Int)] +makeShares {sdss=[]} = [] +makeShares s=:{sdss=[(i,d):xs]} = + [(i, sharedStore ("mTaskSDS-" +++ toString i) 0):makeShares {s & sdss=xs}] + +sendMsg :: [MTaskMSGSend] (Shared ([MTaskMSGRecv],Bool,[MTaskMSGSend],Bool)) -> Task () +sendMsg m ch = upd (\(r,rs,s,ss)->(r,rs,s ++ m,ss)) ch @! () syncNetworkChannel :: String Int String (String -> m) (n -> String) (Shared ([m],Bool,[n],Bool)) -> Task () | iTask m & iTask n syncNetworkChannel server port msgSeparator decodeFun encodeFun channel @@ -64,17 +126,17 @@ syncNetworkChannel server port msgSeparator decodeFun encodeFun channel onDisconnect l (received,receiveStopped,send,sendStopped) = (Ok l,Just (received,True,send,sendStopped)) -//consumeNetworkStream :: ([m] -> Task ()) (Shared ([m],Bool,[n],Bool)) -> Task () | iTask m & iTask n -//consumeNetworkStream processTask channel -// = ((watch channel >>* [OnValue (ifValue ifProcess process)]) >| if (isEmpty received) (return ()) (processTask received) -// @! receiveStopped -// -// empty :: ([m],Bool,[m],Bool) -> ([m],Bool,[m],Bool) -// empty (_,rs,s,ss) = ([],rs,s,ss) +consumeNetworkStream :: ([m] -> Task ()) (Shared ([m],Bool,[n],Bool)) -> Task () | iTask m & iTask n +consumeNetworkStream processTask channel + = ((watch channel >>* [OnValue (ifValue ifProcess process)]) >| if (isEmpty received) (return ()) (processTask received) + @! receiveStopped + + empty :: ([m],Bool,[n],Bool) -> ([m],Bool,[n],Bool) + empty (_,rs,s,ss) = ([],rs,s,ss)