+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
+ where
+ eval event evalOpts tree=:(TCInit taskId ts) iworld=:{IWorld|world}
+ = case TTYopen dev opts world of
+ (False, _, world)
+ # (err, world) = TTYerror world
+ = (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
+ (Error e, iworld) = (ExceptionResult (exception "h"), iworld)
+ (Ok _, iworld) = (ValueResult NoValue {TaskEvalInfo|lastEvent=ts,removedTasks=[],refreshSensitive=True} NoRep (TCBasic taskId ts JSONNull False), iworld)
+
+ eval _ _ tree=:(TCBasic _ ts _ _) iworld
+ = (ValueResult NoValue {TaskEvalInfo|lastEvent=ts,removedTasks=[],refreshSensitive=False} NoRep tree, iworld)
+
+ eval event evalOpts tree=:(TCDestroy _) iworld=:{IWorld|resources,world}
+ # (TTYd tty) = fromJust resources
+ # (ok, world) = TTYclose tty world
+ # iworld = {iworld & world=world,resources=Nothing}
+ = case removeBackgroundTask 42 iworld of
+ (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 en de iworld
+ = case read rw iworld of
+ (Error e, iworld) = iworld
+ (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)
+ # (l, tty) = appFst en (TTYreadline tty)
+ # iworld = {iworld & resources=Just (TTYd tty)}
+ = snd (write (r ++ [l],rs,[],ss) rw iworld)
+ where
+ writet :: [String] !*TTY -> *TTY
+ writet [] t = t
+ writet [x:xs] t = writet xs (TTYwrite t x)
+
+