Approved content

The content below is provided by partner.

filesystem 0.9.3 (microsoft/pxt-filesystem) Fork me

File system

File system driver

To use this package, go to https://pxt.microbit.org, click Add package and search for filesystem.

BETA - This package is still under development and subject to changes.

Usage

The package allows to read and write files to the micro:bit flash.

The entire file system content is ERASED when a new .hex file is download onto the micro:bit.

Writing data

files.appendLine("data.txt", "Hello");
files.appendString("data.txt", "Hello");
files.appendNumber("data.txt", 42);

Reading data

files.readToSerial("data.txt");

Settings

The package allows to save and load number settings based on the file system

files.settingsSaveNumber("calibrated", 1)
let calibrated = files.settingsReadNumber("calibrated");

File class

The File class allows to keep a file instance open, manipulate the pointer position and read from the file.

let f = files.open("data.txt");
f.flush();
f.close();
let f = files.open("data.txt");
f.setPosition(42);
let pos = f.position();

Example: Writing accelerometer data

The following program allows to collect accelerometer data and save it in a data.csv file. When the user presses button A, the micro:bit pauses for 3 seconds, then starts collecting 720 acceleration samples. Each sample is written to the file in a format that can be important by spreadsheet programs (CSV).

let file = "data.csv";
input.onButtonPressed(Button.A, () => {    
    basic.pause(3000);
    files.remove(file);
    files.appendLine(file, "Time\tAcceleration");
    for (let i = 0; i < 100; ++i) {
        let t = input.runningTime();
        let ay = input.acceleration(Dimension.Y);
        files.appendLine(file, t + "\t" + ay);
        control.waitMicros(20);
    }
});
input.onButtonPressed(Button.B, () => {
    files.readToSerial(file);
    basic.showString(":)")
})

Supported targets

License

MIT

Code of Conduct

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

filesystem=github:microsoft/pxt-filesystem#v0.9.3