Skip to content

Commit 13ab67b

Browse files
committed
support for uploading to SD card
1 parent 1116f3c commit 13ab67b

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

plugins/saveOnSend.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@
5858
code = Espruino.Core.Utils.asUTF8Bytes(code);
5959
// Depending on settings, choose how we package code for upload
6060
var isFlashPersistent = Espruino.Config.SAVE_ON_SEND == 2;
61-
var isStorageUpload = Espruino.Config.SAVE_ON_SEND == 3;
62-
var isFlashUpload = Espruino.Config.SAVE_ON_SEND == 1 || isFlashPersistent || isStorageUpload;
63-
if (!isFlashUpload) return callback(code);
61+
var isStorageUpload = Espruino.Config.SAVE_ON_SEND == Espruino.Core.Send.SEND_MODE_STORAGE;
62+
var isSDCardUpload = Espruino.Config.SAVE_ON_SEND == Espruino.Core.Send.SEND_MODE_SDCARD;
63+
var isFlashUpload = Espruino.Config.SAVE_ON_SEND == Espruino.Core.Send.SEND_MODE_FLASH || isFlashPersistent || isStorageUpload;
64+
if (!isFlashUpload && !isSDCardUpload) return callback(code);
6465

6566
var asJS = Espruino.Core.Utils.toJSONishString;
6667

@@ -75,17 +76,27 @@
7576
hasStorage = true;
7677
}
7778
}
79+
const CHUNKSIZE = 1024;
7880

7981
// Now create the commands to do the upload
8082
console.log("Uploading "+code.length+" bytes to flash");
83+
// FIXME: We should use Serial's Connection class packet stuff for file uploads
8184
if (!hasStorage) { // old style
82-
if (isStorageUpload) {
85+
if (isStorageUpload || isSDCardUpload) {
8386
Espruino.Core.Notifications.error("You have pre-1v96 firmware - unable to upload to Storage");
8487
code = "";
8588
} else {
8689
Espruino.Core.Notifications.error("You have pre-1v96 firmware. Upload size is limited by available RAM");
87-
code = "E.setBootCode("+asJS(code)+(isFlashPersistent?",true":"")+");load()\n";
90+
code = "E.setBootCode("+asJS(code)+(isFlashPersistent?",true":"")+");";
8891
}
92+
} else if (isSDCardUpload) {
93+
var filename = Espruino.Config.SAVE_STORAGE_FILE;;
94+
var newCode = [ `let _ul = E.openFile(${asJS(filename)},"w");` ];
95+
var len = code.length;
96+
for (var i=0;i<len;i+=CHUNKSIZE)
97+
newCode.push(`_ul.write(${asJS(code.substr(i,CHUNKSIZE))});`);
98+
newCode.push(`_ul.close();delete _ul;`);
99+
code = newCode.join("\n");
89100
} else { // new style
90101
var filename;
91102
if (isStorageUpload)
@@ -96,19 +107,19 @@
96107
Espruino.Core.Notifications.error("Invalid Storage file name "+JSON.stringify(filename));
97108
code = "";
98109
} else {
99-
var CHUNKSIZE = 1024;
100110
var newCode = [];
101111
var len = code.length;
102112
newCode.push('require("Storage").write('+asJS(filename)+','+asJS(code.substr(0,CHUNKSIZE))+',0,'+len+');');
103113
for (var i=CHUNKSIZE;i<len;i+=CHUNKSIZE)
104114
newCode.push('require("Storage").write('+asJS(filename)+','+asJS(code.substr(i,CHUNKSIZE))+','+i+');');
105115
code = newCode.join("\n");
106-
if (Espruino.Config.LOAD_STORAGE_FILE==2 && isStorageUpload)
107-
code += "\nload("+asJS(filename)+")\n";
108-
else if (Espruino.Config.LOAD_STORAGE_FILE!=0)
109-
code += "\nload()\n";
110116
}
111117
}
118+
if (Espruino.Config.LOAD_STORAGE_FILE==2 && isStorageUpload)
119+
code += "\nload("+asJS(filename)+")\n";
120+
else if (Espruino.Config.LOAD_STORAGE_FILE!=0)
121+
code += "\nload()\n";
122+
else code += "\n";
112123
callback(code);
113124
}
114125

0 commit comments

Comments
 (0)