Skip to content

Commit 1f04f54

Browse files
authored
Merge pull request #62 from FoamyGuy/pr61_followup
feedback from pr61
2 parents b08654f + fed34fb commit 1f04f54

File tree

3 files changed

+68
-132
lines changed

3 files changed

+68
-132
lines changed

LICENSES/BSD-3-Clause.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,3 @@ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
3030
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
3131
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3232
POSSIBILITY OF SUCH DAMAGE.
33-
34-
@file bme68x_defs.h
35-
@date 2021-05-24
36-
@version v4.4.6

adafruit_bme680.py

Lines changed: 68 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
# We have a lot of attributes for this complex sensor.
66
# pylint: disable=too-many-instance-attributes
77
# pylint: disable=no_self_use
8-
# pylint: disable=consider-using-f-string
98

109
"""
1110
`adafruit_bme680`
@@ -14,10 +13,7 @@
1413
CircuitPython library for BME680 temperature, pressure and humidity sensor.
1514
1615
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
2117
2218
2319
Implementation Notes
@@ -61,7 +57,6 @@ def delay_microseconds(nusec):
6157
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BME680.git"
6258

6359

64-
# garberw added begin ===========================
6560
# I2C ADDRESS/BITS/SETTINGS NEW
6661
# -----------------------------------------------------------------------
6762
_BME68X_ENABLE_HEATER = const(0x00)
@@ -83,8 +78,6 @@ def delay_microseconds(nusec):
8378
_BME68X_REG_CTRL_GAS_0 = const(0x70)
8479
_BME68X_REG_CTRL_GAS_1 = const(0x71)
8580

86-
87-
# garberw added end ===========================
8881
# I2C ADDRESS/BITS/SETTINGS
8982
# -----------------------------------------------------------------------
9083
_BME680_CHIPID = const(0x61)
@@ -152,15 +145,6 @@ def delay_microseconds(nusec):
152145
)
153146

154147

155-
# garberw added begin ===========================
156-
INT32 = int
157-
INT16 = int
158-
INT8 = int
159-
UINT32 = int
160-
UINT16 = int
161-
UINT8 = int
162-
163-
164148
def bme_set_bits(reg_data, bitname_msk, bitname_pos, data):
165149
"""
166150
Macro to set bits
@@ -178,17 +162,6 @@ def bme_set_bits_pos_0(reg_data, bitname_msk, data):
178162
return (reg_data & ~bitname_msk) | (data & bitname_msk)
179163

180164

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 ===========================
192165
def _read24(arr: ReadableBuffer) -> float:
193166
"""Parse an unsigned 24-bit value as a floating point and return it."""
194167
ret = 0.0
@@ -244,12 +217,9 @@ def __init__(self, *, refresh_rate: int = 10) -> None:
244217
self._last_reading = 0
245218
self._min_refresh_time = 1 / refresh_rate
246219

247-
# garberw added begin ===========================
248220
self._amb_temp = 25 # Copy required parameters from reference bme68x_dev struct
249221
self.set_gas_heater(320, 150) # heater 320 deg C for 150 msec
250222

251-
# garberw added end ===========================
252-
253223
@property
254224
def pressure_oversample(self) -> int:
255225
"""The oversampling for pressure sensor"""
@@ -482,41 +452,37 @@ def _read(self, register: int, length: int) -> bytearray:
482452
def _write(self, register: int, values: bytearray) -> None:
483453
raise NotImplementedError()
484454

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:
486456
"""
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
493461
"""
494462
try:
495463
if (heater_temp is None) or (heater_time is None):
496464
self._set_heatr_conf(heater_temp or 0, heater_time or 0, enable=False)
497465
else:
498466
self._set_heatr_conf(heater_temp, heater_time)
499-
except GasHeaterException:
467+
except OSError:
500468
return False
501469
return True
502470

503471
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
505473
) -> None:
506474
# 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
515481
try:
516482
self._set_op_mode(_BME68X_SLEEP_MODE)
517483
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)
520486
if enable:
521487
hctrl = _BME68X_ENABLE_HEATER
522488
if self._chip_variant == _BME68X_VARIANT_GAS_HIGH:
@@ -536,86 +502,73 @@ def _set_heatr_conf(
536502
ctrl_gas_data_1 = bme_set_bits(
537503
ctrl_gas_data_1, _BME68X_RUN_GAS_MSK, _BME68X_RUN_GAS_POS, run_gas
538504
)
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])
541507
# HELP check this
508+
finally:
542509
self._set_op_mode(_BME68X_FORCED_MODE)
543-
except GasHeaterException as exc:
544-
self._set_op_mode(_BME68X_FORCED_MODE)
545-
raise exc
546510

547-
def _set_op_mode(self, op_mode: UINT8) -> None:
511+
def _set_op_mode(self, op_mode: int) -> None:
548512
"""
549513
* @brief This API is used to set the operation mode of the sensor
550514
"""
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
554517
# 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
574518

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:
578537
"""
579538
This internal API is used to set heater configurations
580539
"""
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:
594549
"""
595550
This internal API is used to calculate the heater resistance value using float
596551
"""
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
603558

604559
temp = min(temp, 400) # Cap temperature
605560

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)
615568

616569
return heatr_res
617570

618-
def _calc_res_heat(self, temp: UINT16) -> UINT8:
571+
def _calc_res_heat(self, temp: int) -> int:
619572
"""
620573
This internal API is used to calculate the heater resistance value
621574
"""
@@ -633,28 +586,26 @@ def _calc_res_heat(self, temp: UINT16) -> UINT8:
633586
var3: float = gh3 / (1024.0)
634587
var4: float = var1 * (1.0 + (var2 * float(temp)))
635588
var5: float = var4 + (var3 * amb)
636-
res_heat: UINT8 = UINT8(
589+
res_heat: int = int(
637590
3.4 * ((var5 * (4 / (4 + htr)) * (1 / (1 + (htv * 0.002)))) - 25)
638591
)
639592
return res_heat
640593

641-
def _calc_gas_wait(self, dur: UINT16) -> UINT8:
594+
def _calc_gas_wait(self, dur: int) -> int:
642595
"""
643596
This internal API is used to calculate the gas wait
644597
"""
645-
factor: UINT8 = 0
646-
durval: UINT8 = 0xFF # Max duration
598+
factor: int = 0
599+
durval: int = 0xFF # Max duration
647600

648601
if dur < 0xFC0:
649602
return durval
650603
while dur > 0x3F:
651604
dur = dur / 4
652605
factor += 1
653-
durval = UINT8(dur + (factor * 64))
606+
durval = int(dur + (factor * 64))
654607
return durval
655608

656-
# garberw added end ===========================
657-
658609

659610
class Adafruit_BME680_I2C(Adafruit_BME680):
660611
"""Driver for I2C connected BME680.

contributors.md

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)