Skip to content
php-baremetal REGISTRY
Publish a package

php-baremetal/ext-store

bm-ext MIT

Persistent key-value store: survives resets and power cuts.

ext-store · Recipe

Count boots

Count how many times the board has booted: read a number from the store, add one, write it back. The count keeps climbing across resets and power cuts.

The smallest proof that a value lives in flash rather than RAM: a counter bumped once per boot.

Goal

Print boot count : 1, 2, 3... one per reset, and write a one-time message on the very first boot.

What you need

A project with the extension. The store HAL is added by conductor and its partition by the build:

bash terminal
composer require php-baremetal/ext-store
json composer.json
{
  "name": "acme/boot-counter",
  "type": "project",
  "repositories": [
    { "type": "composer", "url": "https://repository.php-baremetal.com", "canonical": true }
  ],
  "require": {
    "php-baremetal/conductor": "^1.0",
    "php-baremetal/ext-store": "^1.0"
  },
  "config": {
    "allow-plugins": { "php-baremetal/conductor": true },
    "platform": { "php": "8.5.11" }
  },
  "extra": {
    "baremetal": {
      "sdk": "esp32@^2.0",
      "board": "waveshare-esp32-s3-touch-lcd-1.85b",
      "runtime": "init-loop",
      "storage": "embedded"
    }
  }
}

The code

setup() runs once per boot. Values are strings, so the count is cast on the way in and out. One write per boot is nothing for the flash.

php project-src/index.php
<?php
function setup(): void
{
    if (!store_available()) {
        echo "the store could not be opened\n";
        return;
    }

    $boots = (int) store_get('boots', '0') + 1;
    store_set('boots', (string) $boots);

    if ($boots === 1) {
        store_set('first_msg', 'hello from boot #1');
    }

    printf("boot count : %d\n", $boots);
    printf("first_msg  : %s\n", store_get('first_msg', '(none)'));
    printf("keys       : %s\n", implode(', ', store_keys()));
}

function loop(int $tick): void
{
    delay(5000);
}

What you will see

Flash with phpbm flash, open phpbm monitor, and reset the board a few times:

text serial output on the fourth boot
boot count : 4
first_msg  : hello from boot #1
keys       : boots, first_msg

The order of keys may differ: store_keys() is unordered.

Related