Skip to content

Commit 17508d1

Browse files
examples: Replace test script with practical examples for wsen-pads. (#228)
* chore(wsen-pads): remove test script * style(wsen-pads): remove unused Pin import * feat(wsen-pads): add pressure_trend example * feat(wsen-pads): add floor_detector example * refactor(wsen-pads): improve pressure_trend example - Compare avererage pressure to average pressure to detect trend * feat(wsen-pads): add threshold_alert example * feat(wsen-pads): add temp_pressure_display example * feat(wsen-pads): add altitude_calibration example * style(wsen-pads): fix linter errors in examples * docs(wsen-pads): add new examples * feat(wsen-pads): add weather_station example - Add a example that reccord weather condition and save them in a CVS file. * docs(wsen-pads): add weather_station example to README * style(wsen-pads): remove unused constant in floor_detector * refactor(wsen-pads): use a single read in temp_pressure_display * fix(wsen-pads): rename threshold_alert typo * fix(wsen-pads): fix weather_station example - Sleeping time put to 5s - Shorter CSV file name * style(wsen-pads): fix weather_station example formatting * test(wsen-pads): add coverage for init errors and status helpers Add missing test scenarios to improve robustness and cover critical driver behaviors: Initialization & identification: - Add test for device absence on I2C bus (WSENPADSDeviceNotFound) - Add test for invalid DEVICE_ID (WSENPADSInvalidDevice) - Add test for boot timeout when BOOT_ON never clears (WSENPADSTimeout) Continuous mode validation: - Add tests ensuring set_continuous() rejects invalid ODR values - Add tests rejecting low-noise mode at unsupported ODR (100 Hz, 200 Hz) Status helpers: - Add tests for pressure_ready(), temperature_ready(), and data_ready() - Validate behavior for all STATUS combinations (none, pressure only, temperature only, both) Reset / reboot behavior: - Add tests ensuring soft_reset() and reboot() reapply default configuration (BDU enabled, power-down mode, auto-increment enabled, low-noise disabled) Calibration: - Add test ensuring calibrate_temperature() rejects identical measured points These tests improve coverage of error paths, state transitions, and public helper methods, ensuring better reliability of the driver in both mock and hardware contexts. * docs(wsen-pads): fix README typo * chore: gitignore node modules * chore: gitignore up to date * style(wsen-pads): fix lint errors * fix(wsen-pads): fix constant temp_pressure_display * fix(wsen-pads): fix constant threshold_alert * fix(wsen-pads): Fix f-string in weather_station and floor height constant. --------- Co-authored-by: Sébastien NEDJAR <sebastien@nedjar.com>
1 parent 0f4691d commit 17508d1

14 files changed

Lines changed: 492 additions & 305 deletions

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ __pycache__
66
*.1
77
78e43b91-b6a2-4e0a-99c2-3f6f74828063_ExportBlock-935e0d48-7286-4b74-aa60-ccd18217ac01
88
node_modules/
9-
CLAUDE.md
9+
CLAUDE.md

lib/wsen-pads/README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -443,14 +443,18 @@ This computes a corrected gain and offset so that the measured temperature bette
443443

444444
Examples are available in the `examples` directory.
445445

446-
| Example | Description |
447-
| ------------- | ----------------------------------- |
448-
| `basic_reader.py` | Basic pressure and temperature read |
449-
| `continuous_reader.py` | Continuous measurement example |
450-
| `one_shot_reader.py` | One-shot measurement example |
451-
| `altitude.py` | Altitude estimation from pressure |
452-
| `test.py` | Driver test script |
453-
446+
| Example | Description |
447+
| -------------------------- | --------------------------------------------------------------------------- |
448+
| `basic_reader.py` | Basic pressure and temperature read |
449+
| `continuous_reader.py` | Continuous measurement example |
450+
| `one_shot_reader.py` | One-shot measurement example |
451+
| `altitude.py` | Altitude estimation from pressure |
452+
| `altitude_calibration.py` | Calibrate sea-level pressure from known altitude and compute corrected altitude |
453+
| `floor_detector.py` | Detect floor changes based on altitude variations |
454+
| `pressure_trend.py` | Track pressure changes over time to detect trends |
455+
| `temp_pressure_display.py` | Display formatted temperature and pressure with ASCII bar graphs |
456+
| `threshold_alert.py` | Monitor pressure and trigger an alert when a threshold is crossed |
457+
| `weather_station.py` | Monitor weather condition and register them in a CSV file on the board |
454458

455459
---
456460

lib/wsen-pads/examples/altitude.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from time import sleep
22

3-
from machine import I2C, Pin
3+
from machine import I2C
44
from wsen_pads import WSEN_PADS
55

66
SEA_LEVEL_PRESSURE = 1013.25 # depends on your location, you can adjust it for better altitude estimation
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from time import sleep
2+
3+
from machine import I2C
4+
from wsen_pads import WSEN_PADS
5+
6+
# Set your known altitude (in meters) for calibration
7+
KNOWN_ALTITUDE = 12 # Example: your location altitude
8+
9+
i2c = I2C(1)
10+
sensor = WSEN_PADS(i2c)
11+
12+
13+
pressure = sensor.pressure_hpa()
14+
15+
# Compute sea-level pressure based on known altitude
16+
# Formula derived from barometric equation
17+
sea_level_pressure = pressure / (1 - (KNOWN_ALTITUDE / 44330.0)) ** 5.255
18+
19+
print("Calibration:")
20+
print(" Measured pressure: {:.1f} hPa".format(pressure))
21+
print(" Known altitude: {:.1f} m".format(KNOWN_ALTITUDE))
22+
print(" Sea-level pressure: {:.1f} hPa".format(sea_level_pressure))
23+
print("-" * 50)
24+
25+
while True:
26+
pressure = sensor.pressure_hpa()
27+
28+
# Compute altitude using calibrated sea-level pressure
29+
altitude = 44330.0 * (1 - (pressure / sea_level_pressure) ** (1 / 5.255))
30+
31+
print("Altitude: {:6.1f} m | Pressure: {:6.1f} hPa".format(altitude, pressure))
32+
33+
sleep(1)

lib/wsen-pads/examples/basic_reader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from time import sleep
22

3-
from machine import I2C, Pin
3+
from machine import I2C
44
from wsen_pads import WSEN_PADS
55

66
# Update the I2C bus number and pins to match your board

lib/wsen-pads/examples/continuous_reader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from time import sleep
22

3-
from machine import I2C, Pin
3+
from machine import I2C
44
from wsen_pads import WSEN_PADS
55
from wsen_pads.const import ODR_10_HZ
66

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""Measure altitude at startup as baseline, then loop and detect floor changes (each ~3m altitude difference = 1 floor). Print "Floor +1", "Floor -1", etc."""
2+
3+
from time import sleep
4+
5+
from machine import I2C
6+
from wsen_pads import WSEN_PADS
7+
from wsen_pads.const import ODR_10_HZ
8+
9+
i2c = I2C(1)
10+
sensor = WSEN_PADS(i2c)
11+
sensor.set_continuous(odr=ODR_10_HZ)
12+
13+
SAMPLES_FOR_BASELINE = 20
14+
SAMPLES_PER_MEASURE = 10
15+
METERS_PER_FLOOR = 3.0
16+
17+
baseline_pressure_list = []
18+
19+
print("Measuring baseline pressure...")
20+
for _ in range(SAMPLES_FOR_BASELINE):
21+
pressure = sensor.pressure_hpa()
22+
baseline_pressure_list.append(pressure)
23+
24+
sleep(0.5)
25+
26+
baseline_pressure = sum(baseline_pressure_list) / len(baseline_pressure_list)
27+
print("Baseline pressure:", baseline_pressure, "hPa")
28+
29+
while True:
30+
pressure_samples = []
31+
32+
print("Measuring...")
33+
for _ in range(SAMPLES_PER_MEASURE):
34+
pressure = sensor.pressure_hpa()
35+
pressure_samples.append(pressure)
36+
37+
sleep(0.5)
38+
39+
avg_pressure = sum(pressure_samples) / len(pressure_samples)
40+
41+
# Calculate altitude difference from baseline using barometric formula
42+
altitude_diff = 44330 * (1 - (avg_pressure / baseline_pressure) ** (1/5.255))
43+
44+
floor_change = round(altitude_diff / METERS_PER_FLOOR)
45+
46+
print("Avg Pressure:", avg_pressure, "hPa Altitude Diff:", altitude_diff, "m Floor Change:", floor_change)

lib/wsen-pads/examples/one_shot_reader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from time import sleep
22

3-
from machine import I2C, Pin
3+
from machine import I2C
44
from wsen_pads import WSEN_PADS
55

66
i2c = I2C(1)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""Read pressure every 10s, keep the last 10 values in a list, print whether pressure is rising, falling, or stable (useful for simple weather prediction)"""
2+
3+
from time import sleep
4+
5+
from machine import I2C
6+
from wsen_pads import WSEN_PADS
7+
from wsen_pads.const import ODR_10_HZ
8+
9+
i2c = I2C(1)
10+
sensor = WSEN_PADS(i2c)
11+
12+
sensor.set_continuous(odr=ODR_10_HZ)
13+
14+
pressure_history = []
15+
MAX_VALUES = 10
16+
THRESHOLD = 0.5 # sensitivity (hPa)
17+
18+
def get_trend(values):
19+
if len(values) < 2:
20+
return "N/A"
21+
22+
half = len(values) // 2
23+
first_half_avg = sum(values[:half]) / len(values[:half])
24+
second_half_avg = sum(values[half:]) / len(values[half:])
25+
26+
diff = second_half_avg - first_half_avg
27+
28+
if abs(diff) < THRESHOLD:
29+
return "stable"
30+
elif diff > 0:
31+
return "rising"
32+
else:
33+
return "falling"
34+
35+
while True:
36+
pressure = sensor.pressure_hpa()
37+
38+
# store value
39+
pressure_history.append(pressure)
40+
41+
# keep only last 10 values
42+
if len(pressure_history) > MAX_VALUES:
43+
pressure_history.pop(0)
44+
45+
trend = get_trend(pressure_history)
46+
47+
print("P:", pressure, "hPa, Pressure is", trend)
48+
49+
sleep(10)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
Read temperature and pressure, format a nice display with units and a simple bar graph using # characters to visualize pressure (e.g. 1013.2 hPa [##########-----])
3+
"""
4+
from time import sleep
5+
6+
from machine import I2C
7+
from wsen_pads import WSEN_PADS
8+
9+
TEMP_MIN = 15.0
10+
TEMP_MAX = 30.0
11+
PRESS_MIN = 960.0
12+
PRESS_MAX = 1060.0
13+
14+
15+
i2c = I2C(1)
16+
sensor = WSEN_PADS(i2c)
17+
18+
def bar_graph(value, vmin, vmax, width=20):
19+
# Clamp value
20+
if value < vmin:
21+
value = vmin
22+
elif value > vmax:
23+
value = vmax
24+
25+
ratio = (value - vmin) / (vmax - vmin)
26+
filled = int(ratio * width)
27+
28+
return "[" + "#" * filled + "-" * (width - filled) + "]"
29+
30+
while True:
31+
pressure, temp = sensor.read()
32+
temp_bar = bar_graph(temp, TEMP_MIN, TEMP_MAX)
33+
press_bar = bar_graph(pressure, PRESS_MIN, PRESS_MAX)
34+
35+
line = "T:{:5.1f}°C {} | P:{:6.1f}hPa {}".format(
36+
temp, temp_bar, pressure, press_bar
37+
)
38+
39+
print("\r" + line, end="")
40+
41+
sleep(1)

0 commit comments

Comments
 (0)