-
Notifications
You must be signed in to change notification settings - Fork 11
/
uart.js
1239 lines (1183 loc) · 47 KB
/
uart.js
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
--------------------------------------------------------------------
Web Bluetooth / Web Serial Interface library for Nordic UART
Copyright 2021 Gordon Williams ([email protected])
https://github.com/espruino/EspruinoWebTools
--------------------------------------------------------------------
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
--------------------------------------------------------------------
This creates a 'Puck' object that can be used from the Web Browser.
Simple usage:
UART.write("LED1.set()\n")
Execute expression and return the result:
UART.eval("BTN.read()").then(function(d) {
alert(d);
});
// or old way:
UART.eval("BTN.read()", function(d) {
alert(d);
});
Or write and wait for a result - this will return all characters,
including echo and linefeed from the REPL so you may want to send
`echo(0)` and use `console.log` when doing this.
UART.write("1+2\n").then(function(d) {
alert(d);
});
// or old way
UART.write("1+2\n", function(d) {
alert(d);
});
Or more advanced usage with control of the connection
- allows multiple connections
UART.connectAsync().then(function(connection) {
if (!connection) throw "Error!";
connection.on('data', function(d) { ... });
connection.on('close', function() { ... });
connection.on('error', function() { ... });
connection.write("1+2\n", function() {
connection.close();
});
});
Auto-connect to previously-used Web Serial devices when they're connected to USB:
navigator.serial.addEventListener("connect", (event) => {
const port = event.target;
UART.connectAsync({serialPort:port}).then(connection=>console.log(connection));
});
...or to a specific VID and PID when it is connected:
navigator.serial.addEventListener("connect", async (event) => {
const port = event.target;
const portInfo = await port.getInfo();
if (portInfo.usbVendorId==0x0483 && portInfo.usbProductId==0xA4F1) {
UART.connectAsync({serialPort:port}).then(connection=>console.log(connection));
} else {
console.log("Unknown device connected");
}
});
You can also configure before opening a connection (see the bottom of this file for more info):
UART.ports = ["Web Serial"]; // force only Web Serial to be used
UART.debug = 3; // show all debug messages
etc...
As of Espruino 2v25 you can also send files:
UART.getConnection().espruinoSendFile("test.txt","This is a test of sending data to Espruino").then(_=>console.log("Done"))
UART.getConnection().espruinoSendFile("test.txt","This is a test of sending data to Espruino's SD card",{fs:true}).then(_=>console.log("Done"))
And receive them:
UART.getConnection().espruinoReceiveFile("test.txt", {}).then(contents=>console.log("Received", JSON.stringify(contents)));
Or evaluate JS on the device and return the response as a JS object:
UART.getConnection().espruinoEval("1+2").then(res => console.log("=",res));
ChangeLog:
...
1.08: Add UART.getConnectionAsync()
Add .espruinoEval(... {stmFix:true}) to work around occasional STM32 USB issue in 2v24 and earlier firmwares
1s->2s packet timeout
connection.write now returns a promise
1.07: Added UART.getConnection().espruinoEval
1.06: Added optional serialPort parameter to UART.connect(), allowing a known Web Serial port to be used
Added connectAsync, and write/eval now return promises
1.05: Better handling of Web Serial disconnects
UART.connect without arguments now works
Fix issue using UART.write/eval if UART was opened with UART.connect()
UART.getConnection() now returns undefined/isConnected()=false if UART has disconnected
1.04: For packet uploads, add ability to ste chunk size, report progress or even skip searching for acks
1.03: Added options for restricting what devices appear
Improve Web Serial Disconnection - didn't work before
1.02: Added better on/emit/removeListener handling
Add .espruinoSendPacket
1.01: Add UART.ports to allow available to user to be restricted
Add configurable baud rate
Updated modal dialog look (with common fn for selector and modal)
1.00: Auto-adjust BLE chunk size up if we receive >20 bytes in a packet
Drop UART.debug to 1 (less info printed)
Fixed flow control on BLE
To do:
* move 'connection.received' handling into removeListener and add an upper limit (100k?)
* add a 'line' event for each line of data that's received
* move XON/XOFF handling into Connection.rxDataHandler
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.UART = factory();
}
}(typeof self !== 'undefined' ? self : this, function () {
const NORDIC_SERVICE = "6e400001-b5a3-f393-e0a9-e50e24dcca9e";
const NORDIC_TX = "6e400002-b5a3-f393-e0a9-e50e24dcca9e";
const NORDIC_RX = "6e400003-b5a3-f393-e0a9-e50e24dcca9e";
if (typeof navigator == "undefined") return;
/// Are we busy, so new requests should be written to the queue (below)?
var isBusy;
/// A queue of operations to perform if UART.write/etc is called while busy
var queue = [];
function ab2str(buf) {
return String.fromCharCode.apply(null, new Uint8Array(buf));
}
function str2ab(str) {
var buf = new ArrayBuffer(str.length);
var bufView = new Uint8Array(buf);
for (var i=0, strLen=str.length; i<strLen; i++)
bufView[i] = str.charCodeAt(i);
return buf;
}
// parse a very relaxed version of JSON (returns undefined on failure)
// Originally from https://github.com/espruino/EspruinoAppLoaderCore/blob/master/js/utils.js
// Lexer from https://github.com/espruino/EspruinoTools/blob/master/core/utils.js
function parseRJSON(str) {
let lex = (function(str) { // Nasty lexer - no comments/etc
var chAlpha="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_$";
var chNum="0123456789";
var chAlphaNum = chAlpha+chNum;
var chWhiteSpace=" \t\n\r";
// https://www-archive.mozilla.org/js/language/js20-2000-07/rationale/syntax.html#regular-expressions
var allowedRegExIDs = ["abstract","break","case","catch","class","const","continue","debugger","default",
"delete","do","else","enum","eval","export","extends","field","final","finally","for","function","goto",
"if","implements","import","in","instanceof","native","new","package","private","protected","public",
"return","static","switch","synchronized","throw","throws","transient","try","typeof","var","volatile","while","with"];
var allowedRegExChars = ['!','%','&','*','+','-','/','<','=','>','?','[','{','}','(',',',';',':']; // based on Espruino jslex.c (may not match spec 100%)
var ch;
var idx = 0;
var lineNumber = 1;
var nextCh = function() {
ch = str[idx++];
if (ch=="\n") lineNumber++;
};
var backCh = function() {
idx--;
ch = str[idx-1];
};
nextCh();
var isIn = function(s,c) { return s.indexOf(c)>=0; } ;
var lastToken = {};
var nextToken = function() {
while (isIn(chWhiteSpace,ch)) {
nextCh();
}
if (ch==undefined) return undefined;
if (ch=="/") {
nextCh();
if (ch=="/") {
// single line comment
while (ch!==undefined && ch!="\n") nextCh();
return nextToken();
} else if (ch=="*") {
nextCh();
var last = ch;
nextCh();
// multiline comment
while (ch!==undefined && !(last=="*" && ch=="/")) {
last = ch;
nextCh();
}
nextCh();
return nextToken();
} else {
backCh(); // push the char back
}
}
var s = "";
var type, value;
var startIdx = idx-1;
if (isIn(chAlpha,ch)) { // ID
type = "ID";
do {
s+=ch;
nextCh();
} while (isIn(chAlphaNum,ch));
} else if (isIn(chNum,ch)) { // NUMBER
type = "NUMBER";
var chRange = chNum;
if (ch=="0") { // Handle
s+=ch;
nextCh();
if ("xXoObB".indexOf(ch)>=0) {
if (ch=="b" || ch=="B") chRange="01";
if (ch=="o" || ch=="O") chRange="01234567";
if (ch=="x" || ch=="X") chRange="0123456789ABCDEFabcdef";
s+=ch;
nextCh();
}
}
while (isIn(chRange,ch) || ch==".") {
s+=ch;
nextCh();
}
} else if (isIn("\"'`/",ch)) { // STRING or regex
s+=ch;
var q = ch;
nextCh();
// Handle case where '/' is just a divide character, not RegEx
if (s=='/' && (lastToken.type=="STRING" || lastToken.type=="NUMBER" ||
(lastToken.type=="ID" && !allowedRegExIDs.includes(lastToken.str)) ||
(lastToken.type=="CHAR" && !allowedRegExChars.includes(lastToken.str))
)) {
// https://www-archive.mozilla.org/js/language/js20-2000-07/rationale/syntax.html#regular-expressions
type = "CHAR";
} else {
type = "STRING"; // should we report this as REGEX?
value = "";
while (ch!==undefined && ch!=q) {
if (ch=="\\") { // handle escape characters
nextCh();
var escape = '\\'+ch;
var escapeExtra = 0;
if (ch=="x") {
nextCh();escape += ch;
nextCh();escape += ch;
value += String.fromCharCode(parseInt(escape.substr(2), 16));
} else if (ch=="u") {
nextCh();escape += ch;
nextCh();escape += ch;
nextCh();escape += ch;
nextCh();escape += ch;
value += String.fromCharCode(parseInt(escape.substr(2), 16));
} else {
try {
value += JSON.parse('"'+escape+'"');
} catch (e) {
value += escape;
}
}
s += escape;
} else {
s+=ch;
value += ch;
}
nextCh();
};
if (ch!==undefined) s+=ch;
nextCh();
}
} else {
type = "CHAR";
s+=ch;
nextCh();
}
if (value===undefined) value=s;
return lastToken={type:type, str:s, value:value, startIdx:startIdx, endIdx:idx-1, lineNumber:lineNumber};
};
return {
next : nextToken
};
})(str);
let tok = lex.next();
function match(s) {
if (tok.str!=s) throw new Error("Expecting "+s+" got "+JSON.stringify(tok.str));
tok = lex.next();
}
function recurse() {
let final = "";
while (tok!==undefined) {
if (tok.type == "NUMBER") {
let v = parseFloat(tok.str);
tok = lex.next();
return v;
}
if (tok.str == "-") {
tok = lex.next();
let v = -parseFloat(tok.str);
tok = lex.next();
return v;
}
if (tok.type == "STRING") {
let v = tok.value;
tok = lex.next();
return v;
}
if (tok.type == "ID") switch (tok.str) {
case "true" : tok = lex.next(); return true;
case "false" : tok = lex.next(); return false;
case "null" : tok = lex.next(); return null;
}
if (tok.str == "[") {
tok = lex.next();
let arr = [];
while (tok.str != ']') {
arr.push(recurse());
if (tok.str != ']') match(",");
}
match("]");
return arr;
}
if (tok.str == "{") {
tok = lex.next();
let obj = {};
while (tok.str != '}') {
let key = tok.type=="STRING" ? tok.value : tok.str;
tok = lex.next();
match(":");
obj[key] = recurse();
if (tok.str != '}') match(",");
}
match("}");
return obj;
}
match("EOF");
}
}
let json = undefined;
try {
json = recurse();
} catch (e) {
console.log("RJSON parse error", e);
}
return json;
}
function handleQueue() {
if (!queue.length) return;
var q = queue.shift();
log(3,"Executing "+JSON.stringify(q)+" from queue");
if (q.type=="eval") uart.eval(q.expr, q.cb).then(q.resolve, q.reject);
else if (q.type=="write") uart.write(q.data, q.callback, q.callbackNewline).then(q.resolve, q.reject);
else log(1,"Unknown queue item "+JSON.stringify(q));
}
function log(level, s) {
if (uart.log) uart.log(level, s);
}
/// Base connection class - BLE/Serial add their write/etc on top of this
class Connection {
// on/emit work for close/data/open/error/ack/nak/packet events
on(evt,cb) { let e = "on"+evt; if (!this[e]) this[e]=[]; this[e].push(cb); }; // on only works with a single handler
emit(evt,data1,data2) { let e = "on"+evt; if (this[e]) this[e].forEach(fn=>fn(data1,data2)); };
removeListener(evt,callback) { let e = "on"+evt; if (this[e]) this[e]=this[e].filter(fn=>fn!=callback); };
// on("open", () => ... ) connection opened
// on("close", () => ... ) connection closed
// on("data", (data) => ... ) when data is received (as string)
// on("packet", (type,data) => ... ) when a packet is received (if .parsePackets=true)
// on("ack", () => ... ) when an ACK is received (if .parsePackets=true)
// on("nak", () => ... ) when an ACK is received (if .parsePackets=true)
isOpen = false; // is the connection actually open?
isOpening = true; // in the process of opening a connection?
txInProgress = false; // is transmission in progress?
parsePackets = false; // If set we parse the input stream for Espruino packet data transfers
received = ""; // The data we've received so far - this gets reset by .write/eval/etc
hadData = false; // used when waiting for a block of data to finish being received
rxDataHandlerLastCh = 0; // used by rxDataHandler - last received character
rxDataHandlerPacket = undefined; // used by rxDataHandler - used for parsing
rxDataHandlerTimeout = undefined; // timeout for unfinished packet
rxDataHandler(data) { // Called when data is received, and passed it on to event listeners
log(3, "Received "+JSON.stringify(data));
// TODO: handle XON/XOFF centrally here?
if (this.parsePackets) {
for (var i=0;i<data.length;i++) {
let ch = data[i];
// handle packet reception
if (this.rxDataHandlerPacket!==undefined) {
this.rxDataHandlerPacket += ch;
ch = undefined;
let flags = (this.rxDataHandlerPacket.charCodeAt(0)<<8) | this.rxDataHandlerPacket.charCodeAt(1);
let len = flags & 0x1FFF;
let rxLen = this.rxDataHandlerPacket.length;
if (rxLen>=2 && rxLen>=(len+2)) {
log(3, "Got packet end");
if (this.rxDataHandlerTimeout) {
clearTimeout(this.rxDataHandlerTimeout);
this.rxDataHandlerTimeout = undefined;
}
this.emit("packet", flags&0xE000, this.rxDataHandlerPacket.substring(2));
this.rxDataHandlerPacket = undefined; // stop packet reception
}
} else if (ch=="\x06") { // handle individual control chars
log(3, "Got ACK");
this.emit("ack");
ch = undefined;
} else if (ch=="\x15") {
log(3, "Got NAK");
this.emit("nak");
ch = undefined;
} else if (ch=="\x10") { // DLE - potential start of packet (ignore)
this.rxDataHandlerLastCh = "\x10";
ch = undefined;
} else if (ch=="\x01" && this.rxDataHandlerLastCh=="\x10") { // SOH
log(3, "Got packet start");
this.rxDataHandlerPacket = "";
this.rxDataHandlerTimeout = setTimeout(()=>{
this.rxDataHandlerTimeout = undefined;
log(0, "Packet timeout (2s)");
this.rxDataHandlerPacket = undefined;
}, 2000);
ch = undefined;
}
if (ch===undefined) { // if we're supposed to remove the char, do it
data = data.substring(0,i)+data.substring(i+1);
i--;
} else
this.rxDataHandlerLastCh = ch;
}
}
this.hadData = true;
if (data.length>0) {
// keep track of received data
if (this.received.length < 100000) // ensure we're not creating a memory leak
this.received += data;
// forward any data
if (this.cb) this.cb(data);
this.emit('data', data);
}
}
/* Send a packet of type "RESPONSE/EVAL/EVENT/FILE_SEND/DATA" to Espruino
options = {
noACK : bool (don't wait to acknowledgement)
}
*/
espruinoSendPacket(pkType, data, options) {
options = options || {};
if ("string"!=typeof data) throw new Error("'data' must be a String");
if (data.length>0x1FFF) throw new Error("'data' too long");
const PKTYPES = {
RESPONSE : 0, // Response to an EVAL packet
EVAL : 0x2000, // execute and return the result as RESPONSE packet
EVENT : 0x4000, // parse as JSON and create `E.on('packet', ...)` event
FILE_SEND : 0x6000, // called before DATA, with {fn:"filename",s:123}
DATA : 0x8000, // Sent after FILE_SEND with blocks of data for the file
FILE_RECV : 0xA000 // receive a file - returns a series of PT_TYPE_DATA packets, with a final zero length packet to end
}
if (!pkType in PKTYPES) throw new Error("'pkType' not one of "+Object.keys(PKTYPES));
let connection = this;
return new Promise((resolve,reject) => {
function tidy() {
connection.removeListener("ack",onACK);
connection.removeListener("nak",onNAK);
}
function onACK(ok) {
tidy();
setTimeout(resolve,0);
}
function onNAK(ok) {
tidy();
setTimeout(reject,0);
}
if (!options.noACK) {
connection.parsePackets = true;
connection.on("ack",onACK);
connection.on("nak",onNAK);
}
let flags = data.length | PKTYPES[pkType];
connection.write(String.fromCharCode(/*DLE*/16,/*SOH*/1,(flags>>8)&0xFF,flags&0xFF)+data, function() {
// write complete
if (options.noACK) {
setTimeout(resolve,0); // if not listening for acks, just resolve immediately
}
}, err => {
tidy();
reject(err);
});
});
}
/* Send a file to Espruino using 2v25 packets.
options = { // mainly passed to Espruino
fs : true // optional -> write using require("fs") (to SD card)
noACK : bool // (don't wait to acknowledgements)
chunkSize : int // size of chunks to send (default 1024) for safety this depends on how big your device's input buffer is if there isn't flow control
progress : (chunkNo,chunkCount)=>{} // callback to report upload progress
timeout : int (optional, milliseconds, default=1000)
} */
espruinoSendFile(filename, data, options) {
if ("string"!=typeof data) throw new Error("'data' must be a String");
let CHUNK = 1024;
options = options||{};
options.fn = filename;
options.s = data.length;
let packetOptions = {};
let progressHandler = (chunkNo,chunkCount)=>{};
if (options.noACK) {
delete options.noACK;
packetOptions.noACK = true;
}
if (options.chunkSize) {
CHUNK = options.chunkSize;
delete options.chunkSize;
}
if (options.progress) {
progressHandler = options.progress;
delete options.progress;
}
let connection = this;
let packetCount = 0, packetTotal = Math.ceil(data.length/CHUNK)+1;
// always ack the FILE_SEND
progressHandler(0, packetTotal);
return connection.espruinoSendPacket("FILE_SEND",JSON.stringify(options)).then(sendData);
// but if noACK don't ack for data
function sendData() {
progressHandler(++packetCount, packetTotal);
if (data.length==0) return Promise.resolve();
let packet = data.substring(0, CHUNK);
data = data.substring(CHUNK);
return connection.espruinoSendPacket("DATA", packet, packetOptions).then(sendData);
}
}
/* Receive a file from Espruino using 2v25 packets.
options = { // mainly passed to Espruino
fs : true // optional -> write using require("fs") (to SD card)
timeout : int // milliseconds timeout (default=1000)
}
} */
espruinoReceiveFile(filename, options) {
options = options||{};
options.fn = filename;
let connection = this;
return new Promise((resolve,reject) => {
let fileContents = "", timeout;
function scheduleTimeout() {
if (timeout) clearTimeout(timeout);
timeout = setTimeout(() => {
timeout = undefined;
cleanup();
reject("espruinoReceiveFile Timeout");
}, options.timeout || 1000);
}
function cleanup() {
connection.removeListener("packet", onPacket);
if (timeout) {
clearTimeout(timeout);
timeout = undefined;
}
}
function onPacket(type,data) {
if (type!=0x8000) return; // ignore things that are not DATA packet
if (data.length==0) { // 0 length packet = EOF
cleanup();
setTimeout(resolve,0,fileContents);
} else {
fileContents += data;
scheduleTimeout();
}
}
connection.parsePackets = true;
connection.on("packet", onPacket);
scheduleTimeout();
connection.espruinoSendPacket("FILE_RECV",JSON.stringify(options)).then(()=>{
// now wait...
}, err => {
cleanup();
reject(err);
});
});
}
/* Send a JS expression to be evaluated on Espruino using using 2v25 packets.
options = {
timeout : int // milliseconds timeout (default=1000)
stmFix : bool // if set, this works around an issue in Espruino STM32 2v24 and earlier where USB could get in a state where it only sent small chunks of data at a time
}*/
espruinoEval(expr, options) {
options = options || {};
if ("string"!=typeof expr) throw new Error("'expr' must be a String");
let connection = this;
return new Promise((resolve,reject) => {
let prodInterval;
function cleanup() {
connection.removeListener("packet", onPacket);
if (timeout) {
clearTimeout(timeout);
timeout = undefined;
}
if (prodInterval) {
clearInterval(prodInterval);
prodInterval = undefined;
}
}
function onPacket(type,data) {
if (type!=0) return; // ignore things that are not a response
cleanup();
setTimeout(resolve,0,parseRJSON(data));
}
connection.parsePackets = true;
connection.on("packet", onPacket);
let timeout = setTimeout(() => {
timeout = undefined;
cleanup();
reject("espruinoEval Timeout");
}, options.timeout || 1000);
connection.espruinoSendPacket("EVAL",expr,{noACK:options.stmFix}).then(()=>{
// resolved/rejected with 'packet' event or timeout
if (options.stmFix)
prodInterval = setInterval(function() {
connection.write(" \x08") // space+backspace
.catch(err=>{
console.error("Error sending STM fix:",err);
cleanup();
});
}, 50);
}, err => {
cleanup();
reject(err);
});
});
}
};
/// Endpoints for each connection method
var endpoints = [];
endpoints.push({
name : "Web Bluetooth",
description : "Bluetooth LE devices",
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>',
isSupported : function() {
if (navigator.platform.indexOf("Win")>=0 &&
(navigator.userAgent.indexOf("Chrome/54")>=0 ||
navigator.userAgent.indexOf("Chrome/55")>=0 ||
navigator.userAgent.indexOf("Chrome/56")>=0)
)
return "Chrome <56 in Windows has navigator.bluetooth but it's not implemented properly";;
if (window && window.location && window.location.protocol=="http:" &&
window.location.hostname!="localhost")
return "Serving off HTTP (not HTTPS) - Web Bluetooth not enabled";
if (navigator.bluetooth) return true;
var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
if (iOS) {
return "To use Web Bluetooth on iOS you'll need the WebBLE App.\nPlease go to https://itunes.apple.com/us/app/webble/id1193531073 to download it.";
} else {
return "This Web Browser doesn't support Web Bluetooth.\nPlease see https://www.espruino.com/Puck.js+Quick+Start";
}
},
connect : function(connection, options) {
options = options || {};
/* options = {
// nothing yet...
}
*/
var DEFAULT_CHUNKSIZE = 20;
var btServer = undefined;
var btService;
var connectionDisconnectCallback;
var txCharacteristic;
var rxCharacteristic;
var txDataQueue = [];
var flowControlXOFF = false;
var chunkSize = DEFAULT_CHUNKSIZE;
connection.close = function(callback) {
connection.isOpening = false;
if (connection.isOpen) {
connection.isOpen = false;
connection.emit('close');
} else {
if (callback) callback(null);
}
if (btServer) {
btServer.disconnect();
btServer = undefined;
txCharacteristic = undefined;
rxCharacteristic = undefined;
}
};
connection.write = function(data, callback) {
return new Promise((resolve,reject) => {
if (data) txDataQueue.push({data:data,callback:callback,maxLength:data.length,resolve:resolve});
if (connection.isOpen && !connection.txInProgress) writeChunk();
function writeChunk() {
if (flowControlXOFF) { // flow control - try again later
setTimeout(writeChunk, 50);
return;
}
var chunk;
if (!txDataQueue.length) {
uart.writeProgress();
return;
}
var txItem = txDataQueue[0];
uart.writeProgress(txItem.maxLength - txItem.data.length, txItem.maxLength);
if (txItem.data.length <= chunkSize) {
chunk = txItem.data;
txItem.data = undefined;
} else {
chunk = txItem.data.substr(0,chunkSize);
txItem.data = txItem.data.substr(chunkSize);
}
connection.txInProgress = true;
log(2, "Sending "+ JSON.stringify(chunk));
txCharacteristic.writeValue(str2ab(chunk)).then(function() {
log(3, "Sent");
if (!txItem.data) {
txDataQueue.shift(); // remove this element
if (txItem.callback)
txItem.callback();
if (txItem.resolve)
txItem.resolve();
}
connection.txInProgress = false;
writeChunk();
}).catch(function(error) {
log(1, 'SEND ERROR: ' + error);
txDataQueue = [];
connection.close();
});
}
});
};
return navigator.bluetooth.requestDevice(uart.optionsBluetooth).then(function(device) {
log(1, 'Device Name: ' + device.name);
log(1, 'Device ID: ' + device.id);
// Was deprecated: Should use getPrimaryServices for this in future
//log('BT> Device UUIDs: ' + device.uuids.join('\n' + ' '.repeat(21)));
device.addEventListener('gattserverdisconnected', function() {
log(1, "Disconnected (gattserverdisconnected)");
connection.close();
});
return device.gatt.connect();
}).then(function(server) {
log(1, "Connected");
btServer = server;
return server.getPrimaryService(NORDIC_SERVICE);
}).then(function(service) {
log(2, "Got service");
btService = service;
return btService.getCharacteristic(NORDIC_RX);
}).then(function (characteristic) {
rxCharacteristic = characteristic;
log(2, "RX characteristic:"+JSON.stringify(rxCharacteristic));
rxCharacteristic.addEventListener('characteristicvaluechanged', function(event) {
var dataview = event.target.value;
if (dataview.byteLength > chunkSize) {
log(2, "Received packet of length "+dataview.byteLength+", increasing chunk size");
chunkSize = dataview.byteLength;
}
if (uart.flowControl) {
for (var i=0;i<dataview.byteLength;i++) {
var ch = dataview.getUint8(i);
if (ch==17) { // XON
log(2,"XON received => resume upload");
flowControlXOFF = false;
}
if (ch==19) { // XOFF
log(2,"XOFF received => pause upload");
flowControlXOFF = true;
}
}
}
connection.rxDataHandler(ab2str(dataview.buffer));
});
return rxCharacteristic.startNotifications();
}).then(function() {
return btService.getCharacteristic(NORDIC_TX);
}).then(function (characteristic) {
txCharacteristic = characteristic;
log(2, "TX characteristic:"+JSON.stringify(txCharacteristic));
}).then(function() {
connection.txInProgress = false;
connection.isOpen = true;
connection.isOpening = false;
isBusy = false;
queue = [];
connection.emit('open');
// if we had any writes queued, do them now
connection.write();
return connection;
}).catch(function(error) {
log(1, 'ERROR: ' + error);
connection.close();
return Promise.reject(error);
});
}
});
endpoints.push({
name : "Web Serial",
description : "USB connected devices",
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>',
isSupported : function() {
if (!navigator.serial)
return "No navigator.serial - Web Serial not enabled";
if (window && window.location && window.location.protocol=="http:" &&
window.location.hostname!="localhost")
return "Serving off HTTP (not HTTPS) - Web Serial not enabled";
return true;
},
connect : function(connection, options) {
options = options || {};
/* options = {
serialPort : force a serialport, otherwise pop up a menu
}
*/
let serialPort, reader, writer;
function disconnected() {
connection.isOpening = false;
if (connection.isOpen) {
log(1, "Disconnected");
connection.isOpen = false;
connection.emit('close');
}
}
connection.close = function(callback) {
if (writer) {
writer.close();
writer = undefined;
}
if (reader) {
reader.cancel();
}
// readLoop will finish and *that* calls disconnect and cleans up
};
connection.write = function(data, callback, alreadyRetried) {
// TODO: progress?
return new Promise((resolve, reject) => {
if (!serialPort || !serialPort.writable) return reject ("Not connected");
if (serialPort.writable.locked) {
if (alreadyRetried)
return reject("Writable stream is locked");
log(0,'Writable stream is locked - retry in 500ms');
setTimeout(()=>{ this.write(data, callback, true).then(resolve, reject); }, 500);
return;
}
writer = serialPort.writable.getWriter();
log(2, "Sending "+ JSON.stringify(data));
writer.write(str2ab(data)).then(function() {
writer.releaseLock();
writer = undefined;
log(3, "Sent");
if (callback) callback();
resolve();
}).catch(function(error) {
if (writer) {
writer.releaseLock();
writer.close();
}
writer = undefined;
log(0,'SEND ERROR: ' + error);
reject(error);
});
});
};
return (options.serialPort ?
Promise.resolve(options.serialPort) :
navigator.serial.requestPort(uart.optionsSerial)).then(function(port) {
log(1, "Connecting to serial port");
serialPort = port;
return port.open({ baudRate: uart.baud });
}).then(function () {
function readLoop() {
reader = serialPort.readable.getReader();
reader.read().then(function ({ value, done }) {
reader.releaseLock();
reader = undefined;
if (value)
connection.rxDataHandler(ab2str(value.buffer));
if (done) { // connection is closed
if (serialPort) {
serialPort.close();
serialPort = undefined;
}
disconnected();
} else { // else continue reading
readLoop();
}
}, function(error) { // read() rejected...
reader.releaseLock();
log(0, 'ERROR: ' + error);
if (serialPort) {
serialPort.close();
serialPort = undefined;
}
disconnected();
});
}
readLoop();
log(1,"Serial connected. Receiving data...");
connection.txInProgress = false;
connection.isOpen = true;
connection.isOpening = false;
return connection;
}).catch(function(error) {
log(0, 'ERROR: ' + error);
disconnected();
return Promise.reject(error);
});
}
});
// ======================================================================
/* Create a modal window.
options = {
title : string
contents = DomElement | string
onClickBackground : function
onClickMenu : function
}
returns {
remove : function(); // remove menu
}
*/
function createModal(options) {
// modal
var e = document.createElement('div');
e.style = 'position:absolute;top:0px;left:0px;right:0px;bottom:0px;opacity:0.5;z-index:100;background:black;';
// menu
var menu = document.createElement('div');
menu.style = 'position:absolute;left:50%;top:50%;transform:translate(-50%,-50%);font-family: Sans-Serif;z-index:101;min-width:300px';
var menutitle = document.createElement('div');
menutitle.innerText = options.title;
menutitle.style = 'color:#fff;background:#000;padding:8px 8px 4px 8px;font-weight:bold;';
menu.appendChild(menutitle);
var items = document.createElement('div');
items.style = 'color:#000;background:#fff;padding:4px 8px 4px 8px;min-height:4em';
if ("string" == typeof options.contents)
items.innerHTML = options.contents;
else
items.appendChild(options.contents);
menu.appendChild(items);
document.body.appendChild(e);
document.body.appendChild(menu);
e.onclick = function(evt) { // clicked modal -> remove
evt.preventDefault();
result.remove();
if (options.onClickBackground)
options.onClickBackground();
};
menu.onclick = function(evt) { // clicked menu
evt.preventDefault();
if (options.onClickMenu)
options.onClickMenu();
};
var result = {
remove : function() {
document.body.removeChild(menu);
document.body.removeChild(e);
}
};
return result;
}
// ======================================================================
var connection;
function connect(options) {
connection = new Connection();
return new Promise((resolve, reject) => {
if (uart.ports.length==0) {
console.error(`UART: No ports in uart.ports`);
return reject(`UART: No ports in uart.ports`);
}
if (uart.ports.length==1) {