not working again.
[mTask.git] / miTask.icl
index 89a3cd6..5df0450 100644 (file)
@@ -47,27 +47,24 @@ deviceSelectorSerial = accWorld getDevices
                                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 =
-//     deviceSelectorNetwork >>= \(p,h)->syncNetworkChannel h p "\n" decode encode` ch ||-
-       deviceSelectorSerial >>= \(s,set)->syncSerialChannel s set decode` encode` ch ||- 
-       sendMsg msgs ch ||-
+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)]
-       )
+               )
+       ) >>* [OnAction ActionFinish (always shutDown)]
        where
-               encode` m
-               | not (trace_tn (toString (toJSON m))) = undef
-               = encode m
-
-               decode` m
-               | not (trace_tn (toString (toJSON m))) = undef
-               = decode m
-
                messageShare :: Shared [String]
                messageShare = sharedStore "mTaskMessagesRecv" []
 
@@ -76,7 +73,7 @@ mTaskTask ch =
                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 _ m (MTMessage s) = upd (\l->take 20 [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 @! ()
@@ -108,39 +105,47 @@ mTaskTask ch =
                        ) ||- 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 =
-                               x =. x +. pinnetje :.
-                               pub x :.
+                               IF (digitalRead D3 ==. lit True) (
+                                       x =. x +. lit 1 :.
+                                       pub x
+                               ) (
+                                       noOp
+                               ) :.
                                IF (pinnetje ==. lit 1) (
-                                       analogWrite A0 (lit 1) :.
-                                       analogWrite A1 (lit 0) :.
-                                       analogWrite A2 (lit 0)
+                                       digitalWrite D0 (lit True) :.
+                                       digitalWrite D1 (lit False) :.
+                                       digitalWrite D2 (lit False)
                                ) (
                                        IF (pinnetje ==. lit 2) (
-                                               analogWrite A0 (lit 0) :.
-                                               analogWrite A1 (lit 1) :.
-                                               analogWrite A2 (lit 0)
+                                               digitalWrite D0 (lit False) :.
+                                               digitalWrite D1 (lit True) :.
+                                               digitalWrite D2 (lit False)
                                        ) (
-                                               analogWrite A0 (lit 0):.
-                                               analogWrite A1 (lit 0):.
-                                               analogWrite A2 (lit 1)
+                                               digitalWrite D0 (lit False) :.
+                                               digitalWrite D1 (lit False) :.
+                                               digitalWrite D2 (lit True)
                                        )
                                )}
-//             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) 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 @! ()
 
@@ -170,22 +175,27 @@ syncSerialChannel dev opts decodeFun encodeFun rw = Task eval
                        (Ok _, iworld) = (DestroyedResult, iworld)
 
 serialDeviceBackgroundTask :: (Shared ([m],Bool,[n],Bool)) (String -> m) (n -> String) !*IWorld -> *IWorld
-serialDeviceBackgroundTask rw en de iworld
+serialDeviceBackgroundTask rw de en iworld
        = case read rw iworld of
-               (Error e, iworld) = iworld
+               (Error e, iworld) = abort "share couldn't be read"
                (Ok (r,rs,s,ss), iworld)
                # (Just (TTYd tty)) = iworld.resources
-               # tty = writet (map de s) tty
-               = case TTYavailable tty of
-                       (False, tty) = {iworld & resources=Just (TTYd tty)}
+               # tty = writet (map en s) tty
+               # (ml, tty) = case TTYavailable tty of
+                       (False, tty) = ([], tty)
                        (_, tty)
-                       # (l, tty) = appFst en (TTYreadline tty)
-                       # iworld = {iworld & resources=Just (TTYd tty)}
-                       = snd (write (r ++ [l],rs,[],ss) rw iworld)
+                       # (l, tty) = TTYreadline tty
+                       = ([de l], tty)
+               # iworld = {iworld & resources=Just (TTYd tty)}
+               = case write (r++ml,rs,[],ss) rw iworld of
+                       (Error e, iworld) = abort "share couldn't be written"
+                       (Ok _, iworld) = case notify rw iworld of
+                               (Error e, iworld) = abort "share couldn't be notified"
+                               (Ok _, iworld) = iworld
        where
                writet :: [String] !*TTY -> *TTY
                writet [] t = t
-               writet [x:xs] t = writet xs (TTYwrite t x)
+               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
@@ -195,8 +205,10 @@ syncNetworkChannel server port msgSeparator decodeFun encodeFun channel
                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))