#================================================================
#================================================================
# C-WaveformGenerator
# 複数チャネル波形出力サンプル
# チャネル設定ファイル
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
#----------------------------------------
# インポートモジュール
#----------------------------------------
import ctypes
import ctypes.wintypes
from CommonFunction import *
#----------------------------------------
# グローバル変数
#----------------------------------------
m_aochannel = ctypes.c_ushort()         # チャネル番号
m_amplitude = ctypes.c_float()          # 振幅
m_offset = ctypes.c_float()             # オフセット
m_amplitude_type = ctypes.c_ushort()    # 振幅タイプ
m_amplitude_unit = ctypes.c_ushort()    # 振幅単位
m_duty = ctypes.c_float()               # Duty
m_phase = ctypes.c_float()              # 位相
m_sweep_type = ctypes.c_ushort()        # 簡易スイープ
m_sweep_time = ctypes.c_ushort()        # スイープ時間
m_sweep_start = ctypes.c_float()        # スイープ変化前
m_sweep_stop = ctypes.c_float()         # スイープ変化後
m_wave_type = ctypes.c_ushort()         # 波形タイプ


#========================================
# データの受け渡し（設定）
#========================================
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

#========================================
# データの受け渡し（取得）
#========================================
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

#========================================
# 関数名    ChannelSetting
# 引数      なし
# 詳細      チャネル設定メニューの表示と処理を行う関数になります
# 戻り値    なし
#========================================
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

    #----------------------------------------
    # 入力待ちループ
    #----------------------------------------
    while True:
        #----------------------------------------
        # 設定パラメータの取得
        #----------------------------------------
        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}"
        #----------------------------------------
        # 設定パラメータの表示
        #----------------------------------------
        print("\n========================================")
        print("設定パラメータ")
        print("========================================")
        print(f"チャネル番号:      CH : {aochannel_string}")
        print("<チャネル情報>")
        print(f"  振幅                : {amplitude_string}")
        print(f"  振幅タイプ          : {amplitude_type_string}")
        print(f"  振幅単位            : {amplitude_unit_string}")
        print(f"  オフセット          : {offset_string}")
        print(f"  Duty(シンメトリ)    : {duty_string} [%]")
        print(f"  位相                : {phase_string} [度]")
        print("<スイープ>")
        print(f"  簡易スイープ        : {sweep_type_string}")
        print(f"  スイープ時間        : {sweep_time_string} [回]")
        print(f"  スイープ変化前      : {sweep_start_string}")
        print(f"  スイープ変化後      : {sweep_stop_string}")
        #----------------------------------------
        # コマンドの表示
        #----------------------------------------
        print("========================================")
        print("メニュー(トップ > チャネル設定)")
        print("========================================")
        print("1 : チャネル情報設定")
        print("2 : スイープ設定")
        print("o : OK")
        print("c : Cancel")
        print("コマンドを入力してください : ", end='', flush=True)
        #----------------------------------------
        # 入力待ち
        #----------------------------------------
        buf = input()
        #----------------------------------------
        # コマンドの識別
        #----------------------------------------
        #--------------------
        # チャネル情報設定
        #--------------------
        if buf == "1":
            print("\n----------------------------------------")
            print("チャネル情報設定を開始します")
            print("----------------------------------------")
            #----------------------------------------
            # 振幅を入力
            #----------------------------------------
            while True:
                print("振幅を入力してください : ", end='', flush=True)
                #----------------------------------------
                # 入力待ち
                #----------------------------------------
                buf = input()
                #----------------------------------------
                # 入力値が数値かとうかをチェック
                #----------------------------------------
                if False == isnum(buf, 0):
                    continue
                break
            m_amplitude = ctypes.c_float(float(buf))
            #----------------------------------------
            # 振幅タイプを入力
            #----------------------------------------
            print("")
            print("----------------------------------------")
            print("パラメータ(振幅タイプ)")
            print("----------------------------------------")
            print("0 : 振幅")
            print("1 : Vp-p")
            while True:
                print("振幅タイプを入力してください : ", end='', flush=True)
                #----------------------------------------
                # 入力待ち
                #----------------------------------------
                buf = input()
                #----------------------------------------
                # 入力値が数値かとうかをチェック
                #----------------------------------------
                if False == isnum(buf, 10):
                    continue
                break
            m_amplitude_type = ctypes.c_ushort(int(buf))
            #----------------------------------------
            # 振幅単位を入力
            #----------------------------------------
            print("")
            print("----------------------------------------")
            print("パラメータ(振幅単位)")
            print("----------------------------------------")
            print("0 : V or mA")
            print("1 : LSB")
            while True:
                print("振幅単位を入力してください : ", end='', flush=True)
                #----------------------------------------
                # 入力待ち
                #----------------------------------------
                buf = input()
                #----------------------------------------
                # 入力値が数値かとうかをチェック
                #----------------------------------------
                if False == isnum(buf, 10):
                    continue
                break
            m_amplitude_unit = ctypes.c_ushort(int(buf))
            #----------------------------------------
            # オフセットを入力
            #----------------------------------------
            print("")
            while True:
                print("オフセットを入力してください : ", end='', flush=True)
                #----------------------------------------
                # 入力待ち
                #----------------------------------------
                buf = input()
                #----------------------------------------
                # 入力値が数値かとうかをチェック
                #----------------------------------------
                if False == isnum(buf, 0):
                    continue
                break
            m_offset = ctypes.c_float(float(buf))
            #----------------------------------------
            # Duty(シンメトリ)を入力
            #----------------------------------------
            print("")
            while True:
                print("Duty(シンメトリ)を入力してください(%) : ", end='', flush=True)
                #----------------------------------------
                # 入力待ち
                #----------------------------------------
                buf = input()
                #----------------------------------------
                # 入力値が数値かとうかをチェック
                #----------------------------------------
                if False == isnum(buf, 0):
                    continue
                break
            m_duty = ctypes.c_float(float(buf))
            #----------------------------------------
            # 位相を入力
            #----------------------------------------
            print("")
            while True:
                print("位相を入力してください(度) : ", end='', flush=True)
                #----------------------------------------
                # 入力待ち
                #----------------------------------------
                buf = input()
                #----------------------------------------
                # 入力値が数値かとうかをチェック
                #----------------------------------------
                if False == isnum(buf, 0):
                    continue
                break
            m_phase = ctypes.c_float(float(buf))
        #--------------------
        # スイープ設定
        #--------------------
        elif buf == "2":
            print("\n----------------------------------------")
            print("スイープ設定を開始します")
            print("----------------------------------------")
            #----------------------------------------
            # 簡易スイープを入力
            #----------------------------------------
            #--------------------
            # サイン波の場合
            #--------------------
            if m_wave_type.value == 0:
                print("----------------------------------------")
                print("パラメータ(簡易スイープ)")
                print("----------------------------------------")
                print("0 : なし")
                print("1 : 周波数スイープ")
                print("2 : 振幅スイープ")
                print("3 : DCオフセットスイープ")
                while True:
                    print("簡易スイープを入力してください : ", end='', flush=True)
                    #----------------------------------------
                    # 入力待ち
                    #----------------------------------------
                    buf = input()
                    #----------------------------------------
                    # 入力値が数値かとうかをチェック
                    #----------------------------------------
                    if False == isnum(buf, 10):
                        continue
                    break
                m_sweep_type = ctypes.c_ushort(int(buf))
            #--------------------
            # 矩形波の場合
            #--------------------
            elif m_wave_type.value == 4:
                print("----------------------------------------")
                print("パラメータ(簡易スイープ)")
                print("----------------------------------------")
                print("0 : なし")
                print("1 : デューティスイープ")
                while True:
                    print("簡易スイープを入力してください : ", end='', flush=True)
                    #----------------------------------------
                    # 入力待ち
                    #----------------------------------------
                    buf = input()
                    #----------------------------------------
                    # 入力値が数値かとうかをチェック
                    #----------------------------------------
                    if False == isnum(buf, 10):
                        continue
                    break
                m_sweep_type = ctypes.c_ushort(int(buf))
            #--------------------
            # その他の場合
            #--------------------
            else:
                print("----------------------------------------")
                print("パラメータ(簡易スイープ)")
                print("----------------------------------------")
                print("0 : なし")
                while True:
                    print("簡易スイープを入力してください : ", end='', flush=True)
                    #----------------------------------------
                    # 入力待ち
                    #----------------------------------------
                    buf = input()
                    #----------------------------------------
                    # 入力値が数値かとうかをチェック
                    #----------------------------------------
                    if False == isnum(buf, 10):
                        continue
                    break
                m_sweep_type = ctypes.c_ushort(int(buf))
            #----------------------------------------
            # スイープ時間を入力
            #----------------------------------------
            print("")
            while True:
                print("スイープ時間を入力してください(1-10回) : ", end='', flush=True)
                #----------------------------------------
                # 入力待ち
                #----------------------------------------
                buf = input()
                #----------------------------------------
                # 入力値が数値かとうかをチェック
                #----------------------------------------
                if False == isnum(buf, 0):
                    continue
                break
            m_sweep_time = ctypes.c_ushort(int(buf))
            #----------------------------------------
            # スイープ変化前を入力
            #----------------------------------------
            print("")
            while True:
                print("スイープ変化前を入力してください : ", end='', flush=True)
                #----------------------------------------
                # 入力待ち
                #----------------------------------------
                buf = input()
                #----------------------------------------
                # 入力値が数値かとうかをチェック
                #----------------------------------------
                if False == isnum(buf, 0):
                    continue
                break
            m_sweep_start = ctypes.c_float(float(buf))
            #----------------------------------------
            # スイープ変化後を入力
            #----------------------------------------
            print("")
            while True:
                print("スイープ変化後を入力してください : ", end='', flush=True)
                #----------------------------------------
                # 入力待ち
                #----------------------------------------
                buf = input()
                #----------------------------------------
                # 入力値が数値かとうかをチェック
                #----------------------------------------
                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
        #--------------------
        # コマンド範囲外
        #--------------------
        else:
            print("\n範囲外の番号になります。\nもう一度入力してください。\n")
            continue