xref: /inferno-os/appl/lib/irmpath.b (revision 37da2899f40661e3e9631e497da8dc59b971cbd0)
1# Driver for Mind Path IR50.
2
3implement Ir;
4
5include "sys.m";
6FD, Dir: import Sys;
7include "ir.m";
8
9sys: Sys;
10
11init(keys, pid: chan of int): int
12{
13	sys = load Sys Sys->PATH;
14
15	cfd := sys->open("#t/eia1ctl", sys->OWRITE);
16	if(cfd == nil)
17		return -1;
18	sys->fprint(cfd, "b1200");	# baud rate
19	sys->fprint(cfd, "d1");		# DTR on
20	sys->fprint(cfd, "r1");		# RTS on
21
22	dfd := sys->open("#t/eia1", sys->OREAD);
23	if(dfd == nil)
24		return -1;
25	cfd = nil;
26
27	spawn reader(keys, pid, dfd);
28	return 0;
29}
30
31reader(keys, pid: chan of int, dfd: ref FD)
32{
33	n: int;
34	dir: Dir;
35	button: int;
36
37	pid <-= sys->pctl(0,nil);
38	(n, dir) = sys->fstat(dfd);
39	if(n >= 0 && dir.length > 0) {
40		while(dir.length) {
41			n = sys->read(dfd, array[dir.length] of byte, dir.length);
42			if(n < 0)
43				break;
44			dir.length -= n;
45		}
46	}
47
48	for(;;) {
49		# Look for 2 consecutive characters that are the same.
50		if((button=getconsec(dfd,2)) < 0)
51			break;
52		case button {
53			'-' => n = Ir->Enter;
54			'+' => n = Ir->Rcl;
55			'1' => n = Ir->One;
56			'2' => n = Ir->Two;
57			'3' => n = Ir->Three;
58			'4' => n = Ir->ChanUP;	# page up
59			'5' => n = Ir->ChanDN;	# page down
60			'U' => continue;
61			'R' =>
62				if((button=getconsec(dfd,2)) < 0)
63					break;
64				case button {
65					'a' or 'e' or 'i' or 'p' =>
66						n = Ir->Up;
67					'b' or 'f' or 'j' or 'k' =>
68						n = Ir->FF;	# right
69					'c' or 'g' or 'l' or 'm' =>
70						n = Ir->Dn;
71					'd' or 'h' or 'n' or 'o' =>
72						n = Ir->Rew;	# left
73					'Z' => n = Ir->Select;
74					* =>	;
75				}
76			* =>	;
77		}
78		keys <-= n;	# Send translated key over channel
79		# Read through to trailer before looking for another key press
80		while((button=getconsec(dfd,2)) != 'U') {
81			if(button <= 0)
82				break;
83		}
84	}
85	keys <-= Ir->Error;
86}
87
88translate(c: int): int
89{
90	return c;
91}
92
93# Gets 'count' consecutive occurrences of a byte.
94getconsec(dfd: ref FD, count: int): int
95{
96	b1:= array[1] of byte;
97	b2:= array[1] of byte;
98
99	n := sys->read(dfd, b1, 1);
100	if(n <= 0) {
101		if(n==0)
102			n = -1;
103		return n;
104	}
105	for(sofar:=1; sofar < count; sofar++) {
106		n = sys->read(dfd, b2, 1);
107		if(n <= 0) {
108			if(n==0)
109				n = -1;
110			return n;
111		}
112		if(b1[0]!=b2[0]) {
113			sofar = 1;
114			b1[0] = b2[0];
115		}
116	}
117	return int b1[0];
118}
119