Skip to content

Commit 90f8cfe

Browse files
committed
get & set the baseline
1 parent 7935dc4 commit 90f8cfe

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

adafruit_sgp30.py

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,68 +33,81 @@
3333
SGP30_DEFAULT_I2C_ADDR = const(0x58)
3434
SGP30_FEATURESET = const(0x0020)
3535

36-
SGP30_COMMAND_SERIALID = const(0x3682)
37-
SGP30_COMMAND_FEATURESET = const(0x202f)
3836
SGP30_CRC8_POLYNOMIAL = const(0x31)
3937
SGP30_CRC8_INIT = const(0xFF)
4038
SGP30_WORD_LEN = const(2)
4139

4240
class Adafruit_SGP30:
4341
def __init__(self, i2c, address=SGP30_DEFAULT_I2C_ADDR):
42+
"""Initialize the sensor, get the serial # and verify that we found a proper SGP30"""
4443
self._device = I2CDevice(i2c, address)
4544

4645
# get unique serial, its 48 bits so we store in an array
47-
self._serial = self.sgp_i2c_read_words_from_cmd(SGP30_COMMAND_SERIALID, 0.01, 3)
48-
print("Serial: ", [hex(i) for i in self._serial])
46+
self._serial = self.sgp_i2c_read_words_from_cmd([0x36, 0x82], 0.01, 3)
4947
# get featuerset
50-
featureset = self.sgp_i2c_read_words_from_cmd(SGP30_COMMAND_FEATURESET, 0.01, 1)
48+
featureset = self.sgp_i2c_read_words_from_cmd([0x20, 0x2f], 0.01, 1)
5149
if featureset[0] != SGP30_FEATURESET:
5250
raise RuntimeError('SGP30 Not detected')
5351
self.sgp_iaq_init()
5452

5553
def sgp_iaq_init(self):
56-
self.sgp_run_profile(["iaq_init", 0x2003, 0, 0.01]) # name, command, signals, delay
54+
"""Initialize the IAQ algorithm"""
55+
# name, command, signals, delay
56+
self.sgp_run_profile(["iaq_init", [0x20, 0x03], 0, 0.01])
5757

5858
def sgp_iaq_measure(self):
59-
return self.sgp_run_profile(["iaq_measure", 0x2008, 2, 0.05]) # name, command, signals, delay
59+
"""Measure the CO2eq and TVOC"""
60+
# name, command, signals, delay
61+
return self.sgp_run_profile(["iaq_measure", [0x20, 0x08], 2, 0.05])
6062

63+
def sgp_get_iaq_baseline(self):
64+
"""Retreive the IAQ algorithm baseline for CO2eq and TVOC"""
65+
# name, command, signals, delay
66+
return self.sgp_run_profile(["iaq_get_baseline", [0x20, 0x15], 2, 0.01])
67+
68+
69+
def sgp_set_iaq_baseline(self, co2eq, tvoc):
70+
if co2eq == 0 and tvoc == 0:
71+
raise RuntimeError('Invalid baseline')
72+
buffer = []
73+
for v in [tvoc, co2eq]:
74+
arr = [v >> 8, v & 0xFF]
75+
arr.append(self.sensirion_common_generate_crc(arr))
76+
buffer += arr
77+
self.sgp_run_profile(["iaq_set_baseline", [0x20, 0x1e] + buffer, 0, 0.01])
78+
79+
80+
# Low level command functions
81+
6182
def sgp_run_profile(self, profile):
83+
"""Run an SGP 'profile' which is a named command set"""
6284
name, command, signals, delay = profile
63-
print("running profile: %s, command 0x%x, %d, delay %f" % (name, command, signals, delay))
85+
#print("\trunning profile: %s, command %s, %d, delay %0.02f" % (name, ["0x%02x" % i for i in command], signals, delay))
6486
return self.sgp_i2c_read_words_from_cmd(command, delay, signals)
6587

66-
"""
67-
with self._device:
68-
self._device.write(bytes([(command >> 8) & 0xFF,
69-
command & 0xFF]))
70-
time.sleep(delay)
71-
if signals > 0:
72-
crc_result = bytearray(signals)
73-
self._device.read_into(crc_result)
74-
print("\tRaw Read: ", crc_result)
75-
"""
7688

7789
def sgp_i2c_read_words_from_cmd(self, command, delay, reply_size):
90+
"""Run an SGP command query, get a reply and CRC results if necessary"""
7891
with self._device:
79-
self._device.write(bytes([(command >> 8) & 0xFF,
80-
command & 0xFF]))
92+
self._device.write(bytes(command))
8193
time.sleep(delay)
8294
if not reply_size:
8395
return None
8496
crc_result = bytearray(reply_size * (SGP30_WORD_LEN +1))
8597
self._device.read_into(crc_result)
86-
print("\tRaw Read: ", crc_result)
98+
#print("\tRaw Read: ", crc_result)
8799
result = []
88100
for i in range(reply_size):
89101
word = [crc_result[3*i], crc_result[3*i+1]]
90102
crc = crc_result[3*i+2]
91103
if self.sensirion_common_generate_crc(word) != crc:
92104
raise RuntimeError('CRC Error')
93105
result.append(word[0] << 8 | word[1])
94-
print("\tOK Data: ", [hex(i) for i in result])
106+
#print("\tOK Data: ", [hex(i) for i in result])
95107
return result
96108

97109
def sensirion_common_generate_crc(self, data):
110+
"""8-bit CRC algorithm for checking data"""
98111
crc = SGP30_CRC8_INIT
99112
# calculates 8-Bit checksum with given polynomial
100113
for byte in data:

0 commit comments

Comments
 (0)