Skip to content

Commit 4b4818c

Browse files
committed
feedback from pr61
1 parent d7909fe commit 4b4818c

File tree

3 files changed

+68
-131
lines changed

3 files changed

+68
-131
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 & 116 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,42 +452,37 @@ def _read(self, register: int, length: int) -> bytearray:
482452
def _write(self, register: int, values: bytearray) -> None:
483453
raise NotImplementedError()
484454

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:
487456
"""
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
494461
"""
495-
if (heater_temp == 0) or (heater_time == 0):
462+
if not heater_temp or not heater_time:
496463
return False
497464
# enable = BME68X_ENABLE
498465
try:
499466
self._set_heatr_conf(heater_temp, heater_time)
500-
except GasHeaterException:
467+
except OSError:
501468
return False
502469
return True
503470

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:
505472
# restrict to BME68X_FORCED_MODE
506-
op_mode: UINT8 = _BME68X_FORCED_MODE
473+
op_mode: int = _BME68X_FORCED_MODE
507474
# restrict to enable = True
508475
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
516481
try:
517482
self._set_op_mode(_BME68X_SLEEP_MODE)
518483
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)
521486
if enable:
522487
hctrl = _BME68X_ENABLE_HEATER
523488
if self._chip_variant == _BME68X_VARIANT_GAS_HIGH:
@@ -537,86 +502,75 @@ def _set_heatr_conf(self, heater_temp: UINT16, heater_time: UINT16) -> None:
537502
ctrl_gas_data_1 = bme_set_bits(
538503
ctrl_gas_data_1, _BME68X_RUN_GAS_MSK, _BME68X_RUN_GAS_POS, run_gas
539504
)
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])
542507
# HELP check this
543508
self._set_op_mode(_BME68X_FORCED_MODE)
544-
except GasHeaterException as exc:
509+
except OSError as exc:
545510
self._set_op_mode(_BME68X_FORCED_MODE)
546511
raise exc
547512

548-
def _set_op_mode(self, op_mode: UINT8) -> None:
513+
def _set_op_mode(self, op_mode: int) -> None:
549514
"""
550515
* @brief This API is used to set the operation mode of the sensor
551516
"""
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
555519
# 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
575520

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:
579539
"""
580540
This internal API is used to set heater configurations
581541
"""
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
593542

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:
595551
"""
596552
This internal API is used to calculate the heater resistance value using float
597553
"""
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
604560

605561
temp = min(temp, 400) # Cap temperature
606562

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

617571
return heatr_res
618572

619-
def _calc_res_heat(self, temp: UINT16) -> UINT8:
573+
def _calc_res_heat(self, temp: int) -> int:
620574
"""
621575
This internal API is used to calculate the heater resistance value
622576
"""
@@ -634,28 +588,26 @@ def _calc_res_heat(self, temp: UINT16) -> UINT8:
634588
var3: float = gh3 / (1024.0)
635589
var4: float = var1 * (1.0 + (var2 * float(temp)))
636590
var5: float = var4 + (var3 * amb)
637-
res_heat: UINT8 = UINT8(
591+
res_heat: int = int(
638592
3.4 * ((var5 * (4 / (4 + htr)) * (1 / (1 + (htv * 0.002)))) - 25)
639593
)
640594
return res_heat
641595

642-
def _calc_gas_wait(self, dur: UINT16) -> UINT8:
596+
def _calc_gas_wait(self, dur: int) -> int:
643597
"""
644598
This internal API is used to calculate the gas wait
645599
"""
646-
factor: UINT8 = 0
647-
durval: UINT8 = 0xFF # Max duration
600+
factor: int = 0
601+
durval: int = 0xFF # Max duration
648602

649603
if dur < 0xFC0:
650604
return durval
651605
while dur > 0x3F:
652606
dur = dur / 4
653607
factor += 1
654-
durval = UINT8(dur + (factor * 64))
608+
durval = int(dur + (factor * 64))
655609
return durval
656610

657-
# garberw added end ===========================
658-
659611

660612
class Adafruit_BME680_I2C(Adafruit_BME680):
661613
"""Driver for I2C connected BME680.

contributors.md

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

0 commit comments

Comments
 (0)