Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ lib/<component>/
- **Device identification**: `device_id()` — returns the device/WHO_AM_I register value. All I2C drivers with an ID register must implement this method.
- **Reset methods**: `reset()` for hardware reset (pin toggle), `soft_reset()` for software reset (register write), `reboot()` for memory reboot (reload trimming).
- **Power methods**: `power_on()` / `power_off()`. All drivers must implement both.
- **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()`).

### Linting

Expand Down
26 changes: 13 additions & 13 deletions lib/apds9960/apds9960/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ def __init__(self, i2c, address=APDS9960_I2C_ADDR, valid_id=APDS9960_DEV_ID):
def device_id(self):
return self.dev_id

def _status(self):
return self._read_reg(APDS9960_REG_STATUS)

def data_ready(self):
s = self._status()
return bool((s & APDS9960_BIT_AVALID) and (s & APDS9960_BIT_PVALID))

def get_mode(self):
return self._read_reg(APDS9960_REG_ENABLE)

Expand Down Expand Up @@ -224,25 +231,18 @@ def power_off(self):
# ambient light and color sensor controls
# *******************************************************************************

# check if there is new light data available
def is_light_available(self):
val = self._read_reg(APDS9960_REG_STATUS)

# shift and mask out AVALID bit
val &= APDS9960_BIT_AVALID

return val == APDS9960_BIT_AVALID
def light_ready(self):
return bool(self._status() & APDS9960_BIT_AVALID)

def is_proximity_available(self):
val = self._read_reg(APDS9960_REG_STATUS)
return (val & APDS9960_BIT_PVALID) == APDS9960_BIT_PVALID
def proximity_ready(self):
return bool(self._status() & APDS9960_BIT_PVALID)

def _ensure_light_enabled(self):
enable = self.get_mode()
if not (enable & (1 << APDS9960_MODE_AMBIENT_LIGHT)) or not (enable & APDS9960_BIT_PON):
self.enable_light_sensor(interrupts=False)
for _ in range(50):
if self.is_light_available():
if self.light_ready():
return
sleep_ms(10)
raise OSError("APDS9960 light data ready timeout")
Expand All @@ -252,7 +252,7 @@ def _ensure_proximity_enabled(self):
if not (enable & (1 << APDS9960_MODE_PROXIMITY)) or not (enable & APDS9960_BIT_PON):
self.enable_proximity_sensor(interrupts=False)
for _ in range(50):
if self.is_proximity_available():
if self.proximity_ready():
return
sleep_ms(10)
raise OSError("APDS9960 proximity data ready timeout")
Expand Down
4 changes: 2 additions & 2 deletions lib/bq27441/bq27441/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,13 +419,13 @@ def flags(self):
return self.read_word(BQ27441_COMMAND_FLAGS)

# Read the CONTROL_STATUS subcommand of control()
def status(self):
def _control_status(self):
return self.read_control_word(BQ27441_CONTROL_STATUS)

# Private Functions
# Check if the BQ27441 - G1A is sealed or not.
def sealed(self):
stat = self.status()
stat = self._control_status()
return stat & BQ27441_STATUS_SS

# Seal the BQ27441 - G1A
Expand Down
12 changes: 11 additions & 1 deletion lib/hts221/hts221/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,19 @@ def device_id(self):
return self._read_reg(HTS221_WHO_AM_I)

# get STATUS register
def status(self):
def _status(self):
return self._read_reg(HTS221_STATUS_REG)

def data_ready(self):
s = self._status()
return bool((s & HTS221_STATUS_T_DA) and (s & HTS221_STATUS_H_DA))

Comment thread
nedseb marked this conversation as resolved.
def temperature_ready(self):
return bool(self._status() & HTS221_STATUS_T_DA)

def humidity_ready(self):
return bool(self._status() & HTS221_STATUS_H_DA)

# power control
def power_off(self):
t = self._read_reg(HTS221_CTRL_REG1) & 0x7F
Expand Down
22 changes: 15 additions & 7 deletions lib/ism330dl/ism330dl/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,21 @@ def motion(self):
# Status
# --------------------------------------------------

def status(self):
s = self._read_u8(REG_STATUS_REG)
return {
"temp_ready": bool(s & STATUS_TDA),
"gyro_ready": bool(s & STATUS_GDA),
"accel_ready": bool(s & STATUS_XLDA),
}
def _status(self):
return self._read_u8(REG_STATUS_REG)

def accel_ready(self):
return bool(self._status() & STATUS_XLDA)

def gyro_ready(self):
return bool(self._status() & STATUS_GDA)

def temperature_ready(self):
return bool(self._status() & STATUS_TDA)

def data_ready(self):
s = self._status()
return bool((s & STATUS_XLDA) and (s & STATUS_GDA) and (s & STATUS_TDA))

# --------------------------------------------------
# Power
Expand Down
5 changes: 1 addition & 4 deletions lib/lis2mdl/examples/magnet_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,7 @@ def test_reads(dev):
print(f"WHO_AM_I=0x{who:02X} expected 0x40 =>", "OK" if who == 0x40 else "FAIL")
ok &= who == 0x40

# STATUS & DATA READY
st1 = dev.read_status()
print(f"Initial STATUS=0x{st1:02X}")
# wait a few ms to let a new frame arrive
# DATA READY
sleep_ms(50)
ready = dev.data_ready()
print("data_ready():", ready, "=>", "OK" if isinstance(ready, bool) else "FAIL")
Expand Down
6 changes: 3 additions & 3 deletions lib/lis2mdl/lis2mdl/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,13 @@ def read_magnet_raw(self):
"""Reads the raw magnetic field (LSB). Same as read_magnet(), but more explicit."""
return self.read_magnet() # (x,y,z) int16 LSB

def read_status(self) -> int:
def _status(self) -> int:
"""Reads STATUS_REG (0x67)."""
return self._read_reg(LIS2MDL_STATUS_REG)

Comment thread
nedseb marked this conversation as resolved.
def data_ready(self) -> bool:
"""True if a new XYZ triplet is ready (Zyxda bit)."""
return bool(self.read_status() & (1 << 3))
return bool(self._status() & (1 << 3))

def read_int_source(self) -> int:
"""Reads INT_SOURCE_REG (0x64): source of the interrupt."""
Expand Down Expand Up @@ -331,7 +331,7 @@ def read_all(self) -> dict:
mag_ut = self.read_magnet_uT()
cal = self.read_magnet_calibrated_norm()
temp = self.read_temperature_c()
st = self.read_status()
st = self._status()
return {"raw": raw, "uT": mag_ut, "cal_norm": cal, "tempC": temp, "status": st}

##
Expand Down
6 changes: 3 additions & 3 deletions lib/vl53l1x/vl53l1x/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def start_ranging(self):
def stop_ranging(self):
self._write_reg(0x0087, 0x00)

def _is_data_ready(self):
def data_ready(self):
polarity = self._read_reg(0x0030) & 0x10
ready_val = 1 if polarity == 0 else 0
return (self._read_reg(0x0031) & 0x01) == ready_val
Expand All @@ -156,10 +156,10 @@ def _clear_interrupt(self):
self._write_reg(0x0086, 0x01)

def _ensure_data(self):
if not self._is_data_ready():
if not self.data_ready():
self.start_ranging()
for _ in range(100):
if self._is_data_ready():
if self.data_ready():
return
machine.lightsleep(10)
raise OSError("VL53L1X data ready timeout")
Expand Down
18 changes: 3 additions & 15 deletions lib/wsen-hids/examples/full_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,12 @@ def test_one_shot(sensor):
try:
humidity_rh, temperature_c = sensor.read_one_shot(timeout_ms=500)

status = sensor.status()
h_ready = sensor.humidity_ready()
t_ready = sensor.temperature_ready()
ready = sensor.data_ready()

print("Humidity : {:.2f} %RH".format(humidity_rh))
print("Temperature : {:.2f} °C".format(temperature_c))
print("STATUS : 0x{:02X}".format(status))
print("humidity_ready :", h_ready)
print("temperature_ready:", t_ready)
print("data_ready :", ready)
Expand Down Expand Up @@ -241,14 +239,12 @@ def test_continuous_mode(sensor, odr, label, wait_ms=1500, loops=5, delay_s=0.5)

for i in range(loops):
humidity_rh, temperature_c = sensor.read()
status = sensor.status()

print(
"#{:d} H={:.2f} %RH T={:.2f} °C STATUS=0x{:02X}".format(
"#{:d} H={:.2f} %RH T={:.2f} °C ready={}".format(
i + 1,
humidity_rh,
temperature_c,
status,
sensor.data_ready(),
)
)

Expand Down Expand Up @@ -290,25 +286,17 @@ def test_status_helpers(sensor):
sensor.set_continuous_mode(odr=ODR_1_HZ)
sleep(1.5)

status = sensor.status()
h_avail = sensor.humidity_ready()
t_avail = sensor.temperature_ready()
ready = sensor.data_ready()

print("STATUS = 0x{:02X}".format(status))
print("humidity_ready() =", h_avail)
print("temperature_ready() =", t_avail)
print("data_ready() =", ready)

sensor.set_one_shot_mode()

# At least one indicator must match STATUS
flags_match = (
h_avail == bool(status & STATUS_H_DA)
and t_avail == bool(status & STATUS_T_DA)
)

if flags_match:
if h_avail or t_avail or ready:
print_pass("STATUS helper methods")
return True
else:
Expand Down
8 changes: 4 additions & 4 deletions lib/wsen-hids/wsen_hids/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,17 +215,17 @@ def reboot(self):
# Status
# -------------------------------------------------------------------------

def status(self):
def _status(self):
return self._read_reg(REG_STATUS)

def humidity_ready(self):
return bool(self.status() & STATUS_H_DA)
return bool(self._status() & STATUS_H_DA)

def temperature_ready(self):
return bool(self.status() & STATUS_T_DA)
return bool(self._status() & STATUS_T_DA)

def data_ready(self):
status = self.status()
status = self._status()
return bool((status & STATUS_H_DA) and (status & STATUS_T_DA))

# -------------------------------------------------------------------------
Expand Down
26 changes: 10 additions & 16 deletions lib/wsen-pads/examples/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ def print_fail(name, err=None):
def dump_registers(sensor):
ctrl1 = sensor._read_u8(REG_CTRL_1)
ctrl2 = sensor._read_u8(REG_CTRL_2)
status = sensor._read_u8(REG_STATUS)
int_source = sensor._read_u8(REG_INT_SOURCE)

print("CTRL_1 = 0x{:02X}".format(ctrl1))
print("CTRL_2 = 0x{:02X}".format(ctrl2))
print("STATUS = 0x{:02X}".format(status))
print("data_ready() =", sensor.data_ready())
print("INT_SOURCE = 0x{:02X}".format(int_source))


Expand Down Expand Up @@ -136,13 +135,12 @@ def test_one_shot(sensor):

raw_p = sensor.pressure_raw()
raw_t = sensor.temperature_raw()
status = sensor.status()

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("STATUS : 0x{:02X}".format(status))
print("data_ready() :", sensor.data_ready())

# Basic sanity checks
MIN_PRESSURE = 260.0
Expand Down Expand Up @@ -190,16 +188,14 @@ def test_continuous_mode(sensor, odr, label, wait_s=2):
temperature_c = sensor.temperature()
raw_p = sensor.pressure_raw()
raw_t = sensor.temperature_raw()
status = sensor.status()

print(
"#{:d} P={:.2f} hPa T={:.2f} °C rawP={} rawT={} STATUS=0x{:02X}".format(
"#{:d} P={:.2f} hPa T={:.2f} °C rawP={} rawT={} ready={}".format(
i + 1,
pressure_hpa,
temperature_c,
raw_p,
raw_t,
status,
sensor.data_ready(),
)
)

Expand Down Expand Up @@ -229,15 +225,13 @@ def test_status_flags(sensor):
sensor.set_continuous(odr=ODR_1_HZ)
sleep(1.5)

status = sensor.status()
p_avail = sensor.pressure_available()
t_avail = sensor.temperature_available()
ready = sensor.is_ready()
p_avail = sensor.pressure_ready()
t_avail = sensor.temperature_ready()
ready = sensor.data_ready()

print("STATUS = 0x{:02X}".format(status))
print("pressure_available() =", p_avail)
print("temperature_available() =", t_avail)
print("is_ready() =", ready)
print("pressure_ready() =", p_avail)
print("temperature_ready() =", t_avail)
print("data_ready() =", ready)

sensor.power_off()

Expand Down
14 changes: 7 additions & 7 deletions lib/wsen-pads/wsen_pads/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,25 +155,25 @@ def device_id(self):
"""Return the value of the DEVICE_ID register."""
return self._read_reg(REG_DEVICE_ID)

def status(self):
def _status(self):
"""Return the raw STATUS register value."""
return self._read_reg(REG_STATUS)

def pressure_available(self):
def pressure_ready(self):
"""Return True when new pressure data is available."""
return bool(self.status() & STATUS_P_DA)
return bool(self._status() & STATUS_P_DA)

def temperature_available(self):
def temperature_ready(self):
Comment thread
nedseb marked this conversation as resolved.
"""Return True when new temperature data is available."""
return bool(self.status() & STATUS_T_DA)
return bool(self._status() & STATUS_T_DA)

def is_ready(self):
def data_ready(self):
"""
Return True when both pressure and temperature data are available.

This is mainly useful in continuous mode.
"""
status = self.status()
status = self._status()
return bool((status & STATUS_P_DA) and (status & STATUS_T_DA))

# ---------------------------------------------------------------------
Expand Down
Loading
Loading