From 51c1fd32774bb432595c717ceda7f00850f150c2 Mon Sep 17 00:00:00 2001 From: kolcz Date: Wed, 3 Apr 2024 14:40:48 +0200 Subject: [PATCH 1/8] Subtract 1 from demominator --- adafruit_scd4x.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_scd4x.py b/adafruit_scd4x.py index 6d33c87..846a6f1 100644 --- a/adafruit_scd4x.py +++ b/adafruit_scd4x.py @@ -216,9 +216,9 @@ def _read_data(self) -> None: self._read_reply(self._buffer, 9) self._co2 = (self._buffer[0] << 8) | self._buffer[1] temp = (self._buffer[3] << 8) | self._buffer[4] - self._temperature = -45 + 175 * (temp / 2**16) + self._temperature = -45 + 175 * (temp / (2**16 - 1)) humi = (self._buffer[6] << 8) | self._buffer[7] - self._relative_humidity = 100 * (humi / 2**16) + self._relative_humidity = 100 * (humi / (2**16 - 1)) @property def data_ready(self) -> bool: From 8d67b789b615fdda14f383dff567b6265a574a09 Mon Sep 17 00:00:00 2001 From: kolcz <17905157+kolcz@users.noreply.github.com> Date: Thu, 15 Aug 2024 20:49:31 +0200 Subject: [PATCH 2/8] Change calculation 2**16 - 1 to exact value and add equations to docstring --- adafruit_scd4x.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/adafruit_scd4x.py b/adafruit_scd4x.py index 846a6f1..4f988bd 100644 --- a/adafruit_scd4x.py +++ b/adafruit_scd4x.py @@ -211,14 +211,18 @@ def self_test(self) -> None: raise RuntimeError("Self test failed") def _read_data(self) -> None: - """Reads the temp/hum/co2 from the sensor and caches it""" + """Reads the temp/hum/co2 from the sensor and caches it. + Equations used for calculation: + CO2 = word[0] + T = -45 + 175 * (word[1] / (2**16 - 1)) + RH = 100 * (word[2] / (2**16 - 1))""" self._send_command(_SCD4X_READMEASUREMENT, cmd_delay=0.001) self._read_reply(self._buffer, 9) self._co2 = (self._buffer[0] << 8) | self._buffer[1] temp = (self._buffer[3] << 8) | self._buffer[4] - self._temperature = -45 + 175 * (temp / (2**16 - 1)) + self._temperature = -45 + 175 * (temp / 65535) humi = (self._buffer[6] << 8) | self._buffer[7] - self._relative_humidity = 100 * (humi / (2**16 - 1)) + self._relative_humidity = 100 * (humi / 65535) @property def data_ready(self) -> bool: @@ -285,7 +289,9 @@ def set_ambient_pressure(self, ambient_pressure: int) -> None: def temperature_offset(self) -> float: """Specifies the offset to be added to the reported measurements to account for a bias in the measured signal. Value is in degrees Celsius with a resolution of 0.01 degrees and a - maximum value of 374 C + maximum value of 374 C. + Equation used for calculation: + T_offset = word[0] * (175 / (2**16 - 1)) .. note:: This value will NOT be saved and will be reset on boot unless saved with @@ -295,15 +301,17 @@ def temperature_offset(self) -> float: self._send_command(_SCD4X_GETTEMPOFFSET, cmd_delay=0.001) self._read_reply(self._buffer, 3) temp = (self._buffer[0] << 8) | self._buffer[1] - return 175.0 * temp / 2**16 + return temp * 175.0 / 65535 @temperature_offset.setter def temperature_offset(self, offset: Union[int, float]) -> None: + """Equation used for calculation: + word[0] = T_offset * ((2**16 -1) / 175)""" if offset > 374: raise AttributeError( "Offset value must be less than or equal to 374 degrees Celsius" ) - temp = int(offset * 2**16 / 175) + temp = int(offset * 65535 / 175) self._set_command_value(_SCD4X_SETTEMPOFFSET, temp) @property From 7159f2cffa23887f3718c5c7b21a03b2bf3ff8f5 Mon Sep 17 00:00:00 2001 From: kolcz <17905157+kolcz@users.noreply.github.com> Date: Thu, 15 Aug 2024 21:08:20 +0200 Subject: [PATCH 3/8] Remove docstring in setter --- adafruit_scd4x.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/adafruit_scd4x.py b/adafruit_scd4x.py index 4f988bd..36f7a7a 100644 --- a/adafruit_scd4x.py +++ b/adafruit_scd4x.py @@ -305,8 +305,6 @@ def temperature_offset(self) -> float: @temperature_offset.setter def temperature_offset(self, offset: Union[int, float]) -> None: - """Equation used for calculation: - word[0] = T_offset * ((2**16 -1) / 175)""" if offset > 374: raise AttributeError( "Offset value must be less than or equal to 374 degrees Celsius" From 64e117681f8a38245d6c1482a1e2d5d73d93b816 Mon Sep 17 00:00:00 2001 From: kolcz <17905157+kolcz@users.noreply.github.com> Date: Thu, 15 Aug 2024 21:14:23 +0200 Subject: [PATCH 4/8] Remove docstring equations --- adafruit_scd4x.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/adafruit_scd4x.py b/adafruit_scd4x.py index 36f7a7a..5238a8b 100644 --- a/adafruit_scd4x.py +++ b/adafruit_scd4x.py @@ -211,11 +211,7 @@ def self_test(self) -> None: raise RuntimeError("Self test failed") def _read_data(self) -> None: - """Reads the temp/hum/co2 from the sensor and caches it. - Equations used for calculation: - CO2 = word[0] - T = -45 + 175 * (word[1] / (2**16 - 1)) - RH = 100 * (word[2] / (2**16 - 1))""" + """Reads the temp/hum/co2 from the sensor and caches it""" self._send_command(_SCD4X_READMEASUREMENT, cmd_delay=0.001) self._read_reply(self._buffer, 9) self._co2 = (self._buffer[0] << 8) | self._buffer[1] @@ -290,8 +286,6 @@ def temperature_offset(self) -> float: """Specifies the offset to be added to the reported measurements to account for a bias in the measured signal. Value is in degrees Celsius with a resolution of 0.01 degrees and a maximum value of 374 C. - Equation used for calculation: - T_offset = word[0] * (175 / (2**16 - 1)) .. note:: This value will NOT be saved and will be reset on boot unless saved with From 75b6f1597abcff059153142286cc315c9ec2be64 Mon Sep 17 00:00:00 2001 From: kolcz <17905157+kolcz@users.noreply.github.com> Date: Thu, 15 Aug 2024 21:24:50 +0200 Subject: [PATCH 5/8] Insert equation in line comments --- adafruit_scd4x.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/adafruit_scd4x.py b/adafruit_scd4x.py index 5238a8b..3ba182a 100644 --- a/adafruit_scd4x.py +++ b/adafruit_scd4x.py @@ -214,11 +214,11 @@ def _read_data(self) -> None: """Reads the temp/hum/co2 from the sensor and caches it""" self._send_command(_SCD4X_READMEASUREMENT, cmd_delay=0.001) self._read_reply(self._buffer, 9) - self._co2 = (self._buffer[0] << 8) | self._buffer[1] + self._co2 = (self._buffer[0] << 8) | self._buffer[1] # CO2 = word[0] temp = (self._buffer[3] << 8) | self._buffer[4] - self._temperature = -45 + 175 * (temp / 65535) + self._temperature = -45 + 175 * (temp / 65535) # T = -45 + 175 * (word[1] / 2**16 - 1) humi = (self._buffer[6] << 8) | self._buffer[7] - self._relative_humidity = 100 * (humi / 65535) + self._relative_humidity = 100 * (humi / 65535) # RH = 100 * (word[2] / (2**16 - 1)) @property def data_ready(self) -> bool: @@ -295,7 +295,7 @@ def temperature_offset(self) -> float: self._send_command(_SCD4X_GETTEMPOFFSET, cmd_delay=0.001) self._read_reply(self._buffer, 3) temp = (self._buffer[0] << 8) | self._buffer[1] - return temp * 175.0 / 65535 + return temp * 175.0 / 65535 # T_offset = word[0] * (175 / (2**16 - 1)) @temperature_offset.setter def temperature_offset(self, offset: Union[int, float]) -> None: @@ -303,7 +303,7 @@ def temperature_offset(self, offset: Union[int, float]) -> None: raise AttributeError( "Offset value must be less than or equal to 374 degrees Celsius" ) - temp = int(offset * 65535 / 175) + temp = int(offset * 65535 / 175) # word[0] = T_offset * ((2**16 - 1) / 175) self._set_command_value(_SCD4X_SETTEMPOFFSET, temp) @property From a2f56c025f49d515512c219cd9f7920c97a1ed0d Mon Sep 17 00:00:00 2001 From: kolcz <17905157+kolcz@users.noreply.github.com> Date: Thu, 15 Aug 2024 21:46:36 +0200 Subject: [PATCH 6/8] Unify intedation --- adafruit_scd4x.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/adafruit_scd4x.py b/adafruit_scd4x.py index 3ba182a..ff50a3c 100644 --- a/adafruit_scd4x.py +++ b/adafruit_scd4x.py @@ -214,11 +214,11 @@ def _read_data(self) -> None: """Reads the temp/hum/co2 from the sensor and caches it""" self._send_command(_SCD4X_READMEASUREMENT, cmd_delay=0.001) self._read_reply(self._buffer, 9) - self._co2 = (self._buffer[0] << 8) | self._buffer[1] # CO2 = word[0] + self._co2 = (self._buffer[0] << 8) | self._buffer[1] # CO2 = word[0] temp = (self._buffer[3] << 8) | self._buffer[4] - self._temperature = -45 + 175 * (temp / 65535) # T = -45 + 175 * (word[1] / 2**16 - 1) + self._temperature = -45 + 175 * (temp / 65535) # T = -45 + 175 * (word[1] / 2**16 - 1) humi = (self._buffer[6] << 8) | self._buffer[7] - self._relative_humidity = 100 * (humi / 65535) # RH = 100 * (word[2] / (2**16 - 1)) + self._relative_humidity = 100 * (humi / 65535) # RH = 100 * (word[2] / (2**16 - 1)) @property def data_ready(self) -> bool: @@ -295,7 +295,7 @@ def temperature_offset(self) -> float: self._send_command(_SCD4X_GETTEMPOFFSET, cmd_delay=0.001) self._read_reply(self._buffer, 3) temp = (self._buffer[0] << 8) | self._buffer[1] - return temp * 175.0 / 65535 # T_offset = word[0] * (175 / (2**16 - 1)) + return temp * 175.0 / 65535 # T_offset = word[0] * (175 / (2**16 - 1)) @temperature_offset.setter def temperature_offset(self, offset: Union[int, float]) -> None: @@ -303,7 +303,7 @@ def temperature_offset(self, offset: Union[int, float]) -> None: raise AttributeError( "Offset value must be less than or equal to 374 degrees Celsius" ) - temp = int(offset * 65535 / 175) # word[0] = T_offset * ((2**16 - 1) / 175) + temp = int(offset * 65535 / 175) # word[0] = T_offset * ((2**16 - 1) / 175) self._set_command_value(_SCD4X_SETTEMPOFFSET, temp) @property From 556109d42569c938dc603f331445cb30ec0ca529 Mon Sep 17 00:00:00 2001 From: kolcz <17905157+kolcz@users.noreply.github.com> Date: Thu, 15 Aug 2024 22:01:49 +0200 Subject: [PATCH 7/8] Reformat by black --- adafruit_scd4x.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/adafruit_scd4x.py b/adafruit_scd4x.py index ff50a3c..8bfabd1 100644 --- a/adafruit_scd4x.py +++ b/adafruit_scd4x.py @@ -214,11 +214,15 @@ def _read_data(self) -> None: """Reads the temp/hum/co2 from the sensor and caches it""" self._send_command(_SCD4X_READMEASUREMENT, cmd_delay=0.001) self._read_reply(self._buffer, 9) - self._co2 = (self._buffer[0] << 8) | self._buffer[1] # CO2 = word[0] + self._co2 = (self._buffer[0] << 8) | self._buffer[1] # CO2 = word[0] temp = (self._buffer[3] << 8) | self._buffer[4] - self._temperature = -45 + 175 * (temp / 65535) # T = -45 + 175 * (word[1] / 2**16 - 1) + self._temperature = -45 + 175 * ( + temp / 65535 + ) # T = -45 + 175 * (word[1] / 2**16 - 1) humi = (self._buffer[6] << 8) | self._buffer[7] - self._relative_humidity = 100 * (humi / 65535) # RH = 100 * (word[2] / (2**16 - 1)) + self._relative_humidity = 100 * ( + humi / 65535 + ) # RH = 100 * (word[2] / (2**16 - 1)) @property def data_ready(self) -> bool: @@ -295,7 +299,7 @@ def temperature_offset(self) -> float: self._send_command(_SCD4X_GETTEMPOFFSET, cmd_delay=0.001) self._read_reply(self._buffer, 3) temp = (self._buffer[0] << 8) | self._buffer[1] - return temp * 175.0 / 65535 # T_offset = word[0] * (175 / (2**16 - 1)) + return temp * 175.0 / 65535 # T_offset = word[0] * (175 / (2**16 - 1)) @temperature_offset.setter def temperature_offset(self, offset: Union[int, float]) -> None: @@ -303,7 +307,7 @@ def temperature_offset(self, offset: Union[int, float]) -> None: raise AttributeError( "Offset value must be less than or equal to 374 degrees Celsius" ) - temp = int(offset * 65535 / 175) # word[0] = T_offset * ((2**16 - 1) / 175) + temp = int(offset * 65535 / 175) # word[0] = T_offset * ((2**16 - 1) / 175) self._set_command_value(_SCD4X_SETTEMPOFFSET, temp) @property From b7c46bc52a86278fac6238d4a7834e3032f5a46c Mon Sep 17 00:00:00 2001 From: kolcz <17905157+kolcz@users.noreply.github.com> Date: Thu, 15 Aug 2024 22:12:36 +0200 Subject: [PATCH 8/8] Move equation comment to line above --- adafruit_scd4x.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/adafruit_scd4x.py b/adafruit_scd4x.py index 8bfabd1..aa62a88 100644 --- a/adafruit_scd4x.py +++ b/adafruit_scd4x.py @@ -214,15 +214,14 @@ def _read_data(self) -> None: """Reads the temp/hum/co2 from the sensor and caches it""" self._send_command(_SCD4X_READMEASUREMENT, cmd_delay=0.001) self._read_reply(self._buffer, 9) - self._co2 = (self._buffer[0] << 8) | self._buffer[1] # CO2 = word[0] + # CO2 = word[0] + self._co2 = (self._buffer[0] << 8) | self._buffer[1] temp = (self._buffer[3] << 8) | self._buffer[4] - self._temperature = -45 + 175 * ( - temp / 65535 - ) # T = -45 + 175 * (word[1] / 2**16 - 1) + # T = -45 + 175 * (word[1] / 2**16 - 1) + self._temperature = -45 + 175 * (temp / 65535) humi = (self._buffer[6] << 8) | self._buffer[7] - self._relative_humidity = 100 * ( - humi / 65535 - ) # RH = 100 * (word[2] / (2**16 - 1)) + # RH = 100 * (word[2] / (2**16 - 1)) + self._relative_humidity = 100 * (humi / 65535) @property def data_ready(self) -> bool: