Skip to content

Commit b1705b6

Browse files
committed
drivers: Standardize device identification to device_id().
1 parent dd5e082 commit b1705b6

12 files changed

Lines changed: 21 additions & 18 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ lib/<component>/
472472
- **Constructor signature**: `def __init__(self, i2c, ..., address=DEFAULT_ADDR)` — first parameter is always `i2c` (not `bus`), address uses keyword argument with a default from `const.py`.
473473
- **Attributes**: `self.i2c` for the I2C bus, `self.address` for the device address (not `self.bus`, `self.addr`).
474474
- **I2C helpers**: use private snake_case methods `_read_reg()`, `_write_reg()` for register access.
475+
- **Device identification**: `device_id()` — returns the device/WHO_AM_I register value. All I2C drivers with an ID register must implement this method.
475476

476477
### Linting
477478

lib/apds9960/apds9960/device.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ def __init__(self, i2c, address=APDS9960_I2C_ADDR, valid_id=APDS9960_DEV_ID):
7979
self._write_reg(APDS9960_REG_GCONF3, APDS9960_DEFAULT_GCONF3)
8080
self.set_gesture_int_enable(APDS9960_DEFAULT_GIEN)
8181

82+
def device_id(self):
83+
return self._read_reg(APDS9960_REG_ID)
84+
8285
def get_mode(self):
8386
return self._read_reg(APDS9960_REG_ENABLE)
8487

lib/bq27441/bq27441/device.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ def disable_shutdown_mode(self):
136136

137137
def is_valid_device(self):
138138
"""Checks if device id returned matches bq27441"""
139-
device_id = self.device_type() # Read device_type from BQ27441
140-
return device_id == BQ27441_DEVICE_ID
139+
return self.device_id() == BQ27441_DEVICE_ID
141140

142141
# Configures the design capacity of the connected battery.
143142
def set_capacity(self, capacity):
@@ -353,8 +352,8 @@ def set_soci_delta(self, delta):
353352
def pulse_gpout(self):
354353
return self.execute_control_word(BQ27441_CONTROL_PULSE_SOC_INT)
355354

356-
# Read the device type - should be 0x0421
357-
def device_type(self):
355+
# Read the device ID - should be 0x0421
356+
def device_id(self):
358357
return self.read_control_word(BQ27441_CONTROL_DEVICE_TYPE)
359358

360359
def get_time_ms(self):

lib/hts221/hts221/device.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def _read_reg16(self, reg):
5757
return (hi << 8) + lo
5858

5959
# Device identification
60-
def who_am_i(self):
60+
def device_id(self):
6161
return self._read_reg(HTS221_WHO_AM_I)
6262

6363
# get STATUS register

lib/ism330dl/ism330dl/device.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ def _read_vector(self, reg):
7777
# Device check
7878
# --------------------------------------------------
7979

80-
def who_am_i(self):
80+
def device_id(self):
8181
return self._read_u8(REG_WHO_AM_I)
8282

8383
def check_device(self):
84-
if self.who_am_i() != ISM330DL_WHO_AM_I_VALUE:
84+
if self.device_id() != ISM330DL_WHO_AM_I_VALUE:
8585
raise ISM330DLNotFound("ISM330DL not detected")
8686

8787
# --------------------------------------------------

lib/lis2mdl/examples/magnet_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def test_reads(dev):
234234
print("\n=== TEST READS ===")
235235

236236
# WHO_AM_I
237-
who = dev.read_who_am_i()
237+
who = dev.device_id()
238238
print(f"WHO_AM_I=0x{who:02X} expected 0x40 =>", "OK" if who == 0x40 else "FAIL")
239239
ok &= who == 0x40
240240

@@ -652,7 +652,7 @@ def test_reboot(dev):
652652
ok &= reboot_cleared
653653

654654
# WHO_AM_I still correct
655-
who = dev.read_who_am_i()
655+
who = dev.device_id()
656656
print(f"WHO_AM_I=0x{who:02X} expected 0x40 =>", "OK" if who == 0x40 else "FAIL")
657657
ok &= who == 0x40
658658

lib/lis2mdl/lis2mdl/device.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ def calibrate_temperature(self, ref_low, measured_low, ref_high, measured_high):
290290

291291
# --- IDENTITY & HARDWARE OFFSETS ---
292292

293-
def read_who_am_i(self) -> int:
293+
def device_id(self) -> int:
294294
"""Reads WHO_AM_I (should be 0x40)."""
295295
return self._read_reg(LIS2MDL_WHO_AM_I)
296296

lib/vl53l1x/vl53l1x/device.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def __init__(self, i2c, address=0x29):
103103
self.address = address
104104
self.reset()
105105
machine.lightsleep(1)
106-
if self.read_model_id() != 0xEACC:
106+
if self.device_id() != 0xEACC:
107107
raise RuntimeError("Failed to find expected ID register values. Check wiring!")
108108
# write default configuration
109109
self.i2c.writeto_mem(self.address, 0x2D, VL53L1X_DEFAULT_CONFIGURATION, addrsize=16)
@@ -126,7 +126,7 @@ def _read_reg16(self, reg):
126126
data = self.i2c.readfrom_mem(self.address, reg, 2, addrsize=16)
127127
return (data[0] << 8) + data[1]
128128

129-
def read_model_id(self):
129+
def device_id(self):
130130
return self._read_reg16(0x010F)
131131

132132
def reset(self):

tests/scenarios/hts221.yaml

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

5252
- name: "Read WHO_AM_I via method"
5353
action: call
54-
method: who_am_i
54+
method: device_id
5555
expect: 0xBC
5656
mode: [mock, hardware]
5757

tests/scenarios/ism330dl.yaml

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

3737
- name: "Read WHO_AM_I via method"
3838
action: call
39-
method: who_am_i
39+
method: device_id
4040
expect: 0x6A
4141
mode: [mock, hardware]
4242

0 commit comments

Comments
 (0)