29
29
from adafruit_bus_device .i2c_device import I2CDevice
30
30
from micropython import const
31
31
32
+ try :
33
+ from typing import List , Tuple
34
+ from busio import I2C
35
+ except ImportError :
36
+ pass
37
+
32
38
__version__ = "0.0.0+auto.0"
33
39
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SGP30.git"
34
40
@@ -80,7 +86,7 @@ class Adafruit_SGP30:
80
86
81
87
"""
82
88
83
- def __init__ (self , i2c , address = _SGP30_DEFAULT_I2C_ADDR ):
89
+ def __init__ (self , i2c : I2C , address : int = _SGP30_DEFAULT_I2C_ADDR ) -> None :
84
90
"""Initialize the sensor, get the serial # and verify that we found a proper SGP30"""
85
91
self ._device = I2CDevice (i2c , address )
86
92
@@ -94,61 +100,63 @@ def __init__(self, i2c, address=_SGP30_DEFAULT_I2C_ADDR):
94
100
95
101
@property
96
102
# pylint: disable=invalid-name
97
- def TVOC (self ):
103
+ def TVOC (self ) -> int :
98
104
"""Total Volatile Organic Compound in parts per billion."""
99
105
return self .iaq_measure ()[1 ]
100
106
101
107
@property
102
108
# pylint: disable=invalid-name
103
- def baseline_TVOC (self ):
109
+ def baseline_TVOC (self ) -> int :
104
110
"""Total Volatile Organic Compound baseline value"""
105
111
return self .get_iaq_baseline ()[1 ]
106
112
107
113
@property
108
114
# pylint: disable=invalid-name
109
- def eCO2 (self ):
115
+ def eCO2 (self ) -> int :
110
116
"""Carbon Dioxide Equivalent in parts per million"""
111
117
return self .iaq_measure ()[0 ]
112
118
113
119
@property
114
120
# pylint: disable=invalid-name
115
- def baseline_eCO2 (self ):
121
+ def baseline_eCO2 (self ) -> int :
116
122
"""Carbon Dioxide Equivalent baseline value"""
117
123
return self .get_iaq_baseline ()[0 ]
118
124
119
125
@property
120
126
# pylint: disable=invalid-name
121
- def Ethanol (self ):
127
+ def Ethanol (self ) -> int :
122
128
"""Ethanol Raw Signal in ticks"""
123
129
return self .raw_measure ()[1 ]
124
130
125
131
@property
126
132
# pylint: disable=invalid-name
127
- def H2 (self ):
133
+ def H2 (self ) -> int :
128
134
"""H2 Raw Signal in ticks"""
129
135
return self .raw_measure ()[0 ]
130
136
131
- def iaq_init (self ):
137
+ def iaq_init (self ) -> List [ int ] :
132
138
"""Initialize the IAQ algorithm"""
133
139
# name, command, signals, delay
134
- self ._run_profile ([ "iaq_init" , [0x20 , 0x03 ], 0 , 0.01 ] )
140
+ self ._run_profile (( "iaq_init" , [0x20 , 0x03 ], 0 , 0.01 ) )
135
141
136
- def iaq_measure (self ):
142
+ def iaq_measure (self ) -> List [ int ] :
137
143
"""Measure the eCO2 and TVOC"""
138
144
# name, command, signals, delay
139
- return self ._run_profile ([ "iaq_measure" , [0x20 , 0x08 ], 2 , 0.05 ] )
145
+ return self ._run_profile (( "iaq_measure" , [0x20 , 0x08 ], 2 , 0.05 ) )
140
146
141
- def raw_measure (self ):
147
+ def raw_measure (self ) -> List [ int ] :
142
148
"""Measure H2 and Ethanol (Raw Signals)"""
143
149
# name, command, signals, delay
144
- return self ._run_profile ([ "raw_measure" , [0x20 , 0x50 ], 2 , 0.025 ] )
150
+ return self ._run_profile (( "raw_measure" , [0x20 , 0x50 ], 2 , 0.025 ) )
145
151
146
- def get_iaq_baseline (self ):
152
+ def get_iaq_baseline (self ) -> List [ int ] :
147
153
"""Retreive the IAQ algorithm baseline for eCO2 and TVOC"""
148
154
# name, command, signals, delay
149
- return self ._run_profile ([ "iaq_get_baseline" , [0x20 , 0x15 ], 2 , 0.01 ] )
155
+ return self ._run_profile (( "iaq_get_baseline" , [0x20 , 0x15 ], 2 , 0.01 ) )
150
156
151
- def set_iaq_baseline (self , eCO2 , TVOC ): # pylint: disable=invalid-name
157
+ def set_iaq_baseline ( # pylint: disable=invalid-name
158
+ self , eCO2 : int , TVOC : int
159
+ ) -> None :
152
160
"""Set the previously recorded IAQ algorithm baseline for eCO2 and TVOC"""
153
161
if eCO2 == 0 and TVOC == 0 :
154
162
raise RuntimeError ("Invalid baseline" )
@@ -157,19 +165,19 @@ def set_iaq_baseline(self, eCO2, TVOC): # pylint: disable=invalid-name
157
165
arr = [value >> 8 , value & 0xFF ]
158
166
arr .append (self ._generate_crc (arr ))
159
167
buffer += arr
160
- self ._run_profile ([ "iaq_set_baseline" , [0x20 , 0x1E ] + buffer , 0 , 0.01 ] )
168
+ self ._run_profile (( "iaq_set_baseline" , [0x20 , 0x1E ] + buffer , 0 , 0.01 ) )
161
169
162
- def set_iaq_humidity (self , gramsPM3 ) : # pylint: disable=invalid-name
170
+ def set_iaq_humidity (self , gramsPM3 : float ) -> None : # pylint: disable=invalid-name
163
171
"""Set the humidity in g/m3 for eCO2 and TVOC compensation algorithm"""
164
172
tmp = int (gramsPM3 * 256 )
165
173
buffer = []
166
174
for value in [tmp ]:
167
175
arr = [value >> 8 , value & 0xFF ]
168
176
arr .append (self ._generate_crc (arr ))
169
177
buffer += arr
170
- self ._run_profile ([ "iaq_set_humidity" , [0x20 , 0x61 ] + buffer , 0 , 0.01 ] )
178
+ self ._run_profile (( "iaq_set_humidity" , [0x20 , 0x61 ] + buffer , 0 , 0.01 ) )
171
179
172
- def set_iaq_relative_humidity (self , celsius , relative_humidity ):
180
+ def set_iaq_relative_humidity (self , celsius : float , relative_humidity : float ):
173
181
"""
174
182
Set the humidity in g/m3 for eCo2 and TVOC compensation algorithm.
175
183
The absolute humidity is calculated from the temperature (Celsius)
@@ -185,7 +193,7 @@ def set_iaq_relative_humidity(self, celsius, relative_humidity):
185
193
186
194
# Low level command functions
187
195
188
- def _run_profile (self , profile ) :
196
+ def _run_profile (self , profile : Tuple [ str , List [ int ], int , float ]) -> List [ int ] :
189
197
"""Run an SGP 'profile' which is a named command set"""
190
198
# pylint: disable=unused-variable
191
199
name , command , signals , delay = profile
@@ -195,7 +203,9 @@ def _run_profile(self, profile):
195
203
# (name, ["0x%02x" % i for i in command], signals, delay))
196
204
return self ._i2c_read_words_from_cmd (command , delay , signals )
197
205
198
- def _i2c_read_words_from_cmd (self , command , delay , reply_size ):
206
+ def _i2c_read_words_from_cmd (
207
+ self , command : List [int ], delay : float , reply_size : int
208
+ ) -> List [int ]:
199
209
"""Run an SGP command query, get a reply and CRC results if necessary"""
200
210
with self ._device :
201
211
self ._device .write (bytes (command ))
@@ -216,7 +226,7 @@ def _i2c_read_words_from_cmd(self, command, delay, reply_size):
216
226
return result
217
227
218
228
# pylint: disable=no-self-use
219
- def _generate_crc (self , data ) :
229
+ def _generate_crc (self , data : bytearray ) -> int :
220
230
"""8-bit CRC algorithm for checking data"""
221
231
crc = _SGP30_CRC8_INIT
222
232
# calculates 8-Bit checksum with given polynomial
0 commit comments