3030 STATUS_IM_UPDATE ,
3131 STATUS_MEASURING ,
3232)
33- from bme280 .exceptions import BME280Error , BME280InvalidDevice , BME280NotFound
33+ from bme280 .exceptions import BME280InvalidDevice , BME280NotFound
3434
3535
3636class BME280 (object ):
@@ -125,7 +125,7 @@ def _wait_boot(self, timeout_ms=50):
125125 if not (self ._read_reg (REG_STATUS ) & STATUS_IM_UPDATE ):
126126 return
127127 sleep_ms (5 )
128- raise BME280Error ("BME280 NVM copy timeout" )
128+ raise OSError ("BME280 NVM copy timeout" )
129129
130130 def device_id (self ):
131131 """Read chip ID register. Expected: 0x60."""
@@ -239,6 +239,21 @@ def humidity_ready(self):
239239 # Forced measurement trigger
240240 # --------------------------------------------------
241241
242+ def _is_sleep_mode (self ):
243+ """Return True if the sensor is in sleep mode."""
244+ return (self ._read_reg (REG_CTRL_MEAS ) & MODE_MASK ) == MODE_SLEEP
245+
246+ def _ensure_data (self ):
247+ """Trigger a forced measurement if the sensor is in sleep mode.
248+
249+ In normal mode this is a no-op. In sleep mode it triggers a
250+ single conversion and waits for completion so that subsequent
251+ register reads return fresh data.
252+ """
253+ if self ._is_sleep_mode ():
254+ self .trigger_one_shot ()
255+ self ._wait_measurement ()
256+
242257 def trigger_one_shot (self ):
243258 """Trigger a single forced measurement. Poll data_ready() for completion."""
244259 ctrl = self ._read_reg (REG_CTRL_MEAS )
@@ -250,7 +265,7 @@ def _wait_measurement(self, timeout_ms=100):
250265 if self .data_ready ():
251266 return
252267 sleep_ms (5 )
253- raise BME280Error ("BME280 measurement timeout" )
268+ raise OSError ("BME280 measurement timeout" )
254269
255270 # --------------------------------------------------
256271 # Raw data burst read
@@ -323,32 +338,60 @@ def _compensate_humidity(self, raw_hum):
323338 # --------------------------------------------------
324339
325340 def temperature (self ):
326- """Return compensated temperature in °C (float)."""
341+ """Return compensated temperature in °C (float).
342+
343+ If the sensor is in sleep mode, a forced measurement is triggered
344+ automatically before reading.
345+ """
346+ self ._ensure_data ()
327347 raw_temp , _ , _ = self ._read_raw ()
328348 return self ._compensate_temperature (raw_temp ) / 100.0
329349
330350 def pressure_hpa (self ):
331- """Return compensated pressure in hPa (float)."""
351+ """Return compensated pressure in hPa (float).
352+
353+ If the sensor is in sleep mode, a forced measurement is triggered
354+ automatically before reading.
355+ """
356+ self ._ensure_data ()
332357 raw_temp , raw_press , _ = self ._read_raw ()
333358 self ._compensate_temperature (raw_temp )
334359 return self ._compensate_pressure (raw_press ) / 25600.0
335360
336361 def humidity (self ):
337- """Return compensated relative humidity in %RH (float)."""
362+ """Return compensated relative humidity in %RH (float).
363+
364+ If the sensor is in sleep mode, a forced measurement is triggered
365+ automatically before reading.
366+ """
367+ self ._ensure_data ()
338368 raw_temp , _ , raw_hum = self ._read_raw ()
339369 self ._compensate_temperature (raw_temp )
340370 return self ._compensate_humidity (raw_hum ) / 1024.0
341371
342372 def read (self ):
343- """Return (temperature_c, pressure_hpa, humidity_rh) tuple."""
373+ """Return (temperature_c, pressure_hpa, humidity_rh) tuple.
374+
375+ If the sensor is in sleep mode, a forced measurement is triggered
376+ automatically before reading.
377+ """
378+ self ._ensure_data ()
344379 raw_temp , raw_press , raw_hum = self ._read_raw ()
345380 temp_c = self ._compensate_temperature (raw_temp ) / 100.0
346381 press_hpa = self ._compensate_pressure (raw_press ) / 25600.0
347382 hum_rh = self ._compensate_humidity (raw_hum ) / 1024.0
348383 return temp_c , press_hpa , hum_rh
349384
350385 def read_one_shot (self ):
351- """Trigger a forced measurement, wait, and return (temp_c, press_hpa, hum_rh)."""
386+ """Trigger a forced measurement, wait, and return (temp_c, press_hpa, hum_rh).
387+
388+ Reads registers directly without calling _ensure_data() to avoid
389+ a double trigger (forced mode returns the sensor to sleep).
390+ """
352391 self .trigger_one_shot ()
353392 self ._wait_measurement ()
354- return self .read ()
393+ raw_temp , raw_press , raw_hum = self ._read_raw ()
394+ temp_c = self ._compensate_temperature (raw_temp ) / 100.0
395+ press_hpa = self ._compensate_pressure (raw_press ) / 25600.0
396+ hum_rh = self ._compensate_humidity (raw_hum ) / 1024.0
397+ return temp_c , press_hpa , hum_rh
0 commit comments