#================================================================
#================================================================
# C-WaveformGenerator
# MultiChannelWave
# Channel setting file
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
#----------------------------------------
# Import modules
#----------------------------------------
import ctypes
import ctypes.wintypes
from CommonFunction import *
#----------------------------------------
# Declaration of global variables
#----------------------------------------
m_aochannel = ctypes.c_ushort()         # Channel
m_amplitude = ctypes.c_float()          # Amplitude
m_offset = ctypes.c_float()             # Offset
m_amplitude_type = ctypes.c_ushort()    # Amplitude type
m_amplitude_unit = ctypes.c_ushort()    # Amplitude unit
m_duty = ctypes.c_float()               # Duty
m_phase = ctypes.c_float()              # Phase
m_sweep_type = ctypes.c_ushort()        # Simple sweep
m_sweep_time = ctypes.c_ushort()        # Sweep time
m_sweep_start = ctypes.c_float()        # Sweep before change
m_sweep_stop = ctypes.c_float()         # Sweep after change
m_wave_type = ctypes.c_ushort()         # Wave type


#========================================
# Data transfer (Set)
#========================================
def chinfo_data_set(aochannel, amplitude, offset, amplitudetype, amplitudeunit, duty, phase, sweeptype, sweeptime, sweepstart, sweepstop, wavetype):
    global m_aochannel, m_amplitude, m_offset, m_amplitude_type, m_amplitude_unit, m_duty, m_phase, m_sweep_type, m_sweep_time, m_sweep_start, m_sweep_stop, m_wave_type
    m_aochannel.value       = aochannel.value
    m_amplitude.value       = amplitude.value
    m_offset.value          = offset.value
    m_amplitude_type.value  = amplitudetype.value
    m_amplitude_unit.value  = amplitudeunit.value
    m_duty.value            = duty.value
    m_phase.value           = phase.value
    m_sweep_type.value      = sweeptype.value
    m_sweep_time.value      = sweeptime.value
    m_sweep_start.value     = sweepstart.value
    m_sweep_stop.value      = sweepstop.value
    m_wave_type.value       = wavetype.value

#========================================
# Data transfer (Get)
#========================================
def chinfo_data_get(aochannel, amplitude, offset, amplitudetype, amplitudeunit, duty, phase, sweeptype, sweeptime, sweepstart, sweepstop):
    global m_aochannel, m_amplitude, m_offset, m_amplitude_type, m_amplitude_unit, m_duty, m_phase, m_sweep_type, m_sweep_time, m_sweep_start, m_sweep_stop
    aochannel.value         = m_aochannel.value
    amplitude.value         = m_amplitude.value
    offset.value            = m_offset.value
    amplitudetype.value     = m_amplitude_type.value
    amplitudeunit.value     = m_amplitude_unit.value
    duty.value              = m_duty.value
    phase.value             = m_phase.value
    sweeptype.value         = m_sweep_type.value
    sweeptime.value         = m_sweep_time.value
    sweepstart.value        = m_sweep_start.value
    sweepstop.value         = m_sweep_stop.value

#========================================
# Function name    ChannelSetting
# Parameter        None
# Details          Function that display and process the channel settings menu
# Return value     None
#========================================
def ChannelSetting():
    global m_aochannel, m_amplitude, m_offset, m_amplitude_type, m_amplitude_unit, m_duty, m_phase, m_sweep_type, m_sweep_time, m_sweep_start, m_sweep_stop, m_wave_type

    #----------------------------------------
    # Loop that wait input
    #----------------------------------------
    while True:
        #----------------------------------------
        # Get setting parameters
        #----------------------------------------
        aochannel_string = f"{m_aochannel.value}"
        amplitude_string = f"{m_amplitude.value}"
        amplitude_type_string = GetAmplitudeTypeString(m_amplitude_type)
        amplitude_unit_string = GetAmplitudeUnitString(m_amplitude_unit)
        offset_string = f"{m_offset.value}"
        duty_string = f"{m_duty.value}"
        phase_string = f"{m_phase.value}"
        sweep_type_string = GetSweepTypeString(m_wave_type, m_sweep_type)
        sweep_time_string = f"{m_sweep_time.value}"
        sweep_start_string = f"{m_sweep_start.value}"
        sweep_stop_string = f"{m_sweep_stop.value}"
        #----------------------------------------
        # Display the setting parameters
        #----------------------------------------
        print("\n========================================")
        print("Setting Parameter")
        print("========================================")
        print(f"Channel:           CH : {aochannel_string}")
        print("<Channel Information>")
        print(f"  Amplitude           : {amplitude_string}")
        print(f"  Amplitude type      : {amplitude_type_string}")
        print(f"  Amplitude unit      : {amplitude_unit_string}")
        print(f"  Offset              : {offset_string}")
        print(f"  Duty                : {duty_string} [%]")
        print(f"  Phase               : {phase_string} [Degree]")
        print("<Sweep>")
        print(f"  Simple sweep        : {sweep_type_string}")
        print(f"  Sweep time          : {sweep_time_string} [waveforms]")
        print(f"  Sweep before change : {sweep_start_string}")
        print(f"  Sweep after change  : {sweep_stop_string}")
        #----------------------------------------
        # Display the command
        #----------------------------------------
        print("========================================")
        print("Menu(Top > Channel Settings)")
        print("========================================")
        print("1 : Channel Information Settings")
        print("2 : Sweep Settings")
        print("o : OK")
        print("c : Cancel")
        print("Please input the command : ", end='', flush=True)
        #----------------------------------------
        # Waiting for input
        #----------------------------------------
        buf = input()
        #----------------------------------------
        # Distinguish the command
        #----------------------------------------
        #--------------------
        # Channel Information Settings
        #--------------------
        if buf == "1":
            print("\n----------------------------------------")
            print("Start the channel information settings")
            print("----------------------------------------")
            #----------------------------------------
            # Input the amplitude
            #----------------------------------------
            while True:
                print("Please input the amplitude : ", end='', flush=True)
                #----------------------------------------
                # Waiting for input
                #----------------------------------------
                buf = input()
                #----------------------------------------
                # Check if the input value is a number
                #----------------------------------------
                if False == isnum(buf, 0):
                    continue
                break
            m_amplitude = ctypes.c_float(float(buf))
            #----------------------------------------
            # Input the amplitude type
            #----------------------------------------
            print("")
            print("----------------------------------------")
            print("Parameter(Amplitude type)")
            print("----------------------------------------")
            print("0 : Amplitude")
            print("1 : Vp-p")
            while True:
                print("Please input the amplitude type : ", end='', flush=True)
                #----------------------------------------
                # Waiting for input
                #----------------------------------------
                buf = input()
                #----------------------------------------
                # Check if the input value is a number
                #----------------------------------------
                if False == isnum(buf, 10):
                    continue
                break
            m_amplitude_type = ctypes.c_ushort(int(buf))
            #----------------------------------------
            # Input the amplitude unit
            #----------------------------------------
            print("")
            print("----------------------------------------")
            print("Parameter(Amplitude unit)")
            print("----------------------------------------")
            print("0 : V or mA")
            print("1 : LSB")
            while True:
                print("Please input the amplitude unit : ", end='', flush=True)
                #----------------------------------------
                # Waiting for input
                #----------------------------------------
                buf = input()
                #----------------------------------------
                # Check if the input value is a number
                #----------------------------------------
                if False == isnum(buf, 10):
                    continue
                break
            m_amplitude_unit = ctypes.c_ushort(int(buf))
            #----------------------------------------
            # Input the offset
            #----------------------------------------
            print("")
            while True:
                print("Please input the offset : ", end='', flush=True)
                #----------------------------------------
                # Waiting for input
                #----------------------------------------
                buf = input()
                #----------------------------------------
                # Check if the input value is a number
                #----------------------------------------
                if False == isnum(buf, 0):
                    continue
                break
            m_offset = ctypes.c_float(float(buf))
            #----------------------------------------
            # Input the duty
            #----------------------------------------
            print("")
            while True:
                print("Please input the duty(%) : ", end='', flush=True)
                #----------------------------------------
                # Waiting for input
                #----------------------------------------
                buf = input()
                #----------------------------------------
                # Check if the input value is a number
                #----------------------------------------
                if False == isnum(buf, 0):
                    continue
                break
            m_duty = ctypes.c_float(float(buf))
            #----------------------------------------
            # Input the phase
            #----------------------------------------
            print("")
            while True:
                print("Please input the phase(Degree) : ", end='', flush=True)
                #----------------------------------------
                # Waiting for input
                #----------------------------------------
                buf = input()
                #----------------------------------------
                # Check if the input value is a number
                #----------------------------------------
                if False == isnum(buf, 0):
                    continue
                break
            m_phase = ctypes.c_float(float(buf))
        #--------------------
        # Sweep Settings
        #--------------------
        elif buf == "2":
            print("\n----------------------------------------")
            print("Start the sweep settings")
            print("----------------------------------------")
            #----------------------------------------
            # Input the simple sweep
            #----------------------------------------
            #--------------------
            # For Sinewave
            #--------------------
            if m_wave_type.value == 0:
                print("----------------------------------------")
                print("Parameter(Simple sweep)")
                print("----------------------------------------")
                print("0 : None")
                print("1 : Frequency sweep")
                print("2 : Amplitude sweep")
                print("3 : DC offset sweep")
                while True:
                    print("Please input the simple sweep : ", end='', flush=True)
                    #----------------------------------------
                    # Waiting for input
                    #----------------------------------------
                    buf = input()
                    #----------------------------------------
                    # Check if the input value is a number
                    #----------------------------------------
                    if False == isnum(buf, 10):
                        continue
                    break
                m_sweep_type = ctypes.c_ushort(int(buf))
            #--------------------
            # For Square
            #--------------------
            elif m_wave_type.value == 4:
                print("----------------------------------------")
                print("Parameter(Simple sweep)")
                print("----------------------------------------")
                print("0 : None")
                print("1 : Duty ratio sweep")
                while True:
                    print("Please input the simple sweep : ", end='', flush=True)
                    #----------------------------------------
                    # Waiting for input
                    #----------------------------------------
                    buf = input()
                    #----------------------------------------
                    # Check if the input value is a number
                    #----------------------------------------
                    if False == isnum(buf, 10):
                        continue
                    break
                m_sweep_type = ctypes.c_ushort(int(buf))
            #--------------------
            # For others
            #--------------------
            else:
                print("----------------------------------------")
                print("Parameter(Simple sweep)")
                print("----------------------------------------")
                print("0 : None")
                while True:
                    print("Please input the simple sweep : ", end='', flush=True)
                    #----------------------------------------
                    # Waiting for input
                    #----------------------------------------
                    buf = input()
                    #----------------------------------------
                    # Check if the input value is a number
                    #----------------------------------------
                    if False == isnum(buf, 10):
                        continue
                    break
                m_sweep_type = ctypes.c_ushort(int(buf))
            #----------------------------------------
            # Input the sweep time
            #----------------------------------------
            print("")
            while True:
                print("Please input the sweep time(1-10 waveforms) : ", end='', flush=True)
                #----------------------------------------
                # Waiting for input
                #----------------------------------------
                buf = input()
                #----------------------------------------
                # Check if the input value is a number
                #----------------------------------------
                if False == isnum(buf, 0):
                    continue
                break
            m_sweep_time = ctypes.c_ushort(int(buf))
            #----------------------------------------
            # Input the sweep before change
            #----------------------------------------
            print("")
            while True:
                print("Please input the sweep before change : ", end='', flush=True)
                #----------------------------------------
                # Waiting for input
                #----------------------------------------
                buf = input()
                #----------------------------------------
                # Check if the input value is a number
                #----------------------------------------
                if False == isnum(buf, 0):
                    continue
                break
            m_sweep_start = ctypes.c_float(float(buf))
            #----------------------------------------
            # Input the sweep after change
            #----------------------------------------
            print("")
            while True:
                print("Please input the sweep after change : ", end='', flush=True)
                #----------------------------------------
                # Waiting for input
                #----------------------------------------
                buf = input()
                #----------------------------------------
                # Check if the input value is a number
                #----------------------------------------
                if False == isnum(buf, 0):
                    continue
                break
            m_sweep_stop = ctypes.c_float(float(buf))
        #--------------------
        # OK
        #--------------------
        elif buf == "o":
            return True
        #--------------------
        # Cancel
        #--------------------
        elif buf == "c":
            return False
        #--------------------
        # Out-of-range command
        #--------------------
        else:
            print("\nThe number is out of range.\nPlease try again.\n")
            continue