@@ -112,6 +112,9 @@ const Comms = {
112
112
}
113
113
}
114
114
} ,
115
+ /* when connected, this is the name of the device we're connected to as far as Espruino is concerned
116
+ (eg Bluetooth/USB/Serial1.println("Foo") ) */
117
+ espruinoDevice : undefined ,
115
118
// ================================================================================
116
119
// Show a message on the screen (if available)
117
120
showMessage : ( txt ) => {
@@ -227,7 +230,7 @@ const Comms = {
227
230
}
228
231
// Actually write the command with a 'print OK' at the end, and use responseHandler
229
232
// to deal with the response. If OK we call uploadCmd to upload the next block
230
- return Comms . write ( `${ cmd } ;${ Comms . getProgressCmd ( currentBytes / maxBytes ) } ${ Const . CONNECTION_DEVICE } .println("OK")\n` , { waitNewLine :true } ) . then ( responseHandler ) ;
233
+ return Comms . write ( `${ cmd } ;${ Comms . getProgressCmd ( currentBytes / maxBytes ) } ${ Comms . espruinoDevice } .println("OK")\n` , { waitNewLine :true } ) . then ( responseHandler ) ;
231
234
}
232
235
233
236
uploadCmd ( )
@@ -352,8 +355,38 @@ const Comms = {
352
355
return ;
353
356
}
354
357
358
+ /* We need to figure out the console device name according to Espruino. For some devices
359
+ it's easy (eg Bangle.js = Bluetooth) and we can hard code with Const.CONNECTION_DEVICE
360
+ but for others we must figure it out */
361
+ let connection = Comms . getConnection ( ) ;
362
+ if ( Comms . espruinoDevice === undefined ) {
363
+ if ( Const . CONNECTION_DEVICE )
364
+ Comms . espruinoDevice = Const . CONNECTION_DEVICE ;
365
+ else {
366
+ Comms . eval ( "process.env.CONSOLE" ) . then ( device => {
367
+ if ( ( "string" == typeof device ) && device . length > 0 )
368
+ Comms . espruinoDevice = device ;
369
+ else throw new Error ( "Unable to find Espruino console device" ) ;
370
+ console . log ( "<COMMS> Set console device to " + device ) ;
371
+ } ) . then ( ( ) => Comms . getDeviceInfo ( true ) ) .
372
+ then ( resolve ) ;
373
+ return ;
374
+ }
375
+ }
376
+
377
+
378
+ if ( Comms . getConnection ( ) . endpoint && Comms . getConnection ( ) . endpoint . name == "Web Serial" && Comms . espruinoDevice == "Bluetooth" ) {
379
+ console . log ( "<COMMS> Using Web Serial, forcing Comms.espruinoDevice='USB'" , result ) ;
380
+ // FIXME: won't work on ESP8266/ESP32!
381
+ Comms . espruinoDevice = "USB" ;
382
+ }
383
+ if ( Comms . getConnection ( ) . endpoint && Comms . getConnection ( ) . endpoint . name == "Web Bluetooth" && Comms . espruinoDevice != "Bluetooth" ) {
384
+ console . log ( "<COMMS> Using Web Bluetooth, forcing Comms.espruinoDevice='Bluetooth'" , result ) ;
385
+ Comms . espruinoDevice = "Bluetooth" ;
386
+ }
387
+
355
388
let cmd , finalJS = `JSON.stringify(require("Storage").getStats?require("Storage").getStats():{})+","+E.toJS([process.env.BOARD,process.env.VERSION,process.env.EXPTR,process.env.MODULES,0|getTime(),E.CRC32(getSerial()+(global.NRF?NRF.getAddress():0))]).substr(1)` ;
356
- let device = Const . CONNECTION_DEVICE ;
389
+ let device = Comms . espruinoDevice ;
357
390
if ( Const . SINGLE_APP_ONLY ) // only one app on device, info file is in app.info
358
391
cmd = `\x10${ device } .println("["+(require("Storage").read("app.info")||"null")+","+${ finalJS } )\n` ;
359
392
else if ( Const . FILES_IN_FS ) // file in a FAT filesystem
@@ -408,8 +441,8 @@ const Comms = {
408
441
// Get an app's info file from Bangle.js
409
442
getAppInfo : app => {
410
443
var cmd ;
411
- if ( Const . FILES_IN_FS ) cmd = `\x10${ Const . CONNECTION_DEVICE } .println(require("fs").readFileSync(${ JSON . stringify ( AppInfo . getAppInfoFilename ( app ) ) } )||"null")\n` ;
412
- else cmd = `\x10${ Const . CONNECTION_DEVICE } .println(require("Storage").read(${ JSON . stringify ( AppInfo . getAppInfoFilename ( app ) ) } )||"null")\n` ;
444
+ if ( Const . FILES_IN_FS ) cmd = `\x10${ Comms . espruinoDevice } .println(require("fs").readFileSync(${ JSON . stringify ( AppInfo . getAppInfoFilename ( app ) ) } )||"null")\n` ;
445
+ else cmd = `\x10${ Comms . espruinoDevice } .println(require("Storage").read(${ JSON . stringify ( AppInfo . getAppInfoFilename ( app ) ) } )||"null")\n` ;
413
446
return Comms . write ( cmd ) .
414
447
then ( appJSON => {
415
448
let app ;
@@ -499,7 +532,7 @@ const Comms = {
499
532
}
500
533
}
501
534
// Use write with newline here so we wait for it to finish
502
- let cmd = `\x10E.showMessage("Erasing...");require("Storage").eraseAll();${ Const . CONNECTION_DEVICE } .println("OK");reset()\n` ;
535
+ let cmd = `\x10E.showMessage("Erasing...");require("Storage").eraseAll();${ Comms . espruinoDevice } .println("OK");reset()\n` ;
503
536
Comms . write ( cmd , { waitNewLine :true } ) . then ( handleResult ) ;
504
537
} ) . then ( ( ) => new Promise ( resolve => {
505
538
console . log ( "<COMMS> removeAllApps: Erase complete, waiting 500ms for 'reset()'" ) ;
@@ -538,8 +571,11 @@ const Comms = {
538
571
539
572
//TODO Switch to an event listener when Puck will support it
540
573
let interval = setInterval ( ( ) => {
541
- if ( connected === Comms . isConnected ( ) ) return ;
542
- connected = Comms . isConnected ( ) ;
574
+ let newConnected = Comms . isConnected ( ) ;
575
+ if ( connected === newConnected ) return ;
576
+ connected = newConnected ;
577
+ if ( ! connected )
578
+ Comms . espruinoDevice = undefined ;
543
579
cb ( connected ) ;
544
580
} , 1000 ) ;
545
581
@@ -610,9 +646,9 @@ const Comms = {
610
646
return Comms . readTextBlock ( `\x03\x10(function() {
611
647
var s = require("Storage").read(${ JSON . stringify ( filename ) } );
612
648
if (s===undefined) s="";
613
- ${ Const . CONNECTION_DEVICE } .println(((s.length+2)/3)<<2);
614
- for (var i=0;i<s.length;i+=${ CHUNKSIZE } ) ${ Const . CONNECTION_DEVICE } .print(btoa(s.substr(i,${ CHUNKSIZE } )));
615
- ${ Const . CONNECTION_DEVICE } .print("\\xFF");
649
+ ${ Comms . espruinoDevice } .println(((s.length+2)/3)<<2);
650
+ for (var i=0;i<s.length;i+=${ CHUNKSIZE } ) ${ Comms . espruinoDevice } .print(btoa(s.substr(i,${ CHUNKSIZE } )));
651
+ ${ Comms . espruinoDevice } .print("\\xFF");
616
652
})()\n` ) . then ( text => {
617
653
return atobSafe ( text ) ;
618
654
} ) ;
@@ -623,10 +659,10 @@ ${Const.CONNECTION_DEVICE}.print("\\xFF");
623
659
console . log ( `<COMMS> readStorageFile ${ JSON . stringify ( filename ) } ` ) ;
624
660
return Comms . readTextBlock ( `\x03\x10(function() {
625
661
var f = require("Storage").open(${ JSON . stringify ( filename ) } ,"r");
626
- ${ Const . CONNECTION_DEVICE } .println(f.getLength());
662
+ ${ Comms . espruinoDevice } .println(f.getLength());
627
663
var l = f.readLine();
628
- while (l!==undefined) { ${ Const . CONNECTION_DEVICE } .print(l); l = f.readLine(); }
629
- ${ Const . CONNECTION_DEVICE } .print("\\xFF");
664
+ while (l!==undefined) { ${ Comms . espruinoDevice } .print(l); l = f.readLine(); }
665
+ ${ Comms . espruinoDevice } .print("\\xFF");
630
666
})()\n` ) ;
631
667
} ,
632
668
// Read a non-storagefile file
0 commit comments