fancier more automagic i2c scan
This commit is contained in:
parent
8cb888956b
commit
9c592cbeec
1 changed files with 36 additions and 23 deletions
|
|
@ -3,33 +3,46 @@
|
|||
# SPDX-License-Identifier: MIT
|
||||
|
||||
"""CircuitPython I2C Device Address Scan"""
|
||||
# If you run this and it seems to hang, try manually unlocking
|
||||
# your I2C bus from the REPL with
|
||||
# >>> import board
|
||||
# >>> board.I2C().unlock()
|
||||
|
||||
import time
|
||||
import board
|
||||
import busio
|
||||
|
||||
# To use default I2C bus (most boards)
|
||||
i2c = board.I2C() # uses board.SCL and board.SDA
|
||||
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
|
||||
# List of potential I2C busses
|
||||
ALL_I2C = ("board.I2C()", "board.STEMMA_I2C()", "busio.I2C(board.GP1, board.GP0)")
|
||||
|
||||
# To create I2C bus on specific pins
|
||||
# import busio
|
||||
# i2c = busio.I2C(board.SCL1, board.SDA1) # QT Py RP2040 STEMMA connector
|
||||
# i2c = busio.I2C(board.GP1, board.GP0) # Pi Pico RP2040
|
||||
# Determine which busses are valid
|
||||
found_i2c = []
|
||||
for name in ALL_I2C:
|
||||
try:
|
||||
print("Checking {}...".format(name), end="")
|
||||
bus = eval(name)
|
||||
bus.unlock()
|
||||
found_i2c.append((name, bus))
|
||||
print("ADDED.")
|
||||
except:
|
||||
print("SKIPPED.")
|
||||
|
||||
while not i2c.try_lock():
|
||||
# Scan valid busses
|
||||
if len(found_i2c):
|
||||
print("-" * 40)
|
||||
print("I2C SCAN")
|
||||
print("-" * 40)
|
||||
while True:
|
||||
for bus_info in found_i2c:
|
||||
name = bus_info[0]
|
||||
bus = bus_info[1]
|
||||
|
||||
while not bus.try_lock():
|
||||
pass
|
||||
|
||||
try:
|
||||
while True:
|
||||
print(
|
||||
"I2C addresses found:",
|
||||
[hex(device_address) for device_address in i2c.scan()],
|
||||
name,
|
||||
"addresses found:",
|
||||
[hex(device_address) for device_address in bus.scan()],
|
||||
)
|
||||
time.sleep(2)
|
||||
|
||||
finally: # unlock the i2c bus when ctrl-c'ing out of the loop
|
||||
i2c.unlock()
|
||||
bus.unlock()
|
||||
|
||||
time.sleep(2)
|
||||
else:
|
||||
print("No valid I2C bus found.")
|
||||
|
|
|
|||
Loading…
Reference in a new issue