5
5
# We have a lot of attributes for this complex sensor.
6
6
# pylint: disable=too-many-instance-attributes
7
7
# pylint: disable=no_self_use
8
- # pylint: disable=consider-using-f-string
9
8
10
9
"""
11
10
`adafruit_bme680`
14
13
CircuitPython library for BME680 temperature, pressure and humidity sensor.
15
14
16
15
17
- * Author(s): Limor Fried
18
-
19
-
20
- original Adafruit_BME680 with addition of set_gas_heater(). Other new members are private.
16
+ * Author(s): Limor Fried, William Garber, many others
21
17
22
18
23
19
Implementation Notes
@@ -61,7 +57,6 @@ def delay_microseconds(nusec):
61
57
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BME680.git"
62
58
63
59
64
- # garberw added begin ===========================
65
60
# I2C ADDRESS/BITS/SETTINGS NEW
66
61
# -----------------------------------------------------------------------
67
62
_BME68X_ENABLE_HEATER = const (0x00 )
@@ -83,8 +78,6 @@ def delay_microseconds(nusec):
83
78
_BME68X_REG_CTRL_GAS_0 = const (0x70 )
84
79
_BME68X_REG_CTRL_GAS_1 = const (0x71 )
85
80
86
-
87
- # garberw added end ===========================
88
81
# I2C ADDRESS/BITS/SETTINGS
89
82
# -----------------------------------------------------------------------
90
83
_BME680_CHIPID = const (0x61 )
@@ -152,15 +145,6 @@ def delay_microseconds(nusec):
152
145
)
153
146
154
147
155
- # garberw added begin ===========================
156
- INT32 = int
157
- INT16 = int
158
- INT8 = int
159
- UINT32 = int
160
- UINT16 = int
161
- UINT8 = int
162
-
163
-
164
148
def bme_set_bits (reg_data , bitname_msk , bitname_pos , data ):
165
149
"""
166
150
Macro to set bits
@@ -178,17 +162,6 @@ def bme_set_bits_pos_0(reg_data, bitname_msk, data):
178
162
return (reg_data & ~ bitname_msk ) | (data & bitname_msk )
179
163
180
164
181
- class GasHeaterException (Exception ):
182
- """
183
- Error during set_gas_heater()
184
- """
185
-
186
- def __init__ (self , msg = "GasHeaterException default" ):
187
- self .msg = msg
188
- super ().__init__ (msg )
189
-
190
-
191
- # garberw added end ===========================
192
165
def _read24 (arr : ReadableBuffer ) -> float :
193
166
"""Parse an unsigned 24-bit value as a floating point and return it."""
194
167
ret = 0.0
@@ -244,12 +217,9 @@ def __init__(self, *, refresh_rate: int = 10) -> None:
244
217
self ._last_reading = 0
245
218
self ._min_refresh_time = 1 / refresh_rate
246
219
247
- # garberw added begin ===========================
248
220
self ._amb_temp = 25 # Copy required parameters from reference bme68x_dev struct
249
221
self .set_gas_heater (320 , 150 ) # heater 320 deg C for 150 msec
250
222
251
- # garberw added end ===========================
252
-
253
223
@property
254
224
def pressure_oversample (self ) -> int :
255
225
"""The oversampling for pressure sensor"""
@@ -482,41 +452,37 @@ def _read(self, register: int, length: int) -> bytearray:
482
452
def _write (self , register : int , values : bytearray ) -> None :
483
453
raise NotImplementedError ()
484
454
485
- def set_gas_heater (self , heater_temp : UINT16 , heater_time : UINT16 ) -> bool :
455
+ def set_gas_heater (self , heater_temp : int , heater_time : int ) -> bool :
486
456
"""
487
- * @brief Enable and configure gas reading + heater (None disables)
488
- * @param heater_temp
489
- * Desired temperature in degrees Centigrade
490
- * @param heater_time
491
- * Time to keep heater on in milliseconds
492
- * @return True on success, False on failure
457
+ Enable and configure gas reading + heater (None disables)
458
+ :param heater_temp: Desired temperature in degrees Centigrade
459
+ :param heater_time: Time to keep heater on in milliseconds
460
+ :return: True on success, False on failure
493
461
"""
494
462
try :
495
463
if (heater_temp is None ) or (heater_time is None ):
496
464
self ._set_heatr_conf (heater_temp or 0 , heater_time or 0 , enable = False )
497
465
else :
498
466
self ._set_heatr_conf (heater_temp , heater_time )
499
- except GasHeaterException :
467
+ except OSError :
500
468
return False
501
469
return True
502
470
503
471
def _set_heatr_conf (
504
- self , heater_temp : UINT16 , heater_time : UINT16 , enable : bool = True
472
+ self , heater_temp : int , heater_time : int , enable : bool = True
505
473
) -> None :
506
474
# restrict to BME68X_FORCED_MODE
507
- op_mode : UINT8 = _BME68X_FORCED_MODE
508
- nb_conv : UINT8 = 0
509
- hctrl : UINT8 = _BME68X_ENABLE_HEATER
510
- run_gas : UINT8 = 0
511
- ctrl_gas_data_0 : UINT8 = 0
512
- ctrl_gas_data_1 : UINT8 = 0
513
- ctrl_gas_addr_0 : UINT8 = _BME68X_REG_CTRL_GAS_0
514
- ctrl_gas_addr_1 : UINT8 = _BME68X_REG_CTRL_GAS_1
475
+ op_mode : int = _BME68X_FORCED_MODE
476
+ nb_conv : int = 0
477
+ hctrl : int = _BME68X_ENABLE_HEATER
478
+ run_gas : int = 0
479
+ ctrl_gas_data_0 : int = 0
480
+ ctrl_gas_data_1 : int = 0
515
481
try :
516
482
self ._set_op_mode (_BME68X_SLEEP_MODE )
517
483
self ._set_conf (heater_temp , heater_time , op_mode )
518
- ctrl_gas_data_0 = self ._read_byte (ctrl_gas_addr_0 )
519
- ctrl_gas_data_1 = self ._read_byte (ctrl_gas_addr_1 )
484
+ ctrl_gas_data_0 = self ._read_byte (_BME68X_REG_CTRL_GAS_0 )
485
+ ctrl_gas_data_1 = self ._read_byte (_BME68X_REG_CTRL_GAS_1 )
520
486
if enable :
521
487
hctrl = _BME68X_ENABLE_HEATER
522
488
if self ._chip_variant == _BME68X_VARIANT_GAS_HIGH :
@@ -536,86 +502,73 @@ def _set_heatr_conf(
536
502
ctrl_gas_data_1 = bme_set_bits (
537
503
ctrl_gas_data_1 , _BME68X_RUN_GAS_MSK , _BME68X_RUN_GAS_POS , run_gas
538
504
)
539
- self ._write (ctrl_gas_addr_0 , [ctrl_gas_data_0 ])
540
- self ._write (ctrl_gas_addr_1 , [ctrl_gas_data_1 ])
505
+ self ._write (_BME68X_REG_CTRL_GAS_0 , [ctrl_gas_data_0 ])
506
+ self ._write (_BME68X_REG_CTRL_GAS_1 , [ctrl_gas_data_1 ])
541
507
# HELP check this
508
+ finally :
542
509
self ._set_op_mode (_BME68X_FORCED_MODE )
543
- except GasHeaterException as exc :
544
- self ._set_op_mode (_BME68X_FORCED_MODE )
545
- raise exc
546
510
547
- def _set_op_mode (self , op_mode : UINT8 ) -> None :
511
+ def _set_op_mode (self , op_mode : int ) -> None :
548
512
"""
549
513
* @brief This API is used to set the operation mode of the sensor
550
514
"""
551
- tmp_pow_mode : UINT8 = 0
552
- pow_mode : UINT8 = _BME68X_FORCED_MODE
553
- reg_addr : UINT8 = _BME680_REG_CTRL_MEAS
515
+ tmp_pow_mode : int = 0
516
+ pow_mode : int = _BME68X_FORCED_MODE
554
517
# Call until in sleep
555
- try :
556
- # was a do {} while() loop
557
- while pow_mode != _BME68X_SLEEP_MODE :
558
- tmp_pow_mode = self ._read_byte (_BME680_REG_CTRL_MEAS )
559
- # Put to sleep before changing mode
560
- pow_mode = tmp_pow_mode & _BME68X_MODE_MSK
561
- if pow_mode != _BME68X_SLEEP_MODE :
562
- tmp_pow_mode &= ~ _BME68X_MODE_MSK # Set to sleep
563
- self ._write (reg_addr , [tmp_pow_mode ])
564
- # dev->delay_us(_BME68X_PERIOD_POLL, dev->intf_ptr) # HELP
565
- delay_microseconds (_BME68X_PERIOD_POLL )
566
- # Already in sleep
567
- if op_mode != _BME68X_SLEEP_MODE :
568
- tmp_pow_mode = (tmp_pow_mode & ~ _BME68X_MODE_MSK ) | (
569
- op_mode & _BME68X_MODE_MSK
570
- )
571
- self ._write (reg_addr , [tmp_pow_mode ])
572
- except GasHeaterException as exc :
573
- raise exc
574
518
575
- def _set_conf (
576
- self , heater_temp : UINT16 , heater_time : UINT16 , op_mode : UINT8
577
- ) -> None :
519
+ # was a do {} while() loop
520
+ while pow_mode != _BME68X_SLEEP_MODE :
521
+ tmp_pow_mode = self ._read_byte (_BME680_REG_CTRL_MEAS )
522
+ # Put to sleep before changing mode
523
+ pow_mode = tmp_pow_mode & _BME68X_MODE_MSK
524
+ if pow_mode != _BME68X_SLEEP_MODE :
525
+ tmp_pow_mode &= ~ _BME68X_MODE_MSK # Set to sleep
526
+ self ._write (_BME680_REG_CTRL_MEAS , [tmp_pow_mode ])
527
+ # dev->delay_us(_BME68X_PERIOD_POLL, dev->intf_ptr) # HELP
528
+ delay_microseconds (_BME68X_PERIOD_POLL )
529
+ # Already in sleep
530
+ if op_mode != _BME68X_SLEEP_MODE :
531
+ tmp_pow_mode = (tmp_pow_mode & ~ _BME68X_MODE_MSK ) | (
532
+ op_mode & _BME68X_MODE_MSK
533
+ )
534
+ self ._write (_BME680_REG_CTRL_MEAS , [tmp_pow_mode ])
535
+
536
+ def _set_conf (self , heater_temp : int , heater_time : int , op_mode : int ) -> None :
578
537
"""
579
538
This internal API is used to set heater configurations
580
539
"""
581
- try :
582
- if op_mode != _BME68X_FORCED_MODE :
583
- raise GasHeaterException ("_set_conf not forced mode" )
584
- rh_reg_addr : UINT8 = _BME680_BME680_RES_HEAT_0
585
- rh_reg_data : UINT8 = self ._calc_res_heat (heater_temp )
586
- gw_reg_addr : UINT8 = _BME680_BME680_GAS_WAIT_0
587
- gw_reg_data : UINT8 = self ._calc_gas_wait (heater_time )
588
- self ._write (rh_reg_addr , [rh_reg_data ])
589
- self ._write (gw_reg_addr , [gw_reg_data ])
590
- except GasHeaterException as exc :
591
- raise exc
592
-
593
- def _calc_res_heat (self , temp : UINT16 ) -> UINT8 :
540
+
541
+ if op_mode != _BME68X_FORCED_MODE :
542
+ raise OSError ("GasHeaterException: _set_conf not forced mode" )
543
+ rh_reg_data : int = self ._calc_res_heat (heater_temp )
544
+ gw_reg_data : int = self ._calc_gas_wait (heater_time )
545
+ self ._write (_BME680_BME680_RES_HEAT_0 , [rh_reg_data ])
546
+ self ._write (_BME680_BME680_GAS_WAIT_0 , [gw_reg_data ])
547
+
548
+ def _calc_res_heat (self , temp : int ) -> int :
594
549
"""
595
550
This internal API is used to calculate the heater resistance value using float
596
551
"""
597
- gh1 : INT8 = self ._gas_calibration [0 ]
598
- gh2 : INT16 = self ._gas_calibration [1 ]
599
- gh3 : INT8 = self ._gas_calibration [2 ]
600
- htr : UINT8 = self ._heat_range
601
- htv : INT8 = self ._heat_val
602
- amb : UINT8 = self ._amb_temp
552
+ gh1 : int = self ._gas_calibration [0 ]
553
+ gh2 : int = self ._gas_calibration [1 ]
554
+ gh3 : int = self ._gas_calibration [2 ]
555
+ htr : int = self ._heat_range
556
+ htv : int = self ._heat_val
557
+ amb : int = self ._amb_temp
603
558
604
559
temp = min (temp , 400 ) # Cap temperature
605
560
606
- var1 : INT32 = ((INT32 (amb ) * gh3 ) / 1000 ) * 256
607
- var2 : INT32 = (gh1 + 784 ) * (
608
- ((((gh2 + 154009 ) * temp * 5 ) / 100 ) + 3276800 ) / 10
609
- )
610
- var3 : INT32 = var1 + (var2 / 2 )
611
- var4 : INT32 = var3 / (htr + 4 )
612
- var5 : INT32 = (131 * htv ) + 65536
613
- heatr_res_x100 : INT32 = INT32 (((var4 / var5 ) - 250 ) * 34 )
614
- heatr_res : UINT8 = UINT8 ((heatr_res_x100 + 50 ) / 100 )
561
+ var1 : int = ((int (amb ) * gh3 ) / 1000 ) * 256
562
+ var2 : int = (gh1 + 784 ) * (((((gh2 + 154009 ) * temp * 5 ) / 100 ) + 3276800 ) / 10 )
563
+ var3 : int = var1 + (var2 / 2 )
564
+ var4 : int = var3 / (htr + 4 )
565
+ var5 : int = (131 * htv ) + 65536
566
+ heatr_res_x100 : int = int (((var4 / var5 ) - 250 ) * 34 )
567
+ heatr_res : int = int ((heatr_res_x100 + 50 ) / 100 )
615
568
616
569
return heatr_res
617
570
618
- def _calc_res_heat (self , temp : UINT16 ) -> UINT8 :
571
+ def _calc_res_heat (self , temp : int ) -> int :
619
572
"""
620
573
This internal API is used to calculate the heater resistance value
621
574
"""
@@ -633,28 +586,26 @@ def _calc_res_heat(self, temp: UINT16) -> UINT8:
633
586
var3 : float = gh3 / (1024.0 )
634
587
var4 : float = var1 * (1.0 + (var2 * float (temp )))
635
588
var5 : float = var4 + (var3 * amb )
636
- res_heat : UINT8 = UINT8 (
589
+ res_heat : int = int (
637
590
3.4 * ((var5 * (4 / (4 + htr )) * (1 / (1 + (htv * 0.002 )))) - 25 )
638
591
)
639
592
return res_heat
640
593
641
- def _calc_gas_wait (self , dur : UINT16 ) -> UINT8 :
594
+ def _calc_gas_wait (self , dur : int ) -> int :
642
595
"""
643
596
This internal API is used to calculate the gas wait
644
597
"""
645
- factor : UINT8 = 0
646
- durval : UINT8 = 0xFF # Max duration
598
+ factor : int = 0
599
+ durval : int = 0xFF # Max duration
647
600
648
601
if dur < 0xFC0 :
649
602
return durval
650
603
while dur > 0x3F :
651
604
dur = dur / 4
652
605
factor += 1
653
- durval = UINT8 (dur + (factor * 64 ))
606
+ durval = int (dur + (factor * 64 ))
654
607
return durval
655
608
656
- # garberw added end ===========================
657
-
658
609
659
610
class Adafruit_BME680_I2C (Adafruit_BME680 ):
660
611
"""Driver for I2C connected BME680.
0 commit comments