Files
RTLPlayground/tools/led_gpio_utils/i2c_read_rtl_gpio.py
T
HL Yi 70be673fca updates based upon vDorst's feedback.
change i2c_read_rtl_gpio sleep tiemr to 1s
update README.md
Sorted port speed
2026-06-22 00:18:32 -05:00

70 lines
2.2 KiB
Python
Executable File

#!/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
SLEEP_INTERVAL = 1
# Handle command line arguments for ignored IOs
IGNORED_IOS = [ ]
# 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, 34, 44],
help='List of GPIO pins to ignore (default: [])')
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) ]
# Open I2C bus
with SMBus(I2C_BUS) as bus:
addr = 0x44
while True:
# Create write message (send register address)
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
first_read = False
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])
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:
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)