-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpi_debugger.js
More file actions
136 lines (75 loc) · 3.21 KB
/
rpi_debugger.js
File metadata and controls
136 lines (75 loc) · 3.21 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
class RPIDebugger {
static state = { stopped: -1, running: 0, starting: -2, unavailable_or_error: -3 };
#line_break_transformer = class {
#chunks;
constructor() {
this.#chunks = "";
}
transform(chunk, controller) {
this.#chunks += chunk;
const lines = this.#chunks.split(/\r?\n|\r|\n/g);
this.#chunks = lines.pop();
lines.forEach((line) => controller.enqueue(line));
}
flush(controller) {
controller.enqueue(this.#chunks);
}
};
#serial_port;
async start_raspberry_pi() {
if (!("serial" in navigator)) {
alert("Serial Communication is not supported in this browser or device.");
this.#publish_state(RPIDebugger.state.unavailable_or_error);
return;
}
this.#publish_state(RPIDebugger.state.starting);
// select the first saved port
await navigator.serial.getPorts().then((ports) => { ports.forEach((port) => { this.#serial_port = port; console.log(port.getInfo().usbVendorId); return; }); });
// ask user if none saved
if (!this.#serial_port) { this.#serial_port = await navigator.serial.requestPort([{ usbVendorId: 0x2E8A }]).catch(() => this.#publish_state(RPIDebugger.state.stopped)); }
if (!(this.#serial_port instanceof SerialPort)) { console.log("Nothing selected? Otherwise close the browser and retry."); return; }
this.#publish_message("Starting...", true);
await this.#serial_port.open({ baudRate: 9600 });
this.#publish_message("Connected at 9600!");
const textDecoder = new TextDecoderStream();
const readableStreamClosed = this.#serial_port.readable.pipeTo(textDecoder.writable);
const reader = textDecoder.readable.pipeThrough(new TransformStream(new this.#line_break_transformer())).getReader();
this.#publish_state(RPIDebugger.state.running);
// Listen to data coming from the serial device.
while (true) {
try {
const { value, done } = await reader.read();
if (done) {
reader.releaseLock();
break;
}
this.#publish_message(value);
}
catch { break; }
}
await reader.cancel().catch(() => {/* Ignore the error */ });
await readableStreamClosed.catch(() => { /* Ignore the error */ });
await this.#serial_port.close();
this.#serial_port = null;
}
async restart_raspberry_pi() {
this.#publish_state(RPIDebugger.state.starting);
await this.#write_to_COMM(/* r */114);
this.#publish_message("Restarting in 2 seconds...", true);
window.setTimeout(async () => { await this.start_raspberry_pi(); }, 2000);
}
async exit_raspberry_pi() { this.#publish_state(RPIDebugger.state.unavailable_or_error); await this.#write_to_COMM(/* q */113); }
#publish_state(ui_state) {
document.dispatchEvent(new CustomEvent("hovenstate", { detail: ui_state }));
}
#publish_message(msg, clear_que) {
document.dispatchEvent(new CustomEvent("hovenlog", { detail: { text: msg, clear: clear_que } }));
}
async #write_to_COMM(c) {
const writer = this.#serial_port.writable.getWriter();
const data = new Uint8Array([c]); // hello
await writer.write(data);
// Allow the serial port to be closed later.
await writer.releaseLock();
}
};