Skip to content

Commit 06243aa

Browse files
committed
make I2C init more standard, move try/except into the wakeup location
1 parent 89927db commit 06243aa

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

adafruit_am2320.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ def __init__(self, i2c_bus, address=AM2320_DEFAULT_ADDR):
9090
def _read_register(self, register, length):
9191
with self._i2c as i2c:
9292
# wake up sensor
93-
i2c.write(bytes([0x00]))
93+
try:
94+
i2c.write(bytes([0x00]))
95+
except OSError:
96+
pass
9497
time.sleep(0.01) # wait 10 ms
9598

9699
# Send command to read register
@@ -103,7 +106,7 @@ def _read_register(self, register, length):
103106
# print("$%02X => %s" % (register, [hex(i) for i in result]))
104107
# Check preamble indicates correct readings
105108
if result[0] != 0x3 or result[1] != length:
106-
raise RuntimeError('I2C modbus read failure')
109+
raise RuntimeError('I2C read failure')
107110
# Check CRC on all but last 2 bytes
108111
crc1 = struct.unpack("<H", bytes(result[-2:]))[0]
109112
crc2 = _crc16(result[0:-2])

examples/am2320_simpletest.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,11 @@
33
import busio
44
import adafruit_am2320
55

6-
# can also use board.SDA and board.SCL for neater looking code!
7-
i2c = busio.I2C(board.D2, board.D0)
6+
# create the I2C shared bus
7+
i2c = busio.I2C(board.SCL, board.SDA)
88
am = adafruit_am2320.AM2320(i2c)
99

10-
1110
while True:
12-
try:
13-
print("Temperature: ", am.temperature)
14-
print("Humidity: ", am.relative_humidity)
15-
except OSError:
16-
# These sensors are a bit flakey, its ok if the readings fail
17-
pass
18-
except RuntimeError:
19-
pass
11+
print("Temperature: ", am.temperature)
12+
print("Humidity: ", am.relative_humidity)
2013
time.sleep(2)

0 commit comments

Comments
 (0)