32
32
from time import sleep
33
33
from struct import unpack_from
34
34
from adafruit_bus_device import i2c_device
35
- from adafruit_sgp40 .voc_algorithm import VOCAlgorithm
36
35
37
36
__version__ = "0.0.0-auto.0"
38
37
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SGP40.git"
@@ -112,7 +111,7 @@ def __init__(self, i2c, address=0x59):
112
111
self .i2c_device = i2c_device .I2CDevice (i2c , address )
113
112
self ._command_buffer = bytearray (2 )
114
113
self ._measure_command = _READ_CMD
115
- self ._voc_algorithm = VOCAlgorithm ()
114
+ self ._voc_algorithm = None
116
115
117
116
self .initialize ()
118
117
@@ -135,8 +134,6 @@ def initialize(self):
135
134
136
135
raise RuntimeError ("Feature set does not match: %s" % hex (featureset [0 ]))
137
136
138
- self ._voc_algorithm .vocalgorithm_init ()
139
-
140
137
# Self Test
141
138
self ._command_buffer [0 ] = 0x28
142
139
self ._command_buffer [1 ] = 0x0E
@@ -234,12 +231,22 @@ def measure_index(self, temperature=25, relative_humidity=50):
234
231
:param float relative_humidity: The relative humidity in percentage, defaults to :const:`50`
235
232
:note VOC index can indicate the quality of the air directly.
236
233
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
241
238
:return int The VOC index measured, ranged from 0 to 500
242
239
"""
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
+
243
250
raw = self .measure_raw (temperature , relative_humidity )
244
251
if raw < 0 :
245
252
return - 1
0 commit comments