-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpasta.js
More file actions
203 lines (172 loc) · 5.69 KB
/
Copy pathpasta.js
File metadata and controls
203 lines (172 loc) · 5.69 KB
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
let port;
let reader;
let writer;
// enc dec
let textDecoder;
let textEncoder;
let keepReading = false;
let buffer = "";
// ad string buffer compensate \n\r
let buffer2 = "";
let timestampMode = true; // timestamp toggle
let outputBuffer = ""; // for save btn
let rawMode = false; // raw toggle - old ver
const terminal = document.getElementById("terminal");
const connectBtn = document.getElementById("connectBtn");
const sendBtn = document.getElementById("sendBtn");
const inputField = document.getElementById("inputField");
const baudRateSelect = document.getElementById("baudRate");
// footer btns
const clearBtn = document.getElementById("clearBtn");
const saveBtn = document.getElementById("saveBtn");
const rawToggle = document.getElementById("rawToggle"); // depre
const portInfo = document.getElementById("portInfo"); // show vendor/product id if possible
const timestampToggle = document.getElementById("timestampToggle");
timestampToggle.checked = timestampMode; // default on
//--------------------------------------
function printToTerminal(text, isSystem = false) {
const line = document.createElement("div");
// timestamp
let ts = "";
const now = new Date();
if (timestampMode) {
//ts = "[" + new Date().toLocaleTimeString() + "] ";
ts = now.toLocaleTimeString("en-US", { hour12: false }) + "." + String(now.getMilliseconds()).padStart(3, "0");
}
line.textContent = ts + " " + text;
//line.className = isSystem ? "text-gray-500" : "text-green-300"; // swap
line.className = isSystem ? "text-green-500" : "text-gray-300"; // swap
terminal.appendChild(line);
terminal.scrollTop = terminal.scrollHeight; // auto scrol
// op buffer
outputBuffer += line.textContent + "\n";
}
//------------------------------------------------------
connectBtn.addEventListener("click", async () => {
if (!port) {
try {
port = await navigator.serial.requestPort();
await port.open({
baudRate: parseInt(baudRateSelect.value, 10),
});
textDecoder = new TextDecoderStream();
textEncoder = new TextEncoderStream();
// stream pipe s
port.readable.pipeTo(textDecoder.writable);
textEncoder.readable.pipeTo(port.writable);
reader = textDecoder.readable.getReader();
writer = textEncoder.writable.getWriter();
keepReading = true;
readLoop();
// clear "not connected" text
clearBtn.click();
// web serial api doest give tthe system port name
const info = port.getInfo ? port.getInfo() : {};
printToTerminal(`[ (${JSON.stringify(info) || "Port connected"}) ]`, true);
printToTerminal(`[ C0nnected @ ${baudRateSelect.value} baud ]`, true);
// update header port info
if (info.usbVendorId) {
portInfo.textContent = `VID: ${info.usbVendorId}, PID: ${info.usbProductId}`;
} else {
portInfo.textContent = "(Port connected)";
}
connectBtn.textContent = "Disconnect";
inputField.disabled = false;
sendBtn.disabled = false;
} catch (err) {
printToTerminal(`[ Error: ${err.message} ]`, true);
}
} else {
disconnect();
}
});
// release
async function disconnect() {
keepReading = false;
if (reader) {
await reader.cancel();
reader.releaseLock();
}
if (writer) writer.releaseLock();
if (port) await port.close();
port = null;
connectBtn.textContent = "Connect";
inputField.disabled = true;
sendBtn.disabled = true;
portInfo.textContent = ""; // clear header info
printToTerminal("[ Disc0nnected ]", true);
}
// rx
async function readLoop() {
while (keepReading && reader) {
try {
const { value, done } = await reader.read();
if (done) break;
if (value) {
buffer += value; // load buffer
if (rawMode) {
printToTerminal(buffer); // dump buffer - raw depre
buffer = "";
} else {
let lines = buffer.split(/\r?\n/); // Split lines
buffer = lines.pop(); // Save last incomplete line
for (let line of lines) {
printToTerminal("> " + line.trim());
}
}
}
} catch (err) {
printToTerminal(`[ Read errror: ${err.message} ]`, true);
break;
}
}
}
//------------------------------------------------------
sendBtn.addEventListener("click", async () => {
const command = inputField.value.trim();
if (command && writer) {
await writer.write(command + "\n");
printToTerminal("> " + command);
inputField.value = "";
}
inputField.focus();
});
// click enter send command
inputField.addEventListener("keydown", async (e) => {
if (e.key === "Enter") {
sendBtn.click();
}
});
//------------------------------------------------------
// clear
clearBtn.addEventListener("click", () => {
terminal.innerHTML = "";
outputBuffer = "";
printToTerminal("[ Cleared ]", true);
});
// save / dwnld
saveBtn.addEventListener("click", () => {
const blob = new Blob([outputBuffer], { type: "text/plain" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
const now_s = new Date();
const timestamp = now_s.toLocaleTimeString("en-US", { hour12: false }) + "." + String(now_s.getMilliseconds()).padStart(3, "0");
a.download = "termicelli_out_" + timestamp + ".txt";
a.click();
URL.revokeObjectURL(url);
});
// raw mode - depre
if (rawToggle) {
rawToggle.addEventListener("change", () => {
rawMode = rawToggle.checked;
printToTerminal(`[ Raw mode ${rawMode ? "ON" : "OFF"} ]`, true);
});
}
// timestamp ts toggle
if (timestampToggle) {
timestampToggle.addEventListener("change", () => {
timestampMode = timestampToggle.checked;
printToTerminal(`[ Timestamp ${timestampMode ? "ON" : "OFF"} ]`, true);
});
}