X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;f=miTask.icl;h=e81f47d369ecf336b0ad9753e4621e1b9db3d76d;hb=184c730e7c9ab350be853218f5e1f9b8866531ce;hp=a698ddd537ba0cee3964edf7b6ea25223734929c;hpb=8c4fb11d9a590d3f9d361c36e0cb476568c00735;p=mTask.git diff --git a/miTask.icl b/miTask.icl index a698ddd..e81f47d 100644 --- a/miTask.icl +++ b/miTask.icl @@ -8,6 +8,7 @@ import mTask from Text import class Text(startsWith,concat,split,join), instance Text String +from Data.Func import $ import Data.Tuple import System.Directory @@ -21,47 +22,76 @@ import iTasks._Framework.Store import TTY derive class iTask Queue, TTYSettings, Parity, BaudRate, ByteSize -derive class iTask MTaskMSGRecv, MTaskMSGSend, SerTCP +derive class iTask MTaskMSGRecv, MTaskMSGSend, SerTCP, UserLED + +derive class iTask MTaskDevice :: SerTCP = Serial | TCP :: *Resource | TTYd !*TTY +:: MTaskDevice = SerialDevice String TTYSettings | TCPDevice String Int Start :: *World -> *World -Start world = startEngine mTaskTask world +Start world = startEngine (mTaskManager + >>* [OnAction (Action "Shutdown") (always $ shutDown)]) world //Start world = startEngine mTaskTask world bc :: Main (ByteCode () Stmt) bc = sds \x=1 In sds \pinnetje=1 In {main = - IF (digitalRead D3 ==. lit True) ( + IF (digitalRead D3) ( x =. x +. lit 1 :. pub x ) ( noOp ) :. IF (pinnetje ==. lit 1) ( - digitalWrite D0 (lit True) :. - digitalWrite D1 (lit False) :. - digitalWrite D2 (lit False) + ledOn LED1 ) ( IF (pinnetje ==. lit 2) ( - digitalWrite D0 (lit False) :. - digitalWrite D1 (lit True) :. - digitalWrite D2 (lit False) + ledOn LED2 ) ( - digitalWrite D0 (lit False) :. - digitalWrite D1 (lit False) :. - digitalWrite D2 (lit True) + ledOn LED3 ) )} -deviceSelector :: (Shared ([MTaskMSGRecv],[MTaskMSGSend],Bool)) -> Task () -deviceSelector ch = enterInformation "Type" [] +bc2 :: UserLED -> Main (ByteCode () Stmt) +bc2 d = {main = ledOn d} + +bc3 :: UserLED -> Main (ByteCode () Stmt) +bc3 d = {main = ledOff d} + +withDevice :: ((Shared ([MTaskMSGRecv],[MTaskMSGSend],Bool)) -> Task a) -> Task a | iTask a +withDevice t = withShared ([], [], False) \ch-> + enterInformation "Type" [] + >>= \ty->case ty of + TCP = (enterInformation "Host" [] -&&- enterInformation "Port" []) + >>= \(port,host)->t ch -|| syncNetworkChannel host port ch + Serial = accWorld getDevices + >>= \dl->(enterChoice "Device" [] dl -&&- updateInformation "Settings" [] zero) + >>= \(dev,set)->t ch -|| syncSerialChannel dev set ch + where + getDevices :: !*World -> *(![String], !*World) + getDevices w = case readDirectory "/dev" w of + (Error (errcode, errmsg), w) = abort errmsg + (Ok entries, w) = (map ((+++) "/dev/") (filter isTTY entries), w) + where + isTTY s = not (isEmpty (filter (flip startsWith s) prefixes)) + prefixes = ["ttyS", "ttyACM", "ttyUSB", "tty.usbserial"] + +mTaskManager :: Task () +mTaskManager = viewSharedInformation "Devices" [] deviceStore + ||- forever (addDevice >>= \d->upd (\l->[d:l]) deviceStore) @! () + where + deviceStore :: Shared [MTaskDevice] + deviceStore = sdsFocus "mTaskDevices" $ memoryStore "" (Just []) + +addDevice :: Task MTaskDevice +addDevice = enterInformation "Enter device 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 + >>= return o uncurry TCPDevice + Serial = accWorld getDevices + >>= \dl->(enterChoice "Device" [] dl -&&- updateInformation "Settings" [] zero) + >>= return o uncurry SerialDevice where getDevices :: !*World -> *(![String], !*World) getDevices w = case readDirectory "/dev" w of @@ -73,15 +103,17 @@ deviceSelector ch = enterInformation "Type" [] mTaskTask :: Task () mTaskTask = let (msgs, sdsShares) = makeMsgs 1000 bc in - withShared ([], msgs, False) (\ch-> - deviceSelector ch -// ||- sendTasks msgs ch//sendMsg msgs ch + withDevice \ch-> + sendMsg msgs ch ||- processMessages ch messageShare sdsShares - ||- viewSharedInformation "channels" [ViewWith lens] ch + ||- forever (enterChoice "Choose led to enable" [] [LED1, LED2, LED3] + >>= \p->sendMsg (fst (makeMsgs 0 (bc2 p))) ch) + ||- forever (enterChoice "Choose led to disable" [] [LED1, LED2, LED3] + >>= \p->sendMsg (fst (makeMsgs 0 (bc3 p))) ch) + ||- viewSharedInformation "channels" [ViewAs lens] ch ||- viewSharedInformation "messages" [] messageShare ||- viewSh sdsShares ch >>* [OnAction ActionFinish (always shutDown)] - ) where messageShare :: Shared [String] messageShare = sharedStore "mTaskMessagesRecv" [] @@ -99,14 +131,18 @@ mTaskTask = let (msgs, sdsShares) = makeMsgs 1000 bc in # (msgs, st) = toMessages timeout (toRealByteCode (unMain bc)) = (msgs, map f st.sdss) where - f (i,d) = (i, sharedStore ("mTaskSDS-" +++ toString i) 0) + f (i,d) = (i, sharedStore ("mTaskSDS-" +++ toString i) (dd d)) + dd [x,y] = toInt x*265 + toInt y updateSDSs :: [(Int, Shared Int)] (Shared [String]) MTaskMSGRecv -> Task () - 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 @! () = updateSDSs xs m n + updateSDSs _ m mtm = case mtm of + MTMessage s = upd (\l->take 5 [s:l]) m @! () + mta=:(MTTaskAck _) = upd (\l->take 5 [toString mta:l]) m @! () + //TODO other recv msgs + _ = return () lens :: ([MTaskMSGRecv], [MTaskMSGSend],Bool) -> ([String], [String]) lens (r,s,_) = (map toString r, map toString s) @@ -132,8 +168,8 @@ mTaskTask = let (msgs, sdsShares) = makeMsgs 1000 bc in 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 +syncSerialChannel :: String TTYSettings (Shared ([MTaskMSGRecv],[MTaskMSGSend],Bool)) -> Task () +syncSerialChannel dev opts rw = Task eval where eval event evalOpts tree=:(TCInit taskId ts) iworld=:{IWorld|world} = case TTYopen dev opts world of @@ -142,12 +178,12 @@ syncSerialChannel dev opts decodeFun encodeFun rw = Task eval = (ExceptionResult (exception err), {iworld & world=world}) (True, tty, world) # iworld = {iworld & world=world, resources=Just (TTYd tty)} - = case addBackgroundTask 42 (BackgroundTask (serialDeviceBackgroundTask rw decodeFun encodeFun)) iworld of + = case addBackgroundTask 42 (BackgroundTask (serialDeviceBackgroundTask rw)) iworld of (Error e, iworld) = (ExceptionResult (exception "h"), iworld) - (Ok _, iworld) = (ValueResult NoValue {TaskEvalInfo|lastEvent=ts,removedTasks=[],refreshSensitive=True} NoRep (TCBasic taskId ts JSONNull False), iworld) + (Ok _, iworld) = (ValueResult NoValue {TaskEvalInfo|lastEvent=ts,removedTasks=[],refreshSensitive=True} NoChange (TCBasic taskId ts JSONNull False), iworld) eval _ _ tree=:(TCBasic _ ts _ _) iworld - = (ValueResult NoValue {TaskEvalInfo|lastEvent=ts,removedTasks=[],refreshSensitive=False} NoRep tree, iworld) + = (ValueResult NoValue {TaskEvalInfo|lastEvent=ts,removedTasks=[],refreshSensitive=False} NoChange tree, iworld) eval event evalOpts tree=:(TCDestroy _) iworld=:{IWorld|resources,world} # (TTYd tty) = fromJust resources @@ -157,28 +193,28 @@ syncSerialChannel dev opts decodeFun encodeFun rw = Task eval (Error e, iworld) = (ExceptionResult (exception "h"), iworld) (Ok _, iworld) = (DestroyedResult, iworld) -serialDeviceBackgroundTask :: (Shared ([m],Bool,[n],Bool)) (String -> m) (n -> String) !*IWorld -> *IWorld -serialDeviceBackgroundTask rw de en iworld +serialDeviceBackgroundTask :: (Shared ([MTaskMSGRecv],[MTaskMSGSend],Bool)) !*IWorld -> (MaybeError TaskException (), *IWorld) +serialDeviceBackgroundTask rw iworld = case read rw iworld of - (Error e, iworld) = abort "share couldn't be read" - (Ok (r,rs,s,ss), iworld) + (Error e, iworld) = (Error $ exception "share couldn't be read", iworld) + (Ok (r,s,ss), iworld) # (Just (TTYd tty)) = iworld.resources - # tty = writet (map en s) tty + # tty = writet (map encode s) tty # (ml, tty) = case TTYavailable tty of (False, tty) = ([], tty) (_, tty) # (l, tty) = TTYreadline tty - = ([de l], tty) + = ([decode 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" + = case write (r++ml,[],False) rw iworld of + (Error e, iworld) = (Error $ exception "share couldn't be written", iworld) (Ok _, iworld) = case notify rw iworld of - (Error e, iworld) = abort "share couldn't be notified" - (Ok _, iworld) = iworld + (Error e, iworld) = (Error $ exception "share couldn't be notified", iworld) + (Ok _, iworld) = (Ok (), iworld) where - writet :: [String] !*TTY -> *TTY - writet [] t = t - writet [x:xs] t = writet xs (TTYwrite x t) + writet :: [String] -> (*TTY -> *TTY) + writet [] = id + writet [x:xs] = writet xs o TTYwrite x syncNetworkChannel :: String Int (Shared ([MTaskMSGRecv], [MTaskMSGSend], Bool)) -> Task ()