#================================================================
#================================================================
# C-WaveformGenerator
# MultiChannelWave
# Device setting file
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
#----------------------------------------
# Import modules
#----------------------------------------
import ctypes
import ctypes.wintypes
from CommonFunction import *
#----------------------------------------
# Declaration of global variables
#----------------------------------------
m_aochannels = ctypes.c_ushort()             # Number of channel
m_frequency = ctypes.c_float()               # Frequency
m_frequencyunit_value = ctypes.c_ushort()    # Frequency unit
m_oscillation_type = ctypes.c_ushort()       # Oscillation type
m_oscillation_start = ctypes.c_ushort()      # Number of oscillation waveform
m_oscillation_stop = ctypes.c_float()        # Number of oscillation stop waveform or Time of oscillation stop


#========================================
# Data transfer (Set)
#========================================
def devinfo_data_set(aochannels, frequency, frequencyunit, oscillationtype, oscillationstart, oscillationstop):
    global m_aochannels, m_frequency, m_frequencyunit_value, m_oscillation_type, m_oscillation_start, m_oscillation_stop
    m_aochannels.value          = aochannels.value
    m_frequency.value           = frequency.value
    m_frequencyunit_value.value = frequencyunit.value
    m_oscillation_type.value    = oscillationtype.value
    m_oscillation_start.value   = oscillationstart.value
    m_oscillation_stop.value    = oscillationstop.value

#========================================
# Data transfer (Get)
#========================================
def devinfo_data_get(aochannels, frequency, frequencyunit, oscillationtype, oscillationstart, oscillationstop):
    global m_aochannels, m_frequency, m_frequencyunit_value, m_oscillation_type, m_oscillation_start, m_oscillation_stop
    aochannels.value            = m_aochannels.value
    frequency.value             = m_frequency.value
    frequencyunit.value         = m_frequencyunit_value.value
    oscillationtype.value       = m_oscillation_type.value
    oscillationstart.value      = m_oscillation_start.value
    oscillationstop.value       = m_oscillation_stop.value

#========================================
# Function name    DeviceSetting
# Parameter        None
# Details          Function that display and process the device settings menu
# Return value     None
#========================================
def DeviceSetting():
    global m_aochannels, m_frequency, m_frequencyunit_value, m_oscillation_type, m_oscillation_start, m_oscillation_stop

    #----------------------------------------
    # Loop that wait input
    #----------------------------------------
    while True:
        #----------------------------------------
        # Get setting parameters
        #----------------------------------------
        aochannels_string = f"{m_aochannels.value}"
        frequency_string = f"{m_frequency.value}"
        frequencyunit_value_string = GetFrequencyUnitValueString(m_frequencyunit_value)
        oscillation_type_string = GetOscillationTypeString(m_oscillation_type)
        oscillation_start_string = f"{m_oscillation_start.value}"
        oscillation_stop_string = f"{m_oscillation_stop.value}"
        #----------------------------------------
        # Display the setting parameters
        #----------------------------------------
        print("\n========================================")
        print("Setting Parameter")
        print("========================================")
        print("<Device Information>")
        print(f"  Number of channel              : {aochannels_string}")
        print(f"  Frequency                      : {frequency_string}")
        print(f"  Frequency unit                 : {frequencyunit_value_string}")
        print("<Oscillation Information>")
        print(f"  Oscillation type               : {oscillation_type_string}")
        print(f"  Number of oscillation waveform : {oscillation_start_string}")
        print(f"  Stop                           : {oscillation_stop_string}")
        #----------------------------------------
        # Display the command
        #----------------------------------------
        print("========================================")
        print("Menu(Top > Device Settings)")
        print("========================================")
        print("1 : Device Information Settings")
        print("2 : Oscillation Information Settings")
        print("o : OK")
        print("c : Cancel")
        print("Please input the command : ", end='', flush=True)
        #----------------------------------------
        # Waiting for input
        #----------------------------------------
        buf = input()
        #----------------------------------------
        # Distinguish the command
        #----------------------------------------
        #--------------------
        # Device Information Settings
        #--------------------
        if buf == "1":
            print("\n----------------------------------------")
            print("Start the device information settings")
            print("----------------------------------------")
            #----------------------------------------
            # Input the number of channels
            #----------------------------------------
            while True:
                print("Please input the number of channels(1-16) : ", end='', flush=True)
                #----------------------------------------
                # Waiting for input
                #----------------------------------------
                buf = input()
                #----------------------------------------
                # Check if the input value is a number
                #----------------------------------------
                if False == isnum(buf, 10):
                    continue
                break
            m_aochannels = ctypes.c_ushort(int(buf))
            #----------------------------------------
            # Input the frequency
            #----------------------------------------
            print("")
            while True:
                print("Please input the frequency : ", end='', flush=True)
                #----------------------------------------
                # Waiting for input
                #----------------------------------------
                buf = input()
                #----------------------------------------
                # Check if the input value is a number
                #----------------------------------------
                if False == isnum(buf, 0):
                    continue
                break
            m_frequency = ctypes.c_float(float(buf))
            #----------------------------------------
            # Input the frequency unit
            #----------------------------------------
            print("")
            print("----------------------------------------")
            print("Parameter(Frequency unit)")
            print("----------------------------------------")
            print("0 : Hz")
            print("1 : KHz")
            print("2 : sec")
            print("3 : msec")
            while True:
                print("Please input the frequency 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_frequencyunit_value = ctypes.c_ushort(int(buf))
        #--------------------
        # Oscillation Information Settings
        #--------------------
        elif buf == "2":
            print("\n----------------------------------------")
            print("Start the oscillation information settings")
            print("----------------------------------------")
            #----------------------------------------
            # Input the oscillation type
            #----------------------------------------
            print("----------------------------------------")
            print("Parameter(Oscillation type)")
            print("----------------------------------------")
            print("0 : None")
            print("1 : Auto burst")
            print("2 : Trigger burst(rising edge)")
            print("3 : Trigger burst(falling edge)")
            while True:
                print("Please input the oscillation 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_oscillation_type = ctypes.c_ushort(int(buf))
            #----------------------------------------
            # Input the number of oscillation waveform
            #----------------------------------------
            print("")
            while True:
                print("Please input the number of oscillation waveform : ", end='', flush=True)
                #----------------------------------------
                # Waiting for input
                #----------------------------------------
                buf = input()
                #----------------------------------------
                # Check if the input value is a number
                #----------------------------------------
                if False == isnum(buf, 10):
                    continue
                break
            m_oscillation_start = ctypes.c_ushort(int(buf))
            #----------------------------------------
            # Input the stop
            #----------------------------------------
            print("")
            while True:
                print("Please input the stop : ", end='', flush=True)
                #----------------------------------------
                # Waiting for input
                #----------------------------------------
                buf = input()
                #----------------------------------------
                # Check if the input value is a number
                #----------------------------------------
                if False == isnum(buf, 0):
                    continue
                break
            m_oscillation_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
