#================================================================
#================================================================
# API-DIO(WDM)
# Interrupt CallBack Sample
#                                                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


#================================================================
# Call Back Function
#================================================================
def int_callback(dev_id, wparam, lparam, param):
    #----------------------------------------
    # Process Interrupt Message
    #----------------------------------------
    #----------------------------------------
    # Interrupt bit No.
    #----------------------------------------
    BitNo = LOWORD(lparam)
    #----------------------------------------
    # Rising:1, Falling:2
    #----------------------------------------
    Kind = HIWORD(lparam)
    #----------------------------------------
    # Up count
    #----------------------------------------
    if Kind == cdio.DIO_INT_RISE:
        up_down = 'UP'
    #----------------------------------------
    # Down count
    #----------------------------------------
    if Kind == cdio.DIO_INT_FALL:
        up_down = 'DOWN'
    #----------------------------------------
    # Display interrupt factor
    #----------------------------------------
    print(f'Interrupt:id={dev_id} bit = {BitNo}, logic = {up_down}')

    return


pint_callback = cdio.PDIO_INT_CALLBACK(int_callback)


#================================================================
#  Main Function
#================================================================
def main():
    dio_id = ctypes.c_short()
    err_str = ctypes.create_string_buffer(256)

    #----------------------------------------
    # Initialization
    #----------------------------------------
    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()
    #----------------------------------------
    # Set Digital Filter
    #----------------------------------------
    lret = cdio.DioSetDigitalFilter(dio_id, 20)
    if lret != cdio.DIO_ERR_SUCCESS:
        cdio.DioGetErrorString(lret, err_str)
        print(f"DioSetDigitalFilter = {lret}: {err_str.value.decode('sjis')}")
    #----------------------------------------
    # Set interrupt event factor
    #----------------------------------------
    for bit_no in range(4):
        lret = cdio.DioNotifyInterrupt(dio_id, bit_no, cdio.DIO_INT_RISE, 0)
        if lret != cdio.DIO_ERR_SUCCESS:
            cdio.DioGetErrorString(lret, err_str)
            print(f"DioNotifyInterrupt = {lret}: {err_str.value.decode('sjis')}")
            sys.exit()
    #----------------------------------------
    # Set Interrupt Call Back
    #----------------------------------------
    lret = cdio.DioSetInterruptCallBackProc(dio_id, pint_callback, 0)
    if lret != cdio.DIO_ERR_SUCCESS:
        cdio.DioGetErrorString(lret, err_str)
        print(f"DioSetInterruptCallBackProc = {lret}: {err_str.value.decode('sjis')}")
        sys.exit()
    #----------------------------------------
    #  Wait Input
    #----------------------------------------
    while True:
        print("Input 'q' + 'enter' to quit")
        buf = input()
        #----------------------------------------
        # End
        #----------------------------------------
        if buf == 'q':
            print(f'quit.')
            break
    #----------------------------------------
    # Exit
    #----------------------------------------
    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')}")
    #----------------------------------------
    # Terminate application
    #----------------------------------------
    sys.exit()


#----------------------------------------
# Call main function
#----------------------------------------
if __name__ == "__main__":
    main()
