Skip to content

Commit 6cf63d9

Browse files
committed
drivers: Standardize read and measurement method naming.
1 parent 6da03de commit 6cf63d9

13 files changed

Lines changed: 77 additions & 77 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ i2c = machine.I2C(1)
250250
apds = APDS9960(i2c)
251251

252252
apds.enable_light_sensor()
253-
light = apds.read_ambient_light()
253+
light = apds.ambient_light()
254254

255255
```
256256

@@ -476,6 +476,7 @@ lib/<component>/
476476
- **Reset methods**: `reset()` for hardware reset (pin toggle), `soft_reset()` for software reset (register write), `reboot()` for memory reboot (reload trimming).
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()`).
479+
- **Measurement methods**: bare noun for the main unit (`temperature()`, `humidity()`, `pressure()`), `read()` for combined reading returning a tuple, `<measurement>_raw()` for raw register values. No `read_` prefix on individual measurements.
479480

480481
### Linting
481482

lib/apds9960/apds9960/device.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def is_gesture_available(self):
169169
return val == APDS9960_BIT_GVALID
170170

171171
# processes a gesture event and returns best guessed gesture
172-
def read_gesture(self):
172+
def gesture(self):
173173
fifo_level = 0
174174
fifo_data = []
175175

@@ -258,7 +258,7 @@ def _ensure_proximity_enabled(self):
258258
raise OSError("APDS9960 proximity data ready timeout")
259259

260260
# reads the ambient (clear) light level as a 16-bit value
261-
def read_ambient_light(self):
261+
def ambient_light(self):
262262
self._ensure_light_enabled()
263263
# read value from clear channel, low byte register
264264
low = self._read_reg(APDS9960_REG_CDATAL)
@@ -269,7 +269,7 @@ def read_ambient_light(self):
269269
return low + (high << 8)
270270

271271
# reads the red light level as a 16-bit value
272-
def read_red_light(self):
272+
def red_light(self):
273273
self._ensure_light_enabled()
274274
# read value from red channel, low byte register
275275
low = self._read_reg(APDS9960_REG_RDATAL)
@@ -280,7 +280,7 @@ def read_red_light(self):
280280
return low + (high << 8)
281281

282282
# reads the green light level as a 16-bit value
283-
def read_green_light(self):
283+
def green_light(self):
284284
self._ensure_light_enabled()
285285
# read value from green channel, low byte register
286286
low = self._read_reg(APDS9960_REG_GDATAL)
@@ -291,7 +291,7 @@ def read_green_light(self):
291291
return low + (high << 8)
292292

293293
# reads the blue light level as a 16-bit value
294-
def read_blue_light(self):
294+
def blue_light(self):
295295
self._ensure_light_enabled()
296296
# read value from blue channel, low byte register
297297
low = self._read_reg(APDS9960_REG_BDATAL)
@@ -306,7 +306,7 @@ def read_blue_light(self):
306306
# *******************************************************************************
307307

308308
# reads the proximity level as an 8-bit value
309-
def read_proximity(self):
309+
def proximity(self):
310310
self._ensure_proximity_enabled()
311311
return self._read_reg(APDS9960_REG_PDATA)
312312

lib/apds9960/examples/ambient_light.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
oval = -1
1616
while True:
1717
sleep(0.25)
18-
val = apds.read_ambient_light()
18+
val = apds.ambient_light()
1919
if val != oval:
2020
print("AmbientLight={}".format(val))
2121
oval = val

lib/apds9960/examples/gesture.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@
3636
while True:
3737
sleep(0.5)
3838
if apds.is_gesture_available():
39-
motion = apds.read_gesture()
39+
motion = apds.gesture()
4040
print("Gesture={}".format(dirs.get(motion, "unknown")))

lib/apds9960/examples/proximity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
oval = -1
1818
while True:
1919
sleep(0.25)
20-
val = apds.read_proximity()
20+
val = apds.proximity()
2121
if val != oval:
2222
print("proximity={}".format(val))
2323
oval = val

lib/hts221/hts221/device.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,3 @@ def calibrate_temperature(self, ref_low, measured_low, ref_high, measured_high):
193193
raise ValueError("measured_low and measured_high must differ")
194194
self._temp_gain = float(ref_high - ref_low) / delta
195195
self._temp_offset = float(ref_low) - self._temp_gain * float(measured_low)
196-
197-
def get(self):
198-
h, t = self.read()
199-
return [h, t]

lib/lis2mdl/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ mag = LIS2MDL(i2c)
6363
### Read magnetic field
6464

6565
```python
66-
x, y, z = mag.read_magnet_uT()
66+
x, y, z = mag.magnetic_field_ut()
6767
print("Magnetic field (µT):", x, y, z)
6868
```
6969

@@ -181,7 +181,7 @@ Expected value: `0x40`
181181
### Read temperature (approximate)
182182

183183
```python
184-
print("Temperature (°C):", mag.read_temperature_c())
184+
print("Temperature (°C):", mag.temperature())
185185
```
186186

187187
### Check data readiness
@@ -204,12 +204,12 @@ print("Register dump:", regs)
204204

205205
| Method | Description |
206206
| ---------------------------------- | ------------------------------ |
207-
| `read_magnet_raw()` | Raw sensor values (int16) |
208-
| `read_magnet_uT()` | Magnetic field in µT |
209-
| `read_magnet_calibrated_norm()` | Calibrated and normalized data |
207+
| `magnetic_field_raw()` | Raw sensor values (int16) |
208+
| `magnetic_field_ut()` | Magnetic field in µT |
209+
| `calibrated_field()` | Calibrated and normalized data |
210210
| `heading_flat_only()` | Flat compass heading |
211211
| `heading_with_tilt_compensation()` | Tilt-corrected heading |
212-
| `read_temperature_c()` | Read relative temperature |
212+
| `temperature()` | Read relative temperature |
213213
| `power_on()` / `power_off()` | Power management |
214214
| `soft_reset()` / `reboot()` | Sensor reset functions |
215215

lib/lis2mdl/examples/magnet_test.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -245,16 +245,16 @@ def test_reads(dev):
245245
ok &= isinstance(ready, bool)
246246

247247
# MAG RAW
248-
xr, yr, zr = dev.read_magnet_raw()
248+
xr, yr, zr = dev.magnetic_field_raw()
249249
print(
250-
f"read_magnet_raw: (X,Y,Z)=({xr},{yr},{zr}) LSB =>",
250+
f"magnetic_field_raw: (X,Y,Z)=({xr},{yr},{zr}) LSB =>",
251251
"OK" if all(isinstance(v, int) for v in (xr, yr, zr)) else "FAIL",
252252
)
253253
ok &= all(isinstance(v, int) for v in (xr, yr, zr))
254254

255255
# MAG µT vs RAW
256-
xu, yu, zu = dev.read_magnet_uT()
257-
print(f"read_magnet_uT: (X,Y,Z)=({xu:.2f},{yu:.2f},{zu:.2f}) µT")
256+
xu, yu, zu = dev.magnetic_field_ut()
257+
print(f"magnetic_field_ut: (X,Y,Z)=({xu:.2f},{yu:.2f},{zu:.2f}) µT")
258258
# check consistency of conversion µT ≈ raw*0.15
259259
ok_conv = (
260260
_approx_equal(xu, xr * 0.15, 0.5)
@@ -273,17 +273,17 @@ def test_reads(dev):
273273
ok &= MAGNETIC_FIELD_MIN <= B <= MAGNETIC_FIELD_MAX # wide, since local disturbances are possible
274274

275275
# CALIBRATION NORM
276-
xc, yc, zc = dev.read_magnet_calibrated_norm()
277-
print(f"read_magnet_calibrated_norm: ({xc:.3f},{yc:.3f},{zc:.3f})")
276+
xc, yc, zc = dev.calibrated_field()
277+
print(f"calibrated_field: ({xc:.3f},{yc:.3f},{zc:.3f})")
278278
# expected: magnitudes ~[-2..+2] after simple calibration
279279
ok_cal_rng = abs(xc) < 5 and abs(yc) < 5 and abs(zc) < 5
280280
print("Calibration norm (|val|<5) =>", "OK" if ok_cal_rng else "WARN")
281281
ok &= ok_cal_rng
282282

283283
# TEMP
284-
t1 = dev.read_temperature_c()
284+
t1 = dev.temperature()
285285
sleep_ms(50)
286-
t2 = dev.read_temperature_c()
286+
t2 = dev.temperature()
287287
print(f"TempC: t1={t1:.2f}°C, t2={t2:.2f}°C (8 LSB/°C, absolute offset unknown)")
288288
# test: type & broad plausible range
289289
ok_temp = (

lib/lis2mdl/lis2mdl/device.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,9 @@ def _ensure_data(self):
178178
sleep_ms(2)
179179
raise OSError("LIS2MDL data ready timeout")
180180

181-
def read_magnet_raw(self):
182-
"""Reads the raw magnetic field (LSB). Same as read_magnet(), but more explicit."""
183-
return self.read_magnet() # (x,y,z) int16 LSB
181+
def magnetic_field_raw(self):
182+
"""Reads the raw magnetic field (LSB). Same as magnetic_field(), but more explicit."""
183+
return self.magnetic_field() # (x,y,z) int16 LSB
184184

185185
def _status(self) -> int:
186186
"""Reads STATUS_REG (0x67)."""
@@ -203,34 +203,34 @@ def _read_reg(self, reg):
203203

204204
_MAG_LSB_TO_uT = 0.15 # 1.5 mG/LSB ≈ 0.15 µT/LSB
205205

206-
def read_magnet_uT(self): # noqa: N802
206+
def magnetic_field_ut(self):
207207
"""Reads the magnetic field in µT, uncalibrated (simple conversion from LSB)."""
208-
x, y, z = self.read_magnet()
208+
x, y, z = self.magnetic_field()
209209
return (
210210
x * self._MAG_LSB_TO_uT,
211211
y * self._MAG_LSB_TO_uT,
212212
z * self._MAG_LSB_TO_uT,
213213
)
214214

215-
def read_magnet_calibrated_norm(self):
215+
def calibrated_field(self):
216216
"""Reads the calibrated field (offset/scale per axis), normalized (unitless, ~circle in XY)."""
217-
x, y, z = self.read_magnet()
217+
x, y, z = self.magnetic_field()
218218
x = (x - self.x_off) / self.x_scale
219219
y = (y - self.y_off) / self.y_scale
220220
z = (z - self.z_off) / self.z_scale
221221
return (x, y, z)
222222

223223
def magnitude_uT(self) -> float: # noqa: N802
224224
"""Total magnetic field strength (µT)."""
225-
x, y, z = self.read_magnet_uT()
225+
x, y, z = self.magnetic_field_ut()
226226
return math.sqrt(x * x + y * y + z * z)
227227

228228
@staticmethod
229229
def _to_int16(v):
230230
# Convert an unsigned 16-bit value to a signed 16-bit value.
231231
return v - 0x10000 if v & 0x8000 else v
232232

233-
def read_magnet(self):
233+
def magnetic_field(self):
234234
# Read the raw magnetic field data (X, Y, Z) from the sensor.
235235
self._ensure_data()
236236
buf = self.i2c.readfrom_mem(self.address, LIS2MDL_OUTX_L_REG | 0x80, 6)
@@ -249,7 +249,7 @@ def read_temperature_raw(self) -> int:
249249
v = (hi << 8) | lo
250250
return v - 0x10000 if (v & 0x8000) else v
251251

252-
def read_temperature_c(self) -> float:
252+
def temperature(self) -> float:
253253
"""Temperature in °C (8 LSB/°C + empirical offset).
254254
255255
The LIS2MDL temperature sensor has no guaranteed absolute zero
@@ -327,10 +327,10 @@ def read_registers(self, start_addr: int, length: int) -> bytes:
327327

328328
def read_all(self) -> dict:
329329
"""Grouped reading useful for debug & logs."""
330-
raw = self.read_magnet_raw()
331-
mag_ut = self.read_magnet_uT()
332-
cal = self.read_magnet_calibrated_norm()
333-
temp = self.read_temperature_c()
330+
raw = self.magnetic_field_raw()
331+
mag_ut = self.magnetic_field_ut()
332+
cal = self.calibrated_field()
333+
temp = self.temperature()
334334
st = self._status()
335335
return {"raw": raw, "uT": mag_ut, "cal_norm": cal, "tempC": temp, "status": st}
336336

@@ -357,7 +357,7 @@ def calibrate_minmax_2d(self, samples=300, delay_ms=20):
357357
xmax = ymax = -1e9
358358

359359
for _ in range(samples):
360-
x, y, _ = self.read_magnet()
360+
x, y, _ = self.magnetic_field()
361361
xmin = min(xmin, x)
362362
xmax = max(xmax, x)
363363
ymin = min(ymin, y)
@@ -382,7 +382,7 @@ def calibrate_minmax_3d(self, samples=600, delay_ms=20):
382382
xmax = ymax = zmax = -1e9
383383

384384
for _ in range(samples):
385-
x, y, z = self.read_magnet()
385+
x, y, z = self.magnetic_field()
386386
xmin = min(xmin, x)
387387
xmax = max(xmax, x)
388388
ymin = min(ymin, y)
@@ -419,7 +419,7 @@ def calibrate_quality(self, samples_check=200, delay_ms=10):
419419
ys = []
420420
zs = []
421421
for _ in range(samples_check):
422-
x, y, z = self.read_magnet()
422+
x, y, z = self.magnetic_field()
423423
xc, yc, zc = self.calibrate_apply(x, y, z)
424424
xs.append(xc)
425425
ys.append(yc)
@@ -536,7 +536,7 @@ def heading_flat_only(self):
536536
Reads the sensor and returns the angle (0..360°) assuming the board is FLAT.
537537
Uses XY (no tilt compensation).
538538
"""
539-
x, y, z = self.read_magnet()
539+
x, y, z = self.magnetic_field()
540540
return self.heading_from_vectors(x, y, z, calibrated=True)
541541

542542
def heading_with_tilt_compensation(self, read_accel):
@@ -545,7 +545,7 @@ def heading_with_tilt_compensation(self, read_accel):
545545
read_accel() must return (ax, ay, az) ~g.
546546
"""
547547

548-
x, y, z = self.read_magnet()
548+
x, y, z = self.magnetic_field()
549549
# 3D calibration
550550
x = (x - self.x_off) / (self.x_scale or 1.0)
551551
y = (y - self.y_off) / (self.y_scale or 1.0)

lib/vl53l1x/vl53l1x/device.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,12 @@ def _ensure_data(self):
164164
machine.lightsleep(10)
165165
raise OSError("VL53L1X data ready timeout")
166166

167-
def read(self):
167+
def distance(self):
168168
self._ensure_data()
169169
data = self.i2c.readfrom_mem(self.address, 0x0089, 17, addrsize=16)
170-
final_crosstalk_corrected_range_mm_sd0 = (data[13] << 8) + data[14]
170+
distance_mm = (data[13] << 8) + data[14]
171171
self._clear_interrupt()
172-
return final_crosstalk_corrected_range_mm_sd0
172+
return distance_mm
173+
174+
def read(self):
175+
return self.distance()

0 commit comments

Comments
 (0)