-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript.js
77 lines (67 loc) · 2.25 KB
/
script.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
/*
WebSerial example
Reads from a webSerial serial port, and writes to it.
Works on Chrome and Edge and Opera browsers.
created 28 Jan 2022
modified 15 May 2022
by Tom Igoe
*/
// the DOM elements that might be changed by various functions:
let portButton; // the open/close port button
let readingsSpan; // DOM element where the incoming readings go
let timeSpan; // DOM element for one special reading
let webserial;
function setup() {
// get the DOM elements and assign any listeners needed:
// user text input:
const textInput = document.getElementById("txt");
textInput.addEventListener("keyup", readTextInput);
// user range input:
const slider = document.getElementById("dim");
slider.addEventListener("change", readRangeInput);
// span for incoming serial messages:
readingsSpan = document.getElementById("readings");
// span for incoming serial messages:
timeSpan = document.getElementById("seconds");
webserial = new WebSerialPort();
if (webserial) {
webserial.on("data", serialRead);
// port open/close button:
portButton = document.getElementById("portButton");
portButton.addEventListener("click", openClosePort);
}
}
async function openClosePort() {
// label for the button will change depending on what you do:
let buttonLabel = "Open port";
// if port is open, close it; if closed, open it:
if (webserial.port) {
await webserial.closePort();
} else {
await webserial.openPort();
buttonLabel = "Close port";
}
// change button label:
portButton.innerHTML = buttonLabel;
}
function serialRead(event) {
readingsSpan.innerHTML = event.detail.data
;
}
function readTextInput(event) {
// this function is triggered with every keystroke in the input field.
// listen for the enter key (keyCode = 13) and skip the rest of
// the function if you get any other key:
if (event.keyCode != 13) {
return;
}
// if you do get an enter keyCode, send the value of the field
// out the serial port:
webserial.sendSerial(event.target.value);
}
function readRangeInput(event) {
// send the range input's value out the serial port:
webserial.sendSerial(event.target.value);
}
// run the setup function when all the page is loaded:
document.addEventListener("DOMContentLoaded", setup);