@@ -273,28 +273,26 @@ class FsFile {
273
273
}
274
274
275
275
/**
276
- * Adds a byte array as a file in the MicroPython filesystem .
276
+ * Adds a byte array as a file to a MicroPython Memory Map .
277
277
*
278
278
* @throws {Error } When the invalid file name is given.
279
279
* @throws {Error } When the the file doesn't have any data.
280
280
* @throws {Error } When there are issues calculating the file system boundaries.
281
281
* @throws {Error } When there is no space left for the file.
282
282
*
283
- * @param intelHex - MicroPython Intel Hex string .
283
+ * @param intelHexMap - Memory map for the MicroPython Intel Hex.
284
284
* @param filename - Name for the file.
285
285
* @param data - Byte array for the file data.
286
- * @returns MicroPython Intel Hex string with the file in the filesystem.
286
+ * @returns MicroPython Memory map with the file in the filesystem.
287
287
*/
288
- function addIntelHexFile (
289
- intelHex : string ,
288
+ function addMemMapFile (
289
+ intelHexMap : MemoryMap ,
290
290
filename : string ,
291
291
data : Uint8Array
292
- ) : string {
292
+ ) : MemoryMap {
293
293
if ( ! filename ) throw new Error ( 'File has to have a file name.' ) ;
294
294
if ( ! data . length ) throw new Error ( `File ${ filename } has to contain data.` ) ;
295
295
296
- const intelHexClean = cleanseOldHexFormat ( intelHex ) ;
297
- const intelHexMap : MemoryMap = MemoryMap . fromHex ( intelHexClean ) ;
298
296
const freeChunks = getFreeChunks ( intelHexMap ) ;
299
297
if ( freeChunks . length === 0 ) {
300
298
throw new Error ( 'There is no storage space left.' ) ;
@@ -305,6 +303,57 @@ function addIntelHexFile(
305
303
const fileFsBytes = fsFile . getFsBytes ( freeChunks ) ;
306
304
intelHexMap . set ( chunksStartAddress , fileFsBytes ) ;
307
305
setPersistentPage ( intelHexMap ) ;
306
+ return intelHexMap ;
307
+ }
308
+
309
+ /**
310
+ * Adds a byte array as a file in the MicroPython filesystem.
311
+ *
312
+ * @throws {Error } When the invalid file name is given.
313
+ * @throws {Error } When the the file doesn't have any data.
314
+ * @throws {Error } When there are issues calculating the file system boundaries.
315
+ * @throws {Error } When there is no space left for the file.
316
+ *
317
+ * @param intelHex - MicroPython Intel Hex string.
318
+ * @param filename - Name for the file.
319
+ * @param data - Byte array for the file data.
320
+ * @returns MicroPython Intel Hex string with the file in the filesystem.
321
+ */
322
+ function addIntelHexFile (
323
+ intelHex : string ,
324
+ filename : string ,
325
+ data : Uint8Array
326
+ ) : string {
327
+ // filename and data checked in addMemMapFile
328
+ const intelHexClean = cleanseOldHexFormat ( intelHex ) ;
329
+ let intelHexMap : MemoryMap = MemoryMap . fromHex ( intelHexClean ) ;
330
+ intelHexMap = addMemMapFile ( intelHexMap , filename , data ) ;
331
+ return intelHexMap . asHexString ( ) + '\n' ;
332
+ }
333
+
334
+ /**
335
+ * Adds a hash table of filenames and byte arrays as files to the MicroPython
336
+ * filesystem.
337
+ *
338
+ * @throws {Error } When the an invalid file name is given.
339
+ * @throws {Error } When the a file doesn't have any data.
340
+ * @throws {Error } When there are issues calculating the file system boundaries.
341
+ * @throws {Error } When there is no space left for a file.
342
+ *
343
+ * @param intelHex - MicroPython Intel Hex string.
344
+ * @param files - Hash table with filenames as the key and byte arrays as the
345
+ * value.
346
+ * @returns MicroPython Intel Hex string with the file in the filesystem.
347
+ */
348
+ function addIntelHexFiles (
349
+ intelHex : string ,
350
+ files : { [ filename : string ] : Uint8Array }
351
+ ) : string {
352
+ const intelHexClean = cleanseOldHexFormat ( intelHex ) ;
353
+ let intelHexMap : MemoryMap = MemoryMap . fromHex ( intelHexClean ) ;
354
+ Object . keys ( files ) . forEach ( ( filename ) => {
355
+ intelHexMap = addMemMapFile ( intelHexMap , filename , files [ filename ] ) ;
356
+ } ) ;
308
357
return intelHexMap . asHexString ( ) + '\n' ;
309
358
}
310
359
@@ -408,4 +457,4 @@ function getIntelHexFiles(
408
457
return files ;
409
458
}
410
459
411
- export { addIntelHexFile , getIntelHexFiles } ;
460
+ export { addIntelHexFile , addIntelHexFiles , getIntelHexFiles } ;
0 commit comments