diff --git a/README.md b/README.md index 6e69916e..eddf228c 100644 --- a/README.md +++ b/README.md @@ -472,6 +472,7 @@ lib// - **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`. - **Attributes**: `self.i2c` for the I2C bus, `self.address` for the device address (not `self.bus`, `self.addr`). - **I2C helpers**: use private snake_case methods `_read_reg()`, `_write_reg()` for register access. +- **Device identification**: `device_id()` — returns the device/WHO_AM_I register value. All I2C drivers with an ID register must implement this method. ### Linting diff --git a/lib/apds9960/apds9960/device.py b/lib/apds9960/apds9960/device.py index 56d07dcb..76f879fb 100644 --- a/lib/apds9960/apds9960/device.py +++ b/lib/apds9960/apds9960/device.py @@ -79,6 +79,9 @@ def __init__(self, i2c, address=APDS9960_I2C_ADDR, valid_id=APDS9960_DEV_ID): self._write_reg(APDS9960_REG_GCONF3, APDS9960_DEFAULT_GCONF3) self.set_gesture_int_enable(APDS9960_DEFAULT_GIEN) + def device_id(self): + return self.dev_id + def get_mode(self): return self._read_reg(APDS9960_REG_ENABLE) diff --git a/lib/bq27441/bq27441/device.py b/lib/bq27441/bq27441/device.py index 8928f5f4..068a1899 100644 --- a/lib/bq27441/bq27441/device.py +++ b/lib/bq27441/bq27441/device.py @@ -136,8 +136,7 @@ def disable_shutdown_mode(self): def is_valid_device(self): """Checks if device id returned matches bq27441""" - device_id = self.device_type() # Read device_type from BQ27441 - return device_id == BQ27441_DEVICE_ID + return self.device_id() == BQ27441_DEVICE_ID # Configures the design capacity of the connected battery. def set_capacity(self, capacity): @@ -353,8 +352,8 @@ def set_soci_delta(self, delta): def pulse_gpout(self): return self.execute_control_word(BQ27441_CONTROL_PULSE_SOC_INT) - # Read the device type - should be 0x0421 - def device_type(self): + # Read the device ID - should be 0x0421 + def device_id(self): return self.read_control_word(BQ27441_CONTROL_DEVICE_TYPE) def get_time_ms(self): diff --git a/lib/hts221/hts221/device.py b/lib/hts221/hts221/device.py index 2ee5839f..42a89791 100644 --- a/lib/hts221/hts221/device.py +++ b/lib/hts221/hts221/device.py @@ -57,7 +57,7 @@ def _read_reg16(self, reg): return (hi << 8) + lo # Device identification - def who_am_i(self): + def device_id(self): return self._read_reg(HTS221_WHO_AM_I) # get STATUS register diff --git a/lib/ism330dl/ism330dl/device.py b/lib/ism330dl/ism330dl/device.py index 28695ee8..9383594b 100644 --- a/lib/ism330dl/ism330dl/device.py +++ b/lib/ism330dl/ism330dl/device.py @@ -77,11 +77,11 @@ def _read_vector(self, reg): # Device check # -------------------------------------------------- - def who_am_i(self): + def device_id(self): return self._read_u8(REG_WHO_AM_I) def check_device(self): - if self.who_am_i() != ISM330DL_WHO_AM_I_VALUE: + if self.device_id() != ISM330DL_WHO_AM_I_VALUE: raise ISM330DLNotFound("ISM330DL not detected") # -------------------------------------------------- diff --git a/lib/lis2mdl/examples/magnet_test.py b/lib/lis2mdl/examples/magnet_test.py index 875b2800..c506d0ad 100644 --- a/lib/lis2mdl/examples/magnet_test.py +++ b/lib/lis2mdl/examples/magnet_test.py @@ -234,7 +234,7 @@ def test_reads(dev): print("\n=== TEST READS ===") # WHO_AM_I - who = dev.read_who_am_i() + who = dev.device_id() print(f"WHO_AM_I=0x{who:02X} expected 0x40 =>", "OK" if who == 0x40 else "FAIL") ok &= who == 0x40 @@ -652,7 +652,7 @@ def test_reboot(dev): ok &= reboot_cleared # WHO_AM_I still correct - who = dev.read_who_am_i() + who = dev.device_id() print(f"WHO_AM_I=0x{who:02X} expected 0x40 =>", "OK" if who == 0x40 else "FAIL") ok &= who == 0x40 diff --git a/lib/lis2mdl/lis2mdl/device.py b/lib/lis2mdl/lis2mdl/device.py index 08e63d67..0f353a44 100644 --- a/lib/lis2mdl/lis2mdl/device.py +++ b/lib/lis2mdl/lis2mdl/device.py @@ -290,7 +290,7 @@ def calibrate_temperature(self, ref_low, measured_low, ref_high, measured_high): # --- IDENTITY & HARDWARE OFFSETS --- - def read_who_am_i(self) -> int: + def device_id(self) -> int: """Reads WHO_AM_I (should be 0x40).""" return self._read_reg(LIS2MDL_WHO_AM_I) diff --git a/lib/vl53l1x/vl53l1x/device.py b/lib/vl53l1x/vl53l1x/device.py index 14f5487b..215635f3 100644 --- a/lib/vl53l1x/vl53l1x/device.py +++ b/lib/vl53l1x/vl53l1x/device.py @@ -103,7 +103,7 @@ def __init__(self, i2c, address=0x29): self.address = address self.reset() machine.lightsleep(1) - if self.read_model_id() != 0xEACC: + if self.device_id() != 0xEACC: raise RuntimeError("Failed to find expected ID register values. Check wiring!") # write default configuration self.i2c.writeto_mem(self.address, 0x2D, VL53L1X_DEFAULT_CONFIGURATION, addrsize=16) @@ -126,7 +126,7 @@ def _read_reg16(self, reg): data = self.i2c.readfrom_mem(self.address, reg, 2, addrsize=16) return (data[0] << 8) + data[1] - def read_model_id(self): + def device_id(self): return self._read_reg16(0x010F) def reset(self): diff --git a/tests/scenarios/apds9960.yaml b/tests/scenarios/apds9960.yaml index 9bad6c2f..bc792fce 100644 --- a/tests/scenarios/apds9960.yaml +++ b/tests/scenarios/apds9960.yaml @@ -55,6 +55,12 @@ tests: expect: 0xAB mode: [mock] + - name: "Read device ID via method" + action: call + method: device_id + expect: 0xAB + mode: [mock] + - name: "Verify device ID register (hardware)" action: read_register register: 0x92 diff --git a/tests/scenarios/hts221.yaml b/tests/scenarios/hts221.yaml index 000afeda..d13db755 100644 --- a/tests/scenarios/hts221.yaml +++ b/tests/scenarios/hts221.yaml @@ -51,7 +51,7 @@ tests: - name: "Read WHO_AM_I via method" action: call - method: who_am_i + method: device_id expect: 0xBC mode: [mock, hardware] diff --git a/tests/scenarios/ism330dl.yaml b/tests/scenarios/ism330dl.yaml index d0a73c40..42f42217 100644 --- a/tests/scenarios/ism330dl.yaml +++ b/tests/scenarios/ism330dl.yaml @@ -36,7 +36,7 @@ tests: - name: "Read WHO_AM_I via method" action: call - method: who_am_i + method: device_id expect: 0x6A mode: [mock, hardware] diff --git a/tests/scenarios/lis2mdl.yaml b/tests/scenarios/lis2mdl.yaml index 5a546a1b..b349e8b8 100644 --- a/tests/scenarios/lis2mdl.yaml +++ b/tests/scenarios/lis2mdl.yaml @@ -42,7 +42,7 @@ tests: - name: "Read WHO_AM_I via method" action: call - method: read_who_am_i + method: device_id expect: 0x40 mode: [mock, hardware] @@ -165,7 +165,7 @@ tests: action: hardware_script script: | dev.soft_reset() - result = dev.read_who_am_i() + result = dev.device_id() expect: 0x40 mode: [hardware] @@ -173,7 +173,7 @@ tests: action: hardware_script script: | dev.reboot() - result = dev.read_who_am_i() + result = dev.device_id() expect: 0x40 mode: [hardware] diff --git a/tests/scenarios/vl53l1x.yaml b/tests/scenarios/vl53l1x.yaml index e71195ff..cba21845 100644 --- a/tests/scenarios/vl53l1x.yaml +++ b/tests/scenarios/vl53l1x.yaml @@ -37,7 +37,7 @@ mock_registers: tests: - name: "Verify model ID register" action: call - method: read_model_id + method: device_id expect: 0xEACC mode: [mock, hardware]