Skip to content

Commit

Permalink
RPi Pins: Check available devices.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed Sep 27, 2023
1 parent 00e16ca commit bd49b5e
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions rpipins/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/bin/env python3
import os
import stat
import re
import subprocess
import sys
Expand Down Expand Up @@ -73,12 +75,22 @@
RIGHT_PINS[n] += [""] * NUM_DEBUG_COLS


def get_current_pin_states():
if hasattr(gpiod, "chip"):
chip = gpiod.chip("/dev/gpiochip4")
def get_gpio_char_device():
for num in (4, 0):
try:
if stat.S_ISCHR(os.stat(f"/dev/gpiochip{num}").st_mode):
return f"/dev/gpiochip{num}"
except FileNotFoundError:
continue
return None


def get_current_pin_states(device):
if hasattr(gpiod, "chip") and device is not None:
chip = gpiod.chip(device)
gpio_user = [line.consumer for line in gpiod.line_iter(chip) if line.offset < NUM_PINS]
elif hasattr(gpiod, "Chip"):
chip = gpiod.Chip("/dev/gpiochip4")
elif hasattr(gpiod, "Chip") and device is not None:
chip = gpiod.Chip(device)
gpio_user = [line.consumer() for line in gpiod.LineIter(chip) if line.offset() < NUM_PINS]
else:
gpio_user = [""] * NUM_PINS
Expand Down Expand Up @@ -110,8 +122,8 @@ def gpio_add_line_state(gpio_states, row):
return changed


def gpio_update_line_states():
gpio_states = get_current_pin_states()
def gpio_update_line_states(device):
gpio_states = get_current_pin_states(device)
changed = False

for row in LEFT_PINS:
Expand Down Expand Up @@ -328,13 +340,15 @@ def main():
#rich.print(rpipins(Options(sys.argv)))
options = Options(sys.argv)

gpio_update_line_states()
device = get_gpio_char_device()

gpio_update_line_states(device)

if options.live:
with Live(rpipins(options), auto_refresh=True) as live:
try:
while True:
if gpio_update_line_states():
if gpio_update_line_states(device):
live.update(rpipins(options), refresh=True)
time.sleep(1.0 / options.fps)
except KeyboardInterrupt:
Expand Down

0 comments on commit bd49b5e

Please sign in to comment.