Skip to content

Commit eb90ce8

Browse files
committed
1.01: Add UART.ports to allow available to user to be restricted
1 parent 71f271a commit eb90ce8

File tree

1 file changed

+44
-10
lines changed

1 file changed

+44
-10
lines changed

uart.js

+44-10
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,23 @@ Or more advanced usage with control of the connection
3535
if (!connection) throw "Error!";
3636
connection.on('data', function(d) { ... });
3737
connection.on('close', function() { ... });
38+
connection.on('error', function() { ... });
3839
connection.write("1+2\n", function() {
3940
connection.close();
4041
});
4142
});
4243
44+
You can also configure before opening a connection (see the bottom of this file for more info):
45+
46+
UART.ports = ["Web Serial"]; // force only Web Serial to be used
47+
UART.debug = 3; // show all debug messages
48+
etc...
49+
4350
ChangeLog:
4451
4552
...
46-
1v00: Auto-adjust BLE chunk size up if we receive >20 bytes in a packet
53+
1.01: Add UART.ports to allow available to user to be restricted
54+
1.00: Auto-adjust BLE chunk size up if we receive >20 bytes in a packet
4755
Drop UART.debug to 1 (less info printed)
4856
Fixed flow control on BLE
4957
*/
@@ -91,7 +99,7 @@ ChangeLog:
9199
}
92100

93101
var endpoints = [];
94-
var WebBluetooth = {
102+
endpoints.push({
95103
name : "Web Bluetooth",
96104
description : "Bluetooth LE devices",
97105
svg : '<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24"><path d="M0 0h24v24H0z" fill="none"/><path d="M17.71 7.71L12 2h-1v7.59L6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 11 14.41V22h1l5.71-5.71-4.3-4.29 4.3-4.29zM13 5.83l1.88 1.88L13 9.59V5.83zm1.88 10.46L13 18.17v-3.76l1.88 1.88z" fill="#ffffff"/></svg>',
@@ -190,6 +198,7 @@ ChangeLog:
190198
filters:[
191199
{ namePrefix: 'Puck.js' },
192200
{ namePrefix: 'Pixl.js' },
201+
{ namePrefix: 'Jolt.js' },
193202
{ namePrefix: 'MDBT42Q' },
194203
{ namePrefix: 'Bangle' },
195204
{ namePrefix: 'RuuviTag' },
@@ -263,8 +272,8 @@ ChangeLog:
263272
});
264273
return connection;
265274
}
266-
};
267-
var WebSerial = {
275+
});
276+
endpoints.push({
268277
name : "Web Serial",
269278
description : "USB connected devices",
270279
svg : '<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24"><path d="M0 0h24v24H0z" fill="none"/><path d="M15 7v4h1v2h-3V5h2l-3-4-3 4h2v8H8v-2.07c.7-.37 1.2-1.08 1.2-1.93 0-1.21-.99-2.2-2.2-2.2-1.21 0-2.2.99-2.2 2.2 0 .85.5 1.56 1.2 1.93V13c0 1.11.89 2 2 2h3v3.05c-.71.37-1.2 1.1-1.2 1.95 0 1.22.99 2.2 2.2 2.2 1.21 0 2.2-.98 2.2-2.2 0-.85-.49-1.58-1.2-1.95V15h3c1.11 0 2-.89 2-2v-2h1V7h-4z" fill="#ffffff"/></svg>',
@@ -339,10 +348,7 @@ ChangeLog:
339348

340349
return connection;
341350
}
342-
};
343-
// ======================================================================
344-
endpoints.push(WebBluetooth);
345-
endpoints.push(WebSerial);
351+
});
346352
// ======================================================================
347353
var connection;
348354
function connect(callback) {
@@ -354,6 +360,18 @@ ChangeLog:
354360
txInProgress : false
355361
};
356362

363+
if (uart.ports.length==0) {
364+
console.error(`UART: No ports in uart.ports`);
365+
return;
366+
}
367+
if (uart.ports.length==1) {
368+
var endpoint = endpoints.find(ep => ep.name == uart.ports[0]);
369+
if (endpoint===undefined) {
370+
console.error(`UART: Port Named "${uart.ports[0]}" not found`);
371+
return;
372+
}
373+
return endpoint.connect(connection, callback);
374+
}
357375
// modal
358376
var e = document.createElement('div');
359377
e.style = 'position:absolute;top:0px;left:0px;right:0px;bottom:0px;opacity:0.5;z-index:100;background:black;';
@@ -367,7 +385,12 @@ ChangeLog:
367385
var items = document.createElement('div');
368386
items.style = 'color:#000;background:#fff;padding:4px 8px 4px 8px;';
369387
menu.appendChild(items);
370-
endpoints.forEach(function(endpoint) {
388+
uart.ports.forEach(function(portName) {
389+
var endpoint = endpoints.find(ep => ep.name == portName);
390+
if (endpoint===undefined) {
391+
console.error(`UART: Port Named "${portName}" not found`);
392+
return;
393+
}
371394
var supported = endpoint.isSupported();
372395
if (supported!==true)
373396
log(0, endpoint.name+" not supported, "+supported);
@@ -384,6 +407,14 @@ ChangeLog:
384407
};
385408
items.appendChild(ep);
386409
});
410+
e.onclick = function(evt) {
411+
evt.preventDefault();
412+
document.body.removeChild(menu);
413+
document.body.removeChild(e);
414+
uart.log(1,"User clicked outside modal - cancelling connect");
415+
connection.isOpening = false;
416+
connection.emit('error', "Model closed.");
417+
};
387418
document.body.appendChild(e);
388419
document.body.appendChild(menu);
389420
return connection;
@@ -454,7 +485,7 @@ ChangeLog:
454485
}, 100);
455486
}
456487

457-
if (connection && (connection.isOpen || connection.isOpening)) {
488+
if (connection && connection.isOpen) {
458489
if (!connection.txInProgress) connection.received = "";
459490
isBusy = true;
460491
return connection.write(data, onWritten);
@@ -501,10 +532,13 @@ ChangeLog:
501532
// ----------------------------------------------------------
502533

503534
var uart = {
535+
version : "1.01",
504536
/// Are we writing debug information? 0 is no, 1 is some, 2 is more, 3 is all.
505537
debug : 1,
506538
/// Should we use flow control? Default is true
507539
flowControl : true,
540+
/// Which ports should be offer to the user? If only one is specified no modal menu is created
541+
ports : ["Web Bluetooth","Web Serial"],
508542
/// Used internally to write log information - you can replace this with your own function
509543
log : function(level, s) { if (level <= this.debug) console.log("<UART> "+s)},
510544
/// Called with the current send progress or undefined when done - you can replace this with your own function

0 commit comments

Comments
 (0)