Skip to content

Commit

Permalink
fixing resolutions
Browse files Browse the repository at this point in the history
  • Loading branch information
jposada202020 committed Jun 28, 2023
1 parent 78fde91 commit 0b99d4c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
12 changes: 6 additions & 6 deletions adafruit_htu31d.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def humidity_resolution(self) -> Literal["0.020%", "0.014%", "0.010%", "0.007%"]
"""

return _HTU31D_HUMIDITY_RES[self._conversion_command >> 4 & 3]
return _HTU31D_HUMIDITY_RES[self._conversion_command >> 3 & 3]

@humidity_resolution.setter
def humidity_resolution(
Expand All @@ -212,9 +212,9 @@ def humidity_resolution(
raise ValueError(
f"Humidity resolution must be one of: {_HTU31D_HUMIDITY_RES}"
)
register = self._conversion_command & 0xCF
register = self._conversion_command & 0xE7
hum_res = _HTU31D_HUMIDITY_RES.index(value)
self._conversion_command = register | hum_res << 4
self._conversion_command = register | hum_res << 3

@property
def temp_resolution(self) -> Literal["0.040", "0.025", "0.016", "0.012"]:
Expand All @@ -229,7 +229,7 @@ def temp_resolution(self) -> Literal["0.040", "0.025", "0.016", "0.012"]:
"""

return _HTU31D_TEMP_RES[self._conversion_command >> 2 & 3]
return _HTU31D_TEMP_RES[self._conversion_command >> 1 & 3]

@temp_resolution.setter
def temp_resolution(
Expand All @@ -239,9 +239,9 @@ def temp_resolution(
raise ValueError(
f"Temperature resolution must be one of: {_HTU31D_TEMP_RES}"
)
register = self._conversion_command & 0xF3
register = self._conversion_command & 0xF9
temp_res = _HTU31D_TEMP_RES.index(value)
self._conversion_command = register | temp_res << 2
self._conversion_command = register | temp_res << 1

@staticmethod
def _crc(value) -> int:
Expand Down
22 changes: 20 additions & 2 deletions examples/htu31d_setting_resolutions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#
# SPDX-License-Identifier: MIT

import time
import board
import adafruit_htu31d

Expand All @@ -24,7 +25,24 @@
# Possible values are "0.020%", "0.014%", "0.010%" and "0.007%"
htu.humidity_resolution = "0.007%"


# Printing the New Values
print("Temperature Resolution: ", htu.temp_resolution)
print("Humidity Resolution: ", htu.humidity_resolution)

hum_res = ["0.020%", "0.014%", "0.010%", "0.007%"]
temp_res = ["0.040", "0.025", "0.016", "0.012"]

while True:
for humidity_resolution in hum_res:
print(f"Current Humidity Resolution: {humidity_resolution}")
for _ in range(3):
print(f"Humidity: {htu.relative_humidity:.2f}")
print(f"Temperature: {htu.temperature:.2f}")
print("")
time.sleep(0.5)
for temperature_resolution in temp_res:
print(f"Current Temperature Resolution: {temperature_resolution}")
for _ in range(3):
print(f"Humidity: {htu.relative_humidity:.2f}")
print(f"Temperature: {htu.temperature:.2f}")
print("")
time.sleep(0.5)

0 comments on commit 0b99d4c

Please sign in to comment.