#================================================================
#================================================================
# C-WaveformGenerator
# 複数チャネル波形出力サンプル
# デバイス設定ファイル
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
#----------------------------------------
# インポートモジュール
#----------------------------------------
import ctypes
import ctypes.wintypes
from CommonFunction import *
#----------------------------------------
# グローバル変数
#----------------------------------------
m_aochannels = ctypes.c_ushort()             # チャネル数
m_frequency = ctypes.c_float()               # 周波数
m_frequencyunit_value = ctypes.c_ushort()    # 周波数単位
m_oscillation_type = ctypes.c_ushort()       # 発振タイプ
m_oscillation_start = ctypes.c_ushort()      # 発振波数
m_oscillation_stop = ctypes.c_float()        # 発振停止波数or発振停止時間


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

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

#========================================
# 関数名    DeviceSetting
# 引数      なし
# 詳細      デバイス設定メニューの表示と処理を行う関数になります
# 戻り値    なし
#========================================
def DeviceSetting():
    global m_aochannels, m_frequency, m_frequencyunit_value, m_oscillation_type, m_oscillation_start, m_oscillation_stop

    #----------------------------------------
    # 入力待ちループ
    #----------------------------------------
    while True:
        #----------------------------------------
        # 設定パラメータの取得
        #----------------------------------------
        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}"
        #----------------------------------------
        # 設定パラメータの表示
        #----------------------------------------
        print("\n========================================")
        print("設定パラメータ")
        print("========================================")
        print("<デバイス情報>")
        print(f"  チャネル数           : {aochannels_string}")
        print(f"  周波数               : {frequency_string}")
        print(f"  周波数単位           : {frequencyunit_value_string}")
        print("<発振情報>")
        print(f"  発振タイプ           : {oscillation_type_string}")
        print(f"  発振波数             : {oscillation_start_string}")
        print(f"  停止                 : {oscillation_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("チャネル数を入力してください(1-16) : ", end='', flush=True)
                #----------------------------------------
                # 入力待ち
                #----------------------------------------
                buf = input()
                #----------------------------------------
                # 入力値が数値かとうかをチェック
                #----------------------------------------
                if False == isnum(buf, 10):
                    continue
                break
            m_aochannels = ctypes.c_ushort(int(buf))
            #----------------------------------------
            # 周波数を入力
            #----------------------------------------
            print("")
            while True:
                print("周波数を入力してください : ", end='', flush=True)
                #----------------------------------------
                # 入力待ち
                #----------------------------------------
                buf = input()
                #----------------------------------------
                # 入力値が数値かとうかをチェック
                #----------------------------------------
                if False == isnum(buf, 0):
                    continue
                break
            m_frequency = ctypes.c_float(float(buf))
            #----------------------------------------
            # 周波数単位を入力
            #----------------------------------------
            print("")
            print("----------------------------------------")
            print("パラメータ(周波数単位)")
            print("----------------------------------------")
            print("0 : Hz")
            print("1 : KHz")
            print("2 : sec")
            print("3 : msec")
            while True:
                print("周波数単位を入力してください : ", end='', flush=True)
                #----------------------------------------
                # 入力待ち
                #----------------------------------------
                buf = input()
                #----------------------------------------
                # 入力値が数値かとうかをチェック
                #----------------------------------------
                if False == isnum(buf, 10):
                    continue
                break
            m_frequencyunit_value = ctypes.c_ushort(int(buf))
        #--------------------
        # 発振情報設定
        #--------------------
        elif buf == "2":
            print("\n----------------------------------------")
            print("発振情報設定を開始します")
            print("----------------------------------------")
            #----------------------------------------
            # 発振タイプを入力
            #----------------------------------------
            print("----------------------------------------")
            print("パラメータ(発振タイプ)")
            print("----------------------------------------")
            print("0 : なし")
            print("1 : オートバースト")
            print("2 : トリガバースト（立ち上がり）")
            print("3 : トリガバースト（立ち下がり）")
            while True:
                print("発振タイプを入力してください : ", end='', flush=True)
                #----------------------------------------
                # 入力待ち
                #----------------------------------------
                buf = input()
                #----------------------------------------
                # 入力値が数値かとうかをチェック
                #----------------------------------------
                if False == isnum(buf, 10):
                    continue
                break
            m_oscillation_type = ctypes.c_ushort(int(buf))
            #----------------------------------------
            # 発振波数を入力
            #----------------------------------------
            print("")
            while True:
                print("発振波数を入力してください : ", end='', flush=True)
                #----------------------------------------
                # 入力待ち
                #----------------------------------------
                buf = input()
                #----------------------------------------
                # 入力値が数値かとうかをチェック
                #----------------------------------------
                if False == isnum(buf, 10):
                    continue
                break
            m_oscillation_start = ctypes.c_ushort(int(buf))
            #----------------------------------------
            # 停止を入力
            #----------------------------------------
            print("")
            while True:
                print("停止を入力してください : ", end='', flush=True)
                #----------------------------------------
                # 入力待ち
                #----------------------------------------
                buf = input()
                #----------------------------------------
                # 入力値が数値かとうかをチェック
                #----------------------------------------
                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
        #--------------------
        # コマンド範囲外
        #--------------------
        else:
            print("\n範囲外の番号になります。\nもう一度入力してください。\n")
            continue
