sleep-interval default value is reduced to 1.

i2c-bus is required argument
replace --bus with --i2c-bus for i2c_dump_rtl_regs.py for better consistency
format lint clean up
This commit is contained in:
HL Yi
2026-06-22 18:02:34 -05:00
parent 4b770a8abc
commit a56e2a419b
3 changed files with 146 additions and 110 deletions
+35 -22
View File
@@ -1,14 +1,13 @@
#!/usr/bin/env python3
import time
import sys
import argparse
from smbus2 import SMBus, i2c_msg
# Configuration
I2C_BUS = 1
DEVICE_ADDR = 0x5C # Replace with your device address
NUM_BYTES = 8 # Bytes to read
NUM_BYTES = 8 # Bytes to read
SLEEP_INTERVAL = 1
# Handle command line arguments for ignored IOs
@@ -16,20 +15,34 @@ SLEEP_INTERVAL = 1
IGNORED_IOS = [28, 31, 32, 34, 35, 42, 43, 44, 45]
# Setup argument parser
parser = argparse.ArgumentParser(description='Read I2C data from RTL GPIO expander')
parser.add_argument('--i2c-bus', '-b', type=int, default=1,
help='I2C bus number (default: 1)')
parser.add_argument('--sleep-interval', '-s', type=int, default=2,
help='Sleep interval in seconds (default: 2)')
parser.add_argument('--ignored-ios', '-i', nargs='*', type=int, default=[28, 31, 32, 34, 35, 42, 43, 44, 45],
help='List of GPIO pins to ignore (default: [28, 31, 32, 34, 35, 42, 43, 44, 45])')
parser = argparse.ArgumentParser(description="Read I2C data from RTL GPIO expander")
parser.add_argument(
"-b",
"--i2c-bus",
type=int,
required=True,
help="I2C bus number (use i2cdetect -l to find out the bus number of the dongle)",
)
parser.add_argument(
"-s",
"--sleep-interval",
type=int,
default=SLEEP_INTERVAL,
help=f"Sleep interval in seconds (default: {SLEEP_INTERVAL})",
)
parser.add_argument(
"-i",
"--ignored-ios",
nargs="*",
type=int,
default=IGNORED_IOS,
help=f"List of GPIO pins to ignore (default: {IGNORED_IOS})",
)
args = parser.parse_args()
I2C_BUS = args.i2c_bus
SLEEP_INTERVAL = args.sleep_interval
IGNORED_IOS = args.ignored_ios
first_read = True
last_data = [ 0 for x in range(NUM_BYTES) ]
last_data = [0 for x in range(NUM_BYTES)]
# Open I2C bus
with SMBus(I2C_BUS) as bus:
@@ -37,14 +50,14 @@ with SMBus(I2C_BUS) as bus:
while True:
# Create write message (send register address)
write = i2c_msg.write(DEVICE_ADDR, [addr>>8, addr & 0xff])
write = i2c_msg.write(DEVICE_ADDR, [addr >> 8, addr & 0xFF])
# Create read message (read NUM_BYTES bytes)
read = i2c_msg.read(DEVICE_ADDR, NUM_BYTES)
# Perform combined transaction
bus.i2c_rdwr(write, read)
new_data = list(read)
if first_read:
delta_data = last_data
@@ -52,19 +65,19 @@ with SMBus(I2C_BUS) as bus:
else:
delta_data = [new_data[x] ^ last_data[x] for x in range(NUM_BYTES)]
last_data = new_data
# Convert read message to list
datastr = " ".join([ f"{x:02x}" for x in new_data])
datastr = " ".join([f"{x:02x}" for x in new_data])
deltastr = ""
chg_list = []
for x in range(NUM_BYTES):
if delta_data[x] == 0:
continue
for y in range(8):
if delta_data[x] & ( 1<<y):
idx = x*8 + y
if idx not in IGNORED_IOS:
if delta_data[x] & (1 << y):
idx = x * 8 + y
if idx not in args.ignored_ios:
deltastr += f"\n{' ':8s}GPIO{idx}"
# deltastr = " ".join([ f"{x:02x}" for x in delta_data])
print(f"{addr:04x}: {datastr}{deltastr}")
time.sleep(SLEEP_INTERVAL)
time.sleep(args.sleep_interval)