#define trace(op, ...) printf("pc: %d, sp: %d, op: " op "\n", pc, sp, ##__VA_ARGS__);
#endif
+#define f16(p) program[pc]*265+program[pc+1]
+
void run_task(struct task *t)
{
uint8_t *program = t->bc;
int sp = 0;
char stack[STACKSIZE] = {0};
debug("Running task with length: %d", plen);
- while(pc != plen){
-// debug("program: %d", program[pc]);
-// debug("stack: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
-// stack[0], stack[1], stack[2], stack[3], stack[4],
-// stack[5], stack[6], stack[7], stack[8], stack[9]);
+ while(pc < plen){
+ debug("program: %d", program[pc]);
+ debug("stack: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
+ 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");
pc++;
break;
case BCPUSH: trace("push %d", program[pc]*265+program[pc+1]);
- stack[sp++] = program[pc]*265 + program[pc+1];
+ stack[sp++] = f16(pc);
pc+=2;
break;
case BCPOP: trace("pop");
sp--;
break;
- case BCSDSSTORE: trace("sds store: %d", program[pc]);
- sds_store(program[pc++], stack[--sp]);
+ case BCSDSSTORE: trace("sds store: %d", f16(pc));
+ sds_store(f16(pc), stack[--sp]);
+ pc+=2;
break;
- case BCSDSFETCH: trace("sds fetch: %d", program[pc]);
- stack[sp++] = sds_fetch(program[pc++]);
+ case BCSDSFETCH: trace("sds fetch: %d", f16(pc));
+ stack[sp++] = sds_fetch(f16(pc));
+ pc+=2;
break;
- case BCSDSPUBLISH: trace("sds publish %d", program[pc]);
- sds_publish(program[pc++]);
+ case BCSDSPUBLISH: trace("sds publish %d", f16(pc));
+ sds_publish(f16(pc));
+ pc+=2;
break;
case BCNOT: trace("not");
stack[sp] = stack[sp] > 0 ? 0 : 1;
case BCJMPT: trace("jmpt to %d", program[pc]);
pc = stack[--sp] ? program[pc]-1 : pc+1;
break;
- case BCJMPF: trace("jmpf to %d", program[pc]);
+ case BCJMPF: trace("jmpf(%d) to %d", stack[sp-1], program[pc]);
pc = stack[--sp] ? pc+1 : program[pc]-1;
break;
case BCSERIALAVAIL: trace("SerialAvailable()");
import TTY
derive class iTask Queue, TTYSettings, Parity, BaudRate, ByteSize
-derive class iTask MTaskMSGRecv, MTaskMSGSend
+derive class iTask MTaskMSGRecv, MTaskMSGSend, SerTCP
+:: SerTCP = Serial | TCP
:: *Resource | TTYd !*TTY
Start :: *World -> *World
-Start world = startEngine (withShared ([], False, [], False) mTaskTask) world
+Start world = startEngine mTaskTask world
//Start world = startEngine mTaskTask world
-//
-deviceSelectorNetwork :: Task (Int, String)
-deviceSelectorNetwork = enterInformation "Port Number?" []
- -&&- enterInformation "Network address" []
-
-deviceSelectorSerial :: Task (String, TTYSettings)
-deviceSelectorSerial = accWorld getDevices
- >>= \dl->(enterChoice "Device" [] dl -&&- deviceSettings)
- where
- deviceSettings = updateInformation "Settings" [] zero
+bc :: Main (ByteCode () Stmt)
+bc = sds \x=1 In sds \pinnetje=1 In {main =
+ IF (digitalRead D3 ==. lit True) (
+ x =. x +. lit 1 :.
+ pub x
+ ) (
+ noOp
+ ) :.
+ IF (pinnetje ==. lit 1) (
+ digitalWrite D0 (lit True) :.
+ digitalWrite D1 (lit False) :.
+ digitalWrite D2 (lit False)
+ ) (
+ IF (pinnetje ==. lit 2) (
+ digitalWrite D0 (lit False) :.
+ digitalWrite D1 (lit True) :.
+ digitalWrite D2 (lit False)
+ ) (
+ digitalWrite D0 (lit False) :.
+ digitalWrite D1 (lit False) :.
+ digitalWrite D2 (lit True)
+ )
+ )}
+
+deviceSelector :: (Shared ([MTaskMSGRecv],[MTaskMSGSend],Bool)) -> Task ()
+deviceSelector ch = enterInformation "Type" []
+ >>= \ty->case ty of
+ TCP = (enterInformation "Host" [] -&&- enterInformation "Port" [])
+ >>= \(port,host)->syncNetworkChannel host port ch
+// Serial = accWorld getDevices
+// >>= \dl->(enterChoice "Device" [] dl -&&- enterInformation "Settings" [])
+// >>= \(dev,set)->syncSerialChannel dev set decode encode ch
+ where
getDevices :: !*World -> *(![String], !*World)
getDevices w = case readDirectory "/dev" w of
(Error (errcode, errmsg), w) = abort errmsg
isTTY s = not (isEmpty (filter (flip startsWith s) prefixes))
prefixes = ["ttyS", "ttyACM", "ttyUSB", "tty.usbserial"]
-derive class iTask SerTCP
-:: SerTCP = Serial | TCP
-mTaskTask :: (Shared ([MTaskMSGRecv],Bool,[MTaskMSGSend],Bool)) -> Task ()
-mTaskTask ch =
- (enterInformation "Choose" [] >>= \st->case st of
- Serial = deviceSelectorSerial >>= \(s,set)->syncSerialChannel s set decode encode ch
- TCP = deviceSelectorNetwork >>= \(p,h)->syncNetworkChannel h p "\n" decode encode ch
- ) ||-
- (
- sendMsg msgs ch >>= \_->(
- consumeNetworkStream (processSDSs sdsShares messageShare) ch ||-
- viewSharedInformation "channels" [ViewWith lens] ch ||-
- viewSharedInformation "messages" [] messageShare ||-
- viewSh sdsShares ch
- )
- ) >>* [OnAction ActionFinish (always shutDown)]
+mTaskTask :: Task ()
+mTaskTask = let (msgs, sdsShares) = makeMsgs 500 bc in
+ withShared ([], msgs, False) (\ch->
+ deviceSelector ch
+// ||- sendTasks msgs ch//sendMsg msgs ch
+// ||- (whileUnchanged ch (process sdsShares messageShare ch))
+ ||- viewSharedInformation "channels" [ViewWith lens] ch
+ ||- viewSharedInformation "messages" [] messageShare
+ ||- viewSh sdsShares ch
+ >>* [OnAction ActionFinish (always shutDown)]
+ )
where
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
+ makeMsgs :: Int (Main (ByteCode () Stmt)) -> ([MTaskMSGSend], [(Int, Shared Int)])
+ makeMsgs timeout bc
+ # (msgs, st) = toMessages timeout (toRealByteCode (unMain bc))
+ = (msgs, map f st.sdss)
+ where
+ f (i,d) = (i, sharedStore ("mTaskSDS-" +++ toString i) 0)
updateSDSs :: [(Int, Shared Int)] (Shared [String]) MTaskMSGRecv -> Task ()
updateSDSs _ m (MTMessage s) = upd (\l->take 20 [s:l]) m @! ()
| 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]
+ lens :: ([MTaskMSGRecv], [MTaskMSGSend],Bool) -> ([String], [String])
+ lens (r,s,_) = (map toString r, map toString s)
- viewSh :: [(Int, Shared Int)] (Shared ([MTaskMSGRecv],Bool,[MTaskMSGSend],Bool)) -> Task ()
+ viewSh :: [(Int, Shared Int)] (Shared ([MTaskMSGRecv],[MTaskMSGSend],Bool)) -> Task ()
viewSh [] ch = return ()
viewSh [(i, sh):xs] ch
# sharename = "SDS-" +++ toString i
)
) ||- viewSh xs ch
- sdsShares = makeShares st
- (msgs, st) = toMessages 1000 (toRealByteCode (unMain bc))
-
- bc :: Main (ByteCode () Stmt)
- bc = sds \x=1 In sds \pinnetje=1 In {main =
- IF (digitalRead D3 ==. lit True) (
- x =. x +. lit 1 :.
- pub x
- ) (
- noOp
- ) :.
- IF (pinnetje ==. lit 1) (
- digitalWrite D0 (lit True) :.
- digitalWrite D1 (lit False) :.
- digitalWrite D2 (lit False)
- ) (
- IF (pinnetje ==. lit 2) (
- digitalWrite D0 (lit False) :.
- digitalWrite D1 (lit True) :.
- digitalWrite D2 (lit False)
- ) (
- digitalWrite D0 (lit False) :.
- digitalWrite D1 (lit False) :.
- digitalWrite D2 (lit True)
- )
- )}
-
-makeShares :: BCState -> [(Int, Shared Int)]
-makeShares {sdss=[]} = []
-makeShares s=:{sdss=[(i,d):xs]} =
- [(i, sharedStore ("mTaskSDS-" +++ toString i) 1):makeShares {s & sdss=xs}]
-
-//makeBytecode :: Int (Main (ByteCode () Stmt)) -> ([MTaskMSGSend], [(Int, Shared Int)])
-//makeBytecode timeout bc
-//# (msgs, st) = toMessages timeout (toRealByteCode (unMain bc))
-//# shares = map (\(i,d)->(i, sharedStore (s i) (dd d))) st.sdss
-//= (msgs, shares)
-// where
-// s i = "mTaskSDS-" +++ toString i
-// dd [x,y] = (toInt x)*265+(toInt y)
-
-
-sendMsg :: [MTaskMSGSend] (Shared ([MTaskMSGRecv],Bool,[MTaskMSGSend],Bool)) -> Task ()
-sendMsg m ch = upd (\(r,rs,s,ss)->(r,rs,s ++ m,ss)) ch @! ()
+sendMsg :: [MTaskMSGSend] (Shared ([MTaskMSGRecv],[MTaskMSGSend],Bool)) -> Task ()
+sendMsg m ch = upd (\(r,s,ss)->(r,s ++ m,True)) ch @! ()
syncSerialChannel :: String TTYSettings (String -> m) (n -> String) (Shared ([m],Bool,[n],Bool)) -> Task () | iTask m & iTask n
syncSerialChannel dev opts decodeFun encodeFun rw = Task eval
# (TTYd tty) = fromJust resources
# (ok, world) = TTYclose tty world
# iworld = {iworld & world=world,resources=Nothing}
- = case removeBackgroundTask 42 iworld of
+ = case removeBackgroundTask 42 iworld of
(Error e, iworld) = (ExceptionResult (exception "h"), iworld)
(Ok _, iworld) = (DestroyedResult, iworld)
writet [x:xs] t = writet xs (TTYwrite x t)
-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
+syncNetworkChannel :: String Int (Shared ([MTaskMSGRecv], [MTaskMSGSend], Bool)) -> Task ()
+syncNetworkChannel server port channel
= tcpconnect server port channel {ConnectionHandlers|onConnect=onConnect,whileConnected=whileConnected,onDisconnect=onDisconnect} @! ()
where
- onConnect _ (received,receiveStopped,send,sendStopped)
- = (Ok "",if (not (isEmpty send)) (Just (received,False,[],sendStopped)) Nothing, map encodeFun send,False)
- whileConnected Nothing acc (received,receiveStopped,send,sendStopped)
- | not (trace_tn "whilec nothing") = undef
- = (Ok acc, Nothing, [], False)
- whileConnected (Just newData) acc (received,receiveStopped,send,sendStopped)
- | not (trace_tn "whilec just") = undef
- # [acc:msgs] = reverse (split msgSeparator (concat [acc,newData]))
- # write = if (not (isEmpty msgs && isEmpty send))
- (Just (received ++ map decodeFun (reverse msgs),receiveStopped,[],sendStopped))
- Nothing
- = (Ok acc,write,map encodeFun send,False)
-
- 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)]) <! id) @! ()
- where
- ifProcess (received,receiveStopped,_,_)
- = receiveStopped || (not (isEmpty received))
-
- process (received,receiveStopped,_,_)
- = upd empty channel
- >>| if (isEmpty received) (return ()) (processTask received)
- @! receiveStopped
+ onConnect :: String ([MTaskMSGRecv],[MTaskMSGSend],Bool) -> (MaybeErrorString String, Maybe ([MTaskMSGRecv],[MTaskMSGSend],Bool), [String], Bool)
+ onConnect _ (msgs,send,sendStopped)
+ = (Ok "", Just (msgs,[],sendStopped), map encode send, False)
+
+ whileConnected :: (Maybe String) String ([MTaskMSGRecv], [MTaskMSGSend],Bool) -> (MaybeErrorString String, Maybe ([MTaskMSGRecv],[MTaskMSGSend],Bool), [String], Bool)
+ whileConnected maybeNewData acc (msgs,send,sendStopped)
+ # l = map decode (maybeToList maybeNewData)
+ # send = if (not sendStopped) [] (map encode send)
+ = (Ok acc, Just (msgs ++ l,[],sendStopped), send, False)
- empty :: ([m],Bool,[n],Bool) -> ([m],Bool,[n],Bool)
- empty (_,rs,s,ss) = ([],rs,s,ss)
+ onDisconnect :: String ([MTaskMSGRecv],[MTaskMSGSend],Bool) -> (MaybeErrorString String, Maybe ([MTaskMSGRecv],[MTaskMSGSend],Bool))
+ onDisconnect l (msgs,send,sendStopped) = (Ok l, Nothing)