Skip to content

Commit 4036ae0

Browse files
committed
Adding support for writing apps to FAT
1 parent ec95ffd commit 4036ae0

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

js/appinfo.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,15 @@ function parseJS(storageFile, options, app) {
175175
var AppInfo = {
176176
/* Get a list of commands needed to upload the file */
177177
getFileUploadCommands : (filename, data) => {
178-
// write code in chunks, in case it is too big to fit in RAM (fix #157)
179-
let cmd = `\x10require('Storage').write(${JSON.stringify(filename)},${asJSExpr(data.substr(0,CHUNKSIZE))},0,${data.length});`;
180-
for (let i=CHUNKSIZE;i<data.length;i+=CHUNKSIZE)
181-
cmd += `\n\x10require('Storage').write(${JSON.stringify(filename)},${asJSExpr(data.substr(i,CHUNKSIZE))},${i});`;
182-
return cmd;
178+
if (Const.FILES_IN_FS) {
179+
return `\n\x10require('fs').writeFileSync(${JSON.stringify(filename)},${asJSExpr(data)});`;
180+
} else {
181+
// write code in chunks, in case it is too big to fit in RAM (fix #157)
182+
let cmd = `\x10require('Storage').write(${JSON.stringify(filename)},${asJSExpr(data.substr(0,CHUNKSIZE))},0,${data.length});`;
183+
for (let i=CHUNKSIZE;i<data.length;i+=CHUNKSIZE)
184+
cmd += `\n\x10require('Storage').write(${JSON.stringify(filename)},${asJSExpr(data.substr(i,CHUNKSIZE))},${i});`;
185+
return cmd;
186+
}
183187
},
184188
/* Get a list of commands needed to upload a storage file */
185189
getStorageFileUploadCommands : (filename, data) => {
@@ -278,6 +282,8 @@ var AppInfo = {
278282
getAppInfoFilename : (app) => {
279283
if (Const.SINGLE_APP_ONLY) // only one app on device, info file is in app.info
280284
return "app.info";
285+
else if (Const.FILES_IN_FS)
286+
return "APPINFO/"+app.id+".info";
281287
else
282288
return app.id+".info";
283289
},

js/comms.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ console.log("=============================================")
88
const Comms = {
99
// Write the given data, returns a promise
1010
write : (data) => new Promise((resolve,reject) => {
11+
if (data===undefined) throw new Error("Comms.write(undefined) called!")
1112
return Puck.write(data,function(result) {
1213
if (result===null) return reject("");
1314
resolve(result);
@@ -38,6 +39,7 @@ const Comms = {
3839
// Reset the device, if opt=="wipe" erase any saved code
3940
reset : (opt) => new Promise((resolve,reject) => {
4041
let tries = 8;
42+
if (Const.NO_RESET) return resolve();
4143
console.log("<COMMS> reset");
4244
Puck.write(`\x03\x10reset(${opt=="wipe"?"1":""});\n`,function rstHandler(result) {
4345
console.log("<COMMS> reset: got "+JSON.stringify(result));
@@ -51,7 +53,7 @@ const Comms = {
5153
} else {
5254
console.log(`<COMMS> reset: rebooted - sending commands to clear out any boot code`);
5355
// see https://github.com/espruino/BangleApps/issues/1759
54-
Puck.write("\x10clearInterval();clearWatch();global.Bangle&&Bangle.removeAllListeners();E.removeAllListeners();NRF.removeAllListeners();\n",function() {
56+
Puck.write("\x10clearInterval();clearWatch();global.Bangle&&Bangle.removeAllListeners();E.removeAllListeners();global.NRF&&NRF.removeAllListeners();\n",function() {
5557
console.log(`<COMMS> reset: complete.`);
5658
setTimeout(resolve,250);
5759
});
@@ -244,7 +246,9 @@ const Comms = {
244246
let device = Const.CONNECTION_DEVICE;
245247
if (Const.SINGLE_APP_ONLY) // only one app on device, info file is in app.info
246248
cmd = `\x10${device}.println("["+(require("Storage").read("app.info")||"null")+","+${finalJS})\n`;
247-
else
249+
else if (Const.FILES_IN_FS) // file in a FAT filesystem
250+
cmd = `\x10${device}.print("[");if (!require("fs").statSync("APPINFO"))require("fs").mkdir("APPINFO");require("fs").readdirSync("APPINFO").forEach(f=>{var j=JSON.parse(require("fs").readFileSync("APPINFO/"+f))||"{}";${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`;
251+
else // the default, files in Storage
248252
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`;
249253
Puck.write(cmd, (appListStr,err) => {
250254
Progress.hide({sticky:true});
@@ -287,7 +291,10 @@ const Comms = {
287291
},
288292
// Get an app's info file from Bangle.js
289293
getAppInfo : app => {
290-
return Comms.write(`\x10${Const.CONNECTION_DEVICE}.println(require("Storage").read(${JSON.stringify(AppInfo.getAppInfoFilename(app))})||"null")\n`).
294+
var cmd;
295+
if (Const.FILES_IN_FS) cmd = `\x10${Const.CONNECTION_DEVICE}.println(require("fs").readFileSync(${JSON.stringify(AppInfo.getAppInfoFilename(app))})||"null")\n`;
296+
else cmd = `\x10${Const.CONNECTION_DEVICE}.println(require("fs").readFileSync(${JSON.stringify("APPINFO/"+AppInfo.getAppInfoFilename(app))})||"null")\n`;
297+
return Comms.write(cmd).
291298
then(appJSON=>{
292299
let app;
293300
try {
@@ -397,7 +404,7 @@ const Comms = {
397404
},
398405
// Reset the device
399406
resetDevice : () => {
400-
let cmd = "reset();load()\n";
407+
let cmd = "load();\n";
401408
return Comms.write(cmd);
402409
},
403410
// Check if we're connected

js/utils.js

+6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ const Const = {
3232
/* Maximum number of apps shown in the library, then a 'Show more...' entry is added.. */
3333
MAX_APPS_SHOWN : 30,
3434

35+
/* If true, store files using 'fs' module which is a FAT filesystem on SD card, not on internal Storage */
36+
FILES_IN_FS : false,
37+
38+
/* Don't try and reset the device when we're connecting/sending apps */
39+
NO_RESET : false,
40+
3541
// APP_DATES_CSV - If set, the URL of a file to get information on the latest apps from
3642
// APP_USAGE_JSON - If set, the URL of a file containing the most-used/most-favourited apps
3743
};

0 commit comments

Comments
 (0)