|
33 | 33 | SGP30_DEFAULT_I2C_ADDR = const(0x58)
|
34 | 34 | SGP30_FEATURESET = const(0x0020)
|
35 | 35 |
|
36 |
| -SGP30_COMMAND_SERIALID = const(0x3682) |
37 |
| -SGP30_COMMAND_FEATURESET = const(0x202f) |
38 | 36 | SGP30_CRC8_POLYNOMIAL = const(0x31)
|
39 | 37 | SGP30_CRC8_INIT = const(0xFF)
|
40 | 38 | SGP30_WORD_LEN = const(2)
|
41 | 39 |
|
42 | 40 | class Adafruit_SGP30:
|
43 | 41 | def __init__(self, i2c, address=SGP30_DEFAULT_I2C_ADDR):
|
| 42 | + """Initialize the sensor, get the serial # and verify that we found a proper SGP30""" |
44 | 43 | self._device = I2CDevice(i2c, address)
|
45 | 44 |
|
46 | 45 | # 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) |
49 | 47 | # 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) |
51 | 49 | if featureset[0] != SGP30_FEATURESET:
|
52 | 50 | raise RuntimeError('SGP30 Not detected')
|
53 | 51 | self.sgp_iaq_init()
|
54 | 52 |
|
55 | 53 | 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]) |
57 | 57 |
|
58 | 58 | 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]) |
60 | 62 |
|
| 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 | + |
61 | 82 | def sgp_run_profile(self, profile):
|
| 83 | + """Run an SGP 'profile' which is a named command set""" |
62 | 84 | 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)) |
64 | 86 | return self.sgp_i2c_read_words_from_cmd(command, delay, signals)
|
65 | 87 |
|
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 |
| - """ |
76 | 88 |
|
77 | 89 | 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""" |
78 | 91 | with self._device:
|
79 |
| - self._device.write(bytes([(command >> 8) & 0xFF, |
80 |
| - command & 0xFF])) |
| 92 | + self._device.write(bytes(command)) |
81 | 93 | time.sleep(delay)
|
82 | 94 | if not reply_size:
|
83 | 95 | return None
|
84 | 96 | crc_result = bytearray(reply_size * (SGP30_WORD_LEN +1))
|
85 | 97 | self._device.read_into(crc_result)
|
86 |
| - print("\tRaw Read: ", crc_result) |
| 98 | + #print("\tRaw Read: ", crc_result) |
87 | 99 | result = []
|
88 | 100 | for i in range(reply_size):
|
89 | 101 | word = [crc_result[3*i], crc_result[3*i+1]]
|
90 | 102 | crc = crc_result[3*i+2]
|
91 | 103 | if self.sensirion_common_generate_crc(word) != crc:
|
92 | 104 | raise RuntimeError('CRC Error')
|
93 | 105 | 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]) |
95 | 107 | return result
|
96 | 108 |
|
97 | 109 | def sensirion_common_generate_crc(self, data):
|
| 110 | + """8-bit CRC algorithm for checking data""" |
98 | 111 | crc = SGP30_CRC8_INIT
|
99 | 112 | # calculates 8-Bit checksum with given polynomial
|
100 | 113 | for byte in data:
|
|
0 commit comments