Skip to content

blackened #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,28 @@ To install in a virtual environment in your current project:
Usage Example
=============

.. todo:: Add a quick, simple example. It and other examples should live in the examples folder and be included in docs/examples.rst.
.. code-block:: python

import time
import busio
import board
import adafruit_htu31d

i2c = busio.I2C(board.SCL, board.SDA)
htu = adafruit_htu31d.HTU31D(i2c)
print("Found HTU31D with serial number", hex(htu.serial_number))

htu.heater = True
print("Heater is on?", htu.heater)
htu.heater = False
print("Heater is on?", htu.heater)

while True:
temperature, relative_humidity = htu.measurements
print("Temperature: %0.1f C" % temperature)
print("Humidity: %0.1f %%" % relative_humidity)
print("")
time.sleep(1)

Contributing
============
Expand Down
19 changes: 10 additions & 9 deletions adafruit_htu31d.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@

_HTU31D_READSERIAL = const(0x0A) # Read Out of Serial Register
_HTU31D_SOFTRESET = const(0x1E) # Soft Reset
_HTU31D_HEATERON = const(0x04) # Enable heater
_HTU31D_HEATEROFF = const(0x02) # Disable heater
_HTU31D_CONVERSION = const(0x40) # Start a conversion
_HTU31D_READTEMPHUM = const(0x00) # Read the conversion values
_HTU31D_HEATERON = const(0x04) # Enable heater
_HTU31D_HEATEROFF = const(0x02) # Disable heater
_HTU31D_CONVERSION = const(0x40) # Start a conversion
_HTU31D_READTEMPHUM = const(0x00) # Read the conversion values


class HTU31D:
"""
Expand Down Expand Up @@ -122,12 +123,12 @@ def measurements(self):
i2c.write_then_readinto(self._buffer, self._buffer, out_end=1)

# separate the read data
temperature, temp_crc, humidity, humidity_crc = struct.unpack_from(">HBHB", self._buffer)
temperature, temp_crc, humidity, humidity_crc = struct.unpack_from(
">HBHB", self._buffer
)

# check CRC of bytes
if temp_crc != self._crc(temperature) or humidity_crc != self._crc(
humidity
):
if temp_crc != self._crc(temperature) or humidity_crc != self._crc(humidity):
raise RuntimeError("Invalid CRC calculated")

# decode data into human values:
Expand All @@ -143,7 +144,7 @@ def measurements(self):

@staticmethod
def _crc(value):
polynom = 0x988000 # x^8 + x^5 + x^4 + 1
polynom = 0x988000 # x^8 + x^5 + x^4 + 1
msb = 0x800000
mask = 0xFF8000
result = value << 8 # Pad with zeros as specified in spec
Expand Down
7 changes: 5 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@


intersphinx_mapping = {
"python": ("https://docs.python.org/3.4", None),"BusDevice": ("https://circuitpython.readthedocs.io/projects/busdevice/en/latest/", None),

"python": ("https://docs.python.org/3.4", None),
"BusDevice": (
"https://circuitpython.readthedocs.io/projects/busdevice/en/latest/",
None,
),
"CircuitPython": ("https://circuitpython.readthedocs.io/en/latest/", None),
}

Expand Down