#!/usr/bin/env python

#coding=utf-8

import ctypes
from ctypes import cdll
from ctypes import *
import time
import bdaqadamio

def OpenLib() :
	global myprocHandle
	ret = bdaqadamio.ADAMDrvOpen(myprocHandle)
	if ret == 0:
		return 0
	else :
		return -99

def CloseLib() :
        global myprocHandle
        if myprocHandle != 0 :
            bdaqadamio.ADAMDrvClose(myprocHandle)

def GetAdam5081Range(i_byRange):
	if i_byRange == 0 :
		print("Bi_Direction")
	elif i_byRange == 1 :
		print("Up_Down")
	elif i_byRange == 2 :
		print("Up")
	elif i_byRange == 3 :
		print("Frequency")
	elif i_byRange == 4 :
		print("AB1X")
	elif i_byRange == 5 :
		print("AB2X")
	elif i_byRange == 6:
		print("AB4X")
	else :
		print("Unknown")
                                                        
def GetRange() :                                        
	byRange = c_int(0)                              
	for iChannel in range(0,m_iChTotal):            
		ret = bdaqadamio.CNT_GetRange(myprocHandle, iSlot, iChannel, byRange)
		if ret == 0 :                                                                  
			print ("Counter Mode:") ,                                              
			GetAdam5081Range(byRange.value)                                        
		else :                                                                         
			print("CNT_GetRange(ch%d) failed[%x]."%(iChannel,ret))                 
                                                                                               
                                                                                               
def GetValues():                                                                               
	global myprocHandle                                                                    
																						   
	lVal = c_int(0)                                                                        
																						   
	for iChannel in range(0,m_iChTotal):                                                   
		if 0 == bdaqadamio.CNT_GetValue(myprocHandle, iSlot, iChannel,lVal) :
			print("Cnt%d : %u\n"% (iChannel, lVal.value))                          
		else :                                                                         
			print("CNT_GetValue(ch%d) failed."%iChannel)                           
                                                                                               
def SetRange() :                                                                               
	global myprocHandle                                                                    
	byRange = 2;    # for 5081 Up mode                                                     
	for iChannel in range(0,m_iChTotal):                                                   
		if 0 == bdaqadamio.CNT_SetRange(myprocHandle, iSlot, iChannel, byRange) : 
			print("CNT_SetRange(ch%d) done."%iChannel)                             
		else :                                                                         
			print("CNT_SetRange failed.")       
                                                                                               
if __name__ == '__main__':                                                                     
                                                                                               
	m_iChTotal = 4                                                                         
	myprocHandle = c_int(0)                                                                
																						   
	iRet = 0                                                                               
	bValue = False                                                                         
	setValue = True                                                                        
																						   
	print("/*** Adam5081 Sample ***/")                                                     
	# Note: You have to change the slot index                                              
	# slot index                                                                           
	slot = raw_input("Select slotid:")                                                     
	iSlot = int(slot)                                                                      
																						   
	iRet = OpenLib()                                                                       
																						   
	if  iRet == 0 :                                                                    
		# Get current channel status                                                   
		GetRange()                                                                     
		GetValues()                                                                    
																					   
		# Set channel range                                                            
		SetRange()                                                                     
																					   
		# Check result                                                                 
		GetRange()                                                                     
		while True:                                                                    
			GetValues()                                                            
			time.sleep(1)                                                          
	else :                                                                                 
		print("OpenLib(%d) failed.\n"%iRet)                                            
																						   
	CloseLib()                                                                             
	print("/***        END         ***/")                                                  
                                                  

