32
32
from time import sleep
33
33
from struct import unpack_from
34
34
import adafruit_bus_device .i2c_device as i2c_device
35
+ from voc_index_algorithm import DFRobot_VOCAlgorithm
35
36
36
37
__version__ = "0.0.0-auto.0"
37
38
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SGP40.git"
@@ -118,6 +119,7 @@ def __init__(self, i2c, address=0x59):
118
119
self .i2c_device = i2c_device .I2CDevice (i2c , address )
119
120
self ._command_buffer = bytearray (2 )
120
121
self ._measure_command = _READ_CMD
122
+ self ._voc_algorithm = DFRobot_VOCAlgorithm ()
121
123
122
124
self .initialize ()
123
125
@@ -138,9 +140,10 @@ def initialize(self):
138
140
featureset = self ._read_word_from_command ()
139
141
if featureset [0 ] != 0x3220 :
140
142
141
- raise RuntimeError ("Feature set does not match: %s" % hex (featureset [0 ]))
143
+ raise RuntimeError ("Feature set does not match: %s" %
144
+ hex (featureset [0 ]))
142
145
143
- # VocAlgorithm_init(&voc_algorithm_params )
146
+ self . _voc_algorithm . vocalgorithm_init ( )
144
147
145
148
# Self Test
146
149
self ._command_buffer [0 ] = 0x28
@@ -232,6 +235,26 @@ def measure_raw(self, temperature=25, relative_humidity=50):
232
235
self ._measure_command = bytearray (_cmd )
233
236
return self .raw
234
237
238
+ def measure_index (self , temperature = 25 , relative_humidity = 50 ):
239
+ """ Measure VOC index after humidity compensation
240
+ :param float temperature: The temperature in degrees Celsius, defaults
241
+ to :const:`25`
242
+ :param float relative_humidity: The relative humidity in percentage, defaults
243
+ to :const:`50`
244
+ :note VOC index can indicate the quality of the air directly. The larger the value, the worse the air quality.
245
+ :note 0-100,no need to ventilate, purify
246
+ :note 100-200,no need to ventilate, purify
247
+ :note 200-400,ventilate, purify
248
+ :note 00-500,ventilate, purify intensely
249
+ :return int The VOC index measured, ranged from 0 to 500
250
+ """
251
+ raw = self .measure_raw (temperature , relative_humidity )
252
+ if raw < 0 :
253
+ return - 1
254
+ else :
255
+ vocIndex = self ._voc_algorithm .vocalgorithm_process (raw )
256
+ return vocIndex
257
+
235
258
def _read_word_from_command (
236
259
self ,
237
260
delay_ms = 10 ,
@@ -264,9 +287,9 @@ def _read_word_from_command(
264
287
i2c .readinto (replybuffer , end = replylen )
265
288
266
289
for i in range (0 , replylen , 3 ):
267
- if not self ._check_crc8 (replybuffer [i : i + 2 ], replybuffer [i + 2 ]):
290
+ if not self ._check_crc8 (replybuffer [i : i + 2 ], replybuffer [i + 2 ]):
268
291
raise RuntimeError ("CRC check failed while reading data" )
269
- readdata_buffer .append (unpack_from (">H" , replybuffer [i : i + 2 ])[0 ])
292
+ readdata_buffer .append (unpack_from (">H" , replybuffer [i : i + 2 ])[0 ])
270
293
271
294
return readdata_buffer
272
295
@@ -294,7 +317,7 @@ def _generate_crc(crc_buffer):
294
317
if crc & 0x80 :
295
318
crc = (
296
319
crc << 1
297
- ) ^ 0x31 # 0x31 is the Seed for SGP40's CRC polynomial
320
+ ) ^ 0x31 # 0x31 is the Seed for SGP40's CRC polynomial
298
321
else :
299
322
crc = crc << 1
300
323
return crc & 0xFF # Returns only bottom 8 bits
0 commit comments