44
44
45
45
# We have a lot of attributes for this complex sensor.
46
46
# pylint: disable=too-many-instance-attributes
47
+ # pylint: disable=no_self_use
48
+ # pylint: disable=consider-using-f-string
47
49
48
50
"""
49
51
`adafruit_bme680`
@@ -82,6 +84,7 @@ def delay_microseconds(nusec):
82
84
"""HELP must be same as dev->delay_us"""
83
85
time .sleep (nusec / 1000000.0 )
84
86
87
+
85
88
try :
86
89
# Used only for type annotations.
87
90
@@ -112,7 +115,7 @@ def delay_microseconds(nusec):
112
115
_BME68X_VARIANT_GAS_HIGH = const (0x01 )
113
116
_BME68X_HCTRL_MSK = const (0x08 )
114
117
_BME68X_HCTRL_POS = const (3 )
115
- _BME68X_NBCONV_MSK = const (0x0f )
118
+ _BME68X_NBCONV_MSK = const (0x0F )
116
119
_BME68X_RUN_GAS_MSK = const (0x30 )
117
120
_BME68X_RUN_GAS_POS = const (4 )
118
121
_BME68X_MODE_MSK = const (0x03 )
@@ -206,6 +209,7 @@ def bme_set_bits(reg_data, bitname_msk, bitname_pos, data):
206
209
"""
207
210
return (reg_data & ~ bitname_msk ) | ((data << bitname_pos ) & bitname_msk )
208
211
212
+
209
213
def bme_set_bits_pos_0 (reg_data , bitname_msk , data ):
210
214
"""
211
215
Macro to set bits starting from position 0
@@ -218,7 +222,8 @@ class GasHeaterException(Exception):
218
222
"""
219
223
Error during set_gas_heater()
220
224
"""
221
- def __init__ (self , msg = "GasHeaterException default" ):
225
+
226
+ def __init__ (self , msg = "GasHeaterException default" ):
222
227
self .msg = msg
223
228
super ().__init__ (msg )
224
229
@@ -281,9 +286,10 @@ def __init__(self, *, refresh_rate: int = 10) -> None:
281
286
282
287
# garberw added begin ===========================
283
288
self ._amb_temp = 25 # Copy required parameters from reference bme68x_dev struct
284
- self .set_gas_heater (320 , 150 ) # heater 320 deg C for 150 msec
289
+ self .set_gas_heater (320 , 150 ) # heater 320 deg C for 150 msec
285
290
286
291
# garberw added end ===========================
292
+
287
293
@property
288
294
def pressure_oversample (self ) -> int :
289
295
"""The oversampling for pressure sensor"""
@@ -562,13 +568,17 @@ def _set_heatr_conf(self, heater_temp: UINT16, heater_time: UINT16) -> None:
562
568
hctrl = _BME68X_DISABLE_HEATER
563
569
run_gas = _BME68X_DISABLE_GAS_MEAS
564
570
565
- ctrl_gas_data_0 = bme_set_bits (ctrl_gas_data_0 , _BME68X_HCTRL_MSK , _BME68X_HCTRL_POS ,
566
- hctrl )
567
- ctrl_gas_data_1 = bme_set_bits_pos_0 (ctrl_gas_data_1 , _BME68X_NBCONV_MSK , nb_conv )
568
- ctrl_gas_data_1 = bme_set_bits (ctrl_gas_data_1 , _BME68X_RUN_GAS_MSK ,
569
- _BME68X_RUN_GAS_POS , run_gas )
570
- self ._write (ctrl_gas_addr_0 , [ ctrl_gas_data_0 ])
571
- self ._write (ctrl_gas_addr_1 , [ ctrl_gas_data_1 ])
571
+ ctrl_gas_data_0 = bme_set_bits (
572
+ ctrl_gas_data_0 , _BME68X_HCTRL_MSK , _BME68X_HCTRL_POS , hctrl
573
+ )
574
+ ctrl_gas_data_1 = bme_set_bits_pos_0 (
575
+ ctrl_gas_data_1 , _BME68X_NBCONV_MSK , nb_conv
576
+ )
577
+ ctrl_gas_data_1 = bme_set_bits (
578
+ ctrl_gas_data_1 , _BME68X_RUN_GAS_MSK , _BME68X_RUN_GAS_POS , run_gas
579
+ )
580
+ self ._write (ctrl_gas_addr_0 , [ctrl_gas_data_0 ])
581
+ self ._write (ctrl_gas_addr_1 , [ctrl_gas_data_1 ])
572
582
# HELP check this
573
583
self ._set_op_mode (_BME68X_FORCED_MODE )
574
584
except GasHeaterException as exc :
@@ -588,20 +598,24 @@ def _set_op_mode(self, op_mode: UINT8) -> None:
588
598
while pow_mode != _BME68X_SLEEP_MODE :
589
599
tmp_pow_mode = self ._read_byte (_BME680_REG_CTRL_MEAS )
590
600
# Put to sleep before changing mode
591
- pow_mode = ( tmp_pow_mode & _BME68X_MODE_MSK )
601
+ pow_mode = tmp_pow_mode & _BME68X_MODE_MSK
592
602
if pow_mode != _BME68X_SLEEP_MODE :
593
603
tmp_pow_mode &= ~ _BME68X_MODE_MSK # Set to sleep
594
- self ._write (reg_addr , [ tmp_pow_mode ])
604
+ self ._write (reg_addr , [tmp_pow_mode ])
595
605
# dev->delay_us(_BME68X_PERIOD_POLL, dev->intf_ptr) # HELP
596
606
delay_microseconds (_BME68X_PERIOD_POLL )
597
607
# Already in sleep
598
608
if op_mode != _BME68X_SLEEP_MODE :
599
- tmp_pow_mode = (tmp_pow_mode & ~ _BME68X_MODE_MSK ) | (op_mode & _BME68X_MODE_MSK )
600
- self ._write (reg_addr , [ tmp_pow_mode ])
609
+ tmp_pow_mode = (tmp_pow_mode & ~ _BME68X_MODE_MSK ) | (
610
+ op_mode & _BME68X_MODE_MSK
611
+ )
612
+ self ._write (reg_addr , [tmp_pow_mode ])
601
613
except GasHeaterException as exc :
602
614
raise exc
603
615
604
- def _set_conf (self , heater_temp : UINT16 , heater_time : UINT16 , op_mode : UINT8 ) -> None :
616
+ def _set_conf (
617
+ self , heater_temp : UINT16 , heater_time : UINT16 , op_mode : UINT8
618
+ ) -> None :
605
619
"""
606
620
This internal API is used to set heater configurations
607
621
"""
@@ -612,28 +626,30 @@ def _set_conf(self, heater_temp: UINT16, heater_time: UINT16, op_mode: UINT8) ->
612
626
rh_reg_data : UINT8 = self ._calc_res_heat (heater_temp )
613
627
gw_reg_addr : UINT8 = _BME680_BME680_GAS_WAIT_0
614
628
gw_reg_data : UINT8 = self ._calc_gas_wait (heater_time )
615
- self ._write (rh_reg_addr , [ rh_reg_data ])
616
- self ._write (gw_reg_addr , [ gw_reg_data ])
629
+ self ._write (rh_reg_addr , [rh_reg_data ])
630
+ self ._write (gw_reg_addr , [gw_reg_data ])
617
631
except GasHeaterException as exc :
618
632
raise exc
619
633
620
634
def _calc_res_heat (self , temp : UINT16 ) -> UINT8 :
621
635
"""
622
636
This internal API is used to calculate the heater resistance value using float
623
637
"""
624
- gh1 : INT8 = self ._gas_calibration [0 ]
638
+ gh1 : INT8 = self ._gas_calibration [0 ]
625
639
gh2 : INT16 = self ._gas_calibration [1 ]
626
- gh3 : INT8 = self ._gas_calibration [2 ]
640
+ gh3 : INT8 = self ._gas_calibration [2 ]
627
641
htr : UINT8 = self ._heat_range
628
- htv : INT8 = self ._heat_val
642
+ htv : INT8 = self ._heat_val
629
643
amb : UINT8 = self ._amb_temp
630
644
631
- temp = min (temp , 400 ) # Cap temperature
645
+ temp = min (temp , 400 ) # Cap temperature
632
646
633
647
var1 : INT32 = ((INT32 (amb ) * gh3 ) / 1000 ) * 256
634
- var2 : INT32 = (gh1 + 784 ) * (((((gh2 + 154009 ) * temp * 5 ) / 100 ) + 3276800 ) / 10 )
648
+ var2 : INT32 = (gh1 + 784 ) * (
649
+ ((((gh2 + 154009 ) * temp * 5 ) / 100 ) + 3276800 ) / 10
650
+ )
635
651
var3 : INT32 = var1 + (var2 / 2 )
636
- var4 : INT32 = ( var3 / (htr + 4 ) )
652
+ var4 : INT32 = var3 / (htr + 4 )
637
653
var5 : INT32 = (131 * htv ) + 65536
638
654
heatr_res_x100 : INT32 = INT32 (((var4 / var5 ) - 250 ) * 34 )
639
655
heatr_res : UINT8 = UINT8 ((heatr_res_x100 + 50 ) / 100 )
@@ -651,35 +667,33 @@ def _calc_res_heat(self, temp: UINT16) -> UINT8:
651
667
htv : float = float (self ._heat_val )
652
668
amb : float = float (self ._amb_temp )
653
669
654
- temp = min (temp , 400 ) # Cap temperature
655
-
656
- var1 : float = ((gh1 / (16.0 )) + 49.0 )
657
- var2 : float = (((gh2 / (32768.0 )) * (0.0005 )) + 0.00235 )
658
- var3 : float = (gh3 / (1024.0 ))
659
- var4 : float = (var1 * (1.0 + (var2 * float (temp ))))
660
- var5 : float = (var4 + (var3 * amb ))
661
- res_heat : UINT8 = UINT8 (3.4 * (
662
- (var5 *
663
- (4 / (4 + htr )) *
664
- (1 / (1 + (htv * 0.002 )))
665
- )
666
- - 25 ))
670
+ temp = min (temp , 400 ) # Cap temperature
671
+
672
+ var1 : float = (gh1 / (16.0 )) + 49.0
673
+ var2 : float = ((gh2 / (32768.0 )) * (0.0005 )) + 0.00235
674
+ var3 : float = gh3 / (1024.0 )
675
+ var4 : float = var1 * (1.0 + (var2 * float (temp )))
676
+ var5 : float = var4 + (var3 * amb )
677
+ res_heat : UINT8 = UINT8 (
678
+ 3.4 * ((var5 * (4 / (4 + htr )) * (1 / (1 + (htv * 0.002 )))) - 25 )
679
+ )
667
680
return res_heat
668
681
669
682
def _calc_gas_wait (self , dur : UINT16 ) -> UINT8 :
670
683
"""
671
684
This internal API is used to calculate the gas wait
672
685
"""
673
686
factor : UINT8 = 0
674
- durval : UINT8 = 0xff # Max duration
687
+ durval : UINT8 = 0xFF # Max duration
675
688
676
- if dur < 0xfc0 :
689
+ if dur < 0xFC0 :
677
690
return durval
678
691
while dur > 0x3F :
679
692
dur = dur / 4
680
693
factor += 1
681
694
durval = UINT8 (dur + (factor * 64 ))
682
695
return durval
696
+
683
697
# garberw added end ===========================
684
698
685
699
0 commit comments