Skip to content

Commit ec95ffd

Browse files
committedDec 4, 2024
Adding code to make Bangle's connection device configurable
1 parent ea066f9 commit ec95ffd

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed
 

‎js/comms.js

+14-12
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ const Comms = {
124124
}
125125
// Actually write the command with a 'print OK' at the end, and use responseHandler
126126
// to deal with the response. If OK we call uploadCmd to upload the next block
127-
Puck.write(`${cmd};${Comms.getProgressCmd(currentBytes / maxBytes)}Bluetooth.println("OK")\n`,responseHandler, true /* wait for a newline*/);
127+
Puck.write(`${cmd};${Comms.getProgressCmd(currentBytes / maxBytes)}${Const.CONNECTION_DEVICE}.println("OK")\n`,responseHandler, true /* wait for a newline*/);
128128
}
129129

130130
uploadCmd()
@@ -240,18 +240,20 @@ const Comms = {
240240
return;
241241
}
242242

243-
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()+NRF.getAddress())]).substr(1)`;
243+
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)`;
244+
let device = Const.CONNECTION_DEVICE;
244245
if (Const.SINGLE_APP_ONLY) // only one app on device, info file is in app.info
245-
cmd = `\x10Bluetooth.println("["+(require("Storage").read("app.info")||"null")+","+${finalJS})\n`;
246+
cmd = `\x10${device}.println("["+(require("Storage").read("app.info")||"null")+","+${finalJS})\n`;
246247
else
247-
cmd = `\x10Bluetooth.print("[");require("Storage").list(/\\.info$/).forEach(f=>{var j=require("Storage").readJSON(f,1)||{};Bluetooth.print(JSON.stringify({id:f.slice(0,-5),version:j.version,files:j.files,data:j.data,type:j.type})+",")});Bluetooth.println(${finalJS})\n`;
248+
cmd = `\x10${device}.print("[");require("Storage").list(/\\.info$/).forEach(f=>{var j=require("Storage").readJSON(f,1)||{};${device}.print(JSON.stringify({id:f.slice(0,-5),version:j.version,files:j.files,data:j.data,type:j.type})+",")});${device}.println(${finalJS})\n`;
248249
Puck.write(cmd, (appListStr,err) => {
249250
Progress.hide({sticky:true});
250251
// we may have received more than one line - we're looking for an array (starting with '[')
251252
var lines = appListStr ? appListStr.split("\n").map(l=>l.trim()) : [];
252253
var appListJSON = lines.find(l => l[0]=="[");
253254
// check to see if we got our data
254255
if (!appListJSON) {
256+
console.log("No JSON, just got: "+JSON.stringify(appListStr));
255257
return reject("No response from device. Is 'Programmable' set to 'Off'?");
256258
}
257259
// now try and parse
@@ -285,7 +287,7 @@ const Comms = {
285287
},
286288
// Get an app's info file from Bangle.js
287289
getAppInfo : app => {
288-
return Comms.write(`\x10Bluetooth.println(require("Storage").read(${JSON.stringify(AppInfo.getAppInfoFilename(app))})||"null")\n`).
290+
return Comms.write(`\x10${Const.CONNECTION_DEVICE}.println(require("Storage").read(${JSON.stringify(AppInfo.getAppInfoFilename(app))})||"null")\n`).
289291
then(appJSON=>{
290292
let app;
291293
try {
@@ -371,7 +373,7 @@ const Comms = {
371373
}
372374
}
373375
// Use write with newline here so we wait for it to finish
374-
let cmd = '\x10E.showMessage("Erasing...");require("Storage").eraseAll();Bluetooth.println("OK");reset()\n';
376+
let cmd = `\x10E.showMessage("Erasing...");require("Storage").eraseAll();${Const.CONNECTION_DEVICE}.println("OK");reset()\n`;
375377
Puck.write(cmd, handleResult, true /* wait for newline */);
376378
}).then(() => new Promise(resolve => {
377379
console.log("<COMMS> removeAllApps: Erase complete, waiting 500ms for 'reset()'");
@@ -489,9 +491,9 @@ const Comms = {
489491
return Comms.readTextBlock(`\x03\x10(function() {
490492
var s = require("Storage").read(${JSON.stringify(filename)});
491493
if (s===undefined) s="";
492-
Bluetooth.println(((s.length+2)/3)<<2);
493-
for (var i=0;i<s.length;i+=${CHUNKSIZE}) Bluetooth.print(btoa(s.substr(i,${CHUNKSIZE})));
494-
Bluetooth.print("\\xFF");
494+
${Const.CONNECTION_DEVICE}.println(((s.length+2)/3)<<2);
495+
for (var i=0;i<s.length;i+=${CHUNKSIZE}) ${Const.CONNECTION_DEVICE}.print(btoa(s.substr(i,${CHUNKSIZE})));
496+
${Const.CONNECTION_DEVICE}.print("\\xFF");
495497
})()\n`).then(text => {
496498
return atobSafe(text);
497499
});
@@ -502,10 +504,10 @@ Bluetooth.print("\\xFF");
502504
console.log(`<COMMS> readStorageFile ${JSON.stringify(filename)}`);
503505
return Comms.readTextBlock(`\x03\x10(function() {
504506
var f = require("Storage").open(${JSON.stringify(filename)},"r");
505-
Bluetooth.println(f.getLength());
507+
${Const.CONNECTION_DEVICE}.println(f.getLength());
506508
var l = f.readLine();
507-
while (l!==undefined) { Bluetooth.print(l); l = f.readLine(); }
508-
Bluetooth.print("\\xFF");
509+
while (l!==undefined) { ${Const.CONNECTION_DEVICE}.print(l); l = f.readLine(); }
510+
${Const.CONNECTION_DEVICE}.print("\\xFF");
509511
})()\n`);
510512
},
511513
// Read a non-storagefile file

‎js/utils.js

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ 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',
28+
2629
/* The code to upload to the device show a progress bar on the screen (should define a fn. called 'p') */
2730
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();",
2831

0 commit comments

Comments
 (0)