Skip to content

Commit 787fba1

Browse files
authored
Merge pull request #2 from ladyada/main
blackened
2 parents 160bd1d + 5e9a8f2 commit 787fba1

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

README.rst

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,28 @@ To install in a virtual environment in your current project:
5959
Usage Example
6060
=============
6161

62-
.. todo:: Add a quick, simple example. It and other examples should live in the examples folder and be included in docs/examples.rst.
62+
.. code-block:: python
63+
64+
import time
65+
import busio
66+
import board
67+
import adafruit_htu31d
68+
69+
i2c = busio.I2C(board.SCL, board.SDA)
70+
htu = adafruit_htu31d.HTU31D(i2c)
71+
print("Found HTU31D with serial number", hex(htu.serial_number))
72+
73+
htu.heater = True
74+
print("Heater is on?", htu.heater)
75+
htu.heater = False
76+
print("Heater is on?", htu.heater)
77+
78+
while True:
79+
temperature, relative_humidity = htu.measurements
80+
print("Temperature: %0.1f C" % temperature)
81+
print("Humidity: %0.1f %%" % relative_humidity)
82+
print("")
83+
time.sleep(1)
6384
6485
Contributing
6586
============

adafruit_htu31d.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@
3939

4040
_HTU31D_READSERIAL = const(0x0A) # Read Out of Serial Register
4141
_HTU31D_SOFTRESET = const(0x1E) # Soft Reset
42-
_HTU31D_HEATERON = const(0x04) # Enable heater
43-
_HTU31D_HEATEROFF = const(0x02) # Disable heater
44-
_HTU31D_CONVERSION = const(0x40) # Start a conversion
45-
_HTU31D_READTEMPHUM = const(0x00) # Read the conversion values
42+
_HTU31D_HEATERON = const(0x04) # Enable heater
43+
_HTU31D_HEATEROFF = const(0x02) # Disable heater
44+
_HTU31D_CONVERSION = const(0x40) # Start a conversion
45+
_HTU31D_READTEMPHUM = const(0x00) # Read the conversion values
46+
4647

4748
class HTU31D:
4849
"""
@@ -122,12 +123,12 @@ def measurements(self):
122123
i2c.write_then_readinto(self._buffer, self._buffer, out_end=1)
123124

124125
# separate the read data
125-
temperature, temp_crc, humidity, humidity_crc = struct.unpack_from(">HBHB", self._buffer)
126+
temperature, temp_crc, humidity, humidity_crc = struct.unpack_from(
127+
">HBHB", self._buffer
128+
)
126129

127130
# check CRC of bytes
128-
if temp_crc != self._crc(temperature) or humidity_crc != self._crc(
129-
humidity
130-
):
131+
if temp_crc != self._crc(temperature) or humidity_crc != self._crc(humidity):
131132
raise RuntimeError("Invalid CRC calculated")
132133

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

144145
@staticmethod
145146
def _crc(value):
146-
polynom = 0x988000 # x^8 + x^5 + x^4 + 1
147+
polynom = 0x988000 # x^8 + x^5 + x^4 + 1
147148
msb = 0x800000
148149
mask = 0xFF8000
149150
result = value << 8 # Pad with zeros as specified in spec

docs/conf.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@
2929

3030

3131
intersphinx_mapping = {
32-
"python": ("https://docs.python.org/3.4", None),"BusDevice": ("https://circuitpython.readthedocs.io/projects/busdevice/en/latest/", None),
33-
32+
"python": ("https://docs.python.org/3.4", None),
33+
"BusDevice": (
34+
"https://circuitpython.readthedocs.io/projects/busdevice/en/latest/",
35+
None,
36+
),
3437
"CircuitPython": ("https://circuitpython.readthedocs.io/en/latest/", None),
3538
}
3639

0 commit comments

Comments
 (0)