@@ -75,6 +75,15 @@ def __init__(self):
7575
7676 self .temperature_fine = 0
7777
78+ def set_from_namedtuple (self , value ):
79+ # Iterate through a tuple supplied by i2cdevice
80+ # and copy its values into the class attributes
81+ for key in self .__dict__ .keys ():
82+ try :
83+ setattr (self , key , getattr (value , key ))
84+ except AttributeError :
85+ pass
86+
7887 def compensate_temperature (self , raw_temperature ):
7988 var1 = (raw_temperature / 16384.0 - self .dig_t1 / 1024.0 ) * self .dig_t2
8089 var2 = raw_temperature / 131072.0 - self .dig_t1 / 8192.0
@@ -205,68 +214,42 @@ def setup(self, mode='normal', temperature_oversampling=16, pressure_oversamplin
205214 mode = "sleep"
206215
207216 try :
208- if self ._bme280 .CHIP_ID .get_id () != CHIP_ID :
209- raise RuntimeError ("Unable to find bme280 on 0x{:02x}, CHIP_ID returned {:02x}" .format (self ._i2c_addr , self ._bme280 .CHIP_ID .get_id ()))
217+ chip = self ._bme280 .get ('CHIP_ID' )
218+ if chip .id != CHIP_ID :
219+ raise RuntimeError ("Unable to find bme280 on 0x{:02x}, CHIP_ID returned {:02x}" .format (self ._i2c_addr , chip .id ))
210220 except IOError :
211221 raise RuntimeError ("Unable to find bme280 on 0x{:02x}, IOError" .format (self ._i2c_addr ))
212222
213- self ._bme280 .RESET . set_reset ( 0xB6 )
223+ self ._bme280 .set ( 'RESET' , reset = 0xB6 )
214224 time .sleep (0.1 )
215225
216- self ._bme280 .CTRL_HUM .set_osrs_h (humidity_oversampling )
217-
218- with self ._bme280 .CTRL_MEAS as CTRL_MEAS :
219- CTRL_MEAS .set_mode (mode )
220- CTRL_MEAS .set_osrs_t (temperature_oversampling )
221- CTRL_MEAS .set_osrs_p (pressure_oversampling )
222- CTRL_MEAS .write ()
223-
224- with self ._bme280 .CONFIG as CONFIG :
225- CONFIG .set_t_sb (temperature_standby )
226- CONFIG .set_filter (2 )
227- CONFIG .write ()
228-
229- with self ._bme280 .CALIBRATION as CALIBRATION :
230- self .calibration .dig_t1 = CALIBRATION .get_dig_t1 ()
231- self .calibration .dig_t2 = CALIBRATION .get_dig_t2 ()
232- self .calibration .dig_t3 = CALIBRATION .get_dig_t3 ()
233-
234- self .calibration .dig_p1 = CALIBRATION .get_dig_p1 ()
235- self .calibration .dig_p2 = CALIBRATION .get_dig_p2 ()
236- self .calibration .dig_p3 = CALIBRATION .get_dig_p3 ()
237- self .calibration .dig_p4 = CALIBRATION .get_dig_p4 ()
238- self .calibration .dig_p5 = CALIBRATION .get_dig_p5 ()
239- self .calibration .dig_p6 = CALIBRATION .get_dig_p6 ()
240- self .calibration .dig_p7 = CALIBRATION .get_dig_p7 ()
241- self .calibration .dig_p8 = CALIBRATION .get_dig_p8 ()
242- self .calibration .dig_p9 = CALIBRATION .get_dig_p9 ()
243-
244- self .calibration .dig_h1 = CALIBRATION .get_dig_h1 ()
245-
246- with self ._bme280 .CALIBRATION2 as CALIBRATION :
247- self .calibration .dig_h2 = CALIBRATION .get_dig_h2 ()
248- self .calibration .dig_h3 = CALIBRATION .get_dig_h3 ()
249- self .calibration .dig_h4 = CALIBRATION .get_dig_h4 ()
250- self .calibration .dig_h5 = CALIBRATION .get_dig_h5 ()
251- self .calibration .dig_h6 = CALIBRATION .get_dig_h6 ()
226+ self ._bme280 .set ('CTRL_HUM' , osrs_h = humidity_oversampling )
227+
228+ self ._bme280 .set ('CTRL_MEAS' ,
229+ mode = mode ,
230+ osrs_t = temperature_oversampling ,
231+ osrs_p = pressure_oversampling )
232+
233+ self ._bme280 .set ('CONFIG' ,
234+ t_sb = temperature_standby ,
235+ filter = 2 )
236+
237+ self .calibration .set_from_namedtuple (self ._bme280 .get ('CALIBRATION' ))
238+ self .calibration .set_from_namedtuple (self ._bme280 .get ('CALIBRATION2' ))
252239
253240 def update_sensor (self ):
254241 self .setup ()
255242
256243 if self ._mode == "forced" :
257- # Trigger a reading in forced mode and wait for result
258- self ._bme280 .CTRL_MEAS .set_mode ("forced" )
259- while self ._bme280 .STATUS .get_measuring ():
244+ self ._bme280 .set ('CTRL_MEAS' , mode = "forced" )
245+ while self ._bme280 .get ('STATUS' ).measuring :
260246 time .sleep (0.001 )
261247
262- with self ._bme280 .DATA as DATA :
263- raw_temperature = DATA .get_temperature ()
264- raw_pressure = DATA .get_pressure ()
265- raw_humidity = DATA .get_humidity ()
248+ raw = self ._bme280 .get ('DATA' )
266249
267- self .temperature = self .calibration .compensate_temperature (raw_temperature )
268- self .pressure = self .calibration .compensate_pressure (raw_pressure ) / 100.0
269- self .humidity = self .calibration .compensate_humidity (raw_humidity )
250+ self .temperature = self .calibration .compensate_temperature (raw . temperature )
251+ self .pressure = self .calibration .compensate_pressure (raw . pressure ) / 100.0
252+ self .humidity = self .calibration .compensate_humidity (raw . humidity )
270253
271254 def get_temperature (self ):
272255 self .update_sensor ()
0 commit comments