Skip to content

Commit 2865f11

Browse files
committed
Handle automatically finding the connected device name on connection if it's not been explicitly specified with Const.CONNECTION_DEVICE
1 parent 3bccb42 commit 2865f11

File tree

2 files changed

+53
-15
lines changed

2 files changed

+53
-15
lines changed

Diff for: js/comms.js

+49-13
Original file line numberDiff line numberDiff line change
@@ -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() {
611647
var s = require("Storage").read(${JSON.stringify(filename)});
612648
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");
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

Diff for: js/utils.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ const Const = {
2323
/* Message to display when an app has been loaded */
2424
MESSAGE_RELOAD : 'Hold BTN3\nto reload',
2525

26-
/* What device are we connecting to Espruino with? */
27-
CONNECTION_DEVICE : 'Bluetooth',
26+
/* What device are we connecting to Espruino with as far as Espruino is concerned?
27+
Eg if CONNECTION_DEVICE="Bluetooth" will Bluetooth.println("Hi") send data back to us?
28+
Leave this as undefined to try and work it out. */
29+
CONNECTION_DEVICE : undefined,
2830

2931
/* The code to upload to the device show a progress bar on the screen (should define a fn. called 'p') */
3032
CODE_PROGRESSBAR : "g.drawRect(10,g.getHeight()-16,g.getWidth()-10,g.getHeight()-8).flip();p=x=>g.fillRect(10,g.getHeight()-16,10+(g.getWidth()-20)*x/100,g.getHeight()-8).flip();",

0 commit comments

Comments
 (0)