-
Notifications
You must be signed in to change notification settings - Fork 4
VOC Index #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
VOC Index #9
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7b80869
added index algorithm
AVanVlack 7cca585
combined algorithm and lib
AVanVlack c7bc34d
Merge branch 'adafruit:main' into main
AVanVlack b0d6406
Merge branch 'adafruit:main' into main
AVanVlack 791e169
mv main to __init__
AVanVlack e5af7e2
added index
AVanVlack 69b093c
fixed built tests
AVanVlack 82368d6
black
AVanVlack 63287a6
fix docstring indent
AVanVlack 8df1cc5
conditional import, constants, example fix
AVanVlack a4960df
fix examples, tested
AVanVlack File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,8 @@ | |
""" | ||
from time import sleep | ||
from struct import unpack_from | ||
import adafruit_bus_device.i2c_device as i2c_device | ||
from adafruit_bus_device import i2c_device | ||
from adafruit_sgp40.voc_algorithm import VOCAlgorithm | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. may want to have this only be imported if the voc algorithm is requested, since its a large py file |
||
|
||
__version__ = "0.0.0-auto.0" | ||
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SGP40.git" | ||
|
@@ -111,6 +112,7 @@ def __init__(self, i2c, address=0x59): | |
self.i2c_device = i2c_device.I2CDevice(i2c, address) | ||
self._command_buffer = bytearray(2) | ||
self._measure_command = _READ_CMD | ||
self._voc_algorithm = VOCAlgorithm() | ||
|
||
self.initialize() | ||
|
||
|
@@ -133,7 +135,7 @@ def initialize(self): | |
|
||
raise RuntimeError("Feature set does not match: %s" % hex(featureset[0])) | ||
|
||
# VocAlgorithm_init(&voc_algorithm_params) | ||
self._voc_algorithm.vocalgorithm_init() | ||
|
||
# Self Test | ||
self._command_buffer[0] = 0x28 | ||
|
@@ -226,6 +228,24 @@ def measure_raw(self, temperature=25, relative_humidity=50): | |
self._measure_command = bytearray(_cmd) | ||
return self.raw | ||
|
||
def measure_index(self, temperature=25, relative_humidity=50): | ||
"""Measure VOC index after humidity compensation | ||
:param float temperature: The temperature in degrees Celsius, defaults to :const:`25` | ||
:param float relative_humidity: The relative humidity in percentage, defaults to :const:`50` | ||
:note VOC index can indicate the quality of the air directly. | ||
The larger the value, the worse the air quality. | ||
:note 0-100,no need to ventilate, purify | ||
:note 100-200,no need to ventilate, purify | ||
:note 200-400,ventilate, purify | ||
:note 00-500,ventilate, purify intensely | ||
:return int The VOC index measured, ranged from 0 to 500 | ||
""" | ||
raw = self.measure_raw(temperature, relative_humidity) | ||
if raw < 0: | ||
return -1 | ||
voc_index = self._voc_algorithm.vocalgorithm_process(raw) | ||
return voc_index | ||
|
||
def _read_word_from_command( | ||
self, | ||
delay_ms=10, | ||
|
@@ -288,7 +308,7 @@ def _generate_crc(crc_buffer): | |
if crc & 0x80: | ||
crc = ( | ||
crc << 1 | ||
) ^ 0x31 # 0x31 is the Seed for SGP40's CRC polynomial | ||
) ^ 0x31 # 0x31 is the Seed for SGP40's CRC polynomial | ||
else: | ||
crc = crc << 1 | ||
return crc & 0xFF # Returns only bottom 8 bits |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There might also need to be a time.sleep(1) here, or we can comment out one of the examples (in this case
compensated_raw_gas
would be better to comment out) because I think the sensor needs full second between readings, and both themeasure_raw
andmeasure_index
make a call to the sensor over i2c.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch, datasheet says 1s typical, .5s min