Skip to content

Commit 4529c6d

Browse files
committed
fix(wsen-pads): Fix ruff lint issues and missing final newlines.
1 parent f802582 commit 4529c6d

1 file changed

Lines changed: 83 additions & 67 deletions

File tree

lib/wsen-pads/examples/station_env.py

Lines changed: 83 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,71 @@
1-
from machine import I2C, SPI, Pin
1+
"""Climate station example using WSEN-PADS, HTS221 and SSD1327 OLED.
2+
3+
Reads temperature and pressure from the WSEN-PADS sensor and humidity
4+
from the HTS221 sensor. Includes a startup calibration phase to compensate
5+
sensor offsets. Displays live temperature, humidity and pressure readings
6+
with horizontal progress bars and a comfort index on a round 128x128 OLED.
7+
"""
28
from time import sleep_ms
9+
310
import ssd1327
4-
from wsen_pads import WSEN_PADS
511
from hts221 import HTS221
6-
12+
from machine import I2C, SPI, Pin
13+
from wsen_pads import WSEN_PADS
14+
715
# === Ecran ===
816
spi = SPI(1)
9-
dc = Pin("DATA_COMMAND_DISPLAY")
17+
dc = Pin("DATA_COMMAND_DISPLAY")
1018
res = Pin("RST_DISPLAY")
11-
cs = Pin("CS_DISPLAY")
19+
cs = Pin("CS_DISPLAY")
1220
display = ssd1327.WS_OLED_128X128_SPI(spi, dc, res, cs)
13-
21+
1422
# === Capteurs ===
1523
i2c = I2C(1)
16-
pads = WSEN_PADS(i2c) # pression + temperature
17-
hts = HTS221(i2c) # humidite
18-
24+
pads = WSEN_PADS(i2c)
25+
hts = HTS221(i2c)
26+
1927
# === Calibration ===
2028
print("Calibration en cours...")
2129
temps = []
22-
hums = []
30+
hums = []
2331
press = []
2432
for _ in range(10):
2533
temps.append(pads.temperature())
2634
hums.append(hts.humidity())
2735
press.append(pads.pressure_hpa())
2836
sleep_ms(200)
29-
30-
OFFSET_TEMP = 20.0 - (sum(temps) / len(temps))
31-
OFFSET_HUM = 50.0 - (sum(hums) / len(hums))
37+
38+
OFFSET_TEMP = 20.0 - (sum(temps) / len(temps))
39+
OFFSET_HUM = 50.0 - (sum(hums) / len(hums))
3240
OFFSET_PRES = 1013.0 - (sum(press) / len(press))
3341
print(f"Offset T:{OFFSET_TEMP:.2f} H:{OFFSET_HUM:.2f} P:{OFFSET_PRES:.2f}")
3442
print("Calibration OK")
35-
43+
44+
3645
# === Dessin ===
37-
def draw_hline(x, y, w, color=255):
46+
def draw_hline(x, y, w, color=15):
3847
for i in range(w):
3948
display.pixel(x + i, y, color)
40-
41-
def draw_vline(x, y, h, color=255):
49+
50+
51+
def draw_vline(x, y, h, color=15):
4252
for i in range(h):
4353
display.pixel(x, y + i, color)
44-
45-
def draw_rect(x, y, w, h, color=255):
54+
55+
56+
def draw_rect(x, y, w, h, color=15):
4657
draw_hline(x, y, w, color)
4758
draw_hline(x, y + h - 1, w, color)
4859
draw_vline(x, y, h, color)
4960
draw_vline(x + w - 1, y, h, color)
50-
51-
def draw_fill_rect(x, y, w, h, color=255):
61+
62+
63+
def draw_fill_rect(x, y, w, h, color=15):
5264
for i in range(h):
5365
draw_hline(x, y + i, w, color)
54-
55-
def draw_circle(cx, cy, r, color=255):
66+
67+
68+
def draw_circle(cx, cy, r, color=15):
5669
x = r
5770
y = 0
5871
err = 0
@@ -70,87 +83,90 @@ def draw_circle(cx, cy, r, color=255):
7083
if 2 * (err - x) + 1 > 0:
7184
x -= 1
7285
err += 1 - 2 * x
73-
74-
def draw_bar_h(x, y, w, h, value, min_val, max_val, color=200):
75-
draw_rect(x, y, w, h, 80)
86+
87+
88+
def draw_bar_h(x, y, w, h, value, min_val, max_val, color=13):
89+
draw_rect(x, y, w, h, 5)
7690
ratio = (value - min_val) / (max_val - min_val)
7791
ratio = max(0.0, min(1.0, ratio))
7892
filled = int((w - 2) * ratio)
7993
if filled > 0:
8094
draw_fill_rect(x + 1, y + 1, filled, h - 2, color)
81-
95+
96+
8297
def comfort_label(temp, hum, pres):
8398
if 18 <= temp <= 26 and 40 <= hum <= 60 and 1000 <= pres <= 1025:
84-
return "IDEAL", 255
99+
return "IDEAL", 15
85100
elif temp > 30 or hum > 75:
86-
return "CHAUD", 180
101+
return "CHAUD", 11
87102
elif temp < 15 or hum < 25:
88-
return "FROID", 150
103+
return "FROID", 9
89104
elif pres < 1000:
90-
return "BASSE P", 150
105+
return "BASSE P", 9
91106
elif pres > 1025:
92-
return "HAUTE P", 200
107+
return "HAUTE P", 13
93108
else:
94-
return "CORRECT", 200
95-
109+
return "CORRECT", 12
110+
111+
96112
def draw_screen(temp, hum, pres):
97113
display.fill(0)
98-
99-
# Bordure circulaire decorative
100-
draw_circle(64, 64, 62, 60)
101-
draw_circle(64, 64, 60, 40)
102-
114+
115+
# Bordure circulaire
116+
draw_circle(64, 64, 62, 4)
117+
draw_circle(64, 64, 60, 2)
118+
103119
# === TITRE ===
104-
display.text("CLIMAT", 35, 20, 255)
105-
draw_hline(19, 29, 90, 100)
106-
120+
display.text("CLIMAT", 35, 20, 15)
121+
draw_hline(19, 29, 90, 6)
122+
107123
# === TEMPERATURE ===
108-
display.text("T", 19, 35, 120)
124+
display.text("T", 19, 35, 7)
109125
temp_str = f"{temp:.1f}C"
110-
display.text(temp_str, 29, 35, 255)
111-
draw_bar_h(19, 44, 90, 5, temp, 0, 50, 220)
112-
126+
display.text(temp_str, 29, 35, 15)
127+
draw_bar_h(19, 44, 90, 5, temp, 0, 50, 13)
128+
113129
# === HUMIDITE ===
114-
display.text("H", 19, 52, 120)
130+
display.text("H", 19, 52, 7)
115131
hum_str = f"{hum:.1f}%"
116-
display.text(hum_str, 29, 52, 255)
117-
draw_bar_h(19, 61, 90, 5, hum, 0, 100, 180)
118-
132+
display.text(hum_str, 29, 52, 15)
133+
draw_bar_h(19, 61, 90, 5, hum, 0, 100, 11)
134+
119135
# === PRESSION ===
120-
display.text("P", 19, 69, 120)
136+
display.text("P", 19, 69, 7)
121137
pres_str = f"{pres:.0f}hPa"
122-
display.text(pres_str, 29, 69, 255)
123-
draw_bar_h(19, 78, 90, 5, pres, 950, 1050, 160)
124-
138+
display.text(pres_str, 29, 69, 15)
139+
draw_bar_h(19, 78, 90, 5, pres, 950, 1050, 10)
140+
125141
# === SEPARATEUR ===
126-
draw_hline(19, 86, 90, 70)
127-
142+
draw_hline(19, 86, 90, 4)
143+
128144
# === CONFORT ===
129145
label, c_color = comfort_label(temp, hum, pres)
130146
cx = 64 - len(label) * 4
131147
display.text(label, cx, 92, c_color)
132-
148+
133149
# === CAPTEURS ===
134-
draw_hline(19, 102, 90, 50)
135-
display.text("PADS+HTS221", 22, 106, 60)
136-
150+
draw_hline(19, 102, 90, 3)
151+
display.text("PADS+HTS221", 22, 106, 4)
152+
137153
display.show()
138-
154+
155+
139156
# === Boucle principale ===
140157
print("Station climatique demarree")
141158
while True:
142159
try:
143160
temp = pads.temperature() + OFFSET_TEMP
144-
hum = hts.humidity() + OFFSET_HUM
145-
pres = pads.pressure_hpa()+ OFFSET_PRES
146-
hum = max(0.0, min(100.0, hum))
161+
hum = hts.humidity() + OFFSET_HUM
162+
pres = pads.pressure_hpa() + OFFSET_PRES
163+
hum = max(0.0, min(100.0, hum))
147164
print(f"T:{temp:.2f}C H:{hum:.2f}% P:{pres:.1f}hPa")
148165
draw_screen(temp, hum, pres)
149166
except Exception as e:
150167
print("Erreur:", e)
151168
display.fill(0)
152-
display.text("ERREUR", 35, 55, 255)
153-
display.text(str(e)[:16], 0, 70, 150)
169+
display.text("ERREUR", 35, 55, 15)
170+
display.text(str(e)[:16], 0, 70, 9)
154171
display.show()
155172
sleep_ms(1000)
156-

0 commit comments

Comments
 (0)