started with layout
[mTask.git] / miTask.icl
index 767cd98..151074e 100644 (file)
@@ -10,6 +10,7 @@ from Text import class Text(startsWith,concat,split,join), instance Text String
 
 from Data.Func import $
 import Data.Tuple
+import Data.List
 import System.Directory
 
 import iTasks.UI.Definition
@@ -24,11 +25,15 @@ import TTY
 derive class iTask Queue, TTYSettings, Parity, BaudRate, ByteSize
 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)
@@ -55,7 +60,6 @@ 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" []
@@ -74,6 +78,40 @@ withDevice t = withShared ([], [], False) \ch->
                                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)
+       ||- whileUnchanged deviceStore (\l->showTabbed l <<@ ArrangeWithTabs) <<@ ArrangeHorizontal
+       where
+               deviceStore :: Shared [MTaskDevice]
+               deviceStore = sdsFocus "mTaskDevices" $ memoryStore "" (Just [])
+
+               showTabbed :: [MTaskDevice] -> Task ()
+               showTabbed [] = viewInformation "" [] "No devices yet" @! ()
+               showTabbed [l:ls] = foldr (\e es->viewInformation "Dev" [] e ||- es) (viewInformation "Dev" [] l @! ()) ls
+
+               mapPar :: (a -> Task a) [a] -> Task ()
+               mapPar f l = foldr1 (\x y->f x ||- y) l <<@ ArrangeWithTabs @! ()
+               allAtOnce t = foldr1 (||-) t @! ()
+               //allAtOnce = (flip (@!) ()) o foldr1 (||-)
+
+addDevice :: Task MTaskDevice
+addDevice = enterInformation "Enter device type" []
+       >>= \ty->case ty of
+               TCP = (enterInformation "Host" [] -&&- enterInformation "Port" [])
+                       >>= 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
+                       (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"]
+
 mTaskTask :: Task ()
 mTaskTask = let (msgs, sdsShares) = makeMsgs 1000 bc in
        withDevice \ch->