Ejemplo n.º 1
0
def option_item_selected_testing(terminal_ui: TerminalUI, option_name: str,
                                 value: Union[int, float, bool,
                                              str], index: int):
    """An example function that could be passed into the TerminalUI option_item_selected_callback argument, which will be called everytime an option fires its change event.
    
    Args:
        terminal_ui (TerminalUI): The TerminalUI object that called the option_item_selected_callback function
        option_name (str): The name of the option that fired the change event
        value (Union[int, float, bool, str]): The value of the option
        index (int): The index of the value if the option is a list of selectable values
    """

    # Add text specifying the option_name and the option_string to the receive textbox
    txt = 'Option Name: %s, Current Value: %s, Index: %s' % (option_name,
                                                             value, str(index))
    terminal_ui.set_receive_text(txt, clear=False)

    # If the enable_options option fired the change event
    if option_name == 'enable_options':
        terminal_ui.enable_option('option_3', value)
        terminal_ui.enable_option('option_4', value)
        terminal_ui.enable_option('option_10', value)
        terminal_ui.enable_option('option_11', value)

    # if the enable_command option fired the change event
    elif option_name == 'enable_command':
        terminal_ui.enable_command(value)

    # if the show_cmd_debug option is fired
    elif option_name == 'show_cmd_debug':
        terminal_ui.command_debug_text_visible(value)
Ejemplo n.º 2
0
def command_entered_testing(terminal_ui: TerminalUI, command: str):
    """An example function that could be passed into the TerminalUI command_entered_callback argument, which will be called everytime a user enters a command.

    Args:
        terminal_ui (TerminalUI): The TerminalUI object that called the command_entered_callback function
        command (str): The command entered by the user
    """

    # Set the command debug textbox to contain the command entered by the user
    terminal_ui.set_command_debug_text(" Debug: %s" % (command))
Ejemplo n.º 3
0
def option_item_selected_testing(terminal_ui: TerminalUI, option_name: str,
                                 value: Union[int, float, bool,
                                              str], index: int):
    """An example function that could be passed into the TerminalUI option_item_selected_callback argument, which will be called everytime an option fires its change event.
    
    Args:
        terminal_ui (TerminalUI): The TerminalUI object that called the option_item_selected_callback function
        option_name (str): The name of the option that fired the change event
        value (Union[int, float, bool, str]): The value of the option
        index (int): The index of the value if the option is a list of selectable values
    """
    global enable_read

    # enable/disable enable_read
    if option_name == 'read_enabled':
        enable_read = value
        terminal_ui.set_receive_text('Read Enabled: %s' % str(value), False)
Ejemplo n.º 4
0
def command_entered_testing(terminal_ui: TerminalUI, command: str):
    """An example function that could be passed into the TerminalUI command_entered_callback argument, which will be called everytime a user enters a command.

    Args:
        terminal_ui (TerminalUI): The TerminalUI object that called the command_entered_callback function
        command (str): The command entered by the user
    """

    # Convert command string into a byte array
    byte_array = bytearray([ord(x) for x in command])
    byte_array_hex_string = ("".join(" 0x%02x" % (x)
                                     for x in byte_array)).strip()

    # Set the command debug textbox to contain the command entered by the user as plain text and as a hex coded byte array
    txt = " Input Text: %s" % command
    txt += "\n HEX Bytes Sent: %s" % byte_array_hex_string
    terminal_ui.set_command_debug_text(txt)
Ejemplo n.º 5
0
def option_item_selected(terminal_ui : TerminalUI, option_name : str, value : int, index : str):

    # terminal_ui.set_receive_text(option_name + " " + str(value), False)

    # Connect/Disconnect with serial port
    if option_name == 'connect':
        if value.lower() == 'open':
            open_serial_port()
        elif value.lower() == 'close':
            close_serial_port()

    # Turn on/off command debug textbox
    if option_name == 'write_debug':
        terminal_ui.command_debug_text_visible(value)

    # Exit
    if option_name == 'exit':
        terminal_ui.exit()
Ejemplo n.º 6
0
def command_entered(terminal_ui : TerminalUI, command):

    # Interpret command
    _, _, idx = terminal_ui.get_option('cmd_interpreter')
    if idx == 0:
        # Plain Text Interpreter
        byte_array = plain_text_interpreter(command)
    elif idx == 1:
        byte_array = smart_command_interpreter(command)

    byte_array_hex_string = ("".join(" 0x%02x"%(x) for x in byte_array)).strip()

    # Debug text
    txt = " Input Text: %s"%command
    txt += "\n HEX Bytes Sent: %s"%byte_array_hex_string
    terminal_ui.set_command_debug_text(txt)

    # Write command
    serial_write(byte_array)
Ejemplo n.º 7
0
### MAIN FUNCTION ###
threads_enabled = True
enable_read = True
read_thread = None

if __name__ == "__main__":
    # Options
    options = {
        'Read Settings': {
            'read_enabled': ([True, False], 'Read Enabled', False)
        }
    }

    # Create TerminalUI object with the title 'Terminal UI v0.1', command_entered_testing function callback,
    # the options specified and the option_item_selected_testing callback function.
    terminal_ui = TerminalUI('Terminal UI v0.1', command_entered_testing,
                             options, option_item_selected_testing)

    # Start serial read thread
    read_thread = threading.Thread(target=read_thread_callback, args=())
    read_thread.start()

    # Run the terminal catching crtl+c keyboard interrupt to close everything appropriately
    try:
        terminal_ui.run()
    except KeyboardInterrupt:
        pass

    # Close appropriately
    enable_read = False
    threads_enabled = False
Ejemplo n.º 8
0
    demo_group_1_option_data['option_13'] = (['A', 'B', 'C'], 'Caption')
    demo_group_1_option_data['option_14'] = ('Editable String', 'Caption')

    # Create a secondary dictionary for the Demo Group 2. Selectable lists do not need to
    # contain values of the same type
    demo_group_2_option_data = {}
    demo_group_2_option_data['option_15'] = (['ABC', 1, 0.5], 'Caption')

    # Set the secondary dictionary of each group
    options['Selectable List Options'] = selectable_list_option_data
    options['Editable Value Options'] = editable_value_option_data
    options['Enable Items'] = enable_group_option_data

    # Each group has two optional parameters which can be passed via a tuple.
    # The first element must be the secondary dictionary containing the option data.
    # The second and third element are booleans which can be used to specify if the
    # group name is shown above the option set as a title and if a blank row is left
    # after the option set respectively.
    # options['Demo Group 1'] = (demo_group_1_option_data, True, True) # show the title and have a blank row after the set
    # options['Demo Group 2'] = (demo_group_2_option_data, False, False) # hide the title and do not have a blank row after the set

    # Create TerminalUI object with the title 'Terminal UI v0.1', command_entered_testing function callback,
    # the options specified and the option_item_selected_testing callback function.
    terminal_ui = TerminalUI('Terminal UI v0.1', command_entered_testing,
                             options, option_item_selected_testing)

    # Set initial value for the enable_options command to be disabled
    terminal_ui.set_option('enable_options', 1)

    # Run the terminal UI
    terminal_ui.run()
Ejemplo n.º 9
0
    options['Serial Connection']['baud_rate'] = (baud_rates, 'Baud', False)
    options['Serial Connection']['startup_delay'] = (0.5, 'Start-Up Delay', 0.5, [0, None], False)
    options['Serial Connection']['read_timeout'] = (0.1, 'Read Timeout', 0.1, [0, None], False)
    options['Serial Connection']['write_timeout'] = (0.1, 'Write Timeout', 0.1, [0, None], False)
    options['Serial Connection']['connect'] = ['OPEN']

    options['Write Options']['cmd_interpreter'] = (['Plain Text', 'Smart Interpreter'], 'Interpreter', False)
    options['Write Options']['write_debug'] = ([True, False], 'Write Debug On', False)

    options['Read Options']['read_interpreter'] = (['ASCII', 'Hex', 'Binary'], 'Interpreter', False)
    options['Read Options']['read_timestamp'] = ([True, False], 'Show Timestamp', False)

    options['Exit'] = ({'exit': (['EXIT PROGRAM'], '', True)}, False)

    # Terminal UI 
    terminal_ui = TerminalUI('Serial Echo v0.1', command_entered, options, option_item_selected)
    
    # Set Default Option Values
    terminal_ui.set_option('baud_rate', 6)
    terminal_ui.set_option('cmd_interpreter', 1)

    # Start serial read thread
    serial_read_thread = threading.Thread(target=serial_read, args=())
    serial_read_thread.start()

    # Run
    try:
        terminal_ui.run()

    # Catch ctrl+c 
    except KeyboardInterrupt: