@@ -35,15 +35,23 @@ Or more advanced usage with control of the connection
35
35
if (!connection) throw "Error!";
36
36
connection.on('data', function(d) { ... });
37
37
connection.on('close', function() { ... });
38
+ connection.on('error', function() { ... });
38
39
connection.write("1+2\n", function() {
39
40
connection.close();
40
41
});
41
42
});
42
43
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
+
43
50
ChangeLog:
44
51
45
52
...
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
47
55
Drop UART.debug to 1 (less info printed)
48
56
Fixed flow control on BLE
49
57
*/
@@ -91,7 +99,7 @@ ChangeLog:
91
99
}
92
100
93
101
var endpoints = [ ] ;
94
- var WebBluetooth = {
102
+ endpoints . push ( {
95
103
name : "Web Bluetooth" ,
96
104
description : "Bluetooth LE devices" ,
97
105
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:
190
198
filters :[
191
199
{ namePrefix : 'Puck.js' } ,
192
200
{ namePrefix : 'Pixl.js' } ,
201
+ { namePrefix : 'Jolt.js' } ,
193
202
{ namePrefix : 'MDBT42Q' } ,
194
203
{ namePrefix : 'Bangle' } ,
195
204
{ namePrefix : 'RuuviTag' } ,
@@ -263,8 +272,8 @@ ChangeLog:
263
272
} ) ;
264
273
return connection ;
265
274
}
266
- } ;
267
- var WebSerial = {
275
+ } ) ;
276
+ endpoints . push ( {
268
277
name : "Web Serial" ,
269
278
description : "USB connected devices" ,
270
279
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:
339
348
340
349
return connection ;
341
350
}
342
- } ;
343
- // ======================================================================
344
- endpoints . push ( WebBluetooth ) ;
345
- endpoints . push ( WebSerial ) ;
351
+ } ) ;
346
352
// ======================================================================
347
353
var connection ;
348
354
function connect ( callback ) {
@@ -354,6 +360,18 @@ ChangeLog:
354
360
txInProgress : false
355
361
} ;
356
362
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
+ }
357
375
// modal
358
376
var e = document . createElement ( 'div' ) ;
359
377
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:
367
385
var items = document . createElement ( 'div' ) ;
368
386
items . style = 'color:#000;background:#fff;padding:4px 8px 4px 8px;' ;
369
387
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
+ }
371
394
var supported = endpoint . isSupported ( ) ;
372
395
if ( supported !== true )
373
396
log ( 0 , endpoint . name + " not supported, " + supported ) ;
@@ -384,6 +407,14 @@ ChangeLog:
384
407
} ;
385
408
items . appendChild ( ep ) ;
386
409
} ) ;
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
+ } ;
387
418
document . body . appendChild ( e ) ;
388
419
document . body . appendChild ( menu ) ;
389
420
return connection ;
@@ -454,7 +485,7 @@ ChangeLog:
454
485
} , 100 ) ;
455
486
}
456
487
457
- if ( connection && ( connection . isOpen || connection . isOpening ) ) {
488
+ if ( connection && connection . isOpen ) {
458
489
if ( ! connection . txInProgress ) connection . received = "" ;
459
490
isBusy = true ;
460
491
return connection . write ( data , onWritten ) ;
@@ -501,10 +532,13 @@ ChangeLog:
501
532
// ----------------------------------------------------------
502
533
503
534
var uart = {
535
+ version : "1.01" ,
504
536
/// Are we writing debug information? 0 is no, 1 is some, 2 is more, 3 is all.
505
537
debug : 1 ,
506
538
/// Should we use flow control? Default is true
507
539
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" ] ,
508
542
/// Used internally to write log information - you can replace this with your own function
509
543
log : function ( level , s ) { if ( level <= this . debug ) console . log ( "<UART> " + s ) } ,
510
544
/// Called with the current send progress or undefined when done - you can replace this with your own function
0 commit comments