#================================================================
#================================================================
# API-SMC(WDM)
# syncmoveサンプル
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================

import ctypes
import ctypes.wintypes
import sys
import csmc

#================================================================
# 外部変数
#================================================================
smc_id          = ctypes.c_short(0)
axis_no         = ctypes.c_short(0)
set_sync_axis   = ctypes.c_short(0)
set_sync_chip   = ctypes.c_short(0)
set_sync_board  = ctypes.c_short(0)
get_sync_axis   = ctypes.c_short(0)
get_sync_chip   = ctypes.c_short(0)
get_sync_board  = ctypes.c_short(0)


#================================================================
# 同期軸設定
#================================================================
def sync_axis():
    global set_sync_axis

    while True:
        print("\n【同期軸設定】---------------------")
        print("  1:軸1           :", end="")
        if (set_sync_axis.value & 0x01) != 0:
            print("同期する")
        else:
            print("同期しない")
        print("  2:軸2           :", end="")
        if (set_sync_axis.value & 0x02) != 0:
            print("同期する")
        else:
            print("同期しない")
        print("  3:軸3           :", end="")
        if (set_sync_axis.value & 0x04) != 0:
            print("同期する")
        else:
            print("同期しない")
        print("  4:軸4           :", end="")
        if (set_sync_axis.value & 0x08) != 0:
            print("同期する")
        else:
            print("同期しない")
        print("  5:軸5           :", end="")
        if (set_sync_axis.value & 0x10) != 0:
            print("同期する")
        else:
            print("同期しない")
        print("  6:軸6           :", end="")
        if (set_sync_axis.value & 0x20) != 0:
            print("同期する")
        else:
            print("同期しない")
        print("  7:軸7           :", end="")
        if (set_sync_axis.value & 0x40) != 0:
            print("同期する")
        else:
            print("同期しない")
        print("  8:軸8           :", end="")
        if (set_sync_axis.value & 0x80) != 0:
            print("同期する")
        else:
            print("同期しない")
        print("  q:戻る")
        print("-----------------------------------------")
        menu = input("番号を入力してください。:")
        #----------------------------------------
        # 終了
        #----------------------------------------
        if menu == 'q':
            break
        xbit = ctypes.c_short(0)
        if menu == '1':
            xbit.value = 0x01
            print("\n【軸1設定】---------------------")
        if menu == '2':
            xbit.value = 0x02
            print("\n【軸2設定】---------------------")
        if menu == '3':
            xbit.value = 0x04
            print("\n【軸3設定】---------------------")
        if menu == '4':
            xbit.value = 0x08
            print("\n【軸4設定】---------------------")
        if menu == '5':
            xbit.value = 0x10
            print("\n【軸5設定】---------------------")
        if menu == '6':
            xbit.value = 0x20
            print("\n【軸6設定】---------------------")
        if menu == '7':
            xbit.value = 0x40
            print("\n【軸7設定】---------------------")
        if menu == '8':
            xbit.value = 0x80
            print("\n【軸8設定】---------------------")
        print("1:同期する")
        print("2:同期しない")
        menu = input()
        if menu == '1':
            set_sync_axis.value |= xbit.value
        if menu == '2':
            set_sync_axis.value &= (~xbit.value)
    return 0


#================================================================
# Chip間の同期
#================================================================
def sync_chip():
    global set_sync_chip

    while True:
        print("\n【Chip間同期設定】---------------------")
        print("  1:同期する")
        print("  2:同期しない")
        print("-----------------------------------------")
        menu = input("番号を入力してください。:")
        if menu == '1':
            set_sync_chip.value = 1
        elif menu == '2':
            set_sync_chip.value = 0
        else:
            continue
        break
    return 0


#================================================================
# ボード間の同期
#================================================================
def sync_board():
    global set_sync_board

    while True:
        print("\n【ボード間同期設定】---------------------")
        print("  1:同期しない")
        print("  2:CN2と接続したボードと同期する")
        print("  3:CN3と接続したボードと同期する")
        print("  4:CN2,CN3と接続したボードと同期する")
        print("-----------------------------------------")
        menu = input("番号を入力してください。:")
        if menu == '1':
            set_sync_board.value = 0
        elif menu == '2':
            set_sync_board.value = 1
        elif menu == '3':
            set_sync_board.value = 2
        elif menu == '4':
            set_sync_board.value = 3
        else:
            continue
        break
    return 0


#================================================================
# 設定の反映
#================================================================
def set_param():
    err_str = ctypes.create_string_buffer(256)
    global smc_id, set_sync_axis, set_sync_chip, set_sync_board
    
    lret = csmc.SmcWSetSync(smc_id, set_sync_axis, set_sync_chip, set_sync_board)
    if lret != 0:
        csmc.SmcWGetErrorString(lret, err_str)
        print(f"SmcWSetSync = {lret}: {err_str.value.decode('sjis')}")
        return -1
    return 0


#================================================================
# パラメータ取得
#================================================================
def get_param():
    err_str = ctypes.create_string_buffer(256)
    global smc_id, get_sync_axis, get_sync_chip, get_sync_board
    
    lret = csmc.SmcWGetSync(smc_id, ctypes.byref(get_sync_axis), ctypes.byref(get_sync_chip), ctypes.byref(get_sync_board))
    if lret != 0:
        csmc.SmcWGetErrorString(lret, err_str)
        print(f"SmcWGetSync = {lret}: {err_str.value.decode('sjis')}")
        return -1
    return 0


#================================================================
# SmcWSyncMotionStart
#================================================================
def start_sync():
    global smc_id, axis_no
    
    csmc.SmcWSyncMotionStart(smc_id, axis_no)
    return 0


#================================================================
# main関数
#================================================================
def main():
    err_str = ctypes.create_string_buffer(256)
    global smc_id, axis_no, get_sync_axis, get_sync_chip, get_sync_board

    #----------------------------------------
    # タイトルの表示
    #----------------------------------------
    print("----------------------------------")
    print("<<< syncmoveサンプル >>>")
    print("----------------------------------")
    #----------------------------------------
    # デバイス名の入力確認
    #----------------------------------------
    device_name = input("使用するボードのデバイス名を入力してください。\nデバイス名:")
    #----------------------------------------
    # デバイスの初期化
    #----------------------------------------
    lret = csmc.SmcWInit(device_name.encode(), ctypes.byref(smc_id))
    if lret != 0:
        csmc.SmcWGetErrorString(lret, err_str)
        print(f"SmcWInit = {lret}: {err_str.value.decode('sjis')}")
        sys.exit()
    axis_no.value = 1
    #----------------------------------------
    # パラメータ取得
    #----------------------------------------
    get_param()
    #----------------------------------------
    # 設定変更
    #----------------------------------------
    while True:
        print("\n【同期動作メニュー】---------------------")
        print("  1:軸の変更")
        print("  2:同期軸設定")
        print("  3:Chip間同期設定")
        print("  4:ボード間の同期")
        print("  5:設定の反映")
        print("  6:パラメータ表示")
        print("  7:SmcWSyncMotionStart")
        print("  q:終了")
        print("-----------------------------------------")
        menu = input("番号を入力してください。:")
        #----------------------------------------
        # 軸の変更
        #----------------------------------------
        if menu == '1':
            while True:
                menu = input("軸番号を入力してください。:")
                #----------------------------------------
                # 入力データが数値かを確認
                #----------------------------------------
                if menu.isdigit() == True:
                    axis_no.value = int(menu)
                    if axis_no.value < 1:
                        continue
                    else:
                        break
            continue
        if menu == '2':
            sync_axis()
        if menu == '3':
            sync_chip()
        if menu == '4':
            sync_board()
        if menu == '5':
            set_param()
        if menu == '6':
            get_param()
            #----------------------------------------
            # 設定内容の表示
            #----------------------------------------
            print(f"同期軸\t\t: {get_sync_axis.value:x}H")
            print(f"Chip間同期\t: {get_sync_chip.value:x}H")
            print(f"ボード間同期\t: {get_sync_board.value:x}H")
        if menu == '7':
            start_sync()
        #----------------------------------------
        # 終了
        #----------------------------------------
        if menu == 'q':
            break
    #----------------------------------------
    # デバイスの終了処理
    #---------------------------------------- 
    lret = csmc.SmcWExit(smc_id)
    if lret != 0:
        csmc.SmcWGetErrorString(lret, err_str)
        print(f"SmcWExit = {lret}: {err_str.value.decode('sjis')}")
        sys.exit()
    #----------------------------------------
    # アプリケーション終了
    #----------------------------------------
    sys.exit()


#----------------------------------------
# main関数呼び出し
#----------------------------------------
if __name__ == "__main__":
    main()
