@@ -112,6 +112,9 @@ const Comms = {
112112 }
113113 }
114114 } ,
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 ,
115118// ================================================================================
116119 // Show a message on the screen (if available)
117120 showMessage : ( txt ) => {
@@ -227,7 +230,7 @@ const Comms = {
227230 }
228231 // Actually write the command with a 'print OK' at the end, and use responseHandler
229232 // 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 ) ;
231234 }
232235
233236 uploadCmd ( )
@@ -352,8 +355,38 @@ const Comms = {
352355 return ;
353356 }
354357
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+
355388 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 ;
357390 if ( Const . SINGLE_APP_ONLY ) // only one app on device, info file is in app.info
358391 cmd = `\x10${ device } .println("["+(require("Storage").read("app.info")||"null")+","+${ finalJS } )\n` ;
359392 else if ( Const . FILES_IN_FS ) // file in a FAT filesystem
@@ -408,8 +441,8 @@ const Comms = {
408441 // Get an app's info file from Bangle.js
409442 getAppInfo : app => {
410443 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` ;
413446 return Comms . write ( cmd ) .
414447 then ( appJSON => {
415448 let app ;
@@ -499,7 +532,7 @@ const Comms = {
499532 }
500533 }
501534 // 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` ;
503536 Comms . write ( cmd , { waitNewLine :true } ) . then ( handleResult ) ;
504537 } ) . then ( ( ) => new Promise ( resolve => {
505538 console . log ( "<COMMS> removeAllApps: Erase complete, waiting 500ms for 'reset()'" ) ;
@@ -538,8 +571,11 @@ const Comms = {
538571
539572 //TODO Switch to an event listener when Puck will support it
540573 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 ;
543579 cb ( connected ) ;
544580 } , 1000 ) ;
545581
@@ -610,9 +646,9 @@ const Comms = {
610646 return Comms . readTextBlock ( `\x03\x10(function() {
611647var s = require("Storage").read(${ JSON . stringify ( filename ) } );
612648if (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");
616652})()\n` ) . then ( text => {
617653 return atobSafe ( text ) ;
618654 } ) ;
@@ -623,10 +659,10 @@ ${Const.CONNECTION_DEVICE}.print("\\xFF");
623659 console . log ( `<COMMS> readStorageFile ${ JSON . stringify ( filename ) } ` ) ;
624660 return Comms . readTextBlock ( `\x03\x10(function() {
625661 var f = require("Storage").open(${ JSON . stringify ( filename ) } ,"r");
626- ${ Const . CONNECTION_DEVICE } .println(f.getLength());
662+ ${ Comms . espruinoDevice } .println(f.getLength());
627663 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");
630666 })()\n` ) ;
631667 } ,
632668 // Read a non-storagefile file
0 commit comments