﻿#================================================================
# czm.py
# module file for CONTEC Impedance Meter device
#                                                CONTEC.Co., Ltd.
#================================================================
import ctypes
import ctypes.wintypes

czm_dll = ctypes.windll.LoadLibrary('czm.dll')

#================================================================
# Type definition
#================================================================
PZM_CALLBACK = ctypes.WINFUNCTYPE(None,
                                       ctypes.c_short, ctypes.wintypes.WPARAM, ctypes.wintypes.LPARAM,
                                       ctypes.c_void_p)
# Message
ZMM_MESSAGE_MEASURE_START = 0x00000001              # Measure start
ZMM_MESSAGE_MEASURE_STOP = 0x00000002               # Measure stop
ZMM_MESSAGE_COMPARATOR_JUDGMENT = 0x00000004        # Comparator judgment
ZMM_MESSAGE_OVERFLOW_ERROR = 0x00000008             # Overflow error
# Status
ZMS_STATUS_BUSY = 0x00000001                        # Busy
ZMS_STATUS_WAIT_TRIGGER = 0x00000002                # Waiting for trigger
ZMS_STATUS_SELF_CALIBRATION_ENABLED = 0x00000100    # Self-calibration enabled
ZMS_STATUS_OVERFLOW = 0x00010000                    # Overflow
#----------------------------------------
# Frequency
#----------------------------------------
ZM_FREQUENCY_120HZ = 0                              # Frequency : 120Hz
ZM_FREQUENCY_1kHZ = 1                               # Frequency : 1kHz
#----------------------------------------
# Signal Level
#----------------------------------------
ZM_LEVEL_500mV = 0                                  # Level : 0.5V
ZM_LEVEL_1V = 1                                     # Level : 1V
#----------------------------------------
# Measurement Speed
#----------------------------------------
ZM_SPEED_FAST = 0                                   # Measurement speed : FAST
ZM_SPEED_NORMAL = 1                                 # Measurement speed : NORMAL
ZM_SPEED_SLOW = 2                                   # Measurement speed : SLOW
#----------------------------------------
# Equivalent Circuit Mode
#----------------------------------------
ZM_CIRCUIT_AUTO = 0                                 # Equivalent circuit mode : AUTO
ZM_CIRCUIT_SERIAL = 1                               # Equivalent circuit mode : SERIAL
ZM_CIRCUIT_PARALLEL = 2                             # Equivalent circuit mode : PARALLEL
#----------------------------------------
# Range
#----------------------------------------
ZM_RANGE_4nF = 3                                    # Range : 4nF
ZM_RANGE_40nF = 4                                   # Range : 40nF
ZM_RANGE_400nF = 5                                  # Range : 400nF
ZM_RANGE_4uF = 6                                    # Range : 4uF
ZM_RANGE_40uF = 7                                   # Range : 40uF
ZM_RANGE_400uF = 8                                  # Range : 400uF
ZM_RANGE_1400uF = 9                                 # Range : 1.4mF
#----------------------------------------
# Correction
#----------------------------------------
ZM_CORRECTION_TYPE_OPEN = 0                         # Correction type : OPEN
ZM_CORRECTION_TYPE_SHORT = 1                        # Correction type : SHORT
ZM_CORRECTION_FORMAT_OPEN_GB = 1                    # Correction format of open : GB
ZM_CORRECTION_FORMAT_SHORT_RX = 1                   # Correction format of short : RX
#----------------------------------------
# Contact Check
#----------------------------------------
ZM_CONTACTCHECK_DISABLE = 0                         # Contact check : Disabled
ZM_CONTACTCHECK_BEFORE = 1                          # Contact check : Before measurement
ZM_CONTACTCHECK_AFTER = 2                           # Contact check after measurement
ZM_CONTACTCHECK_BOTH = 3                            # Contact check before and after measurement
#----------------------------------------
# Enable/Disable
#----------------------------------------
ZM_DISABLE = 0                                      # Disable
ZM_ENABLE = 1                                       # Enable

#================================================================
# Function prototype
#================================================================
#----------------------------------------
# Common function
#----------------------------------------
# C Prototype: long WINAPI ZmInit(char *DeviceName, short *Id);
ZmInit = czm_dll.ZmInit
ZmInit.restype = ctypes.c_long
ZmInit.argtypes = [ctypes.c_char_p, ctypes.POINTER(ctypes.c_short)]

# C Prototype: long WINAPI ZmExit(short Id);
ZmExit = czm_dll.ZmExit
ZmExit.restype = ctypes.c_long
ZmExit.argtypes = [ctypes.c_short]

# C Prototype: long WINAPI ZmResetDevice(short Id);
ZmResetDevice = czm_dll.ZmResetDevice
ZmResetDevice.restype = ctypes.c_long
ZmResetDevice.argtypes = [ctypes.c_short]

# C Prototype: long WINAPI ZmGetErrorString(long ErrorCode , char *ErrorString);
ZmGetErrorString = czm_dll.ZmGetErrorString
ZmGetErrorString.restype = ctypes.c_long
ZmGetErrorString.argtypes = [ctypes.c_long, ctypes.c_char_p]

#----------------------------------------
# Information acquisition function
#----------------------------------------
# C Prototype: long WINAPI ZmQueryDeviceName(short Index, char *DeviceName, char *Device);
ZmQueryDeviceName = czm_dll.ZmQueryDeviceName
ZmQueryDeviceName.restype = ctypes.c_long
ZmQueryDeviceName.argtypes = [ctypes.c_short, ctypes.c_char_p, ctypes.c_char_p]

# C Prototype: long WINAPI ZmGetMaxChannels(short Id, unsigned short *MaxChannels);
ZmGetMaxChannels = czm_dll.ZmGetMaxChannels
ZmGetMaxChannels.restype = ctypes.c_long
ZmGetMaxChannels.argtypes = [ctypes.c_short, ctypes.POINTER(ctypes.c_ushort)]
#----------------------------------------
# Measure condition function
#----------------------------------------
# C Prototype: long WINAPI ZmSetFrequency(short Id, unsigned short Channel, unsigned short Frequency);
ZmSetFrequency = czm_dll.ZmSetFrequency
ZmSetFrequency.restype = ctypes.c_long
ZmSetFrequency.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.c_ushort]

# C Prototype: long WINAPI ZmGetFrequency(short Id, unsigned short Channel, unsigned short *Frequency);
ZmGetFrequency = czm_dll.ZmGetFrequency
ZmGetFrequency.restype = ctypes.c_long
ZmGetFrequency.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.POINTER(ctypes.c_ushort)]

# C Prototype: long WINAPI ZmSetLevel(short Id, unsigned short Channel, unsigned short Level);
ZmSetLevel = czm_dll.ZmSetLevel
ZmSetLevel.restype = ctypes.c_long
ZmSetLevel.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.c_ushort]

# C Prototype: long WINAPI ZmGetLevel(short Id, unsigned short Channel, unsigned short *Level);
ZmGetLevel = czm_dll.ZmGetLevel
ZmGetLevel.restype = ctypes.c_long
ZmGetLevel.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.POINTER(ctypes.c_ushort)]

# C Prototype: long WINAPI ZmSetSpeed(short Id, unsigned short Channel, unsigned short Speed, unsigned int AverageCount);
ZmSetSpeed = czm_dll.ZmSetSpeed
ZmSetSpeed.restype = ctypes.c_long
ZmSetSpeed.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.c_ushort, ctypes.c_uint]

# C Prototype: long WINAPI ZmGetSpeed(short Id, unsigned short Channel, unsigned short *Speed, unsigned int *AverageCount);
ZmGetSpeed = czm_dll.ZmGetSpeed
ZmGetSpeed.restype = ctypes.c_long
ZmGetSpeed.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.POINTER(ctypes.c_ushort), ctypes.POINTER(ctypes.c_uint)]

# C Prototype: long WINAPI ZmSetCircuit(short Id, unsigned short Channel, unsigned short Circuit);
ZmSetCircuit = czm_dll.ZmSetCircuit
ZmSetCircuit.restype = ctypes.c_long
ZmSetCircuit.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.c_ushort]

# C Prototype: long WINAPI ZmGetCircuit(short Id, unsigned short Channel, unsigned short *Circuit);
ZmGetCircuit = czm_dll.ZmGetCircuit
ZmGetCircuit.restype = ctypes.c_long
ZmGetCircuit.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.POINTER(ctypes.c_ushort)]

# C Prototype: long WINAPI ZmSetRange(short Id, unsigned short Channel, unsigned short Range);
ZmSetRange = czm_dll.ZmSetRange
ZmSetRange.restype = ctypes.c_long
ZmSetRange.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.c_ushort]

# C Prototype: long WINAPI ZmGetRange(short Id, unsigned short Channel, unsigned short *Range);
ZmGetRange = czm_dll.ZmGetRange
ZmGetRange.restype = ctypes.c_long
ZmGetRange.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.POINTER(ctypes.c_ushort)]

# C Prototype: long WINAPI ZmCorrection(short Id, unsigned short Channel, unsigned short Type);
ZmCorrection = czm_dll.ZmCorrection
ZmCorrection.restype = ctypes.c_long
ZmCorrection.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.c_ushort]

# C Prototype: long WINAPI ZmSetCorrectionEnable(short Id, unsigned short Channel, unsigned short Type, unsigned short Enable);
ZmSetCorrectionEnable = czm_dll.ZmSetCorrectionEnable
ZmSetCorrectionEnable.restype = ctypes.c_long
ZmSetCorrectionEnable.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.c_ushort, ctypes.c_ushort]

# C Prototype: long WINAPI ZmGetCorrectionEnable(short Id, unsigned short Channel, unsigned short Type, unsigned short *Enable);
ZmGetCorrectionEnable = czm_dll.ZmGetCorrectionEnable
ZmGetCorrectionEnable.restype = ctypes.c_long
ZmGetCorrectionEnable.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.c_ushort, ctypes.POINTER(ctypes.c_ushort)]

# C Prototype: long WINAPI ZmSetCorrectionValue(short Id, unsigned short Channel, unsigned short Type, float correction_value_1, float correction_value_2, unsigned short format);
ZmSetCorrectionValue = czm_dll.ZmSetCorrectionValue
ZmSetCorrectionValue.restype = ctypes.c_long
ZmSetCorrectionValue.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.c_ushort, ctypes.c_float, ctypes.c_float, ctypes.c_ushort]

# C Prototype: long WINAPI ZmGetCorrectionValue(short Id, unsigned short Channel, unsigned short Type, float *correction_value_1, float *correction_value_2, unsigned short format);
ZmGetCorrectionValue = czm_dll.ZmGetCorrectionValue
ZmGetCorrectionValue.restype = ctypes.c_long
ZmGetCorrectionValue.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.c_ushort, ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_float), ctypes.c_ushort]

# C Prototype: long WINAPI long ZmSetStartDelay(short Id, unsigned short Channel, float Time);
ZmSetStartDelay = czm_dll.ZmSetStartDelay
ZmSetStartDelay.restype = ctypes.c_long
ZmSetStartDelay.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.c_float]

# C Prototype: long WINAPI ZmGetStartDelay(short Id, unsigned short Channel, float *Time);
ZmGetStartDelay = czm_dll.ZmGetStartDelay
ZmGetStartDelay.restype = ctypes.c_long
ZmGetStartDelay.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.POINTER(ctypes.c_float)]

# C Prototype: long WINAPI ZmSetSignalOutputDelay(short Id, unsigned short Channel, float Time);
ZmSetSignalOutputDelay = czm_dll.ZmSetSignalOutputDelay
ZmSetSignalOutputDelay.restype = ctypes.c_long
ZmSetSignalOutputDelay.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.c_float]

# C Prototype: long WINAPI ZmGetSignalOutputDelay(short Id, unsigned short Channel, float *Time);
ZmGetSignalOutputDelay = czm_dll.ZmGetSignalOutputDelay
ZmGetSignalOutputDelay.restype = ctypes.c_long
ZmGetSignalOutputDelay.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.POINTER(ctypes.c_float)]

# C Prototype: long WINAPI ZmSetTriggerSyncEnable(short Id, unsigned short Channel, unsigned short Enable);
ZmSetTriggerSyncEnable = czm_dll.ZmSetTriggerSyncEnable
ZmSetTriggerSyncEnable.restype = ctypes.c_long
ZmSetTriggerSyncEnable.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.c_ushort]

# C Prototype: long WINAPI ZmGetTriggerSyncEnable(short Id, unsigned short Channel, unsigned short *Enable);
ZmGetTriggerSyncEnable = czm_dll.ZmGetTriggerSyncEnable
ZmGetTriggerSyncEnable.restype = ctypes.c_long
ZmGetTriggerSyncEnable.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.POINTER(ctypes.c_ushort)]

# C Prototype: long WINAPI ZmSetContactCheck(short Id, unsigned short Channel, unsigned short ContactCheck);
ZmSetContactCheck = czm_dll.ZmSetContactCheck
ZmSetContactCheck.restype = ctypes.c_long
ZmSetContactCheck.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.c_ushort]

# C Prototype: long WINAPI ZmGetContactCheck(short Id, unsigned short Channel, unsigned short *ContactCheck);
ZmGetContactCheck = czm_dll.ZmGetContactCheck
ZmGetContactCheck.restype = ctypes.c_long
ZmGetContactCheck.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.POINTER(ctypes.c_ushort)]
#----------------------------------------
# Measure operation function
#----------------------------------------
# C Prototype: long WINAPI ZmSingleCMeasure(short Id, unsigned short Channel, float *CValue, float *DValue, unsigned int *Status);
ZmSingleCMeasure = czm_dll.ZmSingleCMeasure
ZmSingleCMeasure.restype = ctypes.c_long
ZmSingleCMeasure.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_uint)]