Skip to content

Commit 9e81e41

Browse files
committed
Add some comment for HTU21D library
1 parent c5e861a commit 9e81e41

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

Adafruit_HTU21D/Adafruit_HTU21D.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,22 @@ def __init__(self):
2323
self.i2c = Adafruit_I2C(self.address)
2424

2525
def readUserRegister(self):
26-
# Read a byte
27-
value = self.i2c.readU8(self.READ_USER_REG)
28-
29-
return value
26+
"Read the user register byte"
27+
return self.i2c.readU8(self.READ_USER_REG)
3028

3129
def readTemperatureData(self):
30+
"Read 3 temperature bytes from the sensor"
3231
# value[0], value[1]: Raw temperature data
3332
# value[2]: CRC
3433
value = self.i2c.readList(self.TRIGGER_TEMP_MEASURE_HOLD, 3)
3534

35+
# CRC Check
3636
if not self.crc8check(value):
3737
return -255
3838

3939
rawTempData = ( value[0] << 8 ) + value[1]
4040

41-
# Zero out the status bits but keep them in place
41+
# Clear the status bits
4242
rawTempData = rawTempData & 0xFFFC;
4343

4444
# Calculate the actual temperature
@@ -47,16 +47,18 @@ def readTemperatureData(self):
4747
return actualTemp
4848

4949
def readHumidityData(self):
50+
"Read 3 humidity bytes from the sensor"
5051
# value[0], value[1]: Raw relative humidity data
5152
# value[2]: CRC
5253
value = self.i2c.readList(self.TRIGGER_HUMD_MEASURE_HOLD, 3)
5354

55+
# CRC Check
5456
if not self.crc8check(value):
5557
return -255
5658

5759
rawRHData = ( value[0] << 8 ) + value[1]
5860

59-
# Zero out the status bits but keep them in place
61+
# Clear the status bits
6062
rawRHData = rawRHData & 0xFFFC;
6163

6264
# Calculate the actual RH
@@ -65,6 +67,8 @@ def readHumidityData(self):
6567
return actualRH
6668

6769
def crc8check(self, value):
70+
"Calulate the CRC8 for the data received"
71+
# Ported from Sparkfun Arduino HTU21D Library: https://github.com/sparkfun/HTU21D_Breakout
6872
remainder = ( ( value[0] << 8 ) + value[1] ) << 8
6973
remainder |= value[2]
7074

Adafruit_HTU21D/Adafruit_HTU21D_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
temp = htu.readTemperatureData()
1515
rh = htu.readHumidityData()
1616

17-
if temp > 0 and rh > 0:
17+
if temp > -40 and rh > 0:
1818
print "Temperature: %.2f C, Humidity: %.2f %%" % (temp, rh)
1919
elif temp == -255:
2020
print "Temperature data CRC failed"

0 commit comments

Comments
 (0)