-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
290 lines (219 loc) · 7.32 KB
/
test.py
File metadata and controls
290 lines (219 loc) · 7.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
from machine import I2C, Pin
from time import sleep
from wsen_pads import WSEN_PADS
from wsen_pads.const import (
ODR_1_HZ,
ODR_10_HZ,
REG_CTRL_1,
REG_CTRL_2,
REG_STATUS,
REG_INT_SOURCE,
)
def print_header(title):
print()
print("=" * 60)
print(title)
print("=" * 60)
def print_pass(name):
print("[PASS] {}".format(name))
def print_fail(name, err=None):
if err is None:
print("[FAIL] {}".format(name))
else:
print("[FAIL] {} -> {}".format(name, err))
def dump_registers(sensor):
ctrl1 = sensor._read_u8(REG_CTRL_1)
ctrl2 = sensor._read_u8(REG_CTRL_2)
int_source = sensor._read_u8(REG_INT_SOURCE)
print("CTRL_1 = 0x{:02X}".format(ctrl1))
print("CTRL_2 = 0x{:02X}".format(ctrl2))
print("data_ready() =", sensor.data_ready())
print("INT_SOURCE = 0x{:02X}".format(int_source))
def test_i2c_scan(i2c):
print_header("1) I2C scan")
devices = i2c.scan()
print("I2C devices found:", [hex(x) for x in devices])
if 0x5C in devices or 0x5D in devices:
print_pass("WSEN-PADS address found")
return True
else:
print_fail("WSEN-PADS address not found")
return False
def test_device_id(sensor):
print_header("2) Device ID")
dev_id = sensor.device_id()
print("Device ID:", hex(dev_id))
if dev_id == 0xB3:
print_pass("Device ID matches 0xB3")
return True
else:
print_fail("Device ID matches 0xB3", hex(dev_id))
return False
def test_default_registers(sensor):
print_header("3) Default driver configuration")
dump_registers(sensor)
ctrl1 = sensor._read_u8(REG_CTRL_1)
ctrl2 = sensor._read_u8(REG_CTRL_2)
# BDU should be enabled by the driver
bdu_ok = bool(ctrl1 & 0x02)
# IF_ADD_INC should be enabled by the driver
if_add_inc_ok = bool(ctrl2 & 0x10)
if bdu_ok:
print_pass("BDU enabled")
else:
print_fail("BDU enabled")
if if_add_inc_ok:
print_pass("IF_ADD_INC enabled")
else:
print_fail("IF_ADD_INC enabled")
return bdu_ok and if_add_inc_ok
def test_soft_reset(sensor):
print_header("4) Soft reset")
try:
sensor.soft_reset()
sleep(0.05)
dump_registers(sensor)
if sensor.device_id() == 0xB3:
print_pass("Soft reset")
return True
else:
print_fail("Soft reset", "device ID mismatch after reset")
return False
except Exception as err:
print_fail("Soft reset", err)
return False
def test_reboot(sensor):
print_header("5) Reboot")
try:
sensor.reboot()
sleep(1)
dump_registers(sensor)
if sensor.device_id() == 0xB3:
print_pass("Reboot")
return True
else:
print_fail("Reboot", "device ID mismatch after reboot")
return False
except Exception as err:
print_fail("Reboot", err)
return False
def test_one_shot(sensor):
print_header("6) One-shot read")
try:
pressure_hpa, temperature_c = sensor.read_one_shot()
raw_p = sensor.pressure_raw()
raw_t = sensor.temperature_raw()
print("Raw pressure :", raw_p)
print("Raw temperature :", raw_t)
print("Pressure : {:.2f} hPa".format(pressure_hpa))
print("Temperature : {:.2f} °C".format(temperature_c))
print("data_ready() :", sensor.data_ready())
# Basic sanity checks
MIN_PRESSURE = 260.0
MAX_PRESSURE = 1260.0
MIN_TEMPERATURE = -40.0
MAX_TEMPERATURE = 85.0
pressure_ok = MIN_PRESSURE <= pressure_hpa <= MAX_PRESSURE
temperature_ok = MIN_TEMPERATURE <= temperature_c <= MAX_TEMPERATURE
raw_ok = not (raw_p == 0 and raw_t == 0)
if raw_ok:
print_pass("Raw data is not all zero")
else:
print_fail("Raw data is not all zero")
if pressure_ok:
print_pass("Pressure is in a valid range")
else:
print_fail("Pressure is in a valid range", pressure_hpa)
if temperature_ok:
print_pass("Temperature is in a valid range")
else:
print_fail("Temperature is in a valid range", temperature_c)
return raw_ok and pressure_ok and temperature_ok
except Exception as err:
print_fail("One-shot read", err)
return False
def test_continuous_mode(sensor, odr, label, wait_s=2):
print_header("7) Continuous mode - {}".format(label))
try:
sensor.set_continuous(odr=odr)
print("Waiting {} second(s) for fresh samples...".format(wait_s))
sleep(wait_s)
ok = True
for i in range(5):
pressure_hpa = sensor.pressure()
temperature_c = sensor.temperature()
raw_p = sensor.pressure_raw()
raw_t = sensor.temperature_raw()
print(
"#{:d} P={:.2f} hPa T={:.2f} °C rawP={} rawT={} ready={}".format(
i + 1,
pressure_hpa,
temperature_c,
raw_p,
raw_t,
sensor.data_ready(),
)
)
if raw_p == 0 and raw_t == 0:
ok = False
sleep(0.5)
sensor.power_off()
if ok:
print_pass("Continuous mode - {}".format(label))
else:
print_fail("Continuous mode - {}".format(label), "raw data stayed at zero")
return ok
except Exception as err:
print_fail("Continuous mode - {}".format(label), err)
return False
def test_status_flags(sensor):
print_header("8) STATUS helpers")
try:
sensor.set_continuous(odr=ODR_1_HZ)
sleep(1.5)
p_avail = sensor.pressure_ready()
t_avail = sensor.temperature_ready()
ready = sensor.data_ready()
print("pressure_ready() =", p_avail)
print("temperature_ready() =", t_avail)
print("data_ready() =", ready)
sensor.power_off()
if p_avail or t_avail or ready:
print_pass("STATUS helper methods")
return True
else:
print_fail("STATUS helper methods")
return False
except Exception as err:
print_fail("STATUS helper methods", err)
return False
def main():
print_header("WSEN-PADS full driver test")
i2c = I2C(1)
if not test_i2c_scan(i2c):
print()
print("Stop: sensor not found on I2C bus.")
return
try:
sensor = WSEN_PADS(i2c)
except Exception as err:
print_fail("Driver init", err)
return
results = []
results.append(test_device_id(sensor))
results.append(test_default_registers(sensor))
results.append(test_soft_reset(sensor))
results.append(test_reboot(sensor))
results.append(test_one_shot(sensor))
results.append(test_continuous_mode(sensor, ODR_1_HZ, "1 Hz", wait_s=2))
results.append(test_continuous_mode(sensor, ODR_10_HZ, "10 Hz", wait_s=1))
results.append(test_status_flags(sensor))
print_header("Final result")
passed = sum(1 for x in results if x)
total = len(results)
print("Passed: {}/{}".format(passed, total))
if passed == total:
print("All tests passed.")
else:
print("Some tests failed.")
main()