Skip to content

Commit 7622351

Browse files
upy-fs-builder: Add function to calculate a MicroPython fs total size.
1 parent 392d073 commit 7622351

File tree

5 files changed

+58
-1
lines changed

5 files changed

+58
-1
lines changed

docs/quick-guide.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ var fileSizeInBytes = micropythonFs.size('filename.txt');
3131
var fileList = micropythonFs.ls();
3232

3333
var intelHexWithFs = micropythonFs.getIntelHex();
34+
var MicroPythonFsSize = micropythonFs.getFsSize();
3435
```
3536

3637
Public interface can be found in the `src/fs-interface.ts` file.
@@ -41,7 +42,7 @@ To add and remove the Python code using the old format:
4142
```js
4243
var finalHexStr = microbitFs.addIntelHexAppendedScript(originalIntelHexStr, 'print("hello world!")');
4344
if (microbitFs.isAppendedScriptPresent(finalHexStr)) {
44-
var pythonCode = microbitFs.getIntelHexAppendedScript(finalHexStr)
45+
var pythonCode = microbitFs.getIntelHexAppendedScript(finalHexStr);
4546
}
4647
```
4748

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
addIntelHexFile,
88
addIntelHexFiles,
99
getIntelHexFiles,
10+
getIntelHexFsSize,
1011
testResetFileSystem,
1112
} from '../micropython-fs-builder';
1213

@@ -562,5 +563,15 @@ describe('Reading files from the filesystem.', () => {
562563
// TODO: Read test with chunks that point to each other in an infinite loop
563564
// TODO: Read test with chunks that don't point to each other in Marker/Tail
564565
// TODO: Read test with chunks using the all the start file markers
566+
});
567+
568+
describe('Calculate sizes.', () => {
569+
it('Get how much available fs space there is in a MicroPython hex file.', () => {
570+
const totalSize = getIntelHexFsSize(uPyHexFile);
571+
572+
// Calculated by hand from the uPyHexFile v1.0.1 release.
573+
expect(totalSize).toEqual(27 * 1024);
574+
});
575+
565576
// TODO: Test calculateFileSize()
566577
});

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,17 @@ describe('Test importing files from hex.', () => {
500500
});
501501
});
502502

503+
describe('Test MicroPython hex filesystem size.', () => {
504+
it('Get how much available fs space there is in a MicroPython hex file.', () => {
505+
const micropythonFs = new MicropythonFsHex(uPyHexFile);
506+
507+
const totalSize = micropythonFs.getFsSize();
508+
509+
// Calculated by hand from the uPyHexFile v1.0.1 release.
510+
expect(totalSize).toEqual(27 * 1024);
511+
});
512+
});
513+
503514
/*
504515
import { addIntelHexAppendedScript } from '../appended-script';
505516

src/micropython-fs-builder.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,14 @@ function getIntelHexFiles(
400400
const startAddress: number = getStartAddress(hexMap);
401401
const endAddress: number = getLastPageAddress(hexMap);
402402

403+
// TODO: endAddress as the getLastPageAddress works now because this
404+
// library uses the last page as the "persistent" page, so the filesystem does
405+
// end there. In reality, the persistent page could be the first or the last
406+
// page, so we should get the end address as the magnetometer page and then
407+
// check if the persistent marker is present in the first of last page and take that
408+
// into account in the memory range calculation.
409+
// Note that the persistent marker is only present at the top of the page
410+
403411
// Iterate through the filesystem to collect used chunks and file starts
404412
const usedChunks: { [index: number]: Uint8Array } = {};
405413
const startChunkIndexes: number[] = [];
@@ -480,9 +488,25 @@ function getIntelHexFiles(
480488
return files;
481489
}
482490

491+
/**
492+
* Calculate the MicroPython filesystem size.
493+
*
494+
* @param intelHex - The MicroPython Intel Hex string.
495+
* @returns Size of the filesystem in bytes.
496+
*/
497+
function getIntelHexFsSize(intelHex: string): number {
498+
const intelHexClean = cleanseOldHexFormat(intelHex);
499+
const intelHexMap: MemoryMap = MemoryMap.fromHex(intelHexClean);
500+
const startAddress: number = getStartAddress(intelHexMap);
501+
const endAddress = getEndAddress(intelHexMap);
502+
// Remember that one page is used as persistent page
503+
return endAddress - startAddress - FLASH_PAGE_SIZE;
504+
}
505+
483506
export {
484507
addIntelHexFile,
485508
addIntelHexFiles,
486509
calculateFileSize,
487510
getIntelHexFiles,
511+
getIntelHexFsSize,
488512
};

src/micropython-fs-hex.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
addIntelHexFiles,
55
calculateFileSize,
66
getIntelHexFiles,
7+
getIntelHexFsSize,
78
} from './micropython-fs-builder';
89
import { SimpleFile } from './simple-file';
910

@@ -227,4 +228,13 @@ export class MicropythonFsHex implements FsInterface {
227228
});
228229
return addIntelHexFiles(finalHex, files);
229230
}
231+
232+
/**
233+
* Calculate the MicroPython filesystem total size.
234+
*
235+
* @returns Size of the filesystem in bytes.
236+
*/
237+
getFsSize(): number {
238+
return getIntelHexFsSize(this._intelHex);
239+
}
230240
}

0 commit comments

Comments
 (0)