Skip to content

Commit d24611d

Browse files
upy-fs-builder: Add optional parameter to add files and return Uint8Array. (#15)
1 parent fbe327a commit d24611d

File tree

5 files changed

+65
-7
lines changed

5 files changed

+65
-7
lines changed

docs/quick-guide.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ var fsSize = micropythonFs.getStorageSize();
3838
var fsAvailableSize = micropythonFs.getStorageUsed();
3939
var fsUsedSize = micropythonFs.getStorageRemaining();
4040

41-
// Generate a new hex string with MicroPython and the files
42-
var intelHexWithFs = micropythonFs.getIntelHex();
41+
// Generate a new hex string or Uint8Array with MicroPython and the files
42+
var intelHexStrWithFs = micropythonFs.getIntelHex();
43+
var intelHexBytesWithFs = micropythonFs.getIntelHexBytes();
4344
```
4445

4546
Public interface can be found in the `src/fs-interface.ts` file.

src/__tests__/micropython-fs-builder.spec.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ describe('Writing files to the filesystem.', () => {
165165
},
166166
];
167167

168-
it('Add files to hex.', () => {
168+
it('Add files to hex, return a string.', () => {
169169
const fwWithFsOther = addIntelHexFiles(uPyHexFile, {
170170
[files[0].fileName]: strToBytes(files[0].fileStr),
171171
[files[1].fileName]: strToBytes(files[1].fileStr),
@@ -183,6 +183,28 @@ describe('Writing files to the filesystem.', () => {
183183
expect(file1data).toEqual(files[1].bytes());
184184
});
185185

186+
it('Add files to hex, return a byte array', () => {
187+
const fwWithFsBytes = addIntelHexFiles(
188+
uPyHexFile,
189+
{
190+
[files[0].fileName]: strToBytes(files[0].fileStr),
191+
[files[1].fileName]: strToBytes(files[1].fileStr),
192+
},
193+
true
194+
);
195+
196+
// Address calculated starting at the top of the MicroPython v1.0.1 fs
197+
const startFs = 0x38c00;
198+
const file0data = fwWithFsBytes.slice(startFs, startFs + files[0].fsSize);
199+
const file1start = startFs + files[0].fsSize;
200+
const file1data = fwWithFsBytes.slice(
201+
file1start,
202+
file1start + files[1].fsSize
203+
);
204+
expect(file0data).toEqual(files[0].bytes());
205+
expect(file1data).toEqual(files[1].bytes());
206+
});
207+
186208
// A chunk using up the last byte will also use the next and leave it empty
187209
const fullChunkPlus = {
188210
fileName: 'one_chunk_plus.py',

src/__tests__/micropython-fs-hex.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,18 @@ describe('Test hex generation.', () => {
368368

369369
expect(addIntelHexFilesSpy.mock.calls.length).toEqual(1);
370370
expect(addIntelHexFilesSpy.mock.calls[0][0]).toBe(uPyHexFile);
371+
expect(addIntelHexFilesSpy.mock.calls[0][2]).toBeFalsy();
372+
});
373+
374+
it('getIntelHexBytes called with constructor hex string.', () => {
375+
const microbitFs = new MicropythonFsHex(uPyHexFile);
376+
microbitFs.write('a.txt', 'content');
377+
378+
const returnedIntelHex = microbitFs.getIntelHexBytes();
379+
380+
expect(addIntelHexFilesSpy.mock.calls.length).toEqual(1);
381+
expect(addIntelHexFilesSpy.mock.calls[0][0]).toBe(uPyHexFile);
382+
expect(addIntelHexFilesSpy.mock.calls[0][2]).toBeTruthy();
371383
});
372384

373385
it('getIntelHex called with argument hex string.', () => {

src/micropython-fs-builder.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,14 +387,17 @@ function addIntelHexFile(
387387
*/
388388
function addIntelHexFiles(
389389
intelHex: string,
390-
files: { [filename: string]: Uint8Array }
391-
): string {
390+
files: { [filename: string]: Uint8Array },
391+
returnBytes: boolean = false
392+
): string | Uint8Array {
392393
const intelHexClean = cleanseOldHexFormat(intelHex);
393394
let intelHexMap: MemoryMap = MemoryMap.fromHex(intelHexClean);
394395
Object.keys(files).forEach((filename) => {
395396
intelHexMap = addMemMapFile(intelHexMap, filename, files[filename]);
396397
});
397-
return intelHexMap.asHexString() + '\n';
398+
return returnBytes
399+
? intelHexMap.slicePad(0, FLASH_END)
400+
: intelHexMap.asHexString() + '\n';
398401
}
399402

400403
/**

src/micropython-fs-hex.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,26 @@ export class MicropythonFsHex implements FsInterface {
260260
Object.values(this._files).forEach((file) => {
261261
files[file.filename] = file.getBytes();
262262
});
263-
return addIntelHexFiles(finalHex, files);
263+
return addIntelHexFiles(finalHex, files) as string;
264+
}
265+
266+
/**
267+
* Generate a byte array of the MicroPython and filesystem data.
268+
*
269+
* @throws {Error} When a file doesn't have any data.
270+
* @throws {Error} When there are issues calculating file system boundaries.
271+
* @throws {Error} When there is no space left for a file.
272+
*
273+
* @param intelHex - Optionally provide a different Intel Hex to include the
274+
* filesystem into.
275+
* @returns A Uint8Array with MicroPython and the filesystem included.
276+
*/
277+
getIntelHexBytes(intelHex?: string): Uint8Array {
278+
const finalHex = intelHex || this._intelHex;
279+
const files: { [filename: string]: Uint8Array } = {};
280+
Object.values(this._files).forEach((file) => {
281+
files[file.filename] = file.getBytes();
282+
});
283+
return addIntelHexFiles(finalHex, files, true) as Uint8Array;
264284
}
265285
}

0 commit comments

Comments
 (0)