Skip to content

Commit

Permalink
dfu loop without timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
krautech committed Dec 10, 2024
1 parent 7bc2f4c commit 76c53b7
Showing 1 changed file with 44 additions and 25 deletions.
69 changes: 44 additions & 25 deletions scripts/firmware.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import fnmatch
import platform
import time
import sys

from enum import Enum
from time import sleep
Expand Down Expand Up @@ -209,6 +210,26 @@ def show_mode(mode: str):
mode = mode.center(PAGE_WIDTH)
print(Utils.colored_text(mode, Color.RED))

@staticmethod
def is_key_pressed(timeout: int = 1) -> bool:
if os.name == "nt": # Windows
import msvcrt

start_time = time.time()
while time.time() - start_time < timeout:
if msvcrt.kbhit():
_ = msvcrt.getch() # Consume the key press
return True
return False
else: # Unix-based systems
import select

ready, _, _ = select.select([sys.stdin], [], [], timeout)
if ready:
_ = sys.stdin.read(1) # Consume the key press
return True
return False


class Menu:
title: str
Expand Down Expand Up @@ -1598,45 +1619,43 @@ def check_dfu_util(self) -> bool:
print("dfu-util is not installed. Please install it and try again.")
return False

def dfu_loop(self) -> List[str]:
start_time = time.time()
timeout = 30 # Timeout in seconds
def stop_loop(self, detected_devices: List[str]) -> List[str]:
return detected_devices

def dfu_loop(self) -> List[str]:
detected_devices: List[str] = []
print("Press any key to stop...\n")

try:
while time.time() - start_time < timeout:
while True:
# Run the `lsusb` command
result = subprocess.run(
["lsusb"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
)
lines = result.stdout.splitlines()

# Parse all lines containing "DFU Mode"
# Check for DFU Mode in the output
for line in lines:
if "DFU Mode" in line:
# Extract the device ID (the 6th field in `lsusb` output)
device_id: str = line.split()[
5
] # Assuming field 6 contains the ID
detected_devices.append(device_id) # Add to the list
print(f"Detected DFU device: {device_id}")
if detected_devices:
return detected_devices # Exit both loops immediately

print("No DFU devices found. Retrying in 1 second...")
time.sleep(1) # Wait 1 second before retrying

print("No DFU devices found within the timeout period.")
device_id = line.split()[5] # Extract device ID (6th field)
detected_devices.append(device_id)
print(f"DFU device found: {device_id}")
_ = input("Press any key to return to the main menu.")
return detected_devices # Exit the loop and return devices

print(
"DFU device not found, checking again... Press any key to return to the main menu."
)

# Check for key press with a timeout of 2 seconds
if Utils.is_key_pressed(timeout=2):
return self.stop_loop(detected_devices)

except KeyboardInterrupt:
print("\nQuery canceled by user.")
return []
return self.stop_loop(detected_devices)
except Exception as e:
print(f"Error while querying devices: {e}")
return []

# Return detected devices to avoid further processing if none found
return detected_devices
print(f"Error: {e}")
return detected_devices

def query_devices(self):
Utils.header()
Expand Down

0 comments on commit 76c53b7

Please sign in to comment.