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
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)
noOp
) :.
IF (pinnetje ==. lit 1) (
- digitalWrite D0 (lit True)
+ ledOn LED1
) (
IF (pinnetje ==. lit 2) (
- digitalWrite D1 (lit True)
+ ledOn LED2
) (
- digitalWrite D2 (lit True)
+ ledOn LED3
)
)}
-bc2 :: DigitalPin -> Main (ByteCode () Stmt)
-bc2 d = {main = digitalWrite d (lit True) :. noOp}
-
-bc3 :: DigitalPin -> Main (ByteCode () Stmt)
-bc3 d = {main = digitalWrite d (lit False) :. noOp}
+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->
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->
sendMsg msgs ch
||- processMessages ch messageShare sdsShares
- ||- forever (enterChoice "Choose led to enable" [] [D0, D1, D2]
+ ||- forever (enterChoice "Choose led to enable" [] [LED1, LED2, LED3]
>>= \p->sendMsg (fst (makeMsgs 0 (bc2 p))) ch)
- ||- forever (enterChoice "Choose led to disable" [] [D0, D1, D2]
+ ||- forever (enterChoice "Choose led to disable" [] [LED1, LED2, LED3]
>>= \p->sendMsg (fst (makeMsgs 0 (bc3 p))) ch)
- ||- viewSharedInformation "channels" [ViewWith lens] ch
+ ||- viewSharedInformation "channels" [ViewAs lens] ch
||- viewSharedInformation "messages" [] messageShare
||- viewSh sdsShares ch
>>* [OnAction ActionFinish (always shutDown)]
| 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 20 [s:l]) m @! ()
- mta=:(MTTaskAdded _) = upd (\l->take 20 [toString mta:l]) m @! ()
+ 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])
# iworld = {iworld & world=world, resources=Just (TTYd tty)}
= 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
(Error e, iworld) = (ExceptionResult (exception "h"), iworld)
(Ok _, iworld) = (DestroyedResult, iworld)
-serialDeviceBackgroundTask :: (Shared ([MTaskMSGRecv],[MTaskMSGSend],Bool)) !*IWorld -> *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"
+ (Error e, iworld) = (Error $ exception "share couldn't be read", iworld)
(Ok (r,s,ss), iworld)
# (Just (TTYd tty)) = iworld.resources
# tty = writet (map encode s) tty
= ([decode l], tty)
# iworld = {iworld & resources=Just (TTYd tty)}
= case write (r++ml,[],False) rw iworld of
- (Error e, iworld) = abort "share couldn't be written"
+ (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 [] = id