#================================================================
#================================================================
# API-DIO(WDM)
# トリガサンプル
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================

import ctypes
import ctypes.wintypes
import sys
import cdio

#================================================================
# LOWORD
#================================================================
def LOWORD(a):
    return a & 0xffff


#================================================================
# HOWORD
#================================================================
def HIWORD(a):
    return (a >> 16) & 0xffff


#================================================================
# コールバック関数
#================================================================
def trg_callback(dev_id, wparam, lparam, param):
    #----------------------------------------
    # トリガメッセージの処理
    #----------------------------------------
    #----------------------------------------
    # トリガビット番号
    #----------------------------------------
    BitNo = LOWORD(lparam)
    #----------------------------------------
    # 立ち上がり:1, 立ち下がり:2
    #----------------------------------------
    Kind = HIWORD(lparam)
    #----------------------------------------
    # アップカウント
    #----------------------------------------
    if Kind == cdio.DIO_INT_RISE:
        up_down = 'UP'
    #----------------------------------------
    # ダウンカウント
    #----------------------------------------
    if Kind == cdio.DIO_INT_FALL:
        up_down = 'DOWN'
    #----------------------------------------
    # トリガ要因表示
    #----------------------------------------
    print(f'Trigger:id={dev_id} bit = {BitNo}, logic = {up_down}')

    return


ptrg_callback = cdio.PDIO_TRG_CALLBACK(trg_callback)


#================================================================
# main関数
#================================================================
def main():
    dio_id = ctypes.c_short()
    in_port_num = ctypes.c_short()
    out_port_num = ctypes.c_short()
    err_str = ctypes.create_string_buffer(256)

    #----------------------------------------
    # ドライバ初期化処理
    #----------------------------------------
    dev_name = input('Input device name: ')
    lret = cdio.DioInit(dev_name.encode(), ctypes.byref(dio_id))
    if lret != cdio.DIO_ERR_SUCCESS:
        cdio.DioGetErrorString(lret, err_str)
        print(f"DioInit = {lret}: {err_str.value.decode('sjis')}")
        sys.exit()
    #----------------------------------------
    # ビット数の取得
    #----------------------------------------
    lret = cdio.DioGetMaxPorts(dio_id, ctypes.byref(in_port_num), ctypes.byref(out_port_num))
    if lret != cdio.DIO_ERR_SUCCESS:
        cdio.DioGetErrorString(lret, err_str)
        print(f"DioGetMaxPorts = {lret}: {err_str.value.decode('sjis')}")
        sys.exit()
    max_trg = 8 * in_port_num.value
    if max_trg == 0:
        print(f'Input bit number = 0')
        sys.exit()
    else:
        print(f'Trigger bit = 0 to {max_trg - 1}')
    #----------------------------------------
    # トリガコールバックの設定
    #----------------------------------------
    lret = cdio.DioSetTrgCallBackProc (dio_id, ptrg_callback, 0)
    if lret != cdio.DIO_ERR_SUCCESS:
        cdio.DioGetErrorString(lret, err_str)
        print(f"DioSetTrgCallBackProc = {lret}: {err_str.value.decode('sjis')}")
        sys.exit()
    #----------------------------------------
    # トリガを有効にする
    #----------------------------------------
    for bit_no in range(max_trg):
        lret = cdio.DioNotifyTrg ( dio_id, bit_no, cdio.DIO_TRG_RISE | cdio.DIO_TRG_FALL, 100, 0)
        if lret != cdio.DIO_ERR_SUCCESS:
            cdio.DioGetErrorString(lret, err_str)
            print(f"DioNotifyTrg = {lret}: {err_str.value.decode('sjis')}")
            sys.exit()
    print("Waiting for trigger...")
    #----------------------------------------
    # 入力待ちループ
    #----------------------------------------
    while True:
        print("Input 'q' + 'enter' to quit")
        buf = input()
        #----------------------------------------
        # 終了
        #----------------------------------------
        if buf == 'q':
            print(f'quit.')
            break
    #----------------------------------------
    # ドライバ終了処理
    #----------------------------------------
    lret = cdio.DioExit(dio_id)
    if lret != cdio.DIO_ERR_SUCCESS:
        cdio.DioGetErrorString(lret, err_str)
        print(f"DioExit = {lret}: {err_str.value.decode('sjis')}")
    #----------------------------------------
    # アプリケーション終了
    #----------------------------------------
    sys.exit()


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