-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
155 lines (133 loc) · 3.28 KB
/
app.js
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
const serialRequestBtn = document.querySelector(".serialRequest");
const statusCon = document.querySelector(".status");
const watch = document.querySelector(".watch");
const log = document.querySelector(".log");
const sendForm = document.querySelector(".sendForm");
const sendInput = document.querySelector(".sendInput");
let port;
let sync = true;
const cal = (part, timestamp = 0) => {
let d;
if (timestamp) {
d = new Date(timestamp);
} else {
d = new Date();
}
const months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
const week = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
const hours = d.getHours() < 10 ? "0" + d.getHours() : d.getHours();
const minutes = d.getMinutes() < 10 ? "0" + d.getMinutes() : d.getMinutes();
const seconds = d.getSeconds() < 10 ? "0" + d.getSeconds() : d.getSeconds();
switch (part) {
case "Month":
return months[d.getMonth()];
break;
case "Week":
return week[d.getDay()];
break;
case "Date":
return d.getDate();
break;
case "Hours":
return hours;
break;
case "Minutes":
return minutes;
break;
case "Seconds":
return seconds;
break;
default:
break;
}
};
const addMessage = (text) => {
const t = new Date();
const div = document.createElement("div");
const span = document.createElement("span");
div.textContent = text;
div.append(span);
span.textContent =
cal("Hours", t) + ":" + cal("Minutes", t) + ":" + cal("Seconds", t);
log.prepend(div);
};
const connectSerial = async () => {
try {
port = await navigator.serial.requestPort();
await port.open({ baudRate: 115200 });
const decoder = new TextDecoderStream();
port.readable.pipeTo(decoder.writable);
const inputStream = decoder.readable;
const reader = inputStream.getReader();
while (true) {
const { value, done } = await reader.read();
if (value) {
addMessage(value);
}
if (done) {
console.log("DONE", done);
reader.releaseLock();
break;
}
}
} catch (error) {
addMessage(error);
if (error.toString().includes("The device has been lost")) {
port = undefined;
sync = true;
statusCon.textContent = "";
}
}
};
const writeText = async (text) => {
const encoder = new TextEncoder();
const writer = port.writable.getWriter();
await writer.write(encoder.encode(text));
sendInput.value = "";
writer.releaseLock();
};
serialRequestBtn.addEventListener("click", () => {
if ("serial" in navigator) {
connectSerial();
} else {
addMessage("Web Serial API not supported.");
}
});
sendForm.addEventListener("submit", (e) => {
e.preventDefault();
if (!port) {
addMessage("Not connected");
return;
}
const text = sendInput.value;
if (text.length > 0) {
writeText(text);
sendInput.classList.remove("err");
} else {
sendInput.classList.add("err");
}
});
const loop = setInterval(() => {
watch.textContent =
cal("Hours") + ":" + cal("Minutes") + ":" + cal("Seconds");
if (port) {
if (sync) {
addMessage("Wait connection...");
sync = false;
statusCon.textContent = "Connected";
}
}
}, 1000);