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,42 +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
- # garberw added begin ===========================
486
- 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 :
487
456
"""
488
- * @brief Enable and configure gas reading + heater
489
- * @param heater_temp
490
- * Desired temperature in degrees Centigrade
491
- * @param heater_time
492
- * Time to keep heater on in milliseconds
493
- * @return True on success, False on failure
457
+ Enable and configure gas reading + heater
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
494
461
"""
495
- if ( heater_temp == 0 ) or ( heater_time == 0 ) :
462
+ if not heater_temp or not heater_time :
496
463
return False
497
464
# enable = BME68X_ENABLE
498
465
try :
499
466
self ._set_heatr_conf (heater_temp , heater_time )
500
- except GasHeaterException :
467
+ except OSError :
501
468
return False
502
469
return True
503
470
504
- def _set_heatr_conf (self , heater_temp : UINT16 , heater_time : UINT16 ) -> None :
471
+ def _set_heatr_conf (self , heater_temp : int , heater_time : int ) -> None :
505
472
# restrict to BME68X_FORCED_MODE
506
- op_mode : UINT8 = _BME68X_FORCED_MODE
473
+ op_mode : int = _BME68X_FORCED_MODE
507
474
# restrict to enable = True
508
475
enable : bool = True
509
- nb_conv : UINT8 = 0
510
- hctrl : UINT8 = _BME68X_ENABLE_HEATER
511
- run_gas : UINT8 = 0
512
- ctrl_gas_data_0 : UINT8 = 0
513
- ctrl_gas_data_1 : UINT8 = 0
514
- ctrl_gas_addr_0 : UINT8 = _BME68X_REG_CTRL_GAS_0
515
- ctrl_gas_addr_1 : UINT8 = _BME68X_REG_CTRL_GAS_1
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
516
481
try :
517
482
self ._set_op_mode (_BME68X_SLEEP_MODE )
518
483
self ._set_conf (heater_temp , heater_time , op_mode )
519
- ctrl_gas_data_0 = self ._read_byte (ctrl_gas_addr_0 )
520
- 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 )
521
486
if enable :
522
487
hctrl = _BME68X_ENABLE_HEATER
523
488
if self ._chip_variant == _BME68X_VARIANT_GAS_HIGH :
@@ -537,86 +502,75 @@ def _set_heatr_conf(self, heater_temp: UINT16, heater_time: UINT16) -> None:
537
502
ctrl_gas_data_1 = bme_set_bits (
538
503
ctrl_gas_data_1 , _BME68X_RUN_GAS_MSK , _BME68X_RUN_GAS_POS , run_gas
539
504
)
540
- self ._write (ctrl_gas_addr_0 , [ctrl_gas_data_0 ])
541
- 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 ])
542
507
# HELP check this
543
508
self ._set_op_mode (_BME68X_FORCED_MODE )
544
- except GasHeaterException as exc :
509
+ except OSError as exc :
545
510
self ._set_op_mode (_BME68X_FORCED_MODE )
546
511
raise exc
547
512
548
- def _set_op_mode (self , op_mode : UINT8 ) -> None :
513
+ def _set_op_mode (self , op_mode : int ) -> None :
549
514
"""
550
515
* @brief This API is used to set the operation mode of the sensor
551
516
"""
552
- tmp_pow_mode : UINT8 = 0
553
- pow_mode : UINT8 = _BME68X_FORCED_MODE
554
- reg_addr : UINT8 = _BME680_REG_CTRL_MEAS
517
+ tmp_pow_mode : int = 0
518
+ pow_mode : int = _BME68X_FORCED_MODE
555
519
# Call until in sleep
556
- try :
557
- # was a do {} while() loop
558
- while pow_mode != _BME68X_SLEEP_MODE :
559
- tmp_pow_mode = self ._read_byte (_BME680_REG_CTRL_MEAS )
560
- # Put to sleep before changing mode
561
- pow_mode = tmp_pow_mode & _BME68X_MODE_MSK
562
- if pow_mode != _BME68X_SLEEP_MODE :
563
- tmp_pow_mode &= ~ _BME68X_MODE_MSK # Set to sleep
564
- self ._write (reg_addr , [tmp_pow_mode ])
565
- # dev->delay_us(_BME68X_PERIOD_POLL, dev->intf_ptr) # HELP
566
- delay_microseconds (_BME68X_PERIOD_POLL )
567
- # Already in sleep
568
- if op_mode != _BME68X_SLEEP_MODE :
569
- tmp_pow_mode = (tmp_pow_mode & ~ _BME68X_MODE_MSK ) | (
570
- op_mode & _BME68X_MODE_MSK
571
- )
572
- self ._write (reg_addr , [tmp_pow_mode ])
573
- except GasHeaterException as exc :
574
- raise exc
575
520
576
- def _set_conf (
577
- self , heater_temp : UINT16 , heater_time : UINT16 , op_mode : UINT8
578
- ) -> None :
521
+ # was a do {} while() loop
522
+ while pow_mode != _BME68X_SLEEP_MODE :
523
+ tmp_pow_mode = self ._read_byte (_BME680_REG_CTRL_MEAS )
524
+ # Put to sleep before changing mode
525
+ pow_mode = tmp_pow_mode & _BME68X_MODE_MSK
526
+ if pow_mode != _BME68X_SLEEP_MODE :
527
+ tmp_pow_mode &= ~ _BME68X_MODE_MSK # Set to sleep
528
+ self ._write (_BME680_REG_CTRL_MEAS , [tmp_pow_mode ])
529
+ # dev->delay_us(_BME68X_PERIOD_POLL, dev->intf_ptr) # HELP
530
+ delay_microseconds (_BME68X_PERIOD_POLL )
531
+ # Already in sleep
532
+ if op_mode != _BME68X_SLEEP_MODE :
533
+ tmp_pow_mode = (tmp_pow_mode & ~ _BME68X_MODE_MSK ) | (
534
+ op_mode & _BME68X_MODE_MSK
535
+ )
536
+ self ._write (_BME680_REG_CTRL_MEAS , [tmp_pow_mode ])
537
+
538
+ def _set_conf (self , heater_temp : int , heater_time : int , op_mode : int ) -> None :
579
539
"""
580
540
This internal API is used to set heater configurations
581
541
"""
582
- try :
583
- if op_mode != _BME68X_FORCED_MODE :
584
- raise GasHeaterException ("_set_conf not forced mode" )
585
- rh_reg_addr : UINT8 = _BME680_BME680_RES_HEAT_0
586
- rh_reg_data : UINT8 = self ._calc_res_heat (heater_temp )
587
- gw_reg_addr : UINT8 = _BME680_BME680_GAS_WAIT_0
588
- gw_reg_data : UINT8 = self ._calc_gas_wait (heater_time )
589
- self ._write (rh_reg_addr , [rh_reg_data ])
590
- self ._write (gw_reg_addr , [gw_reg_data ])
591
- except GasHeaterException as exc :
592
- raise exc
593
542
594
- def _calc_res_heat (self , temp : UINT16 ) -> UINT8 :
543
+ if op_mode != _BME68X_FORCED_MODE :
544
+ raise OSError ("GasHeaterException: _set_conf not forced mode" )
545
+ rh_reg_data : int = self ._calc_res_heat (heater_temp )
546
+ gw_reg_data : int = self ._calc_gas_wait (heater_time )
547
+ self ._write (_BME680_BME680_RES_HEAT_0 , [rh_reg_data ])
548
+ self ._write (_BME680_BME680_GAS_WAIT_0 , [gw_reg_data ])
549
+
550
+ def _calc_res_heat (self , temp : int ) -> int :
595
551
"""
596
552
This internal API is used to calculate the heater resistance value using float
597
553
"""
598
- gh1 : INT8 = self ._gas_calibration [0 ]
599
- gh2 : INT16 = self ._gas_calibration [1 ]
600
- gh3 : INT8 = self ._gas_calibration [2 ]
601
- htr : UINT8 = self ._heat_range
602
- htv : INT8 = self ._heat_val
603
- amb : UINT8 = self ._amb_temp
554
+ gh1 : int = self ._gas_calibration [0 ]
555
+ gh2 : int = self ._gas_calibration [1 ]
556
+ gh3 : int = self ._gas_calibration [2 ]
557
+ htr : int = self ._heat_range
558
+ htv : int = self ._heat_val
559
+ amb : int = self ._amb_temp
604
560
605
561
temp = min (temp , 400 ) # Cap temperature
606
562
607
- var1 : INT32 = ((INT32 (amb ) * gh3 ) / 1000 ) * 256
608
- var2 : INT32 = (gh1 + 784 ) * (
609
- ((((gh2 + 154009 ) * temp * 5 ) / 100 ) + 3276800 ) / 10
610
- )
611
- var3 : INT32 = var1 + (var2 / 2 )
612
- var4 : INT32 = var3 / (htr + 4 )
613
- var5 : INT32 = (131 * htv ) + 65536
614
- heatr_res_x100 : INT32 = INT32 (((var4 / var5 ) - 250 ) * 34 )
615
- heatr_res : UINT8 = UINT8 ((heatr_res_x100 + 50 ) / 100 )
563
+ var1 : int = ((int (amb ) * gh3 ) / 1000 ) * 256
564
+ var2 : int = (gh1 + 784 ) * (((((gh2 + 154009 ) * temp * 5 ) / 100 ) + 3276800 ) / 10 )
565
+ var3 : int = var1 + (var2 / 2 )
566
+ var4 : int = var3 / (htr + 4 )
567
+ var5 : int = (131 * htv ) + 65536
568
+ heatr_res_x100 : int = int (((var4 / var5 ) - 250 ) * 34 )
569
+ heatr_res : int = int ((heatr_res_x100 + 50 ) / 100 )
616
570
617
571
return heatr_res
618
572
619
- def _calc_res_heat (self , temp : UINT16 ) -> UINT8 :
573
+ def _calc_res_heat (self , temp : int ) -> int :
620
574
"""
621
575
This internal API is used to calculate the heater resistance value
622
576
"""
@@ -634,28 +588,26 @@ def _calc_res_heat(self, temp: UINT16) -> UINT8:
634
588
var3 : float = gh3 / (1024.0 )
635
589
var4 : float = var1 * (1.0 + (var2 * float (temp )))
636
590
var5 : float = var4 + (var3 * amb )
637
- res_heat : UINT8 = UINT8 (
591
+ res_heat : int = int (
638
592
3.4 * ((var5 * (4 / (4 + htr )) * (1 / (1 + (htv * 0.002 )))) - 25 )
639
593
)
640
594
return res_heat
641
595
642
- def _calc_gas_wait (self , dur : UINT16 ) -> UINT8 :
596
+ def _calc_gas_wait (self , dur : int ) -> int :
643
597
"""
644
598
This internal API is used to calculate the gas wait
645
599
"""
646
- factor : UINT8 = 0
647
- durval : UINT8 = 0xFF # Max duration
600
+ factor : int = 0
601
+ durval : int = 0xFF # Max duration
648
602
649
603
if dur < 0xFC0 :
650
604
return durval
651
605
while dur > 0x3F :
652
606
dur = dur / 4
653
607
factor += 1
654
- durval = UINT8 (dur + (factor * 64 ))
608
+ durval = int (dur + (factor * 64 ))
655
609
return durval
656
610
657
- # garberw added end ===========================
658
-
659
611
660
612
class Adafruit_BME680_I2C (Adafruit_BME680 ):
661
613
"""Driver for I2C connected BME680.
0 commit comments