Skip to content

Commit bf57b26

Browse files
committed
drivers: Standardize continuous and one-shot mode method naming.
1 parent cc0dee0 commit bf57b26

10 files changed

Lines changed: 102 additions & 9 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ lib/<component>/
477477
- **Power methods**: `power_on()` / `power_off()`. All drivers must implement both.
478478
- **Status methods**: `_status()` returns the raw status register as an int (private), `data_ready()` returns True when all channels have new data, `<measurement>_ready()` for per-channel readiness (e.g. `temperature_ready()`, `pressure_ready()`).
479479
- **Measurement methods**: bare noun without unit suffix only for `temperature()` (°C) and `humidity()` (%RH). All others include the unit: `pressure_hpa()`, `distance_mm()`, `voltage_mv()`, `acceleration_g()`, etc. `read()` for combined reading returning a tuple, `<measurement>_raw()` for raw register values.
480+
- **Mode methods**: `set_continuous(odr)` to start continuous measurements, `trigger_one_shot()` for a single conversion, `read_one_shot()` for trigger + wait + return data.
480481

481482
### Linting
482483

lib/hts221/hts221/device.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ def set_odr(self, odr=0):
9191
t = self._read_reg(HTS221_CTRL_REG1) & 0xFC
9292
self._write_reg(HTS221_CTRL_REG1, t | odr)
9393

94+
def set_continuous(self, odr=1):
95+
if odr == 0:
96+
raise ValueError("ODR 0 is one-shot mode, use trigger_one_shot()")
97+
self.power_on()
98+
self.set_odr(odr)
99+
94100
# get/set Humidity and temperature average configuration
95101
def get_av(self):
96102
return self._read_reg(HTS221_AV_CONF)
@@ -117,6 +123,19 @@ def trigger_one_shot(self):
117123
self._write_reg(HTS221_CTRL_REG2, ctrl2 | HTS221_CTRL2_ONE_SHOT)
118124
sleep_ms(15)
119125

126+
def read_one_shot(self):
127+
self.trigger_one_shot()
128+
h = self._read_reg16(HTS221_HUMIDITY_OUT_L)
129+
t = self._read_reg16(HTS221_TEMP_OUT_L)
130+
humidity = self.H0_rH + (self.H1_rH - self.H0_rH) * (h - self.H0_OUT) / (
131+
self.H1_OUT - self.H0_OUT
132+
)
133+
factory = self.T0_degC + (self.T1_degC - self.T0_degC) * (t - self.T0_OUT) / (
134+
self.T1_OUT - self.T0_OUT
135+
)
136+
temperature = self._temp_gain * factory + self._temp_offset
137+
return humidity, temperature
138+
120139
def reboot(self):
121140
ctrl2 = self._read_reg(HTS221_CTRL_REG2)
122141
self._write_reg(HTS221_CTRL_REG2, ctrl2 | HTS221_CTRL2_BOOT)

lib/lis2mdl/lis2mdl/device.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,21 @@ def set_odr(self, hz: int):
7676
reg = (reg & ~(0b11 << 2)) | (odr_bits << 2)
7777
self._write_reg(LIS2MDL_CFG_REG_A, reg)
7878

79+
def set_continuous(self, hz=10):
80+
self.set_odr(hz)
81+
self.set_mode("continuous")
82+
83+
def trigger_one_shot(self):
84+
self.set_mode("single")
85+
86+
def read_one_shot(self):
87+
self.trigger_one_shot()
88+
for _ in range(50):
89+
if self.data_ready():
90+
return self.magnetic_field()
91+
sleep_ms(2)
92+
raise OSError("LIS2MDL one-shot data ready timeout")
93+
7994
def set_low_power(self, enabled: bool):
8095
# LP bit (bit4) : 0=High-Res, 1=Low-Power
8196
reg = self._read_reg(LIS2MDL_CFG_REG_A)

lib/wsen-hids/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ This allows simple usage:
121121
```python
122122
humidity, temperature = sensor.read()
123123

124-
Continuous measurements can be enabled with sensor.set_continuous_mode().
124+
Continuous measurements can be enabled with sensor.set_continuous().
125125

126126
# One-Shot Measurement
127127

@@ -144,7 +144,7 @@ sensor.read_one_shot(timeout_ms=500)
144144
Start continuous measurements:
145145

146146
```python
147-
sensor.set_continuous_mode(WSEN_HIDS.ODR_1_HZ)
147+
sensor.set_continuous(WSEN_HIDS.ODR_1_HZ)
148148
```
149149

150150
Available output data rates:

lib/wsen-hids/examples/continuous_mode.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
sensor = WSEN_HIDS(i2c)
99

10-
sensor.set_continuous_mode(WSEN_HIDS.ODR_1_HZ)
10+
sensor.set_continuous(WSEN_HIDS.ODR_1_HZ)
1111

1212
for _ in range(10):
1313
humidity, temperature = sensor.read()

lib/wsen-hids/examples/full_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,13 @@ def test_continuous_mode(sensor, odr, label, wait_ms=1500, loops=5, delay_s=0.5)
211211
print_header("7) Continuous mode - {}".format(label))
212212

213213
try:
214-
sensor.set_continuous_mode(odr=odr)
214+
sensor.set_continuous(odr=odr)
215215

216216
ctrl1 = read_reg(sensor, REG_CTRL_1)
217217
pd_ok = bool(ctrl1 & CTRL_1_PD)
218218
odr_ok = (ctrl1 & CTRL_1_ODR_MASK) == odr
219219

220-
print("CTRL_1 after set_continuous_mode = 0x{:02X}".format(ctrl1))
220+
print("CTRL_1 after set_continuous = 0x{:02X}".format(ctrl1))
221221

222222
if pd_ok:
223223
print_pass("PD bit set")
@@ -283,7 +283,7 @@ def test_status_helpers(sensor):
283283
print_header("8) STATUS helpers")
284284

285285
try:
286-
sensor.set_continuous_mode(odr=ODR_1_HZ)
286+
sensor.set_continuous(odr=ODR_1_HZ)
287287
sleep(1.5)
288288

289289
h_avail = sensor.humidity_ready()
@@ -312,7 +312,7 @@ def test_unitary_methods(sensor):
312312
print_header("9) humidity() and temperature()")
313313

314314
try:
315-
sensor.set_continuous_mode(odr=ODR_1_HZ)
315+
sensor.set_continuous(odr=ODR_1_HZ)
316316
sleep(1.2)
317317

318318
humidity_rh = sensor.humidity()

lib/wsen-hids/wsen_hids/device.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class WSEN_HIDS(object):
1818
- humidity()
1919
- temperature()
2020
- read_one_shot()
21-
- set_continuous_mode()
21+
- set_continuous()
2222
- set_one_shot_mode()
2323
- enable_bdu()
2424
- enable_heater()
@@ -182,7 +182,7 @@ def set_one_shot_mode(self):
182182
ctrl1 &= ~CTRL_1_ODR_MASK # ODR = 00 => one-shot
183183
self._write_reg(REG_CTRL_1, ctrl1)
184184

185-
def set_continuous_mode(self, odr=ODR_1_HZ):
185+
def set_continuous(self, odr=ODR_1_HZ):
186186
if odr not in (ODR_1_HZ, ODR_7_HZ, ODR_12_5_HZ):
187187
raise ValueError("Invalid ODR for continuous mode")
188188

tests/scenarios/hts221.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,24 @@ tests:
108108
expect_not_none: true
109109
mode: [mock]
110110

111+
- name: "set_continuous enables sensor"
112+
action: script
113+
script: |
114+
dev.power_off()
115+
dev.set_continuous(1)
116+
ctrl1 = i2c.readfrom_mem(dev.address, 0x20, 1)[0]
117+
result = (ctrl1 & 0x80) != 0 and (ctrl1 & 0x03) != 0
118+
expect_true: true
119+
mode: [mock]
120+
121+
- name: "read_one_shot returns tuple"
122+
action: script
123+
script: |
124+
h, t = dev.read_one_shot()
125+
result = isinstance(h, float) and isinstance(t, float)
126+
expect_true: true
127+
mode: [mock]
128+
111129
- name: "Auto-trigger humidity after power_off"
112130
action: call
113131
setup:

tests/scenarios/lis2mdl.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,36 @@ tests:
8686
expect_range: [19.0, 21.0]
8787
mode: [mock]
8888

89+
# ----- Continuous / one-shot -----
90+
91+
- name: "set_continuous writes CFG_REG_A"
92+
action: script
93+
script: |
94+
i2c.clear_write_log()
95+
dev.set_continuous(10)
96+
log = i2c.get_write_log()
97+
wrote_cfg = any(reg == 0x60 for reg, data in log)
98+
result = wrote_cfg
99+
expect_true: true
100+
mode: [mock]
101+
102+
- name: "trigger_one_shot sets single mode"
103+
action: script
104+
script: |
105+
dev.trigger_one_shot()
106+
mode = dev.get_mode()
107+
result = mode == "single"
108+
expect_true: true
109+
mode: [mock]
110+
111+
- name: "read_one_shot returns tuple of 3 ints"
112+
action: script
113+
script: |
114+
x, y, z = dev.read_one_shot()
115+
result = isinstance(x, int) and isinstance(y, int) and isinstance(z, int)
116+
expect_true: true
117+
mode: [mock]
118+
89119
# ----- Auto-trigger -----
90120

91121
- name: "Magnetic field readable after power down"

tests/scenarios/wsen_hids.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ tests:
7777
expect_range: [24.0, 26.0]
7878
mode: [mock]
7979

80+
- name: "set_continuous activates sensor"
81+
action: script
82+
script: |
83+
dev.power_off()
84+
dev.set_continuous()
85+
ctrl1 = i2c.readfrom_mem(dev.address, 0x20, 1)[0]
86+
result = (ctrl1 & 0x80) != 0
87+
expect_true: true
88+
mode: [mock]
89+
8090
- name: "Power off clears PD bit"
8191
action: script
8292
script: |

0 commit comments

Comments
 (0)