예제 #1
0
 def update_slots(self):
     assert self.project.target == 'v5'
     if self.port.is_valid() and bool(self.port.value):
         from pros.serial.devices.vex import V5Device
         from pros.serial.ports import DirectPort
         device = V5Device(DirectPort(self.port.value))
         slot_options = [
             f'{slot}' +
             ('' if program is None else f' (Currently: {program})')
             for slot, program in device.used_slots().items()
         ]
     else:
         slot_options = [str(i) for i in range(1, 9)]
     project_name = self.advanced_options['name'].value
     if 'slot' in self.project.upload_options:
         # first, see if the project has it specified in its upload options
         selected = slot_options[self.project.upload_options['slot'] - 1]
     else:
         # otherwise, try to do a name match
         matched_slots = [
             i for i, slot in enumerate(slot_options)
             if slot.endswith(f'{project_name})')
         ]
         if len(matched_slots) > 0:
             selected = slot_options[matched_slots[0]]
         elif 'slot' in self.advanced_options:
             # or whatever the last value was
             selected = slot_options[
                 int(self.advanced_options['slot'].value[0]) - 1]
         else:
             # or just slot 1
             selected = slot_options[0]
     self.advanced_options['slot'] = parameters.OptionParameter(
         selected, slot_options)
예제 #2
0
def cat_metadata(file_name: str, port: str, vid: int):
    """
    Print metadata for a file
    """
    from pros.serial.devices.vex import V5Device
    from pros.serial.ports import DirectPort
    port = resolve_v5_port(port, 'system')
    if not port:
        return -1

    ser = DirectPort(port)
    device = V5Device(ser)
    print(device.get_file_metadata_by_name(file_name, vid=vid))
예제 #3
0
def rm_file(file_name: str, port: str, vid: int, erase_all: bool):
    """
    Remove a file from the flash filesystem
    """
    from pros.serial.devices.vex import V5Device
    from pros.serial.ports import DirectPort
    port = resolve_v5_port(port, 'system')
    if not port:
        return -1

    ser = DirectPort(port)
    device = V5Device(ser)
    device.erase_file(file_name, vid=vid, erase_all=erase_all)
예제 #4
0
def write_file(file, port: str, remote_file: str, **kwargs):
    """
    Write a file to the V5.
    """
    from pros.serial.ports import DirectPort
    from pros.serial.devices.vex import V5Device
    port = resolve_v5_port(port, 'system')
    if not port:
        return -1

    ser = DirectPort(port)
    device = V5Device(ser)
    device.write_file(file=file, remote_file=remote_file or os.path.basename(file.name), **kwargs)
예제 #5
0
def stop(port: str):
    """
    Stops a V5 program

    If FILE is unspecified or is a directory, then attempts to find the correct filename based on the PROS project
    """
    from pros.serial.devices.vex import V5Device
    from pros.serial.ports import DirectPort
    port = resolve_v5_port(port, 'system')
    if not port:
        return -1
    ser = DirectPort(port)
    device = V5Device(ser)
    device.execute_program_file('', run=False)
예제 #6
0
def read_file(file_name: str, port: str, vid: int, source: str):
    """
    Read file on the flash filesystem to stdout
    """
    from pros.serial.devices.vex import V5Device
    from pros.serial.ports import DirectPort
    port = resolve_v5_port(port, 'system')
    if not port:
        return -1

    ser = DirectPort(port)
    device = V5Device(ser)
    device.read_file(file=click.get_binary_stream('stdout'), remote_file=file_name,
                     vid=vid, target=source)
예제 #7
0
def ls_files(port: str, vid: int, options: int):
    """
    List files on the flash filesystem
    """
    from pros.serial.devices.vex import V5Device
    from pros.serial.ports import DirectPort
    port = resolve_v5_port(port, 'system')
    if not port:
        return -1

    ser = DirectPort(port)
    device = V5Device(ser)
    c = device.get_dir_count(vid=vid, options=options)
    for i in range(0, c):
        print(device.get_file_metadata_by_idx(i))
예제 #8
0
def rm_file(slot: int, port: str, vid: int):
    """
    Remove a program from the flash filesystem
    """
    from pros.serial.devices.vex import V5Device
    from pros.serial.ports import DirectPort
    port = resolve_v5_port(port, 'system')
    if not port:
        return -1

    base_name = 'slot_' + str(slot)
    ser = DirectPort(port)
    device = V5Device(ser)
    device.erase_file(base_name + '.ini', vid=vid)
    device.erase_file(base_name + '.bin', vid=vid)
예제 #9
0
def run(slot: str, port: str):
    """
    Run a V5 program
    """
    from pros.serial.devices.vex import V5Device
    from pros.serial.ports import DirectPort
    file = f'slot_{slot}.bin'
    import re
    if not re.match(r'[\w\.]{1,24}', file):
        logger(__name__).error('file must be a valid V5 filename')
        return 1
    port = resolve_v5_port(port, 'system')
    if not port:
        return -1
    ser = DirectPort(port)
    device = V5Device(ser)
    device.execute_program_file(file, run=True)
예제 #10
0
def rm_file(file_name: str, port: str, vid: int):
    """
    Remove a file from the V5
    """
    from pros.serial.devices.vex import V5Device
    from pros.serial.ports import DirectPort
    port = resolve_v5_port(port, 'system')
    if not port:
        return -1

    ser = DirectPort(port)
    device = V5Device(ser)
    c = device.get_dir_count(vid=vid)
    if file_name != None:
        device.erase_file(file_name, vid=vid)
    else:
        print('Invalid or no filename given')
예제 #11
0
def capture(file_name: str, port: str, force: bool = False):
    """
    Take a screen capture of the display
    """
    from pros.serial.devices.vex import V5Device
    from pros.serial.ports import DirectPort
    import png
    import os

    port = resolve_v5_port(port, 'system')
    if not port:
        return -1
    ser = DirectPort(port)
    device = V5Device(ser)
    i_data, width, height = device.capture_screen()

    if i_data is None:
        print('Failed to capture screen from connected brain.')
        return -1

    # Sanity checking and default values for filenames
    if file_name is None:
        import time
        time_s = time.strftime('%Y-%m-%d-%H%M%S')
        file_name = f'{time_s}_{width}x{height}_pros_capture.png'
    if file_name == '-':
        # Send the data to stdout to allow for piping
        print(i_data, end='')
        return

    if not file_name.endswith('.png'):
        file_name += '.png'

    if not force and os.path.exists(file_name):
        print(f'{file_name} already exists. Refusing to overwrite!')
        print(
            'Re-run this command with the --force argument to overwrite existing files.'
        )
        return -1

    with open(file_name, 'wb') as file_:
        w = png.Writer(width, height, greyscale=False)
        w.write(file_, i_data)

    print(f'Saved screen capture to {file_name}')
예제 #12
0
def rm_all(port: str, vid: int):
    """
    Remove all user files from the V5
    """
    from pros.serial.devices.vex import V5Device
    from pros.serial.ports import DirectPort
    port = resolve_v5_port(port, 'system')
    if not port:
        return -1

    ser = DirectPort(port)
    device = V5Device(ser)
    c = device.get_dir_count(vid=vid)
    files = []
    for i in range(0, c):
        files.append(device.get_file_metadata_by_idx(i)['filename'])
    for file in files:
        device.erase_file(file, vid=vid)
예제 #13
0
def status(port: str):
    """
    Print system information for the V5
    """
    from pros.serial.devices.vex import V5Device
    from pros.serial.ports import DirectPort
    port = resolve_v5_port(port, 'system')
    if not port:
        return -1

    ser = DirectPort(port)
    device = V5Device(ser)
    if ismachineoutput():
        print(device.status)
    else:
        print('Connected to V5 on {}'.format(port))
        print('System version:', device.status['system_version'])
        print('CPU0 F/W version:', device.status['cpu0_version'])
        print('CPU1 SDK version:', device.status['cpu1_version'])
        print('System ID: 0x{:x}'.format(device.status['system_id']))