Skip to content

Commit bea0762

Browse files
committed
examples: Add temp_pressure_display for wsen-pads.
1 parent 972923b commit bea0762

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 machine import I2C
5+
from time import sleep
6+
7+
from wsen_pads import WSEN_PADS
8+
9+
TEMP_MIN = 15.0
10+
TEMP_MAX = 30.0
11+
PRESS_MIN = 1020.0
12+
PRESS_MAX = 1023.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+
temp = sensor.temperature()
32+
pressure = sensor.pressure_hpa()
33+
34+
temp_bar = bar_graph(temp, TEMP_MIN, TEMP_MAX)
35+
press_bar = bar_graph(pressure, PRESS_MIN, PRESS_MAX)
36+
37+
line = "T:{:5.1f}°C {} | P:{:6.1f}hPa {}".format(
38+
temp, temp_bar, pressure, press_bar
39+
)
40+
41+
print("\r" + line, end="")
42+
43+
sleep(1)

0 commit comments

Comments
 (0)