2022-06-30 16:06:12 +00:00
|
|
|
import serial.tools.list_ports as list_ports
|
|
|
|
|
2023-02-15 16:56:25 +00:00
|
|
|
|
2022-06-30 16:06:12 +00:00
|
|
|
# Returns a valid port or None, if it cannot be found
|
|
|
|
def resolve_port(logger, portname: str = "auto"):
|
|
|
|
if portname != "auto":
|
|
|
|
return portname
|
|
|
|
# Try guessing
|
2023-06-08 04:47:27 +00:00
|
|
|
flippers = list(list_ports.grep("flip_"))
|
2022-06-30 16:06:12 +00:00
|
|
|
if len(flippers) == 1:
|
|
|
|
flipper = flippers[0]
|
|
|
|
logger.info(f"Using {flipper.serial_number} on {flipper.device}")
|
|
|
|
return flipper.device
|
|
|
|
elif len(flippers) == 0:
|
|
|
|
logger.error("Failed to find connected Flipper")
|
|
|
|
elif len(flippers) > 1:
|
|
|
|
logger.error("More than one Flipper is attached")
|
2022-09-24 11:30:19 +00:00
|
|
|
logger.error("Failed to guess which port to use")
|