Skip to content

Commit 8d67b78

Browse files
committed
Change calculation 2**16 - 1 to exact value and add equations to docstring
1 parent 51c1fd3 commit 8d67b78

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

adafruit_scd4x.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,18 @@ def self_test(self) -> None:
211211
raise RuntimeError("Self test failed")
212212

213213
def _read_data(self) -> None:
214-
"""Reads the temp/hum/co2 from the sensor and caches it"""
214+
"""Reads the temp/hum/co2 from the sensor and caches it.
215+
Equations used for calculation:
216+
CO2 = word[0]
217+
T = -45 + 175 * (word[1] / (2**16 - 1))
218+
RH = 100 * (word[2] / (2**16 - 1))"""
215219
self._send_command(_SCD4X_READMEASUREMENT, cmd_delay=0.001)
216220
self._read_reply(self._buffer, 9)
217221
self._co2 = (self._buffer[0] << 8) | self._buffer[1]
218222
temp = (self._buffer[3] << 8) | self._buffer[4]
219-
self._temperature = -45 + 175 * (temp / (2**16 - 1))
223+
self._temperature = -45 + 175 * (temp / 65535)
220224
humi = (self._buffer[6] << 8) | self._buffer[7]
221-
self._relative_humidity = 100 * (humi / (2**16 - 1))
225+
self._relative_humidity = 100 * (humi / 65535)
222226

223227
@property
224228
def data_ready(self) -> bool:
@@ -285,7 +289,9 @@ def set_ambient_pressure(self, ambient_pressure: int) -> None:
285289
def temperature_offset(self) -> float:
286290
"""Specifies the offset to be added to the reported measurements to account for a bias in
287291
the measured signal. Value is in degrees Celsius with a resolution of 0.01 degrees and a
288-
maximum value of 374 C
292+
maximum value of 374 C.
293+
Equation used for calculation:
294+
T_offset = word[0] * (175 / (2**16 - 1))
289295
290296
.. note::
291297
This value will NOT be saved and will be reset on boot unless saved with
@@ -295,15 +301,17 @@ def temperature_offset(self) -> float:
295301
self._send_command(_SCD4X_GETTEMPOFFSET, cmd_delay=0.001)
296302
self._read_reply(self._buffer, 3)
297303
temp = (self._buffer[0] << 8) | self._buffer[1]
298-
return 175.0 * temp / 2**16
304+
return temp * 175.0 / 65535
299305

300306
@temperature_offset.setter
301307
def temperature_offset(self, offset: Union[int, float]) -> None:
308+
"""Equation used for calculation:
309+
word[0] = T_offset * ((2**16 -1) / 175)"""
302310
if offset > 374:
303311
raise AttributeError(
304312
"Offset value must be less than or equal to 374 degrees Celsius"
305313
)
306-
temp = int(offset * 2**16 / 175)
314+
temp = int(offset * 65535 / 175)
307315
self._set_command_value(_SCD4X_SETTEMPOFFSET, temp)
308316

309317
@property

0 commit comments

Comments
 (0)