Skip to content

Commit 8df1cc5

Browse files
committed
conditional import, constants, example fix
1 parent 63287a6 commit 8df1cc5

File tree

4 files changed

+120
-111
lines changed

4 files changed

+120
-111
lines changed

README.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ For humidity compensated raw gas and voc index readings, we'll need a secondary
9292
humidity = bme280.relative_humidity
9393
9494
# For compensated raw gas readings
95-
compensated_raw_gas = sgp.measure_index(temperature = temperature, relative_humidity = humidity)
95+
compensated_raw_gas = sgp.raw(temperature = temperature, relative_humidity = humidity)
9696
9797
# For Compensated voc index readings
98-
voc_index = sgp.measure_raw(temperature = temperature, relative_humidity = humidity)
98+
voc_index = sgp.measure_index(temperature = temperature, relative_humidity = humidity)
9999
100100
print(compensated_raw_gas)
101101
print(voc_index)

adafruit_sgp40/__init__.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
from time import sleep
3333
from struct import unpack_from
3434
from adafruit_bus_device import i2c_device
35-
from adafruit_sgp40.voc_algorithm import VOCAlgorithm
3635

3736
__version__ = "0.0.0-auto.0"
3837
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SGP40.git"
@@ -112,7 +111,7 @@ def __init__(self, i2c, address=0x59):
112111
self.i2c_device = i2c_device.I2CDevice(i2c, address)
113112
self._command_buffer = bytearray(2)
114113
self._measure_command = _READ_CMD
115-
self._voc_algorithm = VOCAlgorithm()
114+
self._voc_algorithm = None
116115

117116
self.initialize()
118117

@@ -135,8 +134,6 @@ def initialize(self):
135134

136135
raise RuntimeError("Feature set does not match: %s" % hex(featureset[0]))
137136

138-
self._voc_algorithm.vocalgorithm_init()
139-
140137
# Self Test
141138
self._command_buffer[0] = 0x28
142139
self._command_buffer[1] = 0x0E
@@ -234,12 +231,22 @@ def measure_index(self, temperature=25, relative_humidity=50):
234231
:param float relative_humidity: The relative humidity in percentage, defaults to :const:`50`
235232
:note VOC index can indicate the quality of the air directly.
236233
The larger the value, the worse the air quality.
237-
:note 0-100,no need to ventilate, purify
238-
:note 100-200,no need to ventilate, purify
239-
:note 200-400,ventilate, purify
240-
:note 00-500,ventilate, purify intensely
234+
:note 0-100, no need to ventilate, purify
235+
:note 100-200, no need to ventilate, purify
236+
:note 200-400, ventilate, purify
237+
:note 00-500, ventilate, purify intensely
241238
:return int The VOC index measured, ranged from 0 to 500
242239
"""
240+
# import/setup algorithm only on use of index
241+
# pylint: disable=import-outside-toplevel
242+
from adafruit_sgp40.voc_algorithm import (
243+
VOCAlgorithm,
244+
)
245+
246+
if self._voc_algorithm is None:
247+
self._voc_algorithm = VOCAlgorithm()
248+
self._voc_algorithm.vocalgorithm_init()
249+
243250
raw = self.measure_raw(temperature, relative_humidity)
244251
if raw < 0:
245252
return -1

0 commit comments

Comments
 (0)