-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsketch.js
80 lines (70 loc) · 1.88 KB
/
sketch.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
/* webSerial in p5.js
This sketch shows how to combine the webSerial API
and p5.js
TODO:
* Get open/close port handlers working properly
in sketches
* make autoOpen selectable
* finish all onEvent handlers in API
*
created 28 Jan 2022
by Tom Igoe
*/
// the DOM elements that might be changed by various functions:
let portButton; // the open/close port button
let textToSend = '';
let receivedText = '';
let timeText = '';
let webserial;
async function setup() {
createCanvas(400, 300);
webserial = new WebSerialPort();
if (webserial) {
webserial.on("data", serialRead);
// port open/close button:
portButton = createButton('open port');
portButton.position(10, 10);
portButton.mousePressed(openClosePort);
}
textSize(18);
}
function draw() {
background(255);
text('To send: ' + textToSend, 10, 50);
text('Received: ' + receivedText, 10, 80);
}
function serialRead(event) {
receivedText = event.detail.data;
}
function keyReleased() {
// this function is triggered with every keystroke in the input field.
// listen for the enter key (keyCode = 13)
// add to the textToSend and skip the rest of
// the function if you get any other key:
if (keyCode != 13) {
textToSend += key;
return;
}
// if you do get an enter keyCode, send
// texToSend out the serial port:
webserial.sendSerial(textToSend);
textToSend = '';
}
function mouseReleased() {
// send the mouseX out the serial port:
textToSend = mouseX;
webserial.sendSerial(mouseX);
}
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.html(buttonLabel);
}