Skip to content

Commit

Permalink
Adding support for writing apps to FAT
Browse files Browse the repository at this point in the history
  • Loading branch information
gfwilliams committed Dec 5, 2024
1 parent ec95ffd commit 4036ae0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
16 changes: 11 additions & 5 deletions js/appinfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,15 @@ function parseJS(storageFile, options, app) {
var AppInfo = {
/* Get a list of commands needed to upload the file */
getFileUploadCommands : (filename, data) => {
// write code in chunks, in case it is too big to fit in RAM (fix #157)
let cmd = `\x10require('Storage').write(${JSON.stringify(filename)},${asJSExpr(data.substr(0,CHUNKSIZE))},0,${data.length});`;
for (let i=CHUNKSIZE;i<data.length;i+=CHUNKSIZE)
cmd += `\n\x10require('Storage').write(${JSON.stringify(filename)},${asJSExpr(data.substr(i,CHUNKSIZE))},${i});`;
return cmd;
if (Const.FILES_IN_FS) {
return `\n\x10require('fs').writeFileSync(${JSON.stringify(filename)},${asJSExpr(data)});`;
} else {
// write code in chunks, in case it is too big to fit in RAM (fix #157)
let cmd = `\x10require('Storage').write(${JSON.stringify(filename)},${asJSExpr(data.substr(0,CHUNKSIZE))},0,${data.length});`;
for (let i=CHUNKSIZE;i<data.length;i+=CHUNKSIZE)
cmd += `\n\x10require('Storage').write(${JSON.stringify(filename)},${asJSExpr(data.substr(i,CHUNKSIZE))},${i});`;
return cmd;
}
},
/* Get a list of commands needed to upload a storage file */
getStorageFileUploadCommands : (filename, data) => {
Expand Down Expand Up @@ -278,6 +282,8 @@ var AppInfo = {
getAppInfoFilename : (app) => {
if (Const.SINGLE_APP_ONLY) // only one app on device, info file is in app.info
return "app.info";
else if (Const.FILES_IN_FS)
return "APPINFO/"+app.id+".info";
else
return app.id+".info";
},
Expand Down
15 changes: 11 additions & 4 deletions js/comms.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ console.log("=============================================")
const Comms = {
// Write the given data, returns a promise
write : (data) => new Promise((resolve,reject) => {
if (data===undefined) throw new Error("Comms.write(undefined) called!")
return Puck.write(data,function(result) {
if (result===null) return reject("");
resolve(result);
Expand Down Expand Up @@ -38,6 +39,7 @@ const Comms = {
// Reset the device, if opt=="wipe" erase any saved code
reset : (opt) => new Promise((resolve,reject) => {
let tries = 8;
if (Const.NO_RESET) return resolve();
console.log("<COMMS> reset");
Puck.write(`\x03\x10reset(${opt=="wipe"?"1":""});\n`,function rstHandler(result) {
console.log("<COMMS> reset: got "+JSON.stringify(result));
Expand All @@ -51,7 +53,7 @@ const Comms = {
} else {
console.log(`<COMMS> reset: rebooted - sending commands to clear out any boot code`);
// see https://github.com/espruino/BangleApps/issues/1759
Puck.write("\x10clearInterval();clearWatch();global.Bangle&&Bangle.removeAllListeners();E.removeAllListeners();NRF.removeAllListeners();\n",function() {
Puck.write("\x10clearInterval();clearWatch();global.Bangle&&Bangle.removeAllListeners();E.removeAllListeners();global.NRF&&NRF.removeAllListeners();\n",function() {
console.log(`<COMMS> reset: complete.`);
setTimeout(resolve,250);
});
Expand Down Expand Up @@ -244,7 +246,9 @@ const Comms = {
let device = Const.CONNECTION_DEVICE;
if (Const.SINGLE_APP_ONLY) // only one app on device, info file is in app.info
cmd = `\x10${device}.println("["+(require("Storage").read("app.info")||"null")+","+${finalJS})\n`;
else
else if (Const.FILES_IN_FS) // file in a FAT filesystem
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`;
else // the default, files in Storage
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`;
Puck.write(cmd, (appListStr,err) => {
Progress.hide({sticky:true});
Expand Down Expand Up @@ -287,7 +291,10 @@ const Comms = {
},
// Get an app's info file from Bangle.js
getAppInfo : app => {
return Comms.write(`\x10${Const.CONNECTION_DEVICE}.println(require("Storage").read(${JSON.stringify(AppInfo.getAppInfoFilename(app))})||"null")\n`).
var cmd;
if (Const.FILES_IN_FS) cmd = `\x10${Const.CONNECTION_DEVICE}.println(require("fs").readFileSync(${JSON.stringify(AppInfo.getAppInfoFilename(app))})||"null")\n`;
else cmd = `\x10${Const.CONNECTION_DEVICE}.println(require("fs").readFileSync(${JSON.stringify("APPINFO/"+AppInfo.getAppInfoFilename(app))})||"null")\n`;
return Comms.write(cmd).
then(appJSON=>{
let app;
try {
Expand Down Expand Up @@ -397,7 +404,7 @@ const Comms = {
},
// Reset the device
resetDevice : () => {
let cmd = "reset();load()\n";
let cmd = "load();\n";
return Comms.write(cmd);
},
// Check if we're connected
Expand Down
6 changes: 6 additions & 0 deletions js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ const Const = {
/* Maximum number of apps shown in the library, then a 'Show more...' entry is added.. */
MAX_APPS_SHOWN : 30,

/* If true, store files using 'fs' module which is a FAT filesystem on SD card, not on internal Storage */
FILES_IN_FS : false,

/* Don't try and reset the device when we're connecting/sending apps */
NO_RESET : false,

// APP_DATES_CSV - If set, the URL of a file to get information on the latest apps from
// APP_USAGE_JSON - If set, the URL of a file containing the most-used/most-favourited apps
};
Expand Down

0 comments on commit 4036ae0

Please sign in to comment.