Storage & Persistent Data
Two ways to keep data on the board, and one guarantee that covers both: values live in the board's storage partition, not the application image, so they survive re-flashing the app. (Erasing the whole flash wipes them.)
- Store — typed key/value settings for device state: counters, calibration, flags. One namespace per store.
- File — whole-file text I/O for things a person might read or edit: notes, exported JSON, a boot log.
There is no session on either one — nothing to set up first. Everything is ready the moment your program runs.
Store
Typed key/value settings. Construct a store with a namespace; every read and write rides that namespace, so several stores can coexist without colliding.
Example first
store.ts
import { Store, UART0 } from '@typecad/hal';
const settings = new Store('app');
const boots = settings.getInt('boots', 1); // first boot reads the default: 1
if (boots === 1) {
UART0.writeLine('first run');
}
settings.setInt('boots', boots + 1);
settings.setString('ssid', 'MyNet');
settings.setFloat('offset', -1.25);
settings.setBool('calibrated', true);
UART0.writeLine(settings.getString('ssid', '(none)'));remove / clear remove(key): void
cleanup.ts
settings.remove('ssid'); // delete one key
settings.clear(); // delete every tc/app/* key this program wroteAll methods
| Member | Description |
|---|---|
| new Store(namespace) | One namespace per store — 'app' becomes the tc/app/ settings subtree |
| getInt / getFloat / getString / getBool(key, default) | Typed read; the default is required — unset keys read back exactly what you chose |
| setInt / setFloat / setString / setBool(key, value) | Typed write, committed per key |
| remove(key) | Delete one key |
| clear() | Delete every key this program wrote in its namespace |
File
Whole-file text I/O. Construct with the path; write, read, exists, and remove are all there is.
Example first
file.ts
import { File } from '@typecad/hal';
const log = new File('/log.txt');
log.write('boot\n'); // overwrite (creates the file when absent)
if (log.exists()) {
UART0.writeLine(log.read()); // 'boot\n'
log.remove();
}A boot counter that survives re-flashing is two lines:
boot-counter.ts
const boot = new File('/boot-count.txt');
boot.write(String(Number(boot.read() || '0') + 1));All methods
| Member | Description |
|---|---|
| new File(path) | Whole-file text I/O — no open()/close() |
| write(text) | Overwrite (creates the file when absent); there is no append |
| read() | The whole file — '' for a missing file and an empty file alike |
| exists() | Distinguish missing from empty |
| remove() | Delete the file |
Which one to use
| Store | File | |
|---|---|---|
| Shape | Many typed keys under a namespace | One whole text document per path |
| Best at | Counters, calibration, flags | Notes, exported JSON, logs a person reads |
| Writes | Per-key commits | Whole-file rewrites |
| Types | Typed pairs, required defaults | None — it's text |