DRY: convenience method to get config from USB

This commit is contained in:
Brian S. Stephan 2023-07-07 20:03:41 -05:00
parent 61aadae2ca
commit e2ad75371e
Signed by: bss
GPG Key ID: 3DE06D3180895FCB
2 changed files with 17 additions and 8 deletions

View File

@ -17,8 +17,7 @@ from textual.widgets.tree import TreeNode
from gp2040ce_bintools import core_parser, handler
from gp2040ce_bintools.builder import write_new_config_to_filename
from gp2040ce_bintools.pico import get_bootsel_endpoints, read
from gp2040ce_bintools.storage import STORAGE_MEMORY_ADDRESS, STORAGE_SIZE, get_config, get_config_from_file
from gp2040ce_bintools.storage import get_config_from_file, get_config_from_usb
logger = logging.getLogger(__name__)
@ -136,9 +135,7 @@ class ConfigEditor(App):
usb = kwargs.pop('usb', False)
# load the config
if usb:
endpoint_out, endpoint_in = get_bootsel_endpoints()
storage = read(endpoint_out, endpoint_in, STORAGE_MEMORY_ADDRESS, STORAGE_SIZE)
self.config = get_config(bytes(storage))
self.config, endpoint_out, endpoint_in = get_config_from_usb()
self.source_name = (f"DEVICE ID {hex(endpoint_out.device.idVendor)}:{hex(endpoint_out.device.idProduct)} "
f"on bus {endpoint_out.device.bus} address {endpoint_out.device.address}")
else:

View File

@ -116,6 +116,20 @@ def get_config_from_file(filename: str, whole_board: bool = False, allow_no_file
return get_config(content)
def get_config_from_usb() -> tuple[Message, object, object]:
"""Read the config section from a USB device and get back its config section.
Returns:
the parsed configuration, along with the USB out and in endpoints for reference
"""
# open the USB device and get the config
endpoint_out, endpoint_in = get_bootsel_endpoints()
logger.debug("reading DEVICE ID %s:%s, bus %s, address %s", hex(endpoint_out.device.idVendor),
hex(endpoint_out.device.idProduct), endpoint_out.device.bus, endpoint_out.device.address)
storage = read(endpoint_out, endpoint_in, STORAGE_MEMORY_ADDRESS, STORAGE_SIZE)
return get_config(bytes(storage)), endpoint_out, endpoint_in
def get_storage_section(content: bytes) -> bytes:
"""Pull out what should be the GP2040-CE storage section from a whole board dump.
@ -188,9 +202,7 @@ def visualize():
args, _ = parser.parse_known_args()
if args.usb:
endpoint_out, endpoint_in = get_bootsel_endpoints()
storage = read(endpoint_out, endpoint_in, STORAGE_MEMORY_ADDRESS, STORAGE_SIZE)
config = get_config(bytes(storage))
config, _, _ = get_config_from_usb()
else:
config = get_config_from_file(args.filename, whole_board=args.whole_board)