Skip to content

Commit 92f8795

Browse files
steami_config: Add boot counter. (#352)
* feat(steami_config): add boot counter functionality * feat(steami_config): add boot counter example * docs(steami_config): add boot counter to readme * test(steami_config): add boot counter mock and hardware tests * fix(steami_config): Shorten boot counter JSON key to "bc" for consistency. * fix(steami_config): Fix missing variable assignment in README boot counter. * docs(steami_config): Improve boot counter section wording in README. * style(steami_config): Remove trailing spaces in README. * style(steami_config): Fix comment indentation in boot counter tests. --------- Co-authored-by: Sébastien NEDJAR <sebastien@nedjar.com>
1 parent 49e8153 commit 92f8795

4 files changed

Lines changed: 221 additions & 1 deletion

File tree

lib/steami_config/README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,36 @@ config.apply_accelerometer_calibration(imu)
145145

146146
---
147147

148+
## Boot Counter
149+
150+
Track how many times the board has booted.
151+
152+
### Set boot counter
153+
```python
154+
config.set_boot_count(0)
155+
```
156+
157+
### Get boot counter
158+
```python
159+
count = config.get_boot_count()
160+
print("Boot count:", count)
161+
# -> Boot count: 0
162+
```
163+
164+
### Increment boot counter
165+
```python
166+
config.increment_boot_count()
167+
# -> boot_count = boot_count + 1
168+
```
169+
170+
---
171+
148172
# JSON Format
149173

150174
Data is stored as compact JSON to fit within 1 KB:
151175

152176
```json
153-
{"rev":3,"name":"STeaMi-01","tc":{"hts":{"g":1.0,"o":-0.5}},"cm":{"hx":12.3,"hy":-5.1,"hz":0.8,"sx":1.01,"sy":0.98,"sz":1.0},"ca":{"ox":0.01,"oy":-0.02,"oz":0.03}}
177+
{"rev":3,"name":"STeaMi-01","tc":{"hts":{"g":1.0,"o":-0.5}},"cm":{"hx":12.3,"hy":-5.1,"hz":0.8,"sx":1.01,"sy":0.98,"sz":1.0},"ca":{"ox":0.01,"oy":-0.02,"oz":0.03},"bc":0}
154178
```
155179

156180
| Key | Content |
@@ -165,6 +189,7 @@ Data is stored as compact JSON to fit within 1 KB:
165189
| `cm.sx/sy/sz` | Soft-iron scale factors (X, Y, Z) |
166190
| `ca` | Accelerometer calibration dict |
167191
| `ca.ox/oy/oz` | Bias offsets in g (X, Y, Z) |
192+
| `bc` | Boot counter |
168193

169194
Sensor short keys: `hts` (HTS221), `mag` (LIS2MDL), `ism` (ISM330DL),
170195
`hid` (WSEN-HIDS), `pad` (WSEN-PADS).
@@ -179,6 +204,7 @@ Sensor short keys: `hts` (HTS221), `mag` (LIS2MDL), `ism` (ISM330DL),
179204
| `calibrate_temperature.py` | Calibrate all sensors against WSEN-HIDS reference |
180205
| `calibrate_magnetometer.py` | Calibrate LIS2MDL with OLED display and persistent storage |
181206
| `calibrate_accelerometer.py` | Calibrate ISM330DL accelerometer bias and persist it |
207+
| `boot_counter.py` | Display the amount of boot and increment it |
182208

183209
Run with mpremote:
184210

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
Boot counter example.
3+
Each time the board boots,
4+
it increments the boot count
5+
and saves it to non-volatile storage.
6+
"""
7+
8+
from daplink_bridge import DaplinkBridge
9+
from machine import I2C
10+
from steami_config import SteamiConfig
11+
12+
# --- Hardware init ---
13+
i2c = I2C(1)
14+
bridge = DaplinkBridge(i2c)
15+
16+
config = SteamiConfig(bridge)
17+
config.load()
18+
19+
# --- Boot counter logic ---
20+
config.increment_boot_count()
21+
22+
# Save updated value
23+
config.save()
24+
25+
# Read and display
26+
count = config.get_boot_count()
27+
28+
print("Boot count:", count)

lib/steami_config/steami_config/device.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,22 @@ def apply_accelerometer_calibration(self, ism330dl_instance):
254254
cal["oy"],
255255
cal["oz"],
256256
)
257+
258+
# --------------------------------------------------
259+
# Boot counter
260+
# --------------------------------------------------
261+
262+
def set_boot_count(self, count):
263+
"""Store the number of times the board has booted."""
264+
self._data["bc"] = int(count)
265+
266+
def get_boot_count(self):
267+
"""Return the stored boot count, or None."""
268+
return self._data.get("bc")
269+
270+
def increment_boot_count(self):
271+
"""Increment the stored boot count by 1."""
272+
count = self.get_boot_count()
273+
if count is None:
274+
count = 0
275+
self.set_boot_count(count + 1)

tests/scenarios/steami_config.yaml

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,72 @@ tests:
338338
expect_true: true
339339
mode: [mock]
340340

341+
# -- Mock boot counter --
342+
- name: "Set and get boot counter"
343+
action: script
344+
script: |
345+
dev._data = {}
346+
dev.set_boot_count(427)
347+
result = dev.get_boot_count() == 427
348+
expect_true: true
349+
mode: [mock]
350+
351+
- name: "Get boot counter returns None when not set"
352+
action: script
353+
script: |
354+
dev._data = {}
355+
result = dev.get_boot_count() is None
356+
expect_true: true
357+
mode: [mock]
358+
359+
- name: "Increment boot counter from empty starts at 1"
360+
action: script
361+
script: |
362+
dev._data = {}
363+
dev.increment_boot_count()
364+
result = dev.get_boot_count() == 1
365+
expect_true: true
366+
mode: [mock]
367+
368+
- name: "Increment boot counter increases existing value"
369+
action: script
370+
script: |
371+
dev._data = {}
372+
dev.set_boot_count(41)
373+
dev.increment_boot_count()
374+
result = dev.get_boot_count() == 42
375+
expect_true: true
376+
mode: [mock]
377+
378+
- name: "Boot counter survives save/load"
379+
action: script
380+
script: |
381+
dev._data = {}
382+
dev.set_boot_count(427)
383+
dev.save()
384+
dev2 = SteamiConfig(dev._bridge)
385+
dev2.load()
386+
result = dev2.get_boot_count() == 427
387+
expect_true: true
388+
mode: [mock]
389+
390+
- name: "Boot counter coexists with other config values"
391+
action: script
392+
script: |
393+
dev._data = {}
394+
dev.board_revision = 3
395+
dev.board_name = "STeaMi-Test"
396+
dev.set_boot_count(12)
397+
dev.set_accelerometer_calibration(ox=0.01, oy=-0.02, oz=0.03)
398+
result = (
399+
dev.board_revision == 3
400+
and dev.board_name == "STeaMi-Test"
401+
and dev.get_boot_count() == 12
402+
and dev.get_accelerometer_calibration()["ox"] == 0.01
403+
)
404+
expect_true: true
405+
mode: [mock]
406+
341407
# ----- Hardware -----
342408

343409
- name: "Save and load config on hardware"
@@ -541,3 +607,84 @@ tests:
541607
)
542608
expect_true: true
543609
mode: [hardware]
610+
611+
# -- Hardware Boot Counter --
612+
- name: "Save and load boot counter on hardware"
613+
action: script
614+
script: |
615+
from time import sleep_ms
616+
dev._bridge.clear_config()
617+
dev._data = {}
618+
dev.set_boot_count(427)
619+
dev.save()
620+
sleep_ms(200)
621+
dev2 = SteamiConfig(dev._bridge)
622+
dev2.load()
623+
result = dev2.get_boot_count() == 427
624+
expect_true: true
625+
mode: [hardware]
626+
627+
- name: "Increment boot counter on hardware"
628+
action: script
629+
script: |
630+
from time import sleep_ms
631+
dev._bridge.clear_config()
632+
dev._data = {}
633+
dev.set_boot_count(5)
634+
dev.save()
635+
sleep_ms(200)
636+
637+
dev2 = SteamiConfig(dev._bridge)
638+
dev2.load()
639+
dev2.increment_boot_count()
640+
dev2.save()
641+
sleep_ms(200)
642+
643+
dev3 = SteamiConfig(dev._bridge)
644+
dev3.load()
645+
result = dev3.get_boot_count() == 6
646+
expect_true: true
647+
mode: [hardware]
648+
649+
- name: "Boot counter survives clear_flash"
650+
action: script
651+
script: |
652+
from time import sleep_ms
653+
from daplink_flash import DaplinkFlash
654+
dev._bridge.clear_config()
655+
dev._data = {}
656+
dev.set_boot_count(99)
657+
dev.save()
658+
sleep_ms(200)
659+
660+
flash = DaplinkFlash(dev._bridge)
661+
flash.clear_flash()
662+
sleep_ms(500)
663+
664+
dev2 = SteamiConfig(dev._bridge)
665+
dev2.load()
666+
result = dev2.get_boot_count() == 99
667+
expect_true: true
668+
mode: [hardware]
669+
670+
- name: "Boot counter coexists with temperature calibration on hardware"
671+
action: script
672+
script: |
673+
from time import sleep_ms
674+
dev._bridge.clear_config()
675+
dev._data = {}
676+
dev.set_boot_count(12)
677+
dev.set_temperature_calibration("hts221", gain=1.05, offset=-0.3)
678+
dev.save()
679+
sleep_ms(200)
680+
681+
dev2 = SteamiConfig(dev._bridge)
682+
dev2.load()
683+
tc = dev2.get_temperature_calibration("hts221")
684+
result = (
685+
dev2.get_boot_count() == 12
686+
and tc["gain"] == 1.05
687+
and tc["offset"] == -0.3
688+
)
689+
expect_true: true
690+
mode: [hardware]

0 commit comments

Comments
 (0)