-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathusbte.retro.html
259 lines (231 loc) · 8.55 KB
/
usbte.retro.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<!DOCTYPE html>
<html lang="en">
<!-- StickOS® BASIC -->
<!-- See: https://wicg.github.io/webusb/ -->
<!-- See: https://wicg.github.io/serial/ -->
<!-- top right bottom left -->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=1870">
<title>Retro</title>
<style>
pre { white-space: pre-wrap; word-wrap: break-word; }
a { text-decoration: none; }
#results { line-height: 1.2; }
#command { width: 96%; font-size:120%; }
#copyright { font-size:65%; padding: 0px; margin: 0px; }
* { overflow-wrap: anywhere; word-wrap: anywhere; }
button { overflow-wrap: normal; word-wrap: normal; }
html,button,input,select { font: 34px Arial, Helvetica, sans-serif; padding: 10px 10px 10px 10px; margin: 0px 0px 10px 0px; }
@media(min-width:80em){ html,button,input { font: 22px Arial, Helvetica, sans-serif; padding: 10px 10px 10px 10px; margin: 0px 0px 10px 0px; } }
@supports(-webkit-touch-callout:none) { button,input { -webkit-appearance: none; border-radius: 2px; } }
body,html { margin: 0px; }
</style>
</head>
<body onload='Body()'>
<h1>Retro</h1>
<div id="config">
Select:
<button type='button' onclick="Comm();">Connect COMM</button>
</div>
<pre id='results'></pre>
<input id='command' name='command' disabled='true' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false'/>
<br>
<button type='button' onclick='Enter();'>Enter</button>
<button type='button' onclick='Send(String.fromCharCode(3));'>Ctrl-C</button>
<button type='button' onclick='Send(String.fromCharCode(4));'>Ctrl-D</button>
<button type='button' onclick='Paste();'>Paste</button>
<button type='button' onclick='Clear();'>Clear</button>
<p id='copyright'>
<a href=https://rtestardi.github.io/StickOS target=_blank>StickOS® website</a>
<a href=https://github.com/rtestardi/usbte target=_blank>github.com/rtestardi/usbte</a>
<a href=https://github.com/rtestardi/StickOS2 target=_blank>github.com/rtestardi/StickOS2</a>
<script>
'use strict';
var usb;
var comm;
var epin = 2;
var epout = 3;
var reader;
var writer;
var enc = new TextEncoder();
var dec = new TextDecoder();
var lines = [];
var cursor = 0;
var first = false;
// the user loaded the webpage; set up keyboard listeners
function Body()
{
document.body.addEventListener("keydown", function(event) {
if ((event.keyCode === 67 || event.keyCode === 68) && event.ctrlKey && document.activeElement === document.getElementById("command")) {
// ctrl-c or ctrl-d in command line -- flow thru to device
event.preventDefault();
event.stopPropagation();
Send(String.fromCharCode(event.keyCode - 64));
}
if (event.keyCode === 13) {
// enter -- run the command line
event.preventDefault();
Enter();
}
if (event.keyCode === 40) {
// down arrow -- recall a newer command line
event.preventDefault();
UpDown(false);
}
if (event.keyCode === 38) {
// up arrow -- recall an older command line
event.preventDefault();
UpDown(true);
}
});
}
// the user wants to connect to a USB device's bulk endpoints directly
async function Usb()
{
var filter = [
//{ vendorId: 0x0403, productId: 0xA660 },
//{ vendorId: 0x1b4f, productId: 0xA660 },
//{ vendorId: 0x1b4f, productId: 0xE66E },
//{ vendorId: 0x04D8, productId: 0xE66E },
];
if ('usb' in navigator) {
try {
usb = await navigator.usb.requestDevice({ filters: filter });
await usb.open();
await usb.selectConfiguration(1);
await usb.claimInterface(1);
document.getElementById("config").hidden = true;
Send("\r");
Send("echo on\r");
Send("prompt on\r");
setTimeout(Receive, 1);
document.getElementById("command").disabled = false;
document.getElementById("command").focus();
} catch(err) {
document.getElementById("results").innerHTML += err.message.replace(/</g,'<').replace(/>/g,'>') + "\r";
}
} else {
document.getElementById("status").innerHTML =
`The usb API needs to be enabled in your browser thru:
- <a href=edge://flags/#enable-experimental-web-platform-features>edge://flags/#enable-experimental-web-platform-features</a>
- <a href=chrome://flags/#enable-experimental-web-platform-features>chrome://flags/#enable-experimental-web-platform-features</a>
- <a href=opera://flags/#enable-experimental-web-platform-features>opera://flags/#enable-experimental-web-platform-features</a>
`;
}
}
// the user wants to connect to an emulated COM port
async function Comm()
{
var filter = [
//{ usbVendorId: 0x0403, usbProductId: 0xA660 },
//{ usbVendorId: 0x1b4f, usbProductId: 0xA660 },
//{ usbVendorId: 0x1b4f, usbProductId: 0xE66E },
//{ usbVendorId: 0x04D8, usbProductId: 0xE66E },
];
if ('serial' in navigator) {
try {
comm = await navigator.serial.requestPort({ filters: filter });
await comm.open({ baudRate: 9600, dataBits: 7, parity: "even", stopBits: 2, bufferSize: 1024 });
reader = comm.readable.getReader();
writer = comm.writable.getWriter();
document.getElementById("config").hidden = true;
Send("\r");
Send("echo on\r");
Send("prompt on\r");
setTimeout(Receive, 1);
document.getElementById("command").disabled = false;
document.getElementById("command").focus();
} catch(err) {
document.getElementById("results").innerHTML += err.message.replace(/</g,'<').replace(/>/g,'>') + "\r";
}
} else {
document.getElementById("results").innerHTML +=
`The Web serial API needs to be enabled in your browser thru:
- <a href=edge://flags/#enable-experimental-web-platform-features>edge://flags/#enable-experimental-web-platform-features</a>
- <a href=chrome://flags/#enable-experimental-web-platform-features>chrome://flags/#enable-experimental-web-platform-features</a>
- <a href=opera://flags/#enable-experimental-web-platform-features>opera://flags/#enable-experimental-web-platform-features</a>
`;
}
}
// receive a string from the device
async function Receive()
{
var result;
var str;
if (usb) {
result = await usb.transferIn(epin, 1024);
str = dec.decode(result.data.buffer);
} else {
result = await reader.read();
str = dec.decode(result.value);
}
if (str.match(/\033[[]2J/)) {
// clear screen on cls XXX -- misses fragmented escape
Clear();
} else {
document.getElementById("results").innerHTML += str.replace(/</g,'<').replace(/>/g,'>').replace(/\n/g,'');
}
document.getElementById("command").scrollIntoView();
setTimeout(Receive, 1);
}
// send a string to the device
async function Send(str)
{
if (usb) {
await usb.transferOut(epout, enc.encode(str).buffer);
} else {
await writer.write(enc.encode(str).buffer);
}
document.getElementById("command").value = "";
document.getElementById("command").focus();
}
// the user clicked the Enter button; run the user command line
async function Enter()
{
var str = document.getElementById("command").value;
if (str.length && (! lines.length || str != lines[lines.length-1])) {
lines.push(str);
}
Send(str + "\r");
first = true;
}
// the user typed up-arrow or down-arrow to recall an older or newer command line
function UpDown(up)
{
if (first && up) {
// an initial up-arrow recalls the last hyperlink from the results history
cursor = 0;
}
if (lines.length) {
// recall the requested hyperlink from the results history to the equation line
if (up) {
// older
cursor = (cursor+lines.length-1)%lines.length;
} else {
// newer
cursor = (cursor+1)%lines.length;
}
document.getElementById("command").value = lines[cursor];
first = false;
}
}
// the user clicked the Paste button; paste the clipboard into the command line
async function Paste()
{
var text = await navigator.clipboard.readText();
Send(text + "\r");
}
// the user clicked the Clear button; clear the command, results, and history list
async function Clear()
{
document.getElementById("results").innerHTML = "";
document.getElementById("command").value = "";
document.getElementById("command").focus();
//lines = [];
}
</script>
</body>
</html>