iTasksTTY.icl 3.05 KB
Newer Older
1
2
implementation module iTasksTTY

Mart Lubbers's avatar
Mart Lubbers committed
3
4
import TTY

5
import StdList
Mart Lubbers's avatar
Mart Lubbers committed
6
from StdFunc import o, flip
7
8
9
10
import StdMisc
import StdString

import System.Directory
Mart Lubbers's avatar
Mart Lubbers committed
11
12
import iTasks
from Data.Func import $
13
14
from Text import class Text(startsWith), instance Text String

Mart Lubbers's avatar
Mart Lubbers committed
15
16
17
18
19
20
21
22
23
24
25
import iTasks.UI.Definition

import iTasks._Framework.TaskState
import iTasks._Framework.TaskServer
import iTasks._Framework.IWorld
import iTasks._Framework.Store

:: *Resource | TTYd !*TTY

derive class iTask TTYSettings, Parity, BaudRate, ByteSize

26
27
28
29
30
31
32
getTTYDevices :: !*env -> *(![String], !*env)
getTTYDevices 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"]
Mart Lubbers's avatar
Mart Lubbers committed
33

Mart Lubbers's avatar
Mart Lubbers committed
34
35
syncSerialChannel :: TTYSettings (b -> String) (String -> a) (Shared ([a],[b],Bool)) -> Task () | iTask a & iTask b
syncSerialChannel opts enc dec rw = Task eval
Mart Lubbers's avatar
Mart Lubbers committed
36
37
	where
		eval event evalOpts tree=:(TCInit taskId ts) iworld=:{IWorld|world}
Mart Lubbers's avatar
Mart Lubbers committed
38
		= case TTYopen opts world of
Mart Lubbers's avatar
Mart Lubbers committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
			(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)) iworld of
				(Error e, iworld) = (ExceptionResult (exception "h"), 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} NoChange 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 ([MTaskMSGRecv],[MTaskMSGSend],Bool)) !*IWorld -> (MaybeError TaskException (), *IWorld)
		serialDeviceBackgroundTask rw iworld
			= case read rw iworld of
				(Error e, iworld) = (Error $ exception "share couldn't be read", iworld)
				(Ok (r,s,ss), iworld)
				# (Just (TTYd tty)) = iworld.resources
				# tty = writet (map enc s) tty
				# (ml, tty) = case TTYavailable tty of
					(False, tty) = ([], tty)
					(_, tty)
					# (l, tty) = TTYreadline tty
					= ([dec l], tty)
				# iworld = {iworld & resources=Just (TTYd tty)}
				= 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) = (Error $ exception "share couldn't be notified", iworld)
						(Ok _, iworld) = (Ok (), iworld)
			where
				writet :: [String] -> (*TTY -> *TTY)
				writet [] = id
				writet [x:xs] = writet xs o TTYwrite x