Skip to content

Commit 742988b

Browse files
Rename the constants to be private.
Based on a comment on the Scott & Jimmo deep-dive stream, private constants get treated different and use less space in the .mpy file because of not having to store the name of the constant. CircuitPython 6 MPY Before 6595 bytes After 6113 bytes Saving 482 bytes CircuitPython 7 MPY Before 3671 bytes After 3234 bytes Saving 437 bytes
1 parent 80e30aa commit 742988b

File tree

1 file changed

+35
-36
lines changed

1 file changed

+35
-36
lines changed

adafruit_scd4x.py

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
2727
"""
2828

29-
# imports
3029
import time
3130
import struct
3231
import adafruit_bus_device.i2c_device as i2c_device
@@ -37,23 +36,23 @@
3736
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SCD4X.git"
3837

3938
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)
5756

5857

5958
class SCD4X:
@@ -146,18 +145,18 @@ def relative_humidity(self):
146145
def reinit(self):
147146
"""Reinitializes the sensor by reloading user settings from EEPROM."""
148147
self.stop_periodic_measurement()
149-
self._send_command(SCD4X_REINIT, cmd_delay=0.02)
148+
self._send_command(_SCD4X_REINIT, cmd_delay=0.02)
150149

151150
def factory_reset(self):
152151
"""Resets all configuration settings stored in the EEPROM and erases the
153152
FRC and ASC algorithm history."""
154153
self.stop_periodic_measurement()
155-
self._send_command(SCD4X_FACTORYRESET, cmd_delay=1.2)
154+
self._send_command(_SCD4X_FACTORYRESET, cmd_delay=1.2)
156155

157156
def force_calibration(self, target_co2):
158157
"""Forces the sensor to recalibrate with a given current CO2"""
159158
self.stop_periodic_measurement()
160-
self._set_command_value(SCD4X_FORCEDRECAL, target_co2)
159+
self._set_command_value(_SCD4X_FORCEDRECAL, target_co2)
161160
time.sleep(0.5)
162161
self._read_reply(self._buffer, 3)
163162
correction = struct.unpack_from(">h", self._buffer[0:2])[0]
@@ -178,25 +177,25 @@ def self_calibration_enabled(self):
178177
saved with persist_settings().
179178
180179
"""
181-
self._send_command(SCD4X_GETASCE, cmd_delay=0.001)
180+
self._send_command(_SCD4X_GETASCE, cmd_delay=0.001)
182181
self._read_reply(self._buffer, 3)
183182
return self._buffer[1] == 1
184183

185184
@self_calibration_enabled.setter
186185
def self_calibration_enabled(self, enabled):
187-
self._set_command_value(SCD4X_SETASCE, enabled)
186+
self._set_command_value(_SCD4X_SETASCE, enabled)
188187

189188
def self_test(self):
190189
"""Performs a self test, takes up to 10 seconds"""
191190
self.stop_periodic_measurement()
192-
self._send_command(SCD4X_SELFTEST, cmd_delay=10)
191+
self._send_command(_SCD4X_SELFTEST, cmd_delay=10)
193192
self._read_reply(self._buffer, 3)
194193
if (self._buffer[0] != 0) or (self._buffer[1] != 0):
195194
raise RuntimeError("Self test failed")
196195

197196
def _read_data(self):
198197
"""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)
200199
self._read_reply(self._buffer, 9)
201200
self._co2 = (self._buffer[0] << 8) | self._buffer[1]
202201
temp = (self._buffer[3] << 8) | self._buffer[4]
@@ -207,14 +206,14 @@ def _read_data(self):
207206
@property
208207
def data_ready(self):
209208
"""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)
211210
self._read_reply(self._buffer, 3)
212211
return not ((self._buffer[0] & 0x03 == 0) and (self._buffer[1] == 0))
213212

214213
@property
215214
def serial_number(self):
216215
"""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)
218217
self._read_reply(self._buffer, 9)
219218
return (
220219
self._buffer[0],
@@ -227,21 +226,21 @@ def serial_number(self):
227226

228227
def stop_periodic_measurement(self):
229228
"""Stop measurement mode"""
230-
self._send_command(SCD4X_STOPPERIODICMEASUREMENT, cmd_delay=0.5)
229+
self._send_command(_SCD4X_STOPPERIODICMEASUREMENT, cmd_delay=0.5)
231230

232231
def start_periodic_measurement(self):
233232
"""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)
235234

236235
def persist_settings(self):
237236
"""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)
239238

240239
def set_ambient_pressure(self, ambient_pressure):
241240
"""Set the ambient pressure in hPa at any time to adjust CO2 calculations"""
242241
if ambient_pressure < 0 or ambient_pressure > 65535:
243242
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)
245244

246245
@property
247246
def temperature_offset(self):
@@ -254,7 +253,7 @@ def temperature_offset(self):
254253
persist_settings().
255254
256255
"""
257-
self._send_command(SCD4X_GETTEMPOFFSET, cmd_delay=0.001)
256+
self._send_command(_SCD4X_GETTEMPOFFSET, cmd_delay=0.001)
258257
self._read_reply(self._buffer, 3)
259258
temp = (self._buffer[0] << 8) | self._buffer[1]
260259
return 175.0 * temp / 2 ** 16
@@ -266,7 +265,7 @@ def temperature_offset(self, offset):
266265
"Offset value must be less than or equal to 374 degrees Celsius"
267266
)
268267
temp = int(offset * 2 ** 16 / 175)
269-
self._set_command_value(SCD4X_SETTEMPOFFSET, temp)
268+
self._set_command_value(_SCD4X_SETTEMPOFFSET, temp)
270269

271270
@property
272271
def altitude(self):
@@ -278,15 +277,15 @@ def altitude(self):
278277
This value will NOT be saved and will be reset on boot unless saved with
279278
persist_settings().
280279
"""
281-
self._send_command(SCD4X_GETALTITUDE, cmd_delay=0.001)
280+
self._send_command(_SCD4X_GETALTITUDE, cmd_delay=0.001)
282281
self._read_reply(self._buffer, 3)
283282
return (self._buffer[0] << 8) | self._buffer[1]
284283

285284
@altitude.setter
286285
def altitude(self, height):
287286
if height > 65535:
288287
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)
290289

291290
def _check_buffer_crc(self, buf):
292291
for i in range(0, len(buf), 3):
@@ -296,7 +295,7 @@ def _check_buffer_crc(self, buf):
296295
raise RuntimeError("CRC check failed while reading data")
297296
return True
298297

299-
def _send_command(self, cmd, cmd_delay=0):
298+
def _send_command(self, cmd: int, cmd_delay: float = 0) -> None:
300299
self._cmd[0] = (cmd >> 8) & 0xFF
301300
self._cmd[1] = cmd & 0xFF
302301

0 commit comments

Comments
 (0)