# status output of train controller
#
# provides status of train controller over REPL connection to a terminal
#
#
# initial version 8-Oct-2023 by wolf2018
#
import cfg

# Define terminal control sequences
# usage examples:
#    print(CSI+str(n)+Cup) cursor up by n rows
#    print(CSI+str(r)+";"+str(c)+Cpos) move to row r column c
#
CSI = "\033["    # start sequence for terminal control
Cup = "A"        # curser up
Cdn = "B"        # cursor down
Cfw = "C"        # cursor forward
Cnl = "E"        # cursor to beginning of new line
Cpos = "H"       # cursor position
Cli = "2K"        # clear entire line
Clear = "\33c"
#
rail1_header = "Rail 1    direction    power    speed    overload"
rail2_header = "Rail 2    direction    power    speed    overload"


def clear_terminal():
    # clear terminal
    print(Clear)
    print(CSI+"0;0"+Cpos, end="")    # position cursor at top left corner
    print("Train Controller Status output", end="")
    print(CSI+"3;0"+Cpos, end="")    # position cursor at row 3 left


# output for rail1
def print_rail_status():
    #
    # print rail 1 header
    print(CSI+"3;0"+Cpos, rail1_header, end="")

    # print rail1 status in row 5
    print(CSI+"5;0"+Cpos+CSI+Cli, end="")    # clear row 5
    print(CSI+"5;14"+Cpos, end="")    # position cursor at r 5 c 14
    print(cfg.rail1_status["direction"], end="")
    print(CSI+"5;26"+Cpos, end="")    # position cursor at r 5 c 26
    print(cfg.rail1_status["power"], end="")
    print(CSI+"5;36"+Cpos, end="")    # position cursor at r 5 c 36
    print(cfg.rail1_status["speed"], end="")
    print(CSI+"5;45"+Cpos, end="")    # position cursor at r 5 c 45
    print(cfg.rail1_status["overload"], end="")
    #

    # print rail 2 header
    print(CSI+"7;0"+Cpos, rail2_header, end="")

    # print rail2 status in row 9
    print(CSI+"9;0"+Cpos+CSI+Cli, end="")    # clear row 9
    print(CSI+"9;14"+Cpos, end="")    # position cursor at r 5 c 14
    print(cfg.rail2_status["direction"], end="")
    print(CSI+"9;26"+Cpos, end="")    # position cursor at r 5 c 26
    print(cfg.rail2_status["power"], end="")
    print(CSI+"9;36"+Cpos, end="")    # position cursor at r 5 c 36
    print(cfg.rail2_status["speed"], end="")
    print(CSI+"9;45"+Cpos, end="")    # position cursor at r 5 c 45
    print(cfg.rail2_status["overload"], end="")
