forked from mofosyne/arduino-gameboy-printer-emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webserialConsole.html
138 lines (124 loc) · 4.95 KB
/
webserialConsole.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>WebSerial Arduino Nano Flasher</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- <script src="https://github.com/dbuezas/arduino-web-uploader/releases/download/v1.1.2/main.js"></script> -->
<script src="../arduino-web-uploader.v1.1.2.js"></script>
</head>
<body>
<h1> WebSerial Arduino Serial Console</h1>
This webserial console can be used to check that the device has successfully been flashed.
<br>
As well as a quick way to get image data off the device if you don't have a serial console emulator.
<br>
The gameboy printer emulator is by default set to communicate in 115200 baud.
<br>
This default is already set, so you just need to plug in the device and press connect.
<br>
<div id="WebSerialStatus">Javascript Must Be Enabled To Use WebSerial</div>
<div id="WebSerialControls">
Serial port speed:
<select id="SerialSpeed" name="speed">
<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" selected>115200</option>
</select>
<button id="SerialConnectButton" type="button" disabled>⚡Connect</button>
<button id="SerialResetButton" type="button" disabled>Reset & Clear</button>
</div>
<br>
<textarea id="data_text" name="data" cols="110" rows="20" readonly></textarea>
</body>
<script>
/*
* Web Serial API (Google Chrome)
*
* Useful information used to this implementation:
* https://github.com/svendahlstrand/web-serial-api/
* https://dev.to/unjavascripter/the-amazing-powers-of-the-web-web-serial-api-3ilc
*
*/
var webSerialControlElement = document.getElementById("WebSerialControls");
var consoleDisplayElement = document.getElementById("data_text");
var serialStatusElement = document.getElementById("WebSerialStatus");
var resetButtonElement = document.getElementById('SerialResetButton');
var connectButtonElement = document.getElementById('SerialConnectButton');
var serialSelectElement = document.getElementById('SerialSpeed');
let port;
if ('serial' in navigator) {
serialStatusElement.innerText = 'Web USB Is Available. Press connect to Start.';
// Enable
connectButtonElement.disabled = false;
// Connect Button Handling
connectButtonElement.addEventListener('click',
function () {
if (port) {
// Disconnect
closeReader('Disconnected from Serial Port\n');
}
else {
// Connect
openReader();
}
});
// Reset Button Handling (Pulse DTR and RTS signal, which would usually reset an arduino)
let sendResetSignal_Func = function () {
port.setSignals({ dataTerminalReady: false, requestToSend: false })
.then(function () { return port.setSignals({ dataTerminalReady: true, requestToSend: true }); })
.then(function () { consoleDisplayElement.textContent = ""; });
}
resetButtonElement.addEventListener('click', sendResetSignal_Func);
}
else {
webSerialControlElement.hidden = true;
serialStatusElement.innerText = 'Serial Web API Not Available. Please enable it using chrome://flags/ and enable Experimental Web Platform features';
}
async function openReader() {
port = await navigator.serial.requestPort({});
let strSpd = serialSelectElement.options[serialSelectElement.selectedIndex].value;
let speed = parseInt(strSpd);
await port.open({ baudRate: [speed] });
serialSelectElement.disabled = true;
serialStatusElement.innerText = 'Connected using Web Serial API !\n';
const appendStream = new WritableStream({
write(chunk, controller) {
console.log(chunk);
consoleDisplayElement.textContent += chunk;
},
abort(reason) {
closeReader("serial console : " + reason + "\n");
}
});
port.readable.pipeThrough(new TextDecoderStream()).pipeTo(appendStream);
resetButtonElement.disabled = false;
// Clear received text window
consoleDisplayElement.textContent = "";
if (1) {
// WORKAROUND: Disable connection button until we can figure how to properly disconnect...
connectButtonElement.disabled = true;
connectButtonElement.innerText = '⚡Connected';
}
else {
// TODO: Figure out why port.readable stream is still locked when calling port.close()
connectButtonElement.innerText = '🔌 Disconnect';
}
}
async function closeReader(reasonMsg) {
connectButtonElement.innerText = '⚡ Connect';
serialStatusElement.innerText = reasonMsg;
connectButtonElement.disabled = false;
serialSelectElement.disabled = false;
resetButtonElement.disabled = true;
port.close();
port = undefined;
}
</script>
</html>