33
33
from struct import unpack_from
34
34
from adafruit_bus_device import i2c_device
35
35
36
+ try :
37
+ from typing import List , Optional
38
+ from circuitpython_typing import ReadableBuffer
39
+ from busio import I2C
40
+ except ImportError :
41
+ pass
42
+
36
43
__version__ = "0.0.0+auto.0"
37
44
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SGP40.git"
38
45
@@ -107,15 +114,15 @@ class SGP40:
107
114
108
115
"""
109
116
110
- def __init__ (self , i2c , address = 0x59 ):
117
+ def __init__ (self , i2c : I2C , address : int = 0x59 ) -> None :
111
118
self .i2c_device = i2c_device .I2CDevice (i2c , address )
112
119
self ._command_buffer = bytearray (2 )
113
120
self ._measure_command = _READ_CMD
114
121
self ._voc_algorithm = None
115
122
116
123
self .initialize ()
117
124
118
- def initialize (self ):
125
+ def initialize (self ) -> None :
119
126
"""Reset the sensor to it's initial unconfigured state and configure it with sensible
120
127
defaults so it can be used"""
121
128
# check serial number
@@ -132,7 +139,7 @@ def initialize(self):
132
139
featureset = self ._read_word_from_command ()
133
140
if featureset [0 ] != 0x3220 :
134
141
135
- raise RuntimeError ("Feature set does not match: %s" % hex ( featureset [0 ]) )
142
+ raise RuntimeError (f "Feature set does not match: { featureset [0 ]:#x } " )
136
143
137
144
# Self Test
138
145
self ._command_buffer [0 ] = 0x28
@@ -142,7 +149,7 @@ def initialize(self):
142
149
raise RuntimeError ("Self test failed" )
143
150
self ._reset ()
144
151
145
- def _reset (self ):
152
+ def _reset (self ) -> None :
146
153
# This is a general call Reset. Several sensors may see this and it doesn't appear to
147
154
# ACK before resetting
148
155
self ._command_buffer [0 ] = 0x00
@@ -156,7 +163,7 @@ def _reset(self):
156
163
sleep (1 )
157
164
158
165
@staticmethod
159
- def _celsius_to_ticks (temperature ) :
166
+ def _celsius_to_ticks (temperature : float ) -> List [ int ] :
160
167
"""
161
168
Converts Temperature in Celsius to 'ticks' which are an input parameter
162
169
the sgp40 can use
@@ -175,7 +182,7 @@ def _celsius_to_ticks(temperature):
175
182
return [most_sig_temp_ticks , least_sig_temp_ticks ]
176
183
177
184
@staticmethod
178
- def _relative_humidity_to_ticks (humidity ) :
185
+ def _relative_humidity_to_ticks (humidity : float ) -> List [ int ] :
179
186
"""
180
187
Converts Relative Humidity in % to 'ticks' which are an input parameter
181
188
the sgp40 can use
@@ -194,15 +201,15 @@ def _relative_humidity_to_ticks(humidity):
194
201
return [most_sig_rhumidity_ticks , least_sig_rhumidity_ticks ]
195
202
196
203
@property
197
- def raw (self ):
204
+ def raw (self ) -> int :
198
205
"""The raw gas value"""
199
206
# recycle a single buffer
200
207
self ._command_buffer = self ._measure_command
201
208
read_value = self ._read_word_from_command (delay_ms = 250 )
202
209
self ._command_buffer = bytearray (2 )
203
210
return read_value [0 ]
204
211
205
- def measure_raw (self , temperature = 25 , relative_humidity = 50 ):
212
+ def measure_raw (self , temperature : float = 25 , relative_humidity : float = 50 ):
206
213
"""
207
214
A humidity and temperature compensated raw gas value which helps
208
215
address fluctuations in readings due to changing humidity.
@@ -225,7 +232,9 @@ def measure_raw(self, temperature=25, relative_humidity=50):
225
232
self ._measure_command = bytearray (_cmd )
226
233
return self .raw
227
234
228
- def measure_index (self , temperature = 25 , relative_humidity = 50 ):
235
+ def measure_index (
236
+ self , temperature : float = 25 , relative_humidity : float = 50
237
+ ) -> int :
229
238
"""Measure VOC index after humidity compensation
230
239
:param float temperature: The temperature in degrees Celsius, defaults to :const:`25`
231
240
:param float relative_humidity: The relative humidity in percentage, defaults to :const:`50`
@@ -234,7 +243,7 @@ def measure_index(self, temperature=25, relative_humidity=50):
234
243
:note 0-100, no need to ventilate, purify
235
244
:note 100-200, no need to ventilate, purify
236
245
:note 200-400, ventilate, purify
237
- :note 00 -500, ventilate, purify intensely
246
+ :note 400 -500, ventilate, purify intensely
238
247
:return int The VOC index measured, ranged from 0 to 500
239
248
"""
240
249
# import/setup algorithm only on use of index
@@ -255,9 +264,9 @@ def measure_index(self, temperature=25, relative_humidity=50):
255
264
256
265
def _read_word_from_command (
257
266
self ,
258
- delay_ms = 10 ,
259
- readlen = 1 ,
260
- ):
267
+ delay_ms : int = 10 ,
268
+ readlen : Optional [ int ] = 1 ,
269
+ ) -> Optional [ List [ int ]] :
261
270
"""_read_word_from_command - send a given command code and read the result back
262
271
263
272
Args:
@@ -291,15 +300,15 @@ def _read_word_from_command(
291
300
292
301
return readdata_buffer
293
302
294
- def _check_crc8 (self , crc_buffer , crc_value ) :
303
+ def _check_crc8 (self , crc_buffer : ReadableBuffer , crc_value : int ) -> bool :
295
304
"""
296
305
Checks that the 8 bit CRC Checksum value from the sensor matches the
297
306
received data
298
307
"""
299
308
return crc_value == self ._generate_crc (crc_buffer )
300
309
301
310
@staticmethod
302
- def _generate_crc (crc_buffer ) :
311
+ def _generate_crc (crc_buffer : ReadableBuffer ) -> int :
303
312
"""
304
313
Generates an 8 bit CRC Checksum from the input buffer.
305
314
0 commit comments