def denom_settings():
    global CONFIG

    t.wipe()
    display_header("Denomination enable/inhibit settings")

    opts = dict()
    set_opts = OrderedDict()
    for i, k in enumerate(CONFIG['bv.denom_inhibit'].keys()):
        if id003.DENOM_MAP[k] in id003.ESCROW_USA:
            denom = id003.ESCROW_USA[id003.DENOM_MAP[k]]
        else:
            denom = None

        denom_enabled = CONFIG['bv.denom_inhibit'].getboolean(k)
        opts[i] = k  # index into this config section
        set_opts[k] = denom_enabled  # cache settings before writing to config

        if denom is not None:
            line = k + ' (' + denom + '):\t\t'
        else:
            line = k + ':\t\t\t'

        if denom_enabled:
            line += 'X'
        else:
            line += '_'

        print(line)

    print(
        "\n\nIf a denom is inhibited through these settings that's not inhibited by the\n"
        "appropriate DIP switch on the BV, the BV will go into INHIBIT status."
    )
    print(
        "\nPress Enter to save and go back, or Esc to go back without saving")
    t.set_pos(25, 3)

    max_opt = len(CONFIG['bv.denom_inhibit']) - 1
    cur_opt = 0
    while True:
        x, y = t.get_pos()
        c = t.getch()

        if c == b'\xe0H' and cur_opt > 0:
            # up
            t.set_pos(x, y - 1)
            cur_opt -= 1
        elif c == b'\xe0P' and cur_opt < max_opt:
            # down
            t.set_pos(x, y + 1)
            cur_opt += 1
        elif c == b'\t' and cur_opt == max_opt:
            # wrap around to first option
            t.set_pos(x, 3)
            cur_opt = 0
        elif c == b'\t':
            # next option, same as down
            t.set_pos(x, y + 1)
            cur_opt += 1
        elif c == b'X' or c == b'x':
            set_opts[opts[cur_opt]] = True
            print('X', end='')
            if cur_opt < max_opt:
                t.set_pos(x, y + 1)
                cur_opt += 1
            else:
                t.set_pos(x, y)
        elif c == b' ':
            set_opts[opts[cur_opt]] = False
            print('_', end='')
            if cur_opt < max_opt:
                t.set_pos(x, y + 1)
                cur_opt += 1
            else:
                t.set_pos(x, y)
        elif c == b'\r':
            # save and go back
            CONFIG['bv.denom_inhibit'] = set_opts
            return
        elif c == b'\x1b':
            # escape, go back without saving
            return
def direction_settings():
    global CONFIG

    t.wipe()
    display_header("Direction ihibit settings")

    opts = dict()
    set_opts = OrderedDict()
    for i, k in enumerate(CONFIG['bv.direction'].keys()):
        dir_enabled = CONFIG['bv.direction'].getboolean(k)
        opts[i] = k
        set_opts[k] = dir_enabled

        if k == 'fa':
            print("Front side up, left side in:\t\t", end='')
        elif k == 'fb':
            print("Front side up, right side in:\t\t", end='')
        elif k == 'bb':
            print("Back side up, left side in:\t\t", end='')
        elif k == 'ba':
            print("Back side up, right side in:\t\t", end='')

        start_x, start_y = t.get_pos()
        if dir_enabled:
            print('X')
        else:
            print('_')

    print("\n\n_ = enabled, X = inhibited")
    print(
        "\nPress Enter to save and go back, or Esc to go back without saving")
    t.set_pos(start_x, 3)

    max_opt = len(CONFIG['bv.direction']) - 1
    cur_opt = 0
    while True:
        x, y = t.get_pos()
        c = t.getch()

        if c == b'\xe0H' and cur_opt > 0:
            # up
            t.set_pos(x, y - 1)
            cur_opt -= 1
        elif c == b'\xe0P' and cur_opt < max_opt:
            # down
            t.set_pos(x, y + 1)
            cur_opt += 1
        elif c == b'\t' and cur_opt == max_opt:
            # wrap around to first option
            t.set_pos(x, 3)
            cur_opt = 0
        elif c == b'\t':
            # next option, same as down
            t.set_pos(x, y + 1)
            cur_opt += 1
        elif c == b'X' or c == b'x':
            set_opts[opts[cur_opt]] = True
            print('X', end='')
            if cur_opt < max_opt:
                t.set_pos(x, y + 1)
                cur_opt += 1
            else:
                t.set_pos(x, y)
        elif c == b' ':
            set_opts[opts[cur_opt]] = False
            print('_', end='')
            if cur_opt < max_opt:
                t.set_pos(x, y + 1)
                cur_opt += 1
            else:
                t.set_pos(x, y)
        elif c == b'\r':
            # save and go back
            CONFIG['bv.direction'] = set_opts
            return
        elif c == b'\x1b':
            # escape, go back without saving
            return
def security_settings():
    global CONFIG

    t.wipe()
    display_header("Denomination security settings")

    opts = dict()
    set_opts = OrderedDict()
    for i, k in enumerate(CONFIG['bv.security'].keys()):
        if id003.DENOM_MAP[k] in id003.ESCROW_USA:
            denom = id003.ESCROW_USA[id003.DENOM_MAP[k]]
        else:
            denom = None

        denom_enabled = CONFIG['bv.security'].getboolean(k)
        opts[i] = k
        set_opts[k] = denom_enabled

        if denom is not None:
            line = k + ' (' + denom + '):\t\t'
        else:
            line = k + ':\t\t\t'

        if denom_enabled:
            line += 'X'
        else:
            line += '_'

        print(line)

    print("\n\nX = high security, _ = low security")
    print(
        "\nPress Enter to save and go back, or Esc to go back without saving")
    t.set_pos(25, 3)

    max_opt = len(CONFIG['bv.security']) - 1
    cur_opt = 0
    while True:
        x, y = t.get_pos()
        c = t.getch()

        if c == b'\xe0H' and cur_opt > 0:
            # up
            t.set_pos(x, y - 1)
            cur_opt -= 1
        elif c == b'\xe0P' and cur_opt < max_opt:
            # down
            t.set_pos(x, y + 1)
            cur_opt += 1
        elif c == b'\t' and cur_opt == max_opt:
            # wrap around to first option
            t.set_pos(x, 3)
            cur_opt = 0
        elif c == b'\t':
            # next option, same as down
            t.set_pos(x, y + 1)
            cur_opt += 1
        elif c == b'X' or c == b'x':
            set_opts[opts[cur_opt]] = True
            print('X', end='')
            if cur_opt < max_opt:
                t.set_pos(x, y + 1)
                cur_opt += 1
            else:
                t.set_pos(x, y)
        elif c == b' ':
            set_opts[opts[cur_opt]] = False
            print('_', end='')
            if cur_opt < max_opt:
                t.set_pos(x, y + 1)
                cur_opt += 1
            else:
                t.set_pos(x, y)
        elif c == b'\r':
            # save and go back
            CONFIG['bv.security'] = set_opts
            return
        elif c == b'\x1b':
            # escape, go back without saving
            return
def opt_settings():
    global CONFIG

    t.wipe()
    display_header("Optional function settings")

    opts = dict()
    set_opts = OrderedDict()
    opt_txt = {
        'power_recovery': "Power recovery:\t\t\t\t",
        'auto_retry': "Auto-retry operaton:\t\t\t",
        '24_char_barcode': "Accept 24-character barcodes:\t\t",
        'near_full': "Stacker nearly full event:\t\t",
        'entrance_event': "Entrance sensor event:\t\t\t",
        'encryption': "Encryption:\t\t\t\t",
    }

    for i, k in enumerate(CONFIG['bv.optional'].keys()):
        opt_enabled = CONFIG['bv.optional'].getboolean(k)
        opts[i] = k
        set_opts[k] = opt_enabled

        print(opt_txt[k], end='')
        start_x, start_y = t.get_pos()
        if opt_enabled:
            print('X')
        else:
            print('_')

    print("\n\n_ = disabled, X = enabled")
    print(
        "\nPress Enter to save and go back, or Esc to go back without saving")
    t.set_pos(start_x, 3)

    max_opt = len(CONFIG['bv.optional']) - 1
    cur_opt = 0
    while True:
        x, y = t.get_pos()
        c = t.getch()

        if c == b'\xe0H' and cur_opt > 0:
            # up
            t.set_pos(x, y - 1)
            cur_opt -= 1
        elif c == b'\xe0P' and cur_opt < max_opt:
            # down
            t.set_pos(x, y + 1)
            cur_opt += 1
        elif c == b'\t' and cur_opt == max_opt:
            # wrap around to first option
            t.set_pos(x, 3)
            cur_opt = 0
        elif c == b'\t':
            # next option, same as down
            t.set_pos(x, y + 1)
            cur_opt += 1
        elif c == b'X' or c == b'x':
            set_opts[opts[cur_opt]] = True
            print('X', end='')
            if cur_opt < max_opt:
                t.set_pos(x, y + 1)
                cur_opt += 1
            else:
                t.set_pos(x, y)
        elif c == b' ':
            set_opts[opts[cur_opt]] = False
            print('_', end='')
            if cur_opt < max_opt:
                t.set_pos(x, y + 1)
                cur_opt += 1
            else:
                t.set_pos(x, y)
        elif c == b'\r':
            # save and go back
            CONFIG['bv.optional'] = set_opts
            return
        elif c == b'\x1b':
            # escape, go back without saving
            return
def denom_settings():
    global CONFIG
    
    t.wipe()
    display_header("Denomination enable/inhibit settings")
    
    opts = dict()
    set_opts = OrderedDict()
    for i, k in enumerate(CONFIG['bv.denom_inhibit'].keys()):
        if id003.DENOM_MAP[k] in id003.ESCROW_USA:
            denom = id003.ESCROW_USA[id003.DENOM_MAP[k]]
        else:
            denom = None
            
        denom_enabled = CONFIG['bv.denom_inhibit'].getboolean(k)
        opts[i] = k     # index into this config section
        set_opts[k] = denom_enabled     # cache settings before writing to config
        
        if denom is not None:
            line = k + ' (' + denom + '):\t\t'
        else:
            line = k + ':\t\t\t'
        
        if denom_enabled:
            line += 'X'
        else:
            line += '_'
            
        print(line)
    
    print("\n\nIf a denom is inhibited through these settings that's not inhibited by the\n"
          "appropriate DIP switch on the BV, the BV will go into INHIBIT status.")
    print("\nPress Enter to save and go back, or Esc to go back without saving")
    t.set_pos(25, 3)
    
    max_opt = len(CONFIG['bv.denom_inhibit']) - 1
    cur_opt = 0
    while True:
        x, y = t.get_pos()
        c = t.getch()
        
        if c == b'\xe0H' and cur_opt > 0:
            # up
            t.set_pos(x, y-1)
            cur_opt -= 1
        elif c == b'\xe0P' and cur_opt < max_opt:
            # down
            t.set_pos(x, y+1)
            cur_opt += 1
        elif c == b'\t' and cur_opt == max_opt:
            # wrap around to first option
            t.set_pos(x, 3)
            cur_opt = 0
        elif c == b'\t':
            # next option, same as down
            t.set_pos(x, y+1)
            cur_opt += 1
        elif c == b'X' or c == b'x':
            set_opts[opts[cur_opt]] = True
            print('X', end='')
            if cur_opt < max_opt:
                t.set_pos(x, y+1)
                cur_opt += 1
            else:
                t.set_pos(x, y)
        elif c == b' ':
            set_opts[opts[cur_opt]] = False
            print('_', end='')
            if cur_opt < max_opt:
                t.set_pos(x, y+1)
                cur_opt += 1
            else:
                t.set_pos(x, y)
        elif c == b'\r':
            # save and go back
            CONFIG['bv.denom_inhibit'] = set_opts
            return
        elif c == b'\x1b':
            # escape, go back without saving
            return
def security_settings():
    global CONFIG
    
    t.wipe()
    display_header("Denomination security settings")
    
    opts = dict()
    set_opts = OrderedDict()
    for i, k in enumerate(CONFIG['bv.security'].keys()):
        if id003.DENOM_MAP[k] in id003.ESCROW_USA:
            denom = id003.ESCROW_USA[id003.DENOM_MAP[k]]
        else:
            denom = None
            
        denom_enabled = CONFIG['bv.security'].getboolean(k)
        opts[i] = k
        set_opts[k] = denom_enabled
        
        if denom is not None:
            line = k + ' (' + denom + '):\t\t'
        else:
            line = k + ':\t\t\t'
        
        if denom_enabled:
            line += 'X'
        else:
            line += '_'
            
        print(line)
    
    print("\n\nX = high security, _ = low security")
    print("\nPress Enter to save and go back, or Esc to go back without saving")
    t.set_pos(25, 3)
    
    max_opt = len(CONFIG['bv.security']) - 1
    cur_opt = 0
    while True:
        x, y = t.get_pos()
        c = t.getch()
        
        if c == b'\xe0H' and cur_opt > 0:
            # up
            t.set_pos(x, y-1)
            cur_opt -= 1
        elif c == b'\xe0P' and cur_opt < max_opt:
            # down
            t.set_pos(x, y+1)
            cur_opt += 1
        elif c == b'\t' and cur_opt == max_opt:
            # wrap around to first option
            t.set_pos(x, 3)
            cur_opt = 0
        elif c == b'\t':
            # next option, same as down
            t.set_pos(x, y+1)
            cur_opt += 1
        elif c == b'X' or c == b'x':
            set_opts[opts[cur_opt]] = True
            print('X', end='')
            if cur_opt < max_opt:
                t.set_pos(x, y+1)
                cur_opt += 1
            else:
                t.set_pos(x, y)
        elif c == b' ':
            set_opts[opts[cur_opt]] = False
            print('_', end='')
            if cur_opt < max_opt:
                t.set_pos(x, y+1)
                cur_opt += 1
            else:
                t.set_pos(x, y)
        elif c == b'\r':
            # save and go back
            CONFIG['bv.security'] = set_opts
            return
        elif c == b'\x1b':
            # escape, go back without saving
            return
def direction_settings():
    global CONFIG
    
    t.wipe()
    display_header("Direction ihibit settings")
    
    opts = dict()
    set_opts = OrderedDict()
    for i, k in enumerate(CONFIG['bv.direction'].keys()):
        dir_enabled = CONFIG['bv.direction'].getboolean(k)
        opts[i] = k
        set_opts[k] = dir_enabled
        
        if k == 'fa':
            print("Front side up, left side in:\t\t", end='')
        elif k == 'fb':
            print("Front side up, right side in:\t\t", end='')
        elif k == 'bb':
            print("Back side up, left side in:\t\t", end='')
        elif k == 'ba':
            print("Back side up, right side in:\t\t", end='')
            
        start_x, start_y = t.get_pos()
        if dir_enabled:
            print('X')
        else:
            print('_')
            
    print("\n\n_ = enabled, X = inhibited")
    print("\nPress Enter to save and go back, or Esc to go back without saving")
    t.set_pos(start_x, 3)
    
    max_opt = len(CONFIG['bv.direction']) - 1
    cur_opt = 0
    while True:
        x, y = t.get_pos()
        c = t.getch()
        
        if c == b'\xe0H' and cur_opt > 0:
            # up
            t.set_pos(x, y-1)
            cur_opt -= 1
        elif c == b'\xe0P' and cur_opt < max_opt:
            # down
            t.set_pos(x, y+1)
            cur_opt += 1
        elif c == b'\t' and cur_opt == max_opt:
            # wrap around to first option
            t.set_pos(x, 3)
            cur_opt = 0
        elif c == b'\t':
            # next option, same as down
            t.set_pos(x, y+1)
            cur_opt += 1
        elif c == b'X' or c == b'x':
            set_opts[opts[cur_opt]] = True
            print('X', end='')
            if cur_opt < max_opt:
                t.set_pos(x, y+1)
                cur_opt += 1
            else:
                t.set_pos(x, y)
        elif c == b' ':
            set_opts[opts[cur_opt]] = False
            print('_', end='')
            if cur_opt < max_opt:
                t.set_pos(x, y+1)
                cur_opt += 1
            else:
                t.set_pos(x, y)
        elif c == b'\r':
            # save and go back
            CONFIG['bv.direction'] = set_opts
            return
        elif c == b'\x1b':
            # escape, go back without saving
            return
def opt_settings():
    global CONFIG
    
    t.wipe()
    display_header("Optional function settings")
    
    opts = dict()
    set_opts = OrderedDict()
    opt_txt = {
        'power_recovery': "Power recovery:\t\t\t\t",
        'auto_retry': "Auto-retry operaton:\t\t\t",
        '24_char_barcode': "Accept 24-character barcodes:\t\t",
        'near_full': "Stacker nearly full event:\t\t",
        'entrance_event': "Entrance sensor event:\t\t\t",
        'encryption': "Encryption:\t\t\t\t",
    }
    
    for i, k in enumerate(CONFIG['bv.optional'].keys()):
        opt_enabled = CONFIG['bv.optional'].getboolean(k)
        opts[i] = k
        set_opts[k] = opt_enabled
        
        print(opt_txt[k], end='')
        start_x, start_y = t.get_pos()
        if opt_enabled:
            print('X')
        else:
            print('_')
            
    print("\n\n_ = disabled, X = enabled")
    print("\nPress Enter to save and go back, or Esc to go back without saving")
    t.set_pos(start_x, 3)
    
    max_opt = len(CONFIG['bv.optional']) - 1
    cur_opt = 0
    while True:
        x, y = t.get_pos()
        c = t.getch()
        
        if c == b'\xe0H' and cur_opt > 0:
            # up
            t.set_pos(x, y-1)
            cur_opt -= 1
        elif c == b'\xe0P' and cur_opt < max_opt:
            # down
            t.set_pos(x, y+1)
            cur_opt += 1
        elif c == b'\t' and cur_opt == max_opt:
            # wrap around to first option
            t.set_pos(x, 3)
            cur_opt = 0
        elif c == b'\t':
            # next option, same as down
            t.set_pos(x, y+1)
            cur_opt += 1
        elif c == b'X' or c == b'x':
            set_opts[opts[cur_opt]] = True
            print('X', end='')
            if cur_opt < max_opt:
                t.set_pos(x, y+1)
                cur_opt += 1
            else:
                t.set_pos(x, y)
        elif c == b' ':
            set_opts[opts[cur_opt]] = False
            print('_', end='')
            if cur_opt < max_opt:
                t.set_pos(x, y+1)
                cur_opt += 1
            else:
                t.set_pos(x, y)
        elif c == b'\r':
            # save and go back
            CONFIG['bv.optional'] = set_opts
            return
        elif c == b'\x1b':
            # escape, go back without saving
            return