forked from edrys-org/module-serial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
256 lines (228 loc) · 7.15 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta
http-equiv="Permissions-Policy"
content="clipboard-read=(self), clipboard-write=(self)"
/>
<meta
name="description"
content="A WebSerial module for live serial comms with stations"
/>
<meta name="show-in" content="station" />
<title>Live WebSerial</title>
<script src="https://edrys-labs.github.io/module/edrys.js"></script>
<script
defer
src="https://edrys-labs.github.io/module/vendor/alpine.min.js"
></script>
<link
rel="stylesheet"
href="https://edrys-labs.github.io/module/vendor/water.min.css"
/>
<link
rel="stylesheet"
href="https://edrys-labs.github.io/module/vendor/open-iconic/css/open-iconic.min.css"
/>
<script src="./xterm/xterm.min.js"></script>
<link rel="stylesheet" href="./xterm/xterm.min.css" />
</head>
<body>
<template
x-if="!!edrys"
x-data="{ edrys: undefined }"
x-init="async e => { Edrys.onUpdate((e) => { edrys = {...e} }) }"
>
<div
x-show="edrys.role == 'station'"
x-data="{ baudRate: edrys.module.config.baud || edrys.getItem('baud') || 9600, ready: false }"
x-effect="e => { edrys.setItem('baud', baudRate) }"
>
<div x-show="!ready" id="connect">
<label for="baud" style="float: left; padding: 10px"
>Baud Rate:</label
>
<button
style="float: right"
@click="async (e) => { ready = await serial_connect(baudRate, true); }"
>
Connect
</button>
<select id="baud" x-model="baudRate">
<option value="1200">1200</option>
<option value="2400">2400</option>
<option value="4800">4800</option>
<option value="9600">9600</option>
<option value="19200">19200</option>
<option value="38400">38400</option>
<option value="57600">57600</option>
<option value="115200">115200</option>
</select>
</div>
<div x-show="ready" id="disconnect">
<button
style="z-index: 100; position: absolute; right: 5px; top: 5px"
@click="async (e) => { await serial_close(); }"
>
Disconnect
</button>
</div>
</div>
</template>
<div id="terminal"></div>
<script type="module">
import { FitAddon } from './xterm/xterm-addon-fit.js'
var term = new Terminal({
cursorBlink: true,
macOptionIsMeta: true,
scrollback: true,
})
window.term = term
term.open(document.getElementById('terminal'))
const fitAddon = new FitAddon()
term.loadAddon(fitAddon)
fitAddon.fit()
term.writeln('')
term.writeln('You can copy with ctrl+shift+x')
term.writeln('You can paste with ctrl+shift+v')
term.writeln('')
term.writeln('>>>')
term.attachCustomKeyEventHandler((e) => {
if (e.type !== 'keydown') {
return true
}
if (e.ctrlKey && e.shiftKey) {
const key = e.key.toLowerCase()
if (key === 'v') {
// ctrl+shift+v: paste whatever is in the clipboard
navigator.clipboard.readText().then((toPaste) => {
Edrys.sendMessage('w', toPaste)
})
return false
} else if (key === 'c' || key === 'x') {
// ctrl+shift+x: copy whatever is highlighted to clipboard
// 'x' is used as an alternate to 'c' because ctrl+c is taken
// by the terminal (SIGINT) and ctrl+shift+c is taken by the browser
// (open devtools).
// I'm not aware of ctrl+shift+x being used by anything in the terminal
// or browser
const toCopy = term.getSelection()
navigator.clipboard.writeText(toCopy)
term.focus()
return false
}
}
return true
})
term.onKey((e) => {
Edrys.sendMessage('w', e.key)
})
Edrys.onMessage(({ from, subject, body }) => {
if (subject == 'r') {
term.write(body.replace('\n', '\r').replace('\r', '\n\r'))
}
if (subject == 'w' && Edrys.role == 'station') {
serial_write(body)
}
})
</script>
<script>
Edrys.onReady(async () => {
while (true) {
if (
await serial_connect(
Edrys.module.config.baud || Edrys.getItem('baud') || 9600
)
) {
window.term.writeln('you are connected')
ready = true
port.addEventListener('disconnect', async (e) => {
ready = false
await serial_close()
})
document.getElementById('connect').style.display = 'none'
document.getElementById('disconnect').style.display = 'block'
return
}
}
})
let connected = false
let port = null
async function serial_connect(baudRate, userInitiated = false) {
if (connected) return true
if (userInitiated) {
port = await navigator.serial.requestPort()
return true
}
port = await navigator.serial.getPorts()
if (port !== null && Array.isArray(port) && port.length > 0) {
port = port[0]
} else {
return false
}
if (port && !connected) {
await port.open({ baudRate: baudRate })
connected = true
setTimeout((e) => serial_read(), 1)
}
return connected
}
let reader = null
let readableStreamClosed = null
async function serial_read() {
if (!port) {
connected = false
return
}
const textDecoder = new TextDecoderStream()
readableStreamClosed = port.readable.pipeTo(textDecoder.writable)
reader = textDecoder.readable.getReader()
while (true) {
const { value, done } = await reader.read()
if (done) {
reader.releaseLock()
break
}
Edrys.sendMessage('r', value)
}
}
async function serial_close() {
window.location.reload()
}
let writerInit = false
let writer = null
let writableStreamClosed = null
async function serial_write(text) {
if (!port || !connected) {
connected = false
return
}
if (!writerInit) {
const textEncoder = new TextEncoderStream()
writableStreamClosed = textEncoder.readable.pipeTo(port.writable)
writer = textEncoder.writable.getWriter()
writerInit = true
}
if (writer) await writer.write(text)
}
</script>
<style>
body,
html,
#terminal,
.terminal {
height: 100%;
width: 100%;
max-width: 100%;
margin: 0;
padding: 0;
}
body {
overflow: hidden;
}
</style>
</body>
</html>