DRY: convenience method to get config from USB
This commit is contained in:
parent
61aadae2ca
commit
e2ad75371e
|
@ -17,8 +17,7 @@ from textual.widgets.tree import TreeNode
|
||||||
|
|
||||||
from gp2040ce_bintools import core_parser, handler
|
from gp2040ce_bintools import core_parser, handler
|
||||||
from gp2040ce_bintools.builder import write_new_config_to_filename
|
from gp2040ce_bintools.builder import write_new_config_to_filename
|
||||||
from gp2040ce_bintools.pico import get_bootsel_endpoints, read
|
from gp2040ce_bintools.storage import get_config_from_file, get_config_from_usb
|
||||||
from gp2040ce_bintools.storage import STORAGE_MEMORY_ADDRESS, STORAGE_SIZE, get_config, get_config_from_file
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -136,9 +135,7 @@ class ConfigEditor(App):
|
||||||
usb = kwargs.pop('usb', False)
|
usb = kwargs.pop('usb', False)
|
||||||
# load the config
|
# load the config
|
||||||
if usb:
|
if usb:
|
||||||
endpoint_out, endpoint_in = get_bootsel_endpoints()
|
self.config, endpoint_out, endpoint_in = get_config_from_usb()
|
||||||
storage = read(endpoint_out, endpoint_in, STORAGE_MEMORY_ADDRESS, STORAGE_SIZE)
|
|
||||||
self.config = get_config(bytes(storage))
|
|
||||||
self.source_name = (f"DEVICE ID {hex(endpoint_out.device.idVendor)}:{hex(endpoint_out.device.idProduct)} "
|
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}")
|
f"on bus {endpoint_out.device.bus} address {endpoint_out.device.address}")
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -116,6 +116,20 @@ def get_config_from_file(filename: str, whole_board: bool = False, allow_no_file
|
||||||
return get_config(content)
|
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:
|
def get_storage_section(content: bytes) -> bytes:
|
||||||
"""Pull out what should be the GP2040-CE storage section from a whole board dump.
|
"""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()
|
args, _ = parser.parse_known_args()
|
||||||
|
|
||||||
if args.usb:
|
if args.usb:
|
||||||
endpoint_out, endpoint_in = get_bootsel_endpoints()
|
config, _, _ = get_config_from_usb()
|
||||||
storage = read(endpoint_out, endpoint_in, STORAGE_MEMORY_ADDRESS, STORAGE_SIZE)
|
|
||||||
config = get_config(bytes(storage))
|
|
||||||
else:
|
else:
|
||||||
config = get_config_from_file(args.filename, whole_board=args.whole_board)
|
config = get_config_from_file(args.filename, whole_board=args.whole_board)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue