File system - beta
Перейти к файлу
Peli de Halleux 28e44bd91e
Update README.md
2020-05-07 13:50:22 -07:00
.vscode fixing tasks 2017-04-20 00:31:11 -07:00
.gitattributes missing gitattributes 2017-05-30 13:29:16 -07:00
.gitignore init support for files 2016-11-02 16:29:30 -07:00
.travis.yml added travis file 2018-05-23 08:52:51 -07:00
LICENSE.txt init support for files 2016-11-02 16:29:30 -07:00
README.md Update README.md 2020-05-07 13:50:22 -07:00
enums.d.ts updated for v1 2018-11-06 08:24:27 -08:00
files.cpp updated for v1 2018-11-06 08:24:27 -08:00
files.ts Correct block display from close to flush 2018-07-18 00:59:18 -07:00
icon.png Added icon 2017-04-20 13:15:41 -07:00
pxt.json 0.10.0 2018-11-06 08:25:02 -08:00
shims.d.ts updated for v1 2018-11-06 08:24:27 -08:00
tests.ts updated for v1 2018-11-06 08:24:27 -08:00
tsconfig.json init support for files 2016-11-02 16:29:30 -07:00

README.md

File system driver Build Status

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

~ hint

DEPRECATED - This package is no longer maintained or supported.

~

Usage

The package allows to read and write files to the @boardname@ flash.

~hint

The entire file system content is ERASED when a new .hex file is download onto the @boardname@.

~

Writing data

  • append text and a new line character
files.appendLine("data.txt", "Hello");
  • append text to the file
files.appendString("data.txt", "Hello");
  • append a number (as text) to the file
files.appendNumber("data.txt", 42);

Reading data

  • send the content of a file to serial
files.readToSerial("data.txt");

Settings

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

  • save setting value
files.settingsSaveNumber("calibrated", 1)
  • read setting value
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.

  • open, flush or close the file
let f = files.open("data.txt");
f.flush();
f.close();
  • write strings or buffers
let f = files.open("data.txt");
f.writeString("yay");
  • read data
let f = files.open("data.txt");
let buf = f.readBuffer(64);
let c = f.read();
  • set the cursor position
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 @boardname@ 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

  • for PXT/ microbit
  • for PXT/ calliope

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.