8000 Add callbacks for connect / disconnect · arduino/ArduinoCore-mbed@e2789f4 · GitHub
[go: up one dir, main page]

Skip to content

Commit e2789f4

Browse files
committed
Add callbacks for connect / disconnect
1 parent 5ced73c commit e2789f4

File tree

3 files changed

+40
-36
lines changed

3 files changed

+40
-36
lines changed

libraries/Camera/extras/WebSerialCamera/app.js

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const connectButton = document.getElementById('connect');
22
const refreshButton = document.getElementById('refresh');
33
const startButton = document.getElementById('start');
4-
const disconnectButton = document.getElementById('disconnect');
54
const saveImageButton = document.getElementById('save-image');
65
const canvas = document.getElementById('bitmapCanvas');
76
const ctx = canvas.getContext('2d');
@@ -40,6 +39,15 @@ const stopBits = 2; // Adjust this value based on your device's stop bits
4039
const imageDataProcessor = new ImageDataProcessor(ctx, mode, imageWidth, imageHeight);
4140
const connectionHandler = new SerialConnectionHandler(baudRate, dataBits, stopBits, "even", "hardware", bufferSize);
4241

42+
connectionHandler.onConnect = () => {
43+
connectButton.textContent = 'Disconnect';
44+
renderStream();
45+
};
46+
47+
connectionHandler.onDisconnect = () => {
48+
connectButton.textContent = 'Connect';
49+
};
50+
4351
function renderBitmap(bytes, width, height) {
4452
canvas.width = width;
4553
canvas.height = height;
@@ -65,12 +73,13 @@ async function renderFrame(){
6573

6674
startButton.addEventListener('click', renderStream);
6775
connectButton.addEventListener('click', async () => {
68-
await connectionHandler.requestSerialPort();
69-
if(await connectionHandler.connectSerial()){
70-
renderStream();
76+
if(connectionHandler.isConnected()){
77+
connectionHandler.disconnectSerial();
78+
} else {
79+
await connectionHandler.requestSerialPort();
80+
await connectionHandler.connectSerial();
7181
}
7282
});
73-
disconnectButton.addEventListener('click', () => connectionHandler.disconnectSerial());
7483
refreshButton.addEventListener('click', () => {
7584
renderFrame();
7685
});
@@ -83,31 +92,11 @@ saveImageButton.addEventListener('click', () => {
8392
link.remove();
8493
});
8594

86-
navigator.serial.addEventListener("connect", (e) => {
87-
// Connect to `e.target` or add it to a list of available ports.
88-
console.log('🔌 Serial port became available. VID: 0x' + e.target.getInfo().usbVendorId.toString(16));
89-
connectionHandler.autoConnect().then((connected) => {
90-
if(connected){
91-
renderStream();
92-
};
93-
});
94-
});
95-
96-
navigator.serial.addEventListener("disconnect", (e) => {
97-
// Remove `e.target` from the list of available ports.
98-
console.log('❌ Serial port lost. VID: 0x' + e.target.getInfo().usbVendorId.toString(16));
99-
currentPort = null;
100-
});
101-
10295
// On page load event, try to connect to the serial port
10396
window.addEventListener('load', async () => {
10497
console.log('🚀 Page loaded. Trying to connect to serial port...');
10598
setTimeout(() => {
106-
connectionHandler.autoConnect().then((connected) => {
107-
if (connected) {
108-
renderStream();
109-
};
110-
});
99+
connectionHandler.autoConnect();
111100
}, 1000);
112101
});
113102

libraries/Camera/extras/WebSerialCamera/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
<canvas id="bitmapCanvas"></canvas>
1212
<div id="controls">
1313
<button id="connect">Connect</button>
14-
<button id="disconnect">Disconnect</button>
1514
<button id="save-image">Save Image</button>
1615
<button id="refresh">Refresh</button>
1716
<button id="start">Start</button>

libraries/Camera/extras/WebSerialCamera/serialConnectionHandler.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class SerialConnectionHandler {
1212
this.timeout = timeout;
1313
this.currentPort = null;
1414
this.currentReader = null;
15+
this.registerEvents();
1516
}
1617

1718
async requestSerialPort() {
@@ -27,7 +28,7 @@ class SerialConnectionHandler {
2728
}
2829
}
2930

30-
isConnected(){
31+
isConnected() {
3132
return this.currentPort?.readable != null;
3233
}
3334

@@ -44,6 +45,7 @@ class SerialConnectionHandler {
4445
flowControl: this.flowControl
4546
});
4647
console.log('✅ Connected to serial port.');
48+
if(this.onConnect) this.onConnect();
4749
return true;
4850
} catch (error) {
4951
return false;
@@ -58,6 +60,7 @@ class SerialConnectionHandler {
5860
await this.currentReader?.cancel();
5961
await port.close();
6062
console.log('🔌 Disconnected from serial port.');
63+
if(this.onDisconnect) this.onDisconnect();
6164
} catch (error) {
6265
console.error('💣 Error occurred while disconnecting: ' + error.message);
6366
};
@@ -106,7 +109,7 @@ class SerialConnectionHandler {
106109
if (timeout) {
107110
timeoutID = setTimeout(() => {
108111
console.log('⌛️ Timeout occurred while reading.');
109-
if (this.currentPort.readable) reader?.cancel();
112+
if (this.currentPort?.readable) reader?.cancel();
110113
}, timeout);
111114
}
112115

@@ -129,8 +132,7 @@ class SerialConnectionHandler {
129132
}
130133

131134
} catch (error) {
132-
console.log('💣 Error occurred while reading: ');
133-
console.log(error);
135+
console.log('💣 Error occurred while reading: ' + error.message);
134136
} finally {
135137
keepReading = false;
136138
// console.log('🔓 Releasing reader lock...');
@@ -141,17 +143,17 @@ class SerialConnectionHandler {
141143
return bytesRead;
142144
}
143145

144-
async requestFrame(){
145-
if(!this.currentPort?.writable) {
146-
console.log('🚫 Port is not writable. Ignoring request...');
147-
return;
146+
async requestFrame() {
147+
if (!this.currentPort?.writable) {
148+
console.log('🚫 Port is not writable. Ignoring request...');
149+
return;
148150
}
149151
// console.log('Writing 1 to the serial port...');
150152
// Write a 1 to the serial port
151153
const writer = this.currentPort.writable.getWriter();
152154
await writer.write(new Uint8Array([1]));
153155
await writer.close();
154-
}
156+
}
155157

156158
async getFrame(totalBytes) {
157159
if (!this.currentPort) return;
@@ -161,4 +163,18 @@ class SerialConnectionHandler {
161163
// Read the given amount of bytes
162164
return await this.readBytes(totalBytes, this.timeout);
163165
}
166+
167+
registerEvents() {
168+
navigator.serial.addEventListener("connect", (e) => {
169+
// Connect to `e.target` or add it to a list of available ports.
170+
console.log('🔌 Serial port became available. VID: 0x' + e.target.getInfo().usbVendorId.toString(16));
171+
this.autoConnect();
172+
});
173+
174+
navigator.serial.addEventListener("disconnect", (e) => {
175+
console.log('❌ Serial port lost. VID: 0x' + e.target.getInfo().usbVendorId.toString(16));
176+
this.currentPort = null;
177+
if(this.onDisconnect) this.onDisconnect();
178+
});
179+
}
164180
}

0 commit comments

Comments
 (0)
0