26
26
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
27
27
"""
28
28
29
- # imports
30
29
import time
31
30
import struct
32
31
import adafruit_bus_device .i2c_device as i2c_device
37
36
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SCD4X.git"
38
37
39
38
SCD4X_DEFAULT_ADDR = 0x62
40
- SCD4X_REINIT = const (0x3646 )
41
- SCD4X_FACTORYRESET = const (0x3632 )
42
- SCD4X_FORCEDRECAL = const (0x362F )
43
- SCD4X_SELFTEST = const (0x3639 )
44
- SCD4X_DATAREADY = const (0xE4B8 )
45
- SCD4X_STOPPERIODICMEASUREMENT = const (0x3F86 )
46
- SCD4X_STARTPERIODICMEASUREMENT = const (0x21B1 )
47
- SCD4X_READMEASUREMENT = const (0xEC05 )
48
- SCD4X_SERIALNUMBER = const (0x3682 )
49
- SCD4X_GETTEMPOFFSET = const (0x2318 )
50
- SCD4X_SETTEMPOFFSET = const (0x241D )
51
- SCD4X_GETALTITUDE = const (0x2322 )
52
- SCD4X_SETALTITUDE = const (0x2427 )
53
- SCD4X_SETPRESSURE = const (0xE000 )
54
- SCD4X_PERSISTSETTINGS = const (0x3615 )
55
- SCD4X_GETASCE = const (0x2313 )
56
- SCD4X_SETASCE = const (0x2416 )
39
+ _SCD4X_REINIT = const (0x3646 )
40
+ _SCD4X_FACTORYRESET = const (0x3632 )
41
+ _SCD4X_FORCEDRECAL = const (0x362F )
42
+ _SCD4X_SELFTEST = const (0x3639 )
43
+ _SCD4X_DATAREADY = const (0xE4B8 )
44
+ _SCD4X_STOPPERIODICMEASUREMENT = const (0x3F86 )
45
+ _SCD4X_STARTPERIODICMEASUREMENT = const (0x21B1 )
46
+ _SCD4X_READMEASUREMENT = const (0xEC05 )
47
+ _SCD4X_SERIALNUMBER = const (0x3682 )
48
+ _SCD4X_GETTEMPOFFSET = const (0x2318 )
49
+ _SCD4X_SETTEMPOFFSET = const (0x241D )
50
+ _SCD4X_GETALTITUDE = const (0x2322 )
51
+ _SCD4X_SETALTITUDE = const (0x2427 )
52
+ _SCD4X_SETPRESSURE = const (0xE000 )
53
+ _SCD4X_PERSISTSETTINGS = const (0x3615 )
54
+ _SCD4X_GETASCE = const (0x2313 )
55
+ _SCD4X_SETASCE = const (0x2416 )
57
56
58
57
59
58
class SCD4X :
@@ -146,18 +145,18 @@ def relative_humidity(self):
146
145
def reinit (self ):
147
146
"""Reinitializes the sensor by reloading user settings from EEPROM."""
148
147
self .stop_periodic_measurement ()
149
- self ._send_command (SCD4X_REINIT , cmd_delay = 0.02 )
148
+ self ._send_command (_SCD4X_REINIT , cmd_delay = 0.02 )
150
149
151
150
def factory_reset (self ):
152
151
"""Resets all configuration settings stored in the EEPROM and erases the
153
152
FRC and ASC algorithm history."""
154
153
self .stop_periodic_measurement ()
155
- self ._send_command (SCD4X_FACTORYRESET , cmd_delay = 1.2 )
154
+ self ._send_command (_SCD4X_FACTORYRESET , cmd_delay = 1.2 )
156
155
157
156
def force_calibration (self , target_co2 ):
158
157
"""Forces the sensor to recalibrate with a given current CO2"""
159
158
self .stop_periodic_measurement ()
160
- self ._set_command_value (SCD4X_FORCEDRECAL , target_co2 )
159
+ self ._set_command_value (_SCD4X_FORCEDRECAL , target_co2 )
161
160
time .sleep (0.5 )
162
161
self ._read_reply (self ._buffer , 3 )
163
162
correction = struct .unpack_from (">h" , self ._buffer [0 :2 ])[0 ]
@@ -178,25 +177,25 @@ def self_calibration_enabled(self):
178
177
saved with persist_settings().
179
178
180
179
"""
181
- self ._send_command (SCD4X_GETASCE , cmd_delay = 0.001 )
180
+ self ._send_command (_SCD4X_GETASCE , cmd_delay = 0.001 )
182
181
self ._read_reply (self ._buffer , 3 )
183
182
return self ._buffer [1 ] == 1
184
183
185
184
@self_calibration_enabled .setter
186
185
def self_calibration_enabled (self , enabled ):
187
- self ._set_command_value (SCD4X_SETASCE , enabled )
186
+ self ._set_command_value (_SCD4X_SETASCE , enabled )
188
187
189
188
def self_test (self ):
190
189
"""Performs a self test, takes up to 10 seconds"""
191
190
self .stop_periodic_measurement ()
192
- self ._send_command (SCD4X_SELFTEST , cmd_delay = 10 )
191
+ self ._send_command (_SCD4X_SELFTEST , cmd_delay = 10 )
193
192
self ._read_reply (self ._buffer , 3 )
194
193
if (self ._buffer [0 ] != 0 ) or (self ._buffer [1 ] != 0 ):
195
194
raise RuntimeError ("Self test failed" )
196
195
197
196
def _read_data (self ):
198
197
"""Reads the temp/hum/co2 from the sensor and caches it"""
199
- self ._send_command (SCD4X_READMEASUREMENT , cmd_delay = 0.001 )
198
+ self ._send_command (_SCD4X_READMEASUREMENT , cmd_delay = 0.001 )
200
199
self ._read_reply (self ._buffer , 9 )
201
200
self ._co2 = (self ._buffer [0 ] << 8 ) | self ._buffer [1 ]
202
201
temp = (self ._buffer [3 ] << 8 ) | self ._buffer [4 ]
@@ -207,14 +206,14 @@ def _read_data(self):
207
206
@property
208
207
def data_ready (self ):
209
208
"""Check the sensor to see if new data is available"""
210
- self ._send_command (SCD4X_DATAREADY , cmd_delay = 0.001 )
209
+ self ._send_command (_SCD4X_DATAREADY , cmd_delay = 0.001 )
211
210
self ._read_reply (self ._buffer , 3 )
212
211
return not ((self ._buffer [0 ] & 0x03 == 0 ) and (self ._buffer [1 ] == 0 ))
213
212
214
213
@property
215
214
def serial_number (self ):
216
215
"""Request a 6-tuple containing the unique serial number for this sensor"""
217
- self ._send_command (SCD4X_SERIALNUMBER , cmd_delay = 0.001 )
216
+ self ._send_command (_SCD4X_SERIALNUMBER , cmd_delay = 0.001 )
218
217
self ._read_reply (self ._buffer , 9 )
219
218
return (
220
219
self ._buffer [0 ],
@@ -227,21 +226,21 @@ def serial_number(self):
227
226
228
227
def stop_periodic_measurement (self ):
229
228
"""Stop measurement mode"""
230
- self ._send_command (SCD4X_STOPPERIODICMEASUREMENT , cmd_delay = 0.5 )
229
+ self ._send_command (_SCD4X_STOPPERIODICMEASUREMENT , cmd_delay = 0.5 )
231
230
232
231
def start_periodic_measurement (self ):
233
232
"""Put sensor into working mode, about 5s per measurement"""
234
- self ._send_command (SCD4X_STARTPERIODICMEASUREMENT , cmd_delay = 0.01 )
233
+ self ._send_command (_SCD4X_STARTPERIODICMEASUREMENT , cmd_delay = 0.01 )
235
234
236
235
def persist_settings (self ):
237
236
"""Save temperature offset, altitude offset, and selfcal enable settings to EEPROM"""
238
- self ._send_command (SCD4X_PERSISTSETTINGS , cmd_delay = 0.8 )
237
+ self ._send_command (_SCD4X_PERSISTSETTINGS , cmd_delay = 0.8 )
239
238
240
239
def set_ambient_pressure (self , ambient_pressure ):
241
240
"""Set the ambient pressure in hPa at any time to adjust CO2 calculations"""
242
241
if ambient_pressure < 0 or ambient_pressure > 65535 :
243
242
raise AttributeError ("`ambient_pressure` must be from 0~65535 hPascals" )
244
- self ._set_command_value (SCD4X_SETPRESSURE , ambient_pressure )
243
+ self ._set_command_value (_SCD4X_SETPRESSURE , ambient_pressure )
245
244
246
245
@property
247
246
def temperature_offset (self ):
@@ -254,7 +253,7 @@ def temperature_offset(self):
254
253
persist_settings().
255
254
256
255
"""
257
- self ._send_command (SCD4X_GETTEMPOFFSET , cmd_delay = 0.001 )
256
+ self ._send_command (_SCD4X_GETTEMPOFFSET , cmd_delay = 0.001 )
258
257
self ._read_reply (self ._buffer , 3 )
259
258
temp = (self ._buffer [0 ] << 8 ) | self ._buffer [1 ]
260
259
return 175.0 * temp / 2 ** 16
@@ -266,7 +265,7 @@ def temperature_offset(self, offset):
266
265
"Offset value must be less than or equal to 374 degrees Celsius"
267
266
)
268
267
temp = int (offset * 2 ** 16 / 175 )
269
- self ._set_command_value (SCD4X_SETTEMPOFFSET , temp )
268
+ self ._set_command_value (_SCD4X_SETTEMPOFFSET , temp )
270
269
271
270
@property
272
271
def altitude (self ):
@@ -278,15 +277,15 @@ def altitude(self):
278
277
This value will NOT be saved and will be reset on boot unless saved with
279
278
persist_settings().
280
279
"""
281
- self ._send_command (SCD4X_GETALTITUDE , cmd_delay = 0.001 )
280
+ self ._send_command (_SCD4X_GETALTITUDE , cmd_delay = 0.001 )
282
281
self ._read_reply (self ._buffer , 3 )
283
282
return (self ._buffer [0 ] << 8 ) | self ._buffer [1 ]
284
283
285
284
@altitude .setter
286
285
def altitude (self , height ):
287
286
if height > 65535 :
288
287
raise AttributeError ("Height must be less than or equal to 65535 meters" )
289
- self ._set_command_value (SCD4X_SETALTITUDE , height )
288
+ self ._set_command_value (_SCD4X_SETALTITUDE , height )
290
289
291
290
def _check_buffer_crc (self , buf ):
292
291
for i in range (0 , len (buf ), 3 ):
@@ -296,7 +295,7 @@ def _check_buffer_crc(self, buf):
296
295
raise RuntimeError ("CRC check failed while reading data" )
297
296
return True
298
297
299
- def _send_command (self , cmd , cmd_delay = 0 ) :
298
+ def _send_command (self , cmd : int , cmd_delay : float = 0 ) -> None :
300
299
self ._cmd [0 ] = (cmd >> 8 ) & 0xFF
301
300
self ._cmd [1 ] = cmd & 0xFF
302
301
0 commit comments