TTY.icl 1.75 KB
Newer Older
Mart Lubbers's avatar
Mart Lubbers committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
implementation module TTY

import Data.Maybe

import StdFunc
import StdFile
import StdMisc
import StdBool
import StdString

import code from "tty."

:: *TTY :== Int

instance zero TTYSettings where
	zero = {TTYSettings |
		baudrate = B9600,
		bytesize = BytesizeEight,
		parity = ParityNone,
Mart Lubbers's avatar
Mart Lubbers committed
20
		stop2bits = False,
Mart Lubbers's avatar
Mart Lubbers committed
21
22
23
		xonxoff = False}

instance toInt BaudRate where
Mart Lubbers's avatar
Mart Lubbers committed
24
25
26
27
28
	toInt b = case b of
		B0 = 0; B50 = 1; B75 = 2; B110 = 3; B134 = 4; B150 = 5; B200 = 6
		B300 = 7; B600 = 8; B1200 = 9; B1800 = 10; B2400 = 11; B4800 = 12
		B9600 = 13; B19200 = 14; B38400 = 15; B57600 = 16; B115200 = 17
		B230400 = 18
Mart Lubbers's avatar
Mart Lubbers committed
29
30

instance toInt ByteSize where
Mart Lubbers's avatar
Mart Lubbers committed
31
32
	toInt b = case b of
		BytesizeFive = 0; BytesizeSix = 1; BytesizeSeven = 2; BytesizeEight = 3
Mart Lubbers's avatar
Mart Lubbers committed
33
34

instance toInt Parity where
Mart Lubbers's avatar
Mart Lubbers committed
35
36
37
	toInt p = case p of
		ParityNone = 0; ParityEven = 1; ParityOdd = 2; ParityMark = 3;
		ParitySpace = 4
Mart Lubbers's avatar
Mart Lubbers committed
38
39
40
41
42
43
44

TTYopen :: !String !TTYSettings !*env -> (!Bool, !*TTY, !*env)
TTYopen fn ts w = TTYopen2
	fn
	(toInt ts.baudrate)
	(toInt ts.bytesize)
	(toInt ts.parity)
Mart Lubbers's avatar
Mart Lubbers committed
45
	ts.stop2bits
Mart Lubbers's avatar
Mart Lubbers committed
46
47
48
	ts.xonxoff
	w
	where
Mart Lubbers's avatar
Mart Lubbers committed
49
		TTYopen2 :: !String !Int !Int !Int !Bool !Bool !*env -> (!Bool, !*TTY, !*env)
Mart Lubbers's avatar
Mart Lubbers committed
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
81
82
		TTYopen2 _ _ _ _ _ _ _ = code {
				ccall ttyopen "SIIIII:VII:A"
			}

TTYclose :: !*TTY !*env -> (!Bool, !*env)
TTYclose f w = code {
		ccall ttyclose "I:I:A"
	}

TTYerror :: !*env -> (!String, !*env)
TTYerror w = code {
		ccall ttyerror ":S:A"
	}

TTYreadc :: !*TTY -> (!Char, !*TTY)
TTYreadc w = code {
		ccall ttyreadc "I:VII"
	}

TTYreadline :: !*TTY -> (!String, !*TTY)
TTYreadline t = code {
		ccall ttyreadline "I:VSI"
}

Start :: *World -> (!String, *World)
Start w
#! (ok, tty, w) = TTYopen "/dev/ttyUSB0" zero w
| not ok = TTYerror w
#! (c, tty) = TTYreadline tty
#! (ok, w) = TTYclose tty w
| not ok = TTYerror w
#! (s, w) = TTYerror w
= (c, w)